diff --git a/src/main/java/emu/grasscutter/scripts/ScriptLoader.java b/src/main/java/emu/grasscutter/scripts/ScriptLoader.java index c55a76251..9ee97fd4d 100644 --- a/src/main/java/emu/grasscutter/scripts/ScriptLoader.java +++ b/src/main/java/emu/grasscutter/scripts/ScriptLoader.java @@ -44,16 +44,16 @@ public class ScriptLoader { if (sm != null) { throw new Exception("Script loader already initialized"); } - + // Create script engine sm = new ScriptEngineManager(); engine = sm.getEngineByName("luaj"); factory = getEngine().getFactory(); - + // Lua stuff fileType = "lua"; serializer = new LuaSerializer(); - + // Set engine to replace require as a temporary fix to missing scripts LuajContext ctx = (LuajContext) engine.getContext(); ctx.globals.set("require", new OneArgFunction() { @@ -62,11 +62,11 @@ public class ScriptLoader { return LuaValue.ZERO; } }); - + LuaTable table = new LuaTable(); Arrays.stream(EntityType.values()).forEach(e -> table.set(e.name().toUpperCase(), e.getValue())); ctx.globals.set("EntityType", table); - + ctx.globals.set("EventType", CoerceJavaToLua.coerce(new EventType())); // TODO - make static class to avoid instantiating a new class every scene ctx.globals.set("GadgetState", CoerceJavaToLua.coerce(new ScriptGadgetState())); ctx.globals.set("RegionShape", CoerceJavaToLua.coerce(new ScriptRegionShape())); @@ -75,11 +75,11 @@ public class ScriptLoader { scriptLibLua = CoerceJavaToLua.coerce(scriptLib); ctx.globals.set("ScriptLib", scriptLibLua); } - + public static ScriptEngine getEngine() { return engine; } - + public static String getScriptType() { return fileType; } @@ -109,7 +109,7 @@ public class ScriptLoader { return sc.get(); } - Grasscutter.getLogger().info("Loading script " + path); + Grasscutter.getLogger().debug("Loading script " + path); File file = new File(path); diff --git a/src/main/java/emu/grasscutter/scripts/data/SceneBlock.java b/src/main/java/emu/grasscutter/scripts/data/SceneBlock.java index 3584f3fd5..5ec0a15db 100644 --- a/src/main/java/emu/grasscutter/scripts/data/SceneBlock.java +++ b/src/main/java/emu/grasscutter/scripts/data/SceneBlock.java @@ -32,7 +32,7 @@ public class SceneBlock { private transient boolean loaded; // Not an actual variable in the scripts either public boolean isLoaded() { - return loaded; + return this.loaded; } public void setLoaded(boolean loaded) { @@ -40,19 +40,19 @@ public class SceneBlock { } public boolean contains(Position pos) { - return pos.getX() <= max.getX() && pos.getX() >= min.getX() && - pos.getZ() <= max.getZ() && pos.getZ() >= min.getZ(); + return pos.getX() <= this.max.getX() && pos.getX() >= this.min.getX() && + pos.getZ() <= this.max.getZ() && pos.getZ() >= this.min.getZ(); } public SceneBlock load(int sceneId, Bindings bindings){ - if(loaded){ + if(this.loaded){ return this; } this.sceneId = sceneId; - setLoaded(true); + this.setLoaded(true); CompiledScript cs = ScriptLoader.getScriptByPath( - SCRIPT("Scene/" + sceneId + "/scene" + sceneId + "_block" + id + "." + ScriptLoader.getScriptType())); + SCRIPT("Scene/" + sceneId + "/scene" + sceneId + "_block" + this.id + "." + ScriptLoader.getScriptType())); if (cs == null) { return null; @@ -63,19 +63,19 @@ public class SceneBlock { cs.eval(bindings); // Set groups - groups = ScriptLoader.getSerializer().toList(SceneGroup.class, bindings.get("groups")).stream() + this.groups = ScriptLoader.getSerializer().toList(SceneGroup.class, bindings.get("groups")).stream() .collect(Collectors.toMap(x -> x.id, y -> y)); - groups.values().forEach(g -> g.block_id = id); - this.sceneGroupIndex = SceneIndexManager.buildIndex(3, groups.values(), g -> g.pos.toPoint()); - } catch (ScriptException e) { - Grasscutter.getLogger().error("Error loading block " + id + " in scene " + sceneId, e); + this.groups.values().forEach(g -> g.block_id = this.id); + this.sceneGroupIndex = SceneIndexManager.buildIndex(3, this.groups.values(), g -> g.pos.toPoint()); + } catch (ScriptException exception) { + Grasscutter.getLogger().error("An error occurred while loading block " + this.id + " in scene " + sceneId, exception); } - Grasscutter.getLogger().info("scene {} block {} is loaded successfully.", sceneId, id); + Grasscutter.getLogger().debug("Successfully loaded block {} in scene {}.", this.id, sceneId); return this; } public Rectangle toRectangle() { - return Rectangle.create(min.toXZDoubleArray(), max.toXZDoubleArray()); + return Rectangle.create(this.min.toXZDoubleArray(), this.max.toXZDoubleArray()); } } \ No newline at end of file diff --git a/src/main/java/emu/grasscutter/scripts/data/SceneGroup.java b/src/main/java/emu/grasscutter/scripts/data/SceneGroup.java index 4455e78dd..442ddf903 100644 --- a/src/main/java/emu/grasscutter/scripts/data/SceneGroup.java +++ b/src/main/java/emu/grasscutter/scripts/data/SceneGroup.java @@ -25,7 +25,7 @@ import static emu.grasscutter.Configuration.SCRIPT; @Setter public class SceneGroup { public transient int block_id; // Not an actual variable in the scripts but we will keep it here for reference - + public int id; public int refresh_id; public Position pos; @@ -52,133 +52,133 @@ public class SceneGroup { } public boolean isLoaded() { - return loaded; + return this.loaded; } - + public void setLoaded(boolean loaded) { this.loaded = loaded; } - + public int getBusinessType() { return this.business == null ? 0 : this.business.type; } - + public List getGarbageGadgets() { return this.garbages == null ? null : this.garbages.gadgets; } public CompiledScript getScript() { - return script; + return this.script; } public SceneSuite getSuiteByIndex(int index) { - return suites.get(index - 1); + return this.suites.get(index - 1); } public Bindings getBindings() { - return bindings; + return this.bindings; } public synchronized SceneGroup load(int sceneId){ - if(loaded){ + if(this.loaded){ return this; } - // Set flag here so if there is no script, we dont call this function over and over again. - setLoaded(true); + // Set flag here so if there is no script, we don't call this function over and over again. + this.setLoaded(true); this.bindings = ScriptLoader.getEngine().createBindings(); CompiledScript cs = ScriptLoader.getScriptByPath( - SCRIPT("Scene/" + sceneId + "/scene" + sceneId + "_group" + id + "." + ScriptLoader.getScriptType())); + SCRIPT("Scene/" + sceneId + "/scene" + sceneId + "_group" + this.id + "." + ScriptLoader.getScriptType())); if (cs == null) { return this; } this.script = cs; - + // Eval script try { - cs.eval(bindings); + cs.eval(this.bindings); // Set - monsters = ScriptLoader.getSerializer().toList(SceneMonster.class, bindings.get("monsters")).stream() + this.monsters = ScriptLoader.getSerializer().toList(SceneMonster.class, this.bindings.get("monsters")).stream() .collect(Collectors.toMap(x -> x.config_id, y -> y)); - monsters.values().forEach(m -> m.group = this); + this.monsters.values().forEach(m -> m.group = this); - gadgets = ScriptLoader.getSerializer().toList(SceneGadget.class, bindings.get("gadgets")).stream() + this.gadgets = ScriptLoader.getSerializer().toList(SceneGadget.class, this.bindings.get("gadgets")).stream() .collect(Collectors.toMap(x -> x.config_id, y -> y)); - gadgets.values().forEach(m -> m.group = this); + this.gadgets.values().forEach(m -> m.group = this); - triggers = ScriptLoader.getSerializer().toList(SceneTrigger.class, bindings.get("triggers")).stream() + this.triggers = ScriptLoader.getSerializer().toList(SceneTrigger.class, this.bindings.get("triggers")).stream() .collect(Collectors.toMap(x -> x.name, y -> y)); - triggers.values().forEach(t -> t.currentGroup = this); + this.triggers.values().forEach(t -> t.currentGroup = this); - suites = ScriptLoader.getSerializer().toList(SceneSuite.class, bindings.get("suites")); - regions = ScriptLoader.getSerializer().toList(SceneRegion.class, bindings.get("regions")).stream() + this.suites = ScriptLoader.getSerializer().toList(SceneSuite.class, this.bindings.get("suites")); + this.regions = ScriptLoader.getSerializer().toList(SceneRegion.class, this.bindings.get("regions")).stream() .collect(Collectors.toMap(x -> x.config_id, y -> y)); - regions.values().forEach(m -> m.group = this); + this.regions.values().forEach(m -> m.group = this); - init_config = ScriptLoader.getSerializer().toObject(SceneInitConfig.class, bindings.get("init_config")); + this.init_config = ScriptLoader.getSerializer().toObject(SceneInitConfig.class, this.bindings.get("init_config")); - // Garbages TODO fix properly later - Object garbagesValue = bindings.get("garbages"); - if (garbagesValue != null && garbagesValue instanceof LuaValue garbagesTable) { - garbages = new SceneGarbage(); + // Garbages // TODO: fix properly later + Object garbagesValue = this.bindings.get("garbages"); + if (garbagesValue instanceof LuaValue garbagesTable) { + this.garbages = new SceneGarbage(); if (garbagesTable.checktable().get("gadgets") != LuaValue.NIL) { - garbages.gadgets = ScriptLoader.getSerializer().toList(SceneGadget.class, garbagesTable.checktable().get("gadgets").checktable()); - garbages.gadgets.forEach(m -> m.group = this); + this.garbages.gadgets = ScriptLoader.getSerializer().toList(SceneGadget.class, garbagesTable.checktable().get("gadgets").checktable()); + this.garbages.gadgets.forEach(m -> m.group = this); } } - + // Add variables to suite - variables = ScriptLoader.getSerializer().toList(SceneVar.class, bindings.get("variables")); + this.variables = ScriptLoader.getSerializer().toList(SceneVar.class, this.bindings.get("variables")); // NPC in groups - npc = ScriptLoader.getSerializer().toList(SceneNPC.class, bindings.get("npcs")).stream() + this.npc = ScriptLoader.getSerializer().toList(SceneNPC.class, this.bindings.get("npcs")).stream() .collect(Collectors.toMap(x -> x.npc_id, y -> y)); - npc.values().forEach(n -> n.group = this); + this.npc.values().forEach(n -> n.group = this); // Add monsters and gadgets to suite - for (SceneSuite suite : suites) { + for (SceneSuite suite : this.suites) { suite.sceneMonsters = new ArrayList<>( suite.monsters.stream() - .filter(monsters::containsKey) - .map(monsters::get) + .filter(this.monsters::containsKey) + .map(this.monsters::get) .toList() ); suite.sceneGadgets = new ArrayList<>( suite.gadgets.stream() - .filter(gadgets::containsKey) - .map(gadgets::get) + .filter(this.gadgets::containsKey) + .map(this.gadgets::get) .toList() ); suite.sceneTriggers = new ArrayList<>( suite.triggers.stream() - .filter(triggers::containsKey) - .map(triggers::get) + .filter(this.triggers::containsKey) + .map(this.triggers::get) .toList() ); suite.sceneRegions = new ArrayList<>( suite.regions.stream() - .filter(regions::containsKey) - .map(regions::get) + .filter(this.regions::containsKey) + .map(this.regions::get) .toList() ); } } catch (ScriptException e) { - Grasscutter.getLogger().error("Error loading group " + id + " in scene " + sceneId, e); + Grasscutter.getLogger().error("An error occurred while loading group " + this.id + " in scene " + sceneId + ".", e); } - Grasscutter.getLogger().info("group {} in scene {} is loaded successfully.", id, sceneId); + Grasscutter.getLogger().debug("Successfully loaded group {} in scene {}.", this.id, sceneId); return this; } public Optional searchBossChestInGroup() { - return gadgets.values().stream() + return this.gadgets.values().stream() .filter(g -> g.boss_chest != null && g.boss_chest.monster_config_id > 0) .map(g -> g.boss_chest) .findFirst(); diff --git a/src/main/java/emu/grasscutter/scripts/data/SceneMeta.java b/src/main/java/emu/grasscutter/scripts/data/SceneMeta.java index 212c2c85d..9d6cf1f1a 100644 --- a/src/main/java/emu/grasscutter/scripts/data/SceneMeta.java +++ b/src/main/java/emu/grasscutter/scripts/data/SceneMeta.java @@ -43,18 +43,18 @@ public class SceneMeta { } // Create bindings - context = ScriptLoader.getEngine().createBindings(); + this.context = ScriptLoader.getEngine().createBindings(); // Eval script try { - cs.eval(context); + cs.eval(this.context); - this.config = ScriptLoader.getSerializer().toObject(SceneConfig.class, context.get("scene_config")); + this.config = ScriptLoader.getSerializer().toObject(SceneConfig.class, this.context.get("scene_config")); // TODO optimize later // Create blocks - List blockIds = ScriptLoader.getSerializer().toList(Integer.class, context.get("blocks")); - List blocks = ScriptLoader.getSerializer().toList(SceneBlock.class, context.get("block_rects")); + List blockIds = ScriptLoader.getSerializer().toList(Integer.class, this.context.get("blocks")); + List blocks = ScriptLoader.getSerializer().toList(SceneBlock.class, this.context.get("block_rects")); for (int i = 0; i < blocks.size(); i++) { SceneBlock block = blocks.get(i); @@ -65,11 +65,11 @@ public class SceneMeta { this.blocks = blocks.stream().collect(Collectors.toMap(b -> b.id, b -> b)); this.sceneBlockIndex = SceneIndexManager.buildIndex(2, blocks, SceneBlock::toRectangle); - } catch (ScriptException e) { - Grasscutter.getLogger().error("Error running script", e); + } catch (ScriptException exception) { + Grasscutter.getLogger().error("An error occurred while running a script.", exception); return null; } - Grasscutter.getLogger().info("scene {} metadata is loaded successfully.", sceneId); + Grasscutter.getLogger().debug("Successfully loaded metadata in scene {}.", sceneId); return this; } }