From 9d94888da368e0cc27ade4ca37a5843d566a9345 Mon Sep 17 00:00:00 2001 From: Nazrin Date: Sat, 17 Jun 2023 08:00:10 -0700 Subject: [PATCH] Fix race condition with worktops (#2216) * Fix race condition with worktops * Update ScriptLib.java * Update ScriptLib.java --------- Co-authored-by: Magix <27646710+KingRainbow44@users.noreply.github.com> --- .../java/emu/grasscutter/scripts/ScriptLib.java | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/main/java/emu/grasscutter/scripts/ScriptLib.java b/src/main/java/emu/grasscutter/scripts/ScriptLib.java index dd975f6a3..84297bd5c 100644 --- a/src/main/java/emu/grasscutter/scripts/ScriptLib.java +++ b/src/main/java/emu/grasscutter/scripts/ScriptLib.java @@ -152,18 +152,17 @@ public class ScriptLib { logger.debug("[LUA] Call SetWorktopOptions with {}", printTable(table)); var callParams = this.callParams.getIfExists(); var group = this.currentGroup.getIfExists(); - if(callParams == null || group == null){ + if (callParams == null || group == null) { return 1; } var configId = callParams.param1; var entity = getSceneScriptManager().getScene().getEntityByConfigId(configId); - - int[] worktopOptions = new int[table.length()]; - for(int i = 1 ;i<=table.length() ;i++){ + var worktopOptions = new int[table.length()]; + for (int i = 1; i<=table.length(); i++) { worktopOptions[i-1] = table.get(i).optint(-1); } - if(!(entity instanceof EntityGadget gadget)|| worktopOptions.length == 0){ + if (!(entity instanceof EntityGadget gadget) || worktopOptions.length == 0) { return 2; } @@ -172,9 +171,10 @@ public class ScriptLib { } worktop.addWorktopOptions(worktopOptions); - - var scene = getSceneScriptManager().getScene(); - scene.broadcastPacket(new PacketWorktopOptionNotify(gadget)); + // Done in order to synchronize with addEntities in Scene.class. + synchronized (this.getSceneScriptManager().getScene()) { + scene.broadcastPacket(new PacketWorktopOptionNotify(gadget)); + } return 0; }