From cd77fb90a3449f9ccdd8902c93dfa686f4b5347b Mon Sep 17 00:00:00 2001 From: Melledy <52122272+Melledy@users.noreply.github.com> Date: Thu, 19 May 2022 00:36:38 -0700 Subject: [PATCH] Move gadget/monster creation events to after they are spawned in scene --- .../grasscutter/game/entity/EntityGadget.java | 8 +++++++ .../game/entity/EntityMonster.java | 6 ++++++ .../grasscutter/game/entity/GameEntity.java | 21 +++++++++++++++---- .../emu/grasscutter/game/world/Scene.java | 1 + .../scripts/SceneScriptManager.java | 3 --- 5 files changed, 32 insertions(+), 7 deletions(-) diff --git a/src/main/java/emu/grasscutter/game/entity/EntityGadget.java b/src/main/java/emu/grasscutter/game/entity/EntityGadget.java index db7e4b80e..099950226 100644 --- a/src/main/java/emu/grasscutter/game/entity/EntityGadget.java +++ b/src/main/java/emu/grasscutter/game/entity/EntityGadget.java @@ -28,6 +28,8 @@ import emu.grasscutter.net.proto.SceneEntityInfoOuterClass.SceneEntityInfo; import emu.grasscutter.net.proto.SceneGadgetInfoOuterClass.SceneGadgetInfo; import emu.grasscutter.net.proto.VectorOuterClass.Vector; import emu.grasscutter.net.proto.WorktopInfoOuterClass.WorktopInfo; +import emu.grasscutter.scripts.constants.EventType; +import emu.grasscutter.scripts.data.ScriptArgs; import emu.grasscutter.server.packet.send.PacketGadgetStateNotify; import emu.grasscutter.utils.Position; import emu.grasscutter.utils.ProtoHelper; @@ -138,6 +140,12 @@ public class EntityGadget extends EntityBaseGadget { public Int2FloatOpenHashMap getFightProperties() { return null; } + + @Override + public void onCreate() { + // Lua event + getScene().getScriptManager().callEvent(EventType.EVENT_GADGET_CREATE, new ScriptArgs(this.getConfigId())); + } @Override public void onDeath(int killerId) { diff --git a/src/main/java/emu/grasscutter/game/entity/EntityMonster.java b/src/main/java/emu/grasscutter/game/entity/EntityMonster.java index cc1ac1d22..0ccdfef2e 100644 --- a/src/main/java/emu/grasscutter/game/entity/EntityMonster.java +++ b/src/main/java/emu/grasscutter/game/entity/EntityMonster.java @@ -109,6 +109,12 @@ public class EntityMonster extends GameEntity { public void setPoseId(int poseId) { this.poseId = poseId; } + + @Override + public void onCreate() { + // Lua event + getScene().getScriptManager().callEvent(EventType.EVENT_ANY_MONSTER_LIVE, new ScriptArgs(this.getConfigId())); + } @Override public void onDeath(int killerId) { diff --git a/src/main/java/emu/grasscutter/game/entity/GameEntity.java b/src/main/java/emu/grasscutter/game/entity/GameEntity.java index 0cdcc3ebd..515a31cb5 100644 --- a/src/main/java/emu/grasscutter/game/entity/GameEntity.java +++ b/src/main/java/emu/grasscutter/game/entity/GameEntity.java @@ -107,10 +107,6 @@ public abstract class GameEntity { public void setLastMoveReliableSeq(int lastMoveReliableSeq) { this.lastMoveReliableSeq = lastMoveReliableSeq; } - - public abstract SceneEntityInfo toProto(); - - public abstract void onDeath(int killerId); public void setFightProperty(FightProperty prop, float value) { this.getFightProperties().put(prop.getId(), value); @@ -219,4 +215,21 @@ public abstract class GameEntity { getScene().killEntity(this, 0); } } + + /** + * Called when this entity is added to the world + */ + public void onCreate() { + + } + + /** + * Called when this entity dies + * @param killerId Entity id of the entity that killed this entity + */ + public void onDeath(int killerId) { + + } + + public abstract SceneEntityInfo toProto(); } diff --git a/src/main/java/emu/grasscutter/game/world/Scene.java b/src/main/java/emu/grasscutter/game/world/Scene.java index 2e2b34c7d..0852ffa6c 100644 --- a/src/main/java/emu/grasscutter/game/world/Scene.java +++ b/src/main/java/emu/grasscutter/game/world/Scene.java @@ -311,6 +311,7 @@ public class Scene { private void addEntityDirectly(GameEntity entity) { getEntities().put(entity.getId(), entity); + entity.onCreate(); // Call entity create event } public synchronized void addEntity(GameEntity entity) { diff --git a/src/main/java/emu/grasscutter/scripts/SceneScriptManager.java b/src/main/java/emu/grasscutter/scripts/SceneScriptManager.java index bbb475717..11041dee8 100644 --- a/src/main/java/emu/grasscutter/scripts/SceneScriptManager.java +++ b/src/main/java/emu/grasscutter/scripts/SceneScriptManager.java @@ -371,9 +371,6 @@ public class SceneScriptManager { this.getScriptMonsterSpawnService() .onMonsterCreatedListener.forEach(action -> action.onNotify(entity)); - // Lua event - callEvent(EventType.EVENT_ANY_MONSTER_LIVE, new ScriptArgs(entity.getConfigId())); - return entity; }