From de8b0be36e7449550bbda889ad316a41b1fc92dd Mon Sep 17 00:00:00 2001 From: ImmuState Date: Thu, 26 May 2022 02:37:40 -0700 Subject: [PATCH] Introduce a simpler way to get the original owner of an EntityClientGadget --- .../game/entity/EntityClientGadget.java | 14 ++++++++++++++ .../game/managers/EnergyManager/EnergyManager.java | 13 +++++-------- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/src/main/java/emu/grasscutter/game/entity/EntityClientGadget.java b/src/main/java/emu/grasscutter/game/entity/EntityClientGadget.java index 77b76566a..5ba18d399 100644 --- a/src/main/java/emu/grasscutter/game/entity/EntityClientGadget.java +++ b/src/main/java/emu/grasscutter/game/entity/EntityClientGadget.java @@ -35,6 +35,8 @@ public class EntityClientGadget extends EntityBaseGadget { private int ownerEntityId; private int targetEntityId; private boolean asyncLoad; + + private int originalOwnerEntityId; public EntityClientGadget(Scene scene, Player player, EvtCreateGadgetNotify notify) { super(scene); @@ -48,6 +50,14 @@ public class EntityClientGadget extends EntityBaseGadget { this.ownerEntityId = notify.getPropOwnerEntityId(); this.targetEntityId = notify.getTargetEntityId(); this.asyncLoad = notify.getIsAsyncLoad(); + + GameEntity owner = scene.getEntityById(this.ownerEntityId); + if (owner instanceof EntityClientGadget ownerGadget) { + this.originalOwnerEntityId = ownerGadget.getOriginalOwnerEntityId(); + } + else { + this.originalOwnerEntityId = this.ownerEntityId; + } } @Override @@ -79,6 +89,10 @@ public class EntityClientGadget extends EntityBaseGadget { return this.asyncLoad; } + public int getOriginalOwnerEntityId() { + return this.originalOwnerEntityId; + } + @Override public void onDeath(int killerId) { diff --git a/src/main/java/emu/grasscutter/game/managers/EnergyManager/EnergyManager.java b/src/main/java/emu/grasscutter/game/managers/EnergyManager/EnergyManager.java index 5bdb803fa..03ffd6697 100644 --- a/src/main/java/emu/grasscutter/game/managers/EnergyManager/EnergyManager.java +++ b/src/main/java/emu/grasscutter/game/managers/EnergyManager/EnergyManager.java @@ -88,7 +88,7 @@ public class EnergyManager { // or it can be an `EntityClientGadget`, owned (some way up the owner hierarchy) by the avatar // that cast the skill. int res = 0; - + // Try to get the invoking entity from the scene. GameEntity entity = player.getScene().getEntityById(invokeEntityId); @@ -98,13 +98,10 @@ public class EnergyManager { if (!(entity instanceof EntityClientGadget)) { res = invokeEntityId; } - // If the entity is a `EntityClientGadget`, we need to "walk up" the owner hierarchy, - // until the owner is no longer a gadget. This should then be the ID of the casting avatar. + // If the entity is a `EntityClientGadget`, we need to find the ID of the original + // owner of that gadget. else { - while (entity instanceof EntityClientGadget gadget) { - res = gadget.getOwnerEntityId(); - entity = player.getScene().getEntityById(gadget.getOwnerEntityId()); - } + res = ((EntityClientGadget)entity).getOriginalOwnerEntityId(); } return res; @@ -139,9 +136,9 @@ public class EnergyManager { .findFirst(); // Bug: invokes twice sometimes, Ayato, Keqing - // Amber not getting element properly // ToDo: deal with press, hold difference. deal with charge(Beidou, Yunjin) if (avatarEntity.isPresent()) { + Grasscutter.getLogger().info("Found entity: {}", avatarEntity.get()); Avatar avatar = avatarEntity.get().getAvatar(); if (avatar != null) {