From 5acb9b9f2332683a641ceeef575529c8d2d01261 Mon Sep 17 00:00:00 2001 From: PeyaPeyaPeyang Date: Sun, 19 Nov 2023 20:38:14 +0900 Subject: [PATCH] refactor(actions): Generics of EntityArgumentHolder --- .../actions/entity/AbstractEntityAction.java | 18 +++++++++++-- .../entity/AbstractEntityActionArgument.java | 8 +++--- .../action/actions/entity/EntityAction.java | 4 +-- .../actions/entity/EntityArgumentHolder.java | 27 +++++++++++++------ .../actions/entity/EntityDamageAction.java | 4 +-- .../entity/EntityDamageByEntityAction.java | 2 +- .../actions/entity/EntityDeathAction.java | 4 +-- .../actions/entity/EntityDropItemAction.java | 4 +-- .../actions/entity/EntityMoveAction.java | 4 +-- .../entity/EntityPickupItemAction.java | 7 +++-- .../actions/entity/EntityPlaceAction.java | 5 ++-- .../player/PlayerBucketEntityAction.java | 4 +-- .../player/PlayerInteractEntityAction.java | 4 +-- 13 files changed, 60 insertions(+), 35 deletions(-) diff --git a/ScenamaticaActionEngine/src/main/java/org/kunlab/scenamatica/action/actions/entity/AbstractEntityAction.java b/ScenamaticaActionEngine/src/main/java/org/kunlab/scenamatica/action/actions/entity/AbstractEntityAction.java index 0fe9da1a3..bc51a7e5a 100644 --- a/ScenamaticaActionEngine/src/main/java/org/kunlab/scenamatica/action/actions/entity/AbstractEntityAction.java +++ b/ScenamaticaActionEngine/src/main/java/org/kunlab/scenamatica/action/actions/entity/AbstractEntityAction.java @@ -8,6 +8,7 @@ import org.kunlab.scenamatica.action.utils.EntityUtils; import org.kunlab.scenamatica.interfaces.scenario.ScenarioEngine; import org.kunlab.scenamatica.interfaces.scenariofile.BeanSerializer; +import org.kunlab.scenamatica.interfaces.scenariofile.entity.EntityBean; import java.util.ArrayList; import java.util.List; @@ -49,11 +50,24 @@ protected boolean checkMatchedEntity(String specifier, @NotNull Entity actualEnt .anyMatch(entity -> Objects.equals(entity.getUniqueId(), actualEntity.getUniqueId())); } - protected EntityArgumentHolder deserializeTarget(Map map, BeanSerializer serializer) + protected EntityArgumentHolder deserializeTarget(Map map, BeanSerializer serializer) { return EntityArgumentHolder.tryDeserialize( map.get(AbstractEntityActionArgument.KEY_TARGET_ENTITY), - serializer + serializer, + EntityBean.class + ); + } + + protected EntityArgumentHolder deserializeTarget( + Map map, + BeanSerializer serializer, + Class beanClass) + { + return EntityArgumentHolder.tryDeserialize( + map.get(AbstractEntityActionArgument.KEY_TARGET_ENTITY), + serializer, + beanClass ); } } diff --git a/ScenamaticaActionEngine/src/main/java/org/kunlab/scenamatica/action/actions/entity/AbstractEntityActionArgument.java b/ScenamaticaActionEngine/src/main/java/org/kunlab/scenamatica/action/actions/entity/AbstractEntityActionArgument.java index 999e15071..f0431ae0c 100644 --- a/ScenamaticaActionEngine/src/main/java/org/kunlab/scenamatica/action/actions/entity/AbstractEntityActionArgument.java +++ b/ScenamaticaActionEngine/src/main/java/org/kunlab/scenamatica/action/actions/entity/AbstractEntityActionArgument.java @@ -11,12 +11,12 @@ import java.util.Objects; @AllArgsConstructor -public abstract class AbstractEntityActionArgument extends AbstractActionArgument +public abstract class AbstractEntityActionArgument extends AbstractActionArgument { public static final String KEY_TARGET_ENTITY = "target"; @Nullable - private final EntityArgumentHolder entity; + private final EntityArgumentHolder entity; @Override public boolean isSame(TriggerArgument argument) @@ -27,7 +27,7 @@ public boolean isSame(TriggerArgument argument) if (!AbstractEntityActionArgument.class.isAssignableFrom(argument.getClass())) return false; - return Objects.equals(this.entity, ((AbstractEntityActionArgument) argument).entity); + return Objects.equals(this.entity, ((AbstractEntityActionArgument) argument).entity); } private void ensureTargetIsSet() @@ -70,7 +70,7 @@ public String getArgumentString() return this.entity.getArgumentString(); } - public EntityArgumentHolder getTargetHolder() + public EntityArgumentHolder getTargetHolder() { return this.entity; } diff --git a/ScenamaticaActionEngine/src/main/java/org/kunlab/scenamatica/action/actions/entity/EntityAction.java b/ScenamaticaActionEngine/src/main/java/org/kunlab/scenamatica/action/actions/entity/EntityAction.java index 11b131206..5c2188c10 100644 --- a/ScenamaticaActionEngine/src/main/java/org/kunlab/scenamatica/action/actions/entity/EntityAction.java +++ b/ScenamaticaActionEngine/src/main/java/org/kunlab/scenamatica/action/actions/entity/EntityAction.java @@ -80,13 +80,13 @@ public Argument deserializeArgument(@NotNull Map map, @NotNull B @Value @EqualsAndHashCode(callSuper = true) - public static class Argument extends AbstractEntityActionArgument + public static class Argument extends AbstractEntityActionArgument { public static final String KEY_ENTITY = "entity"; EntityBean entity; - public Argument(EntityArgumentHolder target, @Nullable EntityBean entity) + public Argument(EntityArgumentHolder target, @Nullable EntityBean entity) { super(target); this.entity = entity; diff --git a/ScenamaticaActionEngine/src/main/java/org/kunlab/scenamatica/action/actions/entity/EntityArgumentHolder.java b/ScenamaticaActionEngine/src/main/java/org/kunlab/scenamatica/action/actions/entity/EntityArgumentHolder.java index 5a2de0dae..5ee31f21c 100644 --- a/ScenamaticaActionEngine/src/main/java/org/kunlab/scenamatica/action/actions/entity/EntityArgumentHolder.java +++ b/ScenamaticaActionEngine/src/main/java/org/kunlab/scenamatica/action/actions/entity/EntityArgumentHolder.java @@ -15,9 +15,9 @@ @Getter @EqualsAndHashCode -public class EntityArgumentHolder +public class EntityArgumentHolder { - public static final EntityArgumentHolder EMPTY = new EntityArgumentHolder(null); + public static final EntityArgumentHolder EMPTY = new EntityArgumentHolder<>(null); @Nullable protected final String targetSpecifier; @@ -38,36 +38,47 @@ public EntityArgumentHolder(@Nullable Object mayTarget) } } - public static EntityArgumentHolder tryDeserialize(Object obj, BeanSerializer serializer) + public static EntityArgumentHolder tryDeserialize( + Object obj, + BeanSerializer serializer, + Class beanClass + ) { if (obj == null) - return EMPTY; + // noinspection unchecked + return (EntityArgumentHolder) EMPTY; if (obj instanceof String || obj instanceof EntityBean) - return new EntityArgumentHolder(obj); + return new EntityArgumentHolder<>(obj); if (obj instanceof Map) { // noinspection unchecked Map map = (Map) obj; - return new EntityArgumentHolder(serializer.deserialize(map, EntityBean.class)); + return new EntityArgumentHolder<>(serializer.deserialize(map, beanClass)); } throw new IllegalArgumentException("Cannot deserialize EntityArgumentHolder from " + obj); } + public static EntityArgumentHolder tryDeserialize(Object obj, BeanSerializer serializer) + { + return tryDeserialize(obj, serializer, EntityBean.class); + } + public boolean isSelectable() { return this.targetSpecifier != null; } - public Entity selectTarget() + public E selectTarget() { if (this.targetSpecifier == null) throw new IllegalStateException("Cannot select target from targetBean"); - return EntityUtils.getPlayerOrEntityOrThrow(this.targetSpecifier); + //noinspection unchecked + return (E) EntityUtils.getPlayerOrEntityOrThrow(this.targetSpecifier); } public String getTargetString() diff --git a/ScenamaticaActionEngine/src/main/java/org/kunlab/scenamatica/action/actions/entity/EntityDamageAction.java b/ScenamaticaActionEngine/src/main/java/org/kunlab/scenamatica/action/actions/entity/EntityDamageAction.java index 47cea653a..b9b69342e 100644 --- a/ScenamaticaActionEngine/src/main/java/org/kunlab/scenamatica/action/actions/entity/EntityDamageAction.java +++ b/ScenamaticaActionEngine/src/main/java/org/kunlab/scenamatica/action/actions/entity/EntityDamageAction.java @@ -112,7 +112,7 @@ public A deserializeArgument(@NotNull Map map, @NotNull BeanSeri @Getter @EqualsAndHashCode(callSuper = true) - public static class Argument extends AbstractEntityActionArgument + public static class Argument extends AbstractEntityActionArgument { public static final String KEY_DAMAGE_CAUSE = "cause"; public static final String KEY_AMOUNT = "amount"; // 最終ダメージ @@ -122,7 +122,7 @@ public static class Argument extends AbstractEntityActionArgument private final Double amount; private final Map modifiers; - public Argument(EntityArgumentHolder target, EntityDamageEvent.DamageCause cause, Double amount, Map modifiers) + public Argument(EntityArgumentHolder target, EntityDamageEvent.DamageCause cause, Double amount, Map modifiers) { super(target); this.cause = cause; diff --git a/ScenamaticaActionEngine/src/main/java/org/kunlab/scenamatica/action/actions/entity/EntityDamageByEntityAction.java b/ScenamaticaActionEngine/src/main/java/org/kunlab/scenamatica/action/actions/entity/EntityDamageByEntityAction.java index 396b7db76..5d4f665c8 100644 --- a/ScenamaticaActionEngine/src/main/java/org/kunlab/scenamatica/action/actions/entity/EntityDamageByEntityAction.java +++ b/ScenamaticaActionEngine/src/main/java/org/kunlab/scenamatica/action/actions/entity/EntityDamageByEntityAction.java @@ -91,7 +91,7 @@ public static class Argument extends EntityDamageAction.Argument String damager; @SuppressWarnings("deprecation") // DamageModifier は消えるとか言ってるけど多分きえない。たぶん。というかまだある。 - public Argument(EntityArgumentHolder target, EntityDamageEvent.DamageCause cause, Double amount, Map modifiers, String damager) + public Argument(EntityArgumentHolder target, EntityDamageEvent.DamageCause cause, Double amount, Map modifiers, String damager) { super(target, cause, amount, modifiers); this.damager = damager; diff --git a/ScenamaticaActionEngine/src/main/java/org/kunlab/scenamatica/action/actions/entity/EntityDeathAction.java b/ScenamaticaActionEngine/src/main/java/org/kunlab/scenamatica/action/actions/entity/EntityDeathAction.java index 92842b7b7..d6f6c0b1d 100644 --- a/ScenamaticaActionEngine/src/main/java/org/kunlab/scenamatica/action/actions/entity/EntityDeathAction.java +++ b/ScenamaticaActionEngine/src/main/java/org/kunlab/scenamatica/action/actions/entity/EntityDeathAction.java @@ -117,7 +117,7 @@ public Argument deserializeArgument(@NotNull Map map, @NotNull B @Value @EqualsAndHashCode(callSuper = true) - public static class Argument extends AbstractEntityActionArgument + public static class Argument extends AbstractEntityActionArgument { public static final String KEY_DROPS = "drops"; public static final String KEY_DROP_EXP = "dropExp"; @@ -139,7 +139,7 @@ public static class Argument extends AbstractEntityActionArgument Float deathSoundVolume; Float deathSoundPitch; - public Argument(@Nullable EntityArgumentHolder mayTarget, List drops, Integer dropExp, Double reviveHealth, Boolean shouldPlayDeathSound, Sound deathSound, SoundCategory deathSoundCategory, Float deathSoundVolume, Float deathSoundPitch) + public Argument(@Nullable EntityArgumentHolder mayTarget, List drops, Integer dropExp, Double reviveHealth, Boolean shouldPlayDeathSound, Sound deathSound, SoundCategory deathSoundCategory, Float deathSoundVolume, Float deathSoundPitch) { super(mayTarget); this.drops = drops; diff --git a/ScenamaticaActionEngine/src/main/java/org/kunlab/scenamatica/action/actions/entity/EntityDropItemAction.java b/ScenamaticaActionEngine/src/main/java/org/kunlab/scenamatica/action/actions/entity/EntityDropItemAction.java index 2694e4949..20f7dddb7 100644 --- a/ScenamaticaActionEngine/src/main/java/org/kunlab/scenamatica/action/actions/entity/EntityDropItemAction.java +++ b/ScenamaticaActionEngine/src/main/java/org/kunlab/scenamatica/action/actions/entity/EntityDropItemAction.java @@ -98,13 +98,13 @@ public Argument deserializeArgument(@NotNull Map map, @NotNull B @Value @EqualsAndHashCode(callSuper = true) - public static class Argument extends AbstractEntityActionArgument + public static class Argument extends AbstractEntityActionArgument { public static final String KEY_DROP_ITEM = "item"; EntityItemBean item; - public Argument(@Nullable EntityArgumentHolder mayTarget, EntityItemBean item) + public Argument(@Nullable EntityArgumentHolder mayTarget, EntityItemBean item) { super(mayTarget); this.item = item; diff --git a/ScenamaticaActionEngine/src/main/java/org/kunlab/scenamatica/action/actions/entity/EntityMoveAction.java b/ScenamaticaActionEngine/src/main/java/org/kunlab/scenamatica/action/actions/entity/EntityMoveAction.java index a194591bf..325123e40 100644 --- a/ScenamaticaActionEngine/src/main/java/org/kunlab/scenamatica/action/actions/entity/EntityMoveAction.java +++ b/ScenamaticaActionEngine/src/main/java/org/kunlab/scenamatica/action/actions/entity/EntityMoveAction.java @@ -88,7 +88,7 @@ public Argument deserializeArgument(@NotNull Map map, @NotNull B @Value @EqualsAndHashCode(callSuper = true) - public static class Argument extends AbstractEntityActionArgument + public static class Argument extends AbstractEntityActionArgument { public static final String KEY_FROM = "from"; public static final String KEY_TO = "to"; @@ -99,7 +99,7 @@ public static class Argument extends AbstractEntityActionArgument // Execute のときのみ. デフォは true -> テレポート. boolean useAI; - public Argument(@Nullable EntityArgumentHolder mayTarget, Location from, Location to, boolean useAI) + public Argument(@Nullable EntityArgumentHolder mayTarget, Location from, Location to, boolean useAI) { super(mayTarget); this.from = from; diff --git a/ScenamaticaActionEngine/src/main/java/org/kunlab/scenamatica/action/actions/entity/EntityPickupItemAction.java b/ScenamaticaActionEngine/src/main/java/org/kunlab/scenamatica/action/actions/entity/EntityPickupItemAction.java index ca811649c..99890fd9a 100644 --- a/ScenamaticaActionEngine/src/main/java/org/kunlab/scenamatica/action/actions/entity/EntityPickupItemAction.java +++ b/ScenamaticaActionEngine/src/main/java/org/kunlab/scenamatica/action/actions/entity/EntityPickupItemAction.java @@ -147,7 +147,7 @@ public Argument deserializeArgument(@NotNull Map map, @NotNull B @Value @EqualsAndHashCode(callSuper = true) - public static class Argument extends AbstractEntityActionArgument + public static class Argument extends AbstractEntityActionArgument { public static final String KEY_REMAINING = "remaining"; public static final String KEY_ITEM = "item"; @@ -156,7 +156,7 @@ public static class Argument extends AbstractEntityActionArgument EntityItemBean item; String itemSelector; - public Argument(@Nullable EntityArgumentHolder mayTarget, Integer remaining, EntityItemBean item) + public Argument(@Nullable EntityArgumentHolder mayTarget, Integer remaining, EntityItemBean item) { super(mayTarget); this.remaining = remaining; @@ -164,7 +164,7 @@ public Argument(@Nullable EntityArgumentHolder mayTarget, Integer remaining, Ent this.itemSelector = null; } - public Argument(@Nullable EntityArgumentHolder mayTarget, Integer remaining, String itemSelector) + public Argument(@Nullable EntityArgumentHolder mayTarget, Integer remaining, String itemSelector) { super(mayTarget); this.remaining = remaining; @@ -182,7 +182,6 @@ public Item selectItem() if (!this.isItemSelectable()) throw new IllegalArgumentException("Item is not selectable."); - assert this.itemSelector != null; List entities = EntityUtils.selectEntities(this.itemSelector); if (entities.isEmpty()) throw new IllegalStateException("No entity found: " + this.itemSelector); diff --git a/ScenamaticaActionEngine/src/main/java/org/kunlab/scenamatica/action/actions/entity/EntityPlaceAction.java b/ScenamaticaActionEngine/src/main/java/org/kunlab/scenamatica/action/actions/entity/EntityPlaceAction.java index bc38a4311..eec68657a 100644 --- a/ScenamaticaActionEngine/src/main/java/org/kunlab/scenamatica/action/actions/entity/EntityPlaceAction.java +++ b/ScenamaticaActionEngine/src/main/java/org/kunlab/scenamatica/action/actions/entity/EntityPlaceAction.java @@ -5,6 +5,7 @@ import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.block.BlockFace; +import org.bukkit.entity.Entity; import org.bukkit.entity.Player; import org.bukkit.event.Event; import org.bukkit.event.entity.EntityPlaceEvent; @@ -170,7 +171,7 @@ public Argument deserializeArgument(@NotNull Map map, @NotNull B @Value @EqualsAndHashCode(callSuper = true) - public static class Argument extends AbstractEntityActionArgument + public static class Argument extends AbstractEntityActionArgument { public static final String KEY_PLAYER = "player"; public static final String KEY_BLOCK = "block"; @@ -180,7 +181,7 @@ public static class Argument extends AbstractEntityActionArgument BlockBean block; BlockFace blockFace; - public Argument(EntityArgumentHolder target, String playerSpecifier, BlockBean block, BlockFace blockFace) + public Argument(EntityArgumentHolder target, String playerSpecifier, BlockBean block, BlockFace blockFace) { super(target); this.playerSpecifier = playerSpecifier; diff --git a/ScenamaticaActionEngine/src/main/java/org/kunlab/scenamatica/action/actions/player/PlayerBucketEntityAction.java b/ScenamaticaActionEngine/src/main/java/org/kunlab/scenamatica/action/actions/player/PlayerBucketEntityAction.java index 56dd4497b..390e075f3 100644 --- a/ScenamaticaActionEngine/src/main/java/org/kunlab/scenamatica/action/actions/player/PlayerBucketEntityAction.java +++ b/ScenamaticaActionEngine/src/main/java/org/kunlab/scenamatica/action/actions/player/PlayerBucketEntityAction.java @@ -145,11 +145,11 @@ public static class Argument extends AbstractPlayerActionArgument public static final String KEY_ORIGINAL_BUCKET = "bucket"; public static final String KEY_ENTITY_BUCKET = "entityBucket"; - EntityArgumentHolder entity; + EntityArgumentHolder entity; ItemStackBean originalBucket; ItemStackBean entityBucket; - public Argument(String target, EntityArgumentHolder entity, ItemStackBean originalBucket, ItemStackBean entityBucket) + public Argument(String target, EntityArgumentHolder entity, ItemStackBean originalBucket, ItemStackBean entityBucket) { super(target); this.entity = entity; diff --git a/ScenamaticaActionEngine/src/main/java/org/kunlab/scenamatica/action/actions/player/PlayerInteractEntityAction.java b/ScenamaticaActionEngine/src/main/java/org/kunlab/scenamatica/action/actions/player/PlayerInteractEntityAction.java index 2534bbe35..e797c7e1e 100644 --- a/ScenamaticaActionEngine/src/main/java/org/kunlab/scenamatica/action/actions/player/PlayerInteractEntityAction.java +++ b/ScenamaticaActionEngine/src/main/java/org/kunlab/scenamatica/action/actions/player/PlayerInteractEntityAction.java @@ -112,10 +112,10 @@ public static class Argument extends AbstractPlayerActionArgument public static final String KEY_ENTITY = "entity"; public static final String KEY_HAND = "hand"; - EntityArgumentHolder entity; + EntityArgumentHolder entity; EquipmentSlot hand; - public Argument(String target, EntityArgumentHolder entity, EquipmentSlot hand) + public Argument(String target, EntityArgumentHolder entity, EquipmentSlot hand) { super(target); this.entity = entity;