mirror of
https://github.com/Grasscutters/Grasscutter.git
synced 2025-12-16 17:05:20 +01:00
optimize npc group load & fix some NPE in suite
This commit is contained in:
@@ -5,15 +5,11 @@ import emu.grasscutter.scripts.ScriptLoader;
|
||||
import emu.grasscutter.utils.Position;
|
||||
import lombok.Setter;
|
||||
import lombok.ToString;
|
||||
import org.luaj.vm2.LuaValue;
|
||||
|
||||
import javax.script.Bindings;
|
||||
import javax.script.CompiledScript;
|
||||
import javax.script.ScriptException;
|
||||
|
||||
import org.luaj.vm2.LuaTable;
|
||||
import org.luaj.vm2.LuaValue;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
@@ -33,7 +29,6 @@ public class SceneGroup {
|
||||
public Map<Integer,SceneMonster> monsters; // <ConfigId, Monster>
|
||||
public Map<Integer, SceneGadget> gadgets; // <ConfigId, Gadgets>
|
||||
public Map<String, SceneTrigger> triggers;
|
||||
public Map<Integer, SceneNPC> npc; // <NpcId, NPC>
|
||||
public Map<Integer, SceneRegion> regions;
|
||||
public List<SceneSuite> suites;
|
||||
public List<SceneVar> variables;
|
||||
@@ -132,42 +127,10 @@ public class SceneGroup {
|
||||
}
|
||||
|
||||
// Add variables to suite
|
||||
this.variables = ScriptLoader.getSerializer().toList(SceneVar.class, this.bindings.get("variables"));
|
||||
// NPC in groups
|
||||
this.npc = ScriptLoader.getSerializer().toList(SceneNPC.class, this.bindings.get("npcs")).stream()
|
||||
.collect(Collectors.toMap(x -> x.npc_id, y -> y));
|
||||
this.npc.values().forEach(n -> n.group = this);
|
||||
this.variables = ScriptLoader.getSerializer().toList(SceneVar.class, this.bindings.get("variables"));
|
||||
|
||||
// Add monsters and gadgets to suite
|
||||
for (SceneSuite suite : this.suites) {
|
||||
suite.sceneMonsters = new ArrayList<>(
|
||||
suite.monsters.stream()
|
||||
.filter(this.monsters::containsKey)
|
||||
.map(this.monsters::get)
|
||||
.toList()
|
||||
);
|
||||
|
||||
suite.sceneGadgets = new ArrayList<>(
|
||||
suite.gadgets.stream()
|
||||
.filter(this.gadgets::containsKey)
|
||||
.map(this.gadgets::get)
|
||||
.toList()
|
||||
);
|
||||
|
||||
suite.sceneTriggers = new ArrayList<>(
|
||||
suite.triggers.stream()
|
||||
.filter(this.triggers::containsKey)
|
||||
.map(this.triggers::get)
|
||||
.toList()
|
||||
);
|
||||
|
||||
suite.sceneRegions = new ArrayList<>(
|
||||
suite.regions.stream()
|
||||
.filter(this.regions::containsKey)
|
||||
.map(this.regions::get)
|
||||
.toList()
|
||||
);
|
||||
}
|
||||
this.suites.forEach(i -> i.init(this));
|
||||
|
||||
} catch (ScriptException e) {
|
||||
Grasscutter.getLogger().error("An error occurred while loading group " + this.id + " in scene " + sceneId + ".", e);
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package emu.grasscutter.scripts.data;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import lombok.Setter;
|
||||
@@ -8,15 +9,53 @@ import lombok.ToString;
|
||||
@ToString
|
||||
@Setter
|
||||
public class SceneSuite {
|
||||
public List<Integer> monsters;
|
||||
public List<Integer> gadgets;
|
||||
public List<String> triggers;
|
||||
public List<Integer> regions;
|
||||
// make it refer the default empty list to avoid NPE caused by some group
|
||||
public List<Integer> monsters = List.of();
|
||||
public List<Integer> gadgets = List.of();
|
||||
public List<String> triggers = List.of();
|
||||
public List<Integer> regions = List.of();
|
||||
public int rand_weight;
|
||||
|
||||
public transient List<SceneMonster> sceneMonsters;
|
||||
public transient List<SceneGadget> sceneGadgets;
|
||||
public transient List<SceneTrigger> sceneTriggers;
|
||||
public transient List<SceneRegion> sceneRegions;
|
||||
public transient List<SceneMonster> sceneMonsters = List.of();
|
||||
public transient List<SceneGadget> sceneGadgets = List.of();
|
||||
public transient List<SceneTrigger> sceneTriggers = List.of();
|
||||
public transient List<SceneRegion> sceneRegions = List.of();
|
||||
|
||||
public void init(SceneGroup sceneGroup) {
|
||||
if(sceneGroup.monsters != null){
|
||||
this.sceneMonsters = new ArrayList<>(
|
||||
this.monsters.stream()
|
||||
.filter(sceneGroup.monsters::containsKey)
|
||||
.map(sceneGroup.monsters::get)
|
||||
.toList()
|
||||
);
|
||||
}
|
||||
|
||||
if(sceneGroup.gadgets != null){
|
||||
this.sceneGadgets = new ArrayList<>(
|
||||
this.gadgets.stream()
|
||||
.filter(sceneGroup.gadgets::containsKey)
|
||||
.map(sceneGroup.gadgets::get)
|
||||
.toList()
|
||||
);
|
||||
}
|
||||
|
||||
if(sceneGroup.triggers != null) {
|
||||
this.sceneTriggers = new ArrayList<>(
|
||||
this.triggers.stream()
|
||||
.filter(sceneGroup.triggers::containsKey)
|
||||
.map(sceneGroup.triggers::get)
|
||||
.toList()
|
||||
);
|
||||
}
|
||||
if(sceneGroup.regions != null) {
|
||||
this.sceneRegions = new ArrayList<>(
|
||||
this.regions.stream()
|
||||
.filter(sceneGroup.regions::containsKey)
|
||||
.map(sceneGroup.regions::get)
|
||||
.toList()
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user