From adbecb59daf3fb0a80e1c6d064c48d66a2fc503d Mon Sep 17 00:00:00 2001 From: PeyaPeyaPeyang Date: Wed, 15 Nov 2023 21:12:50 +0900 Subject: [PATCH] refactor(commons): Refactor map controlling operations with util method --- .../action/ActionCompilerImpl.java | 6 +-- .../actions/block/AbstractBlockAction.java | 6 +-- .../entity/AbstractEntityActionArgument.java | 11 +++-- .../action/actions/entity/EntityAction.java | 6 +-- .../actions/entity/EntityArgumentHolder.java | 2 +- .../entity/EntityDamageByEntityAction.java | 2 +- .../actions/entity/EntityDeathAction.java | 2 +- .../actions/entity/EntityDropItemAction.java | 6 +-- .../entity/EntityPickupItemAction.java | 8 ++-- .../actions/entity/EntityPlaceAction.java | 6 +-- .../actions/entity/EntitySpawnAction.java | 6 +-- .../inventory/AbstractInventoryAction.java | 6 +-- .../interact/InventoryClickAction.java | 12 +---- .../interact/InventoryCreativeAction.java | 6 +-- .../player/PlayerBucketEntityAction.java | 20 ++++----- .../actions/player/PlayerDropItemAction.java | 5 +-- .../player/PlayerHarvestBlockAction.java | 11 ++--- .../player/PlayerHotbarSlotAction.java | 7 +-- .../player/PlayerInteractBlockAction.java | 6 +-- .../actions/player/PlayerItemBreakAction.java | 6 +-- .../player/PlayerItemConsumeAction.java | 12 +---- .../player/PlayerItemDamageAction.java | 9 ++-- .../player/PlayerLaunchProjectileAction.java | 4 +- .../player/bucket/BucketActionArgument.java | 18 ++------ .../scenamatica/commons/utils/MapUtils.java | 36 +++++++++++++++ .../reporter/packets/test/PacketTestEnd.java | 2 +- .../reporter/packets/test/PacketTestSkip.java | 3 +- .../packets/test/PacketTestStart.java | 2 +- .../scenariofile/DefinitionsMapper.java | 6 +-- .../scenariofile/ScenarioFileParser.java | 2 +- .../beans/ScenarioFileBeanImpl.java | 45 ++++--------------- .../beans/context/ContextBeanImpl.java | 39 ++++------------ .../beans/context/PlayerBeanImpl.java | 7 +-- .../beans/entity/EntityBeanImpl.java | 20 +++------ .../entity/entities/HumanEntityBeanImpl.java | 28 ++++-------- .../entity/entities/ProjectileBeanImpl.java | 20 ++++----- .../beans/inventory/InventoryBeanImpl.java | 12 +---- .../beans/inventory/ItemStackBeanImpl.java | 11 +---- .../inventory/PlayerInventoryBeanImpl.java | 40 ++++------------- .../beans/misc/BlockBeanImpl.java | 28 +++--------- .../beans/scenario/ActionBeanImpl.java | 12 +---- .../beans/scenario/ScenarioBeanImpl.java | 6 +-- .../beans/trigger/TriggerBeanImpl.java | 33 ++++---------- .../entity/ProjectileBeanSerializeTest.java | 2 +- 44 files changed, 161 insertions(+), 376 deletions(-) diff --git a/ScenamaticaActionEngine/src/main/java/org/kunlab/scenamatica/action/ActionCompilerImpl.java b/ScenamaticaActionEngine/src/main/java/org/kunlab/scenamatica/action/ActionCompilerImpl.java index 12389bbb6..fdd5ef336 100644 --- a/ScenamaticaActionEngine/src/main/java/org/kunlab/scenamatica/action/ActionCompilerImpl.java +++ b/ScenamaticaActionEngine/src/main/java/org/kunlab/scenamatica/action/ActionCompilerImpl.java @@ -70,11 +70,7 @@ else if (actionToBeNegated instanceof NegateAction) { argument = actionToBeNegated.deserializeArgument( - MapUtils.checkAndCastMap( - arguments.get(NegateAction.Argument.KEY_ARGUMENTS), - String.class, - Object.class - ), + MapUtils.checkAndCastMap(NegateAction.Argument.KEY_ARGUMENTS), engine.getManager().getRegistry().getScenarioFileManager().getSerializer() ); } diff --git a/ScenamaticaActionEngine/src/main/java/org/kunlab/scenamatica/action/actions/block/AbstractBlockAction.java b/ScenamaticaActionEngine/src/main/java/org/kunlab/scenamatica/action/actions/block/AbstractBlockAction.java index e6d3b51e8..ab7b698f1 100644 --- a/ScenamaticaActionEngine/src/main/java/org/kunlab/scenamatica/action/actions/block/AbstractBlockAction.java +++ b/ScenamaticaActionEngine/src/main/java/org/kunlab/scenamatica/action/actions/block/AbstractBlockAction.java @@ -45,11 +45,7 @@ public BlockBean deserializeBlockOrNull(@NotNull Map map, @NotNu return null; return serializer.deserialize( - MapUtils.checkAndCastMap( - map.get(AbstractBlockActionArgument.KEY_BLOCK), - String.class, - Object.class - ), + MapUtils.checkAndCastMap(AbstractBlockActionArgument.KEY_BLOCK), BlockBean.class ); } 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 62e713059..999e15071 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 @@ -18,11 +18,6 @@ public abstract class AbstractEntityActionArgument extends AbstractActionArgumen @Nullable private final EntityArgumentHolder entity; - public AbstractEntityActionArgument(@Nullable EntityArgumentHolder target) - { - this.entity = target; - } - @Override public boolean isSame(TriggerArgument argument) { @@ -75,6 +70,11 @@ public String getArgumentString() return this.entity.getArgumentString(); } + public EntityArgumentHolder getTargetHolder() + { + return this.entity; + } + public EntityBean getTargetBean() { return this.entity == null ? null: this.entity.getTargetBean(); @@ -91,6 +91,5 @@ public void throwIfNotSelectable() { if (!this.isSelectable()) throw new IllegalArgumentException("Cannot select target for this action, please specify target with valid selector."); - } } 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 ab66e683f..a33d2a022 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 @@ -66,11 +66,7 @@ public Argument deserializeArgument(@NotNull Map map, @NotNull B EntityBean bean; if (map.containsKey(Argument.KEY_ENTITY)) bean = serializer.deserialize( - MapUtils.checkAndCastMap( - map.get(Argument.KEY_ENTITY), - String.class, - Object.class - ), + MapUtils.checkAndCastMap(Argument.KEY_ENTITY), EntityBean.class ); else 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 18c0e2ae2..5a2de0dae 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 @@ -51,7 +51,7 @@ public static EntityArgumentHolder tryDeserialize(Object obj, BeanSerializer ser // noinspection unchecked Map map = (Map) obj; - return new EntityArgumentHolder(serializer.deserializeEntity(map)); + return new EntityArgumentHolder(serializer.deserialize(map, EntityBean.class)); } throw new IllegalArgumentException("Cannot deserialize EntityArgumentHolder from " + obj); 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 bfcdff356..396b7db76 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 @@ -74,7 +74,7 @@ public Argument deserializeArgument(@NotNull Map map, @NotNull B String damager = MapUtils.getOrNull(map, Argument.KEY_DAMAGER); return new Argument( - base.getTargetRaw(), + base.getTargetHolder(), base.getCause(), base.getAmount(), base.getModifiers(), 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 9e264c9b3..92842b7b7 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 @@ -98,7 +98,7 @@ public Argument deserializeArgument(@NotNull Map map, @NotNull B drops = new ArrayList<>(); List> dropMaps = MapUtils.getAsList(map, Argument.KEY_DROPS); for (Map dropMap : dropMaps) - drops.add(serializer.deserializeItemStack(dropMap)); + drops.add(serializer.deserialize(dropMap, ItemStackBean.class)); } return new Argument( 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 0a2b66ef9..5af86d0a9 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 @@ -86,11 +86,7 @@ public Argument deserializeArgument(@NotNull Map map, @NotNull B EntityItemBean item = null; if (map.containsKey(Argument.KEY_DROP_ITEM)) item = serializer.deserialize( - MapUtils.checkAndCastMap( - map.get(Argument.KEY_DROP_ITEM), - String.class, - Object.class - ), + MapUtils.checkAndCastMap(Argument.KEY_DROP_ITEM), EntityItemBean.class ); 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 9c5993de4..5a09e9533 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 @@ -138,11 +138,9 @@ public Argument deserializeArgument(@NotNull Map map, @NotNull B return new Argument( super.deserializeTarget(map, serializer), remaining, - serializer.deserializeEntityItem(MapUtils.checkAndCastMap( - map.get(Argument.KEY_ITEM), - String.class, - Object.class - ) + serializer.deserialize( + MapUtils.checkAndCastMap(Argument.KEY_ITEM), + EntityItemBean.class ) ); } 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 e4489cb63..08320ed6a 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 @@ -156,11 +156,7 @@ public Argument deserializeArgument(@NotNull Map map, @NotNull B BlockBean blockBean = null; if (map.containsKey(Argument.KEY_BLOCK)) blockBean = serializer.deserialize( - MapUtils.checkAndCastMap( - map.get(Argument.KEY_BLOCK), - String.class, - Object.class - ), + MapUtils.checkAndCastMap(Argument.KEY_BLOCK), BlockBean.class ); diff --git a/ScenamaticaActionEngine/src/main/java/org/kunlab/scenamatica/action/actions/entity/EntitySpawnAction.java b/ScenamaticaActionEngine/src/main/java/org/kunlab/scenamatica/action/actions/entity/EntitySpawnAction.java index 3686f6dad..66700d3f2 100644 --- a/ScenamaticaActionEngine/src/main/java/org/kunlab/scenamatica/action/actions/entity/EntitySpawnAction.java +++ b/ScenamaticaActionEngine/src/main/java/org/kunlab/scenamatica/action/actions/entity/EntitySpawnAction.java @@ -91,11 +91,7 @@ public Argument deserializeArgument(@NotNull Map map, @NotNull B return new Argument( serializer.deserialize( - MapUtils.checkAndCastMap( - map.get(Argument.KEY_ENTITY), - String.class, - Object.class - ), + MapUtils.checkAndCastMap(Argument.KEY_ENTITY), EntityBean.class )); } diff --git a/ScenamaticaActionEngine/src/main/java/org/kunlab/scenamatica/action/actions/inventory/AbstractInventoryAction.java b/ScenamaticaActionEngine/src/main/java/org/kunlab/scenamatica/action/actions/inventory/AbstractInventoryAction.java index 3d950920e..674708583 100644 --- a/ScenamaticaActionEngine/src/main/java/org/kunlab/scenamatica/action/actions/inventory/AbstractInventoryAction.java +++ b/ScenamaticaActionEngine/src/main/java/org/kunlab/scenamatica/action/actions/inventory/AbstractInventoryAction.java @@ -46,11 +46,7 @@ protected InventoryBean deserializeInventoryIfContains(Map map, return null; return serializer.deserialize( - MapUtils.checkAndCastMap( - map.get(AbstractInventoryArgument.KEY_INVENTORY), - String.class, - Object.class - ), + MapUtils.checkAndCastMap(AbstractInventoryArgument.KEY_INVENTORY), InventoryBean.class ); } diff --git a/ScenamaticaActionEngine/src/main/java/org/kunlab/scenamatica/action/actions/inventory/interact/InventoryClickAction.java b/ScenamaticaActionEngine/src/main/java/org/kunlab/scenamatica/action/actions/inventory/interact/InventoryClickAction.java index 6da7df257..e030446f0 100644 --- a/ScenamaticaActionEngine/src/main/java/org/kunlab/scenamatica/action/actions/inventory/interact/InventoryClickAction.java +++ b/ScenamaticaActionEngine/src/main/java/org/kunlab/scenamatica/action/actions/inventory/interact/InventoryClickAction.java @@ -116,22 +116,14 @@ public T deserializeArgument(@NotNull Map map, @NotNull BeanSeri ItemStackBean clickedItem = null; if (map.containsKey(Argument.KEY_CLICKED_ITEM)) clickedItem = serializer.deserialize( - MapUtils.checkAndCastMap( - map.get(Argument.KEY_CLICKED_ITEM), - String.class, - Object.class - ), + MapUtils.checkAndCastMap(Argument.KEY_CLICKED_ITEM), ItemStackBean.class ); ItemStackBean cursorItem = null; if (map.containsKey(Argument.KEY_CURSOR_ITEM)) cursorItem = serializer.deserialize( - MapUtils.checkAndCastMap( - map.get(Argument.KEY_CURSOR_ITEM), - String.class, - Object.class - ), + MapUtils.checkAndCastMap(Argument.KEY_CURSOR_ITEM), ItemStackBean.class ); diff --git a/ScenamaticaActionEngine/src/main/java/org/kunlab/scenamatica/action/actions/inventory/interact/InventoryCreativeAction.java b/ScenamaticaActionEngine/src/main/java/org/kunlab/scenamatica/action/actions/inventory/interact/InventoryCreativeAction.java index ffe133a34..6fa536871 100644 --- a/ScenamaticaActionEngine/src/main/java/org/kunlab/scenamatica/action/actions/inventory/interact/InventoryCreativeAction.java +++ b/ScenamaticaActionEngine/src/main/java/org/kunlab/scenamatica/action/actions/inventory/interact/InventoryCreativeAction.java @@ -70,11 +70,7 @@ public Argument deserializeArgument(@NotNull Map map, @NotNull B ItemStackBean stack = null; if (map.containsKey(Argument.KEY_ITEM)) stack = serializer.deserialize( - MapUtils.checkAndCastMap( - map.get(Argument.KEY_ITEM), - String.class, - Object.class - ), + MapUtils.checkAndCastMap(Argument.KEY_ITEM), ItemStackBean.class ); 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 333ce46ff..a89207d26 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 @@ -117,21 +117,17 @@ public Argument deserializeArgument(@NotNull Map map, @NotNull B { ItemStackBean originalBucket = null; if (map.containsKey(Argument.KEY_ORIGINAL_BUCKET)) - originalBucket = serializer.deserializeItemStack( - MapUtils.checkAndCastMap( - map.get(Argument.KEY_ORIGINAL_BUCKET), - String.class, - Object.class - )); + originalBucket = serializer.deserialize( + MapUtils.checkAndCastMap(Argument.KEY_ORIGINAL_BUCKET), + ItemStackBean.class + ); ItemStackBean entityBucket = null; if (map.containsKey(Argument.KEY_ENTITY_BUCKET)) - entityBucket = serializer.deserializeItemStack( - MapUtils.checkAndCastMap( - map.get(Argument.KEY_ENTITY_BUCKET), - String.class, - Object.class - )); + entityBucket = serializer.deserialize( + MapUtils.checkAndCastMap(Argument.KEY_ENTITY_BUCKET), + ItemStackBean.class + ); return new Argument( super.deserializeTarget(map), diff --git a/ScenamaticaActionEngine/src/main/java/org/kunlab/scenamatica/action/actions/player/PlayerDropItemAction.java b/ScenamaticaActionEngine/src/main/java/org/kunlab/scenamatica/action/actions/player/PlayerDropItemAction.java index 8d7cddeaf..5324e67b6 100644 --- a/ScenamaticaActionEngine/src/main/java/org/kunlab/scenamatica/action/actions/player/PlayerDropItemAction.java +++ b/ScenamaticaActionEngine/src/main/java/org/kunlab/scenamatica/action/actions/player/PlayerDropItemAction.java @@ -75,10 +75,7 @@ public Argument deserializeArgument(@NotNull Map map, @NotNull B EntityItemBean item = null; if (map.containsKey(Argument.KEY_ITEM)) item = serializer.deserialize( - MapUtils.checkAndCastMap( - map.get(Argument.KEY_ITEM), - String.class, Object.class - ), + MapUtils.checkAndCastMap(Argument.KEY_ITEM), EntityItemBean.class ); diff --git a/ScenamaticaActionEngine/src/main/java/org/kunlab/scenamatica/action/actions/player/PlayerHarvestBlockAction.java b/ScenamaticaActionEngine/src/main/java/org/kunlab/scenamatica/action/actions/player/PlayerHarvestBlockAction.java index df4952e6b..2c7ab4d83 100644 --- a/ScenamaticaActionEngine/src/main/java/org/kunlab/scenamatica/action/actions/player/PlayerHarvestBlockAction.java +++ b/ScenamaticaActionEngine/src/main/java/org/kunlab/scenamatica/action/actions/player/PlayerHarvestBlockAction.java @@ -91,12 +91,9 @@ public Argument deserializeArgument(@NotNull Map map, @NotNull B { BlockBean block = null; if (map.containsKey(Argument.KEY_HARVESTED_BLOCK)) - block = serializer.deserializeBlock( - MapUtils.checkAndCastMap( - map.get(Argument.KEY_HARVESTED_BLOCK), - String.class, - Object.class - ) + block = serializer.deserialize( + MapUtils.checkAndCastMap(Argument.KEY_HARVESTED_BLOCK), + BlockBean.class ); List items = new ArrayList<>(); @@ -104,7 +101,7 @@ public Argument deserializeArgument(@NotNull Map map, @NotNull B { List> itemMaps = MapUtils.getAsList(map, Argument.KEY_ITEMS_HARVESTED); for (Map itemMap : itemMaps) - items.add(serializer.deserializeItemStack(itemMap)); + items.add(serializer.deserialize(itemMap, ItemStackBean.class)); } return new Argument( diff --git a/ScenamaticaActionEngine/src/main/java/org/kunlab/scenamatica/action/actions/player/PlayerHotbarSlotAction.java b/ScenamaticaActionEngine/src/main/java/org/kunlab/scenamatica/action/actions/player/PlayerHotbarSlotAction.java index 92f32b736..0663945ac 100644 --- a/ScenamaticaActionEngine/src/main/java/org/kunlab/scenamatica/action/actions/player/PlayerHotbarSlotAction.java +++ b/ScenamaticaActionEngine/src/main/java/org/kunlab/scenamatica/action/actions/player/PlayerHotbarSlotAction.java @@ -87,11 +87,8 @@ public Argument deserializeArgument(@NotNull Map map, @NotNull B ItemStackBean item = null; if (map.containsKey(Argument.KEY_CURRENT_ITEM)) { - Map itemMap = MapUtils.checkAndCastMap( - map.get(Argument.KEY_CURRENT_ITEM), - String.class, - Object.class - ); + Map itemMap = + MapUtils.checkAndCastMap(Argument.KEY_CURRENT_ITEM); serializer.validate(itemMap, ItemStackBean.class); diff --git a/ScenamaticaActionEngine/src/main/java/org/kunlab/scenamatica/action/actions/player/PlayerInteractBlockAction.java b/ScenamaticaActionEngine/src/main/java/org/kunlab/scenamatica/action/actions/player/PlayerInteractBlockAction.java index afb6e7fb1..80a8175c2 100644 --- a/ScenamaticaActionEngine/src/main/java/org/kunlab/scenamatica/action/actions/player/PlayerInteractBlockAction.java +++ b/ScenamaticaActionEngine/src/main/java/org/kunlab/scenamatica/action/actions/player/PlayerInteractBlockAction.java @@ -110,11 +110,7 @@ public Argument deserializeArgument(@NotNull Map map, @NotNull B BlockBean block = null; if (map.containsKey(Argument.KEY_BLOCK)) block = serializer.deserialize( - MapUtils.checkAndCastMap( - map.get(Argument.KEY_BLOCK), - String.class, - Object.class - ), + MapUtils.checkAndCastMap(Argument.KEY_BLOCK), BlockBean.class ); diff --git a/ScenamaticaActionEngine/src/main/java/org/kunlab/scenamatica/action/actions/player/PlayerItemBreakAction.java b/ScenamaticaActionEngine/src/main/java/org/kunlab/scenamatica/action/actions/player/PlayerItemBreakAction.java index 610d44fde..3900ca95c 100644 --- a/ScenamaticaActionEngine/src/main/java/org/kunlab/scenamatica/action/actions/player/PlayerItemBreakAction.java +++ b/ScenamaticaActionEngine/src/main/java/org/kunlab/scenamatica/action/actions/player/PlayerItemBreakAction.java @@ -160,11 +160,7 @@ public Argument deserializeArgument(@NotNull Map map, @NotNull B ItemStackBean item = null; if (map.containsKey(Argument.KEY_ITEM)) item = serializer.deserialize( - MapUtils.checkAndCastMap( - map.get(Argument.KEY_ITEM), - String.class, - Object.class - ), + MapUtils.checkAndCastMap(Argument.KEY_ITEM), ItemStackBean.class ); diff --git a/ScenamaticaActionEngine/src/main/java/org/kunlab/scenamatica/action/actions/player/PlayerItemConsumeAction.java b/ScenamaticaActionEngine/src/main/java/org/kunlab/scenamatica/action/actions/player/PlayerItemConsumeAction.java index ab91f4d78..b30323190 100644 --- a/ScenamaticaActionEngine/src/main/java/org/kunlab/scenamatica/action/actions/player/PlayerItemConsumeAction.java +++ b/ScenamaticaActionEngine/src/main/java/org/kunlab/scenamatica/action/actions/player/PlayerItemConsumeAction.java @@ -90,22 +90,14 @@ public Argument deserializeArgument(@NotNull Map map, @NotNull B ItemStackBean item = null; if (map.containsKey(Argument.KEY_ITEM)) item = serializer.deserialize( - MapUtils.checkAndCastMap( - map.get(Argument.KEY_ITEM), - String.class, - Object.class - ), + MapUtils.checkAndCastMap(Argument.KEY_ITEM), ItemStackBean.class ); ItemStackBean replacement = null; if (map.containsKey(Argument.KEY_REPLACEMENT)) replacement = serializer.deserialize( - MapUtils.checkAndCastMap( - map.get(Argument.KEY_REPLACEMENT), - String.class, - Object.class - ), + MapUtils.checkAndCastMap(Argument.KEY_REPLACEMENT), ItemStackBean.class ); diff --git a/ScenamaticaActionEngine/src/main/java/org/kunlab/scenamatica/action/actions/player/PlayerItemDamageAction.java b/ScenamaticaActionEngine/src/main/java/org/kunlab/scenamatica/action/actions/player/PlayerItemDamageAction.java index 7f50ef1ae..1a71d9805 100644 --- a/ScenamaticaActionEngine/src/main/java/org/kunlab/scenamatica/action/actions/player/PlayerItemDamageAction.java +++ b/ScenamaticaActionEngine/src/main/java/org/kunlab/scenamatica/action/actions/player/PlayerItemDamageAction.java @@ -88,12 +88,9 @@ public Argument deserializeArgument(@NotNull Map map, @NotNull B { ItemStackBean item = null; if (map.containsKey(Argument.KEY_ITEM)) - item = serializer.deserializeItemStack( - MapUtils.checkAndCastMap( - map.get(Argument.KEY_ITEM), - String.class, - Object.class - ) + item = serializer.deserialize( + MapUtils.checkAndCastMap(Argument.KEY_ITEM), + ItemStackBean.class ); return new Argument( diff --git a/ScenamaticaActionEngine/src/main/java/org/kunlab/scenamatica/action/actions/player/PlayerLaunchProjectileAction.java b/ScenamaticaActionEngine/src/main/java/org/kunlab/scenamatica/action/actions/player/PlayerLaunchProjectileAction.java index aeba4ade5..6cc7f0c31 100644 --- a/ScenamaticaActionEngine/src/main/java/org/kunlab/scenamatica/action/actions/player/PlayerLaunchProjectileAction.java +++ b/ScenamaticaActionEngine/src/main/java/org/kunlab/scenamatica/action/actions/player/PlayerLaunchProjectileAction.java @@ -95,9 +95,7 @@ public Argument deserializeArgument(@NotNull Map map, @NotNull B MapUtils.checkTypeIfContains(map, Argument.KEY_PROJECTILE_EPSILON, Double.class); // ItemStackBeanImpl.checkMap(map, PlayerLaunchProjectileActionArgument.KEY_PROJECTILE_SHOOTER); - Map velocityMap = MapUtils.checkAndCastMap(map.get(Argument.KEY_PROJECTILE_VELOCITY), - String.class, Object.class - ); + Map velocityMap = MapUtils.checkAndCastMap(map.get(Argument.KEY_PROJECTILE_VELOCITY)); double epsilon; if (map.containsKey(Argument.KEY_PROJECTILE_EPSILON) && map.get(Argument.KEY_PROJECTILE_EPSILON) != null) diff --git a/ScenamaticaActionEngine/src/main/java/org/kunlab/scenamatica/action/actions/player/bucket/BucketActionArgument.java b/ScenamaticaActionEngine/src/main/java/org/kunlab/scenamatica/action/actions/player/bucket/BucketActionArgument.java index 403be4180..0f042ae9a 100644 --- a/ScenamaticaActionEngine/src/main/java/org/kunlab/scenamatica/action/actions/player/bucket/BucketActionArgument.java +++ b/ScenamaticaActionEngine/src/main/java/org/kunlab/scenamatica/action/actions/player/bucket/BucketActionArgument.java @@ -60,33 +60,21 @@ public static BucketActionArgument deserialize(Map map, BeanSeri ItemStackBean itemStack = null; if (map.containsKey(BucketActionArgument.KEY_ITEM_STACK)) itemStack = serializer.deserialize( - MapUtils.checkAndCastMap( - map.get(BucketActionArgument.KEY_ITEM_STACK), - String.class, - Object.class - ), + MapUtils.checkAndCastMap(BucketActionArgument.KEY_ITEM_STACK), ItemStackBean.class ); BlockBean block = null; if (map.containsKey(BucketActionArgument.KEY_BLOCK)) block = serializer.deserialize( - MapUtils.checkAndCastMap( - map.get(BucketActionArgument.KEY_BLOCK), - String.class, - Object.class - ), + MapUtils.checkAndCastMap(BucketActionArgument.KEY_BLOCK), BlockBean.class ); BlockBean blockClicked = null; if (map.containsKey(BucketActionArgument.KEY_BLOCK_CLICKED)) blockClicked = serializer.deserialize( - MapUtils.checkAndCastMap( - map.get(BucketActionArgument.KEY_BLOCK_CLICKED), - String.class, - Object.class - ), + MapUtils.checkAndCastMap(BucketActionArgument.KEY_BLOCK_CLICKED), BlockBean.class ); diff --git a/ScenamaticaCommons/src/main/java/org/kunlab/scenamatica/commons/utils/MapUtils.java b/ScenamaticaCommons/src/main/java/org/kunlab/scenamatica/commons/utils/MapUtils.java index ad6787239..93aaff14c 100644 --- a/ScenamaticaCommons/src/main/java/org/kunlab/scenamatica/commons/utils/MapUtils.java +++ b/ScenamaticaCommons/src/main/java/org/kunlab/scenamatica/commons/utils/MapUtils.java @@ -224,6 +224,15 @@ public static Map checkAndCastMap(Object mayMap, Class keyType, return (Map) map; } + @SuppressWarnings("unchecked") + public static Map checkAndCastMap(Object mayMap) + { + if (!(mayMap instanceof Map)) + throw new IllegalArgumentException("Unexpected type of value: " + mayMap + " (expected: Map)"); + + return (Map) mayMap; + } + public static > void checkEnumName(Map map, String key, Class enumType) { MapUtils.checkContainsKey(map, key); @@ -289,6 +298,33 @@ public static T getOrNull(@NotNull Map map, @NotNull String return (T) map.get(key); } + @SuppressWarnings("unchecked") + public static List getOrEmptyList(@NotNull Map map, @NotNull String key) + { + if (!map.containsKey(key)) + return Collections.emptyList(); + + return (List) map.get(key); + } + + @SuppressWarnings("unchecked") + public static Map getOrEmptyMap(@NotNull Map map, @NotNull String key) + { + if (!map.containsKey(key)) + return Collections.emptyMap(); + + return (Map) map.get(key); + } + + @SuppressWarnings("unchecked") + public static Map getAndCastOrEmptyMap(@NotNull Map map, @NotNull String key) + { + if (!map.containsKey(key)) + return Collections.emptyMap(); + + return checkAndCastMap(map.get(key)); + } + @Nullable public static > T getAsEnumOrNull(@NotNull Map map, @NotNull String key, @NotNull Class enumType) { diff --git a/ScenamaticaPlugin/src/main/java/org/kunlab/scenamatica/reporter/packets/test/PacketTestEnd.java b/ScenamaticaPlugin/src/main/java/org/kunlab/scenamatica/reporter/packets/test/PacketTestEnd.java index 41efc49e0..03acda400 100644 --- a/ScenamaticaPlugin/src/main/java/org/kunlab/scenamatica/reporter/packets/test/PacketTestEnd.java +++ b/ScenamaticaPlugin/src/main/java/org/kunlab/scenamatica/reporter/packets/test/PacketTestEnd.java @@ -58,7 +58,7 @@ public Map serializeThis() Map result = super.serialize(); - result.put(KEY_SCENARIO, BeanSerializerImpl.getInstance().serializeScenarioFile(this.scenario)); + result.put(KEY_SCENARIO, BeanSerializerImpl.getInstance().serialize(this.scenario, ScenarioFileBean.class)); result.put(KEY_STATE, this.state.name()); result.put(KEY_CAUSE, this.cause.name()); result.put(KEY_STARTED_AT, this.startedAt); diff --git a/ScenamaticaPlugin/src/main/java/org/kunlab/scenamatica/reporter/packets/test/PacketTestSkip.java b/ScenamaticaPlugin/src/main/java/org/kunlab/scenamatica/reporter/packets/test/PacketTestSkip.java index f2b53921d..3a5c35fd9 100644 --- a/ScenamaticaPlugin/src/main/java/org/kunlab/scenamatica/reporter/packets/test/PacketTestSkip.java +++ b/ScenamaticaPlugin/src/main/java/org/kunlab/scenamatica/reporter/packets/test/PacketTestSkip.java @@ -3,6 +3,7 @@ import org.jetbrains.annotations.NotNull; import org.kunlab.scenamatica.interfaces.scenario.ScenarioEngine; import org.kunlab.scenamatica.interfaces.scenario.runtime.CompiledScenarioAction; +import org.kunlab.scenamatica.interfaces.scenariofile.scenario.ScenarioBean; import java.util.Map; @@ -28,7 +29,7 @@ public Map serialize() Map result = super.serialize(); result.put(KEY_REASON_ACTION, this.engine.getManager().getRegistry().getScenarioFileManager() - .getSerializer().serializeScenario(this.action.getBean())); + .getSerializer().serialize(this.action.getBean(), ScenarioBean.class)); return result; } diff --git a/ScenamaticaPlugin/src/main/java/org/kunlab/scenamatica/reporter/packets/test/PacketTestStart.java b/ScenamaticaPlugin/src/main/java/org/kunlab/scenamatica/reporter/packets/test/PacketTestStart.java index a5a69350e..9dbefa836 100644 --- a/ScenamaticaPlugin/src/main/java/org/kunlab/scenamatica/reporter/packets/test/PacketTestStart.java +++ b/ScenamaticaPlugin/src/main/java/org/kunlab/scenamatica/reporter/packets/test/PacketTestStart.java @@ -53,7 +53,7 @@ public Map serializeThis() Map result = new HashMap<>(); result.put(KEY_SCENARIO, this.engine.getManager().getRegistry().getScenarioFileManager().getSerializer() - .serializeScenarioFile(this.scenario)); + .serialize(this.scenario, ScenarioFileBean.class)); result.put(KEY_AUTO_START, this.isAutoStart); result.put(KEY_STARTED_AT, this.startedAt); diff --git a/ScenamaticaScenarioFile/src/main/java/org/kunlab/scenamatica/scenariofile/DefinitionsMapper.java b/ScenamaticaScenarioFile/src/main/java/org/kunlab/scenamatica/scenariofile/DefinitionsMapper.java index 6c54a9165..49a25c4c1 100644 --- a/ScenamaticaScenarioFile/src/main/java/org/kunlab/scenamatica/scenariofile/DefinitionsMapper.java +++ b/ScenamaticaScenarioFile/src/main/java/org/kunlab/scenamatica/scenariofile/DefinitionsMapper.java @@ -32,11 +32,7 @@ public static void resolveReferences(Map map) if (!map.containsKey(KEY_DEFINITIONS)) return; - Map schema = MapUtils.checkAndCastMap( - map.get(KEY_DEFINITIONS), - String.class, - Object.class - ); + Map schema = MapUtils.checkAndCastMap(map.get(KEY_DEFINITIONS)); processDefSet(map, schema); } diff --git a/ScenamaticaScenarioFile/src/main/java/org/kunlab/scenamatica/scenariofile/ScenarioFileParser.java b/ScenamaticaScenarioFile/src/main/java/org/kunlab/scenamatica/scenariofile/ScenarioFileParser.java index 86e691669..daba572d4 100644 --- a/ScenamaticaScenarioFile/src/main/java/org/kunlab/scenamatica/scenariofile/ScenarioFileParser.java +++ b/ScenamaticaScenarioFile/src/main/java/org/kunlab/scenamatica/scenariofile/ScenarioFileParser.java @@ -36,7 +36,7 @@ public static ScenarioFileBean fromMap(Map map, @Nullable String if (!map.containsKey("scenamatica")) // シナリオファイルには scenamatica キーが最上位に必須。 throw new NotAScenarioFileException(fileName); - return BeanSerializerImpl.getInstance().deserializeScenarioFile(map); + return BeanSerializerImpl.getInstance().deserialize(map, ScenarioFileBean.class); } catch (IllegalArgumentException e) { diff --git a/ScenamaticaScenarioFile/src/main/java/org/kunlab/scenamatica/scenariofile/beans/ScenarioFileBeanImpl.java b/ScenamaticaScenarioFile/src/main/java/org/kunlab/scenamatica/scenariofile/beans/ScenarioFileBeanImpl.java index 11fde0466..406440724 100644 --- a/ScenamaticaScenarioFile/src/main/java/org/kunlab/scenamatica/scenariofile/beans/ScenarioFileBeanImpl.java +++ b/ScenamaticaScenarioFile/src/main/java/org/kunlab/scenamatica/scenariofile/beans/ScenarioFileBeanImpl.java @@ -92,28 +92,17 @@ public static void validate(@NotNull Map map, @NotNull BeanSeria if (map.containsKey(KEY_CONTEXT)) serializer.validate( - MapUtils.checkAndCastMap( - map.get(KEY_CONTEXT), - String.class, - Object.class - ), + MapUtils.checkAndCastMap(map.get(KEY_CONTEXT)), ContextBean.class ); MapUtils.checkType(map, KEY_TRIGGERS, List.class); - ((List) map.get(KEY_TRIGGERS)).forEach( - o -> serializer.validate(MapUtils.checkAndCastMap( - o, - String.class, - Object.class - ), TriggerBean.class)); + ((List) map.get(KEY_TRIGGERS)) + .forEach(o -> serializer.validate(MapUtils.checkAndCastMap(o), TriggerBean.class)); MapUtils.checkType(map, KEY_SCENARIO, List.class); - ((List) map.get(KEY_SCENARIO)).forEach(o -> serializer.validate(MapUtils.checkAndCastMap( - o, - String.class, - Object.class - ), ScenarioBean.class)); + ((List) map.get(KEY_SCENARIO)) + .forEach(o -> serializer.validate(MapUtils.checkAndCastMap(o), ScenarioBean.class)); } @NotNull @@ -146,35 +135,19 @@ public static ScenarioFileBean deserialize(@NotNull Map map, @No } List triggers = ((List) map.get(KEY_TRIGGERS)).stream() - .map(o -> serializer.deserialize(MapUtils.checkAndCastMap( - o, - String.class, - Object.class - ), TriggerBean.class)) + .map(o -> serializer.deserialize(MapUtils.checkAndCastMap(o), TriggerBean.class)) .collect(Collectors.toList()); List scenario = ((List) map.get(KEY_SCENARIO)).stream() - .map(o -> serializer.deserialize(MapUtils.checkAndCastMap( - o, - String.class, - Object.class - ), ScenarioBean.class)) + .map(o -> serializer.deserialize(MapUtils.checkAndCastMap(o), ScenarioBean.class)) .collect(Collectors.toList()); ContextBean context = null; if (map.containsKey(KEY_CONTEXT)) - context = serializer.deserialize(MapUtils.checkAndCastMap( - map.get(KEY_CONTEXT), - String.class, - Object.class - ), ContextBean.class); + context = serializer.deserialize(MapUtils.checkAndCastMap(map.get(KEY_CONTEXT)), ContextBean.class); ActionBean runIf = null; if (map.containsKey(KEY_RUN_IF)) - runIf = serializer.deserialize(MapUtils.checkAndCastMap( - map.get(KEY_RUN_IF), - String.class, - Object.class - ), ActionBean.class); + runIf = serializer.deserialize(MapUtils.checkAndCastMap(map.get(KEY_RUN_IF)), ActionBean.class); return new ScenarioFileBeanImpl( scenamatica, diff --git a/ScenamaticaScenarioFile/src/main/java/org/kunlab/scenamatica/scenariofile/beans/context/ContextBeanImpl.java b/ScenamaticaScenarioFile/src/main/java/org/kunlab/scenamatica/scenariofile/beans/context/ContextBeanImpl.java index 3160a2f6e..e87a9df18 100644 --- a/ScenamaticaScenarioFile/src/main/java/org/kunlab/scenamatica/scenariofile/beans/context/ContextBeanImpl.java +++ b/ScenamaticaScenarioFile/src/main/java/org/kunlab/scenamatica/scenariofile/beans/context/ContextBeanImpl.java @@ -67,11 +67,7 @@ public static void validate(@NotNull Map map, @NotNull BeanSeria for (Object player : (List) actors) serializer.validate( - MapUtils.checkAndCastMap( - player, - String.class, - Object.class - ), + MapUtils.checkAndCastMap(player), PlayerBean.class ); } @@ -84,22 +80,14 @@ public static void validate(@NotNull Map map, @NotNull BeanSeria for (Object entity : (List) entities) serializer.validate( - MapUtils.checkAndCastMap( - entity, - String.class, - Object.class - ), + MapUtils.checkAndCastMap(entity), EntityBean.class ); } if (map.containsKey(KEY_STAGE)) serializer.validate( - MapUtils.checkAndCastMap( - map.get(KEY_STAGE), - String.class, - Object.class - ), + MapUtils.checkAndCastMap(map.get(KEY_STAGE)), StageBean.class ); } @@ -111,31 +99,22 @@ public static ContextBean deserialize(@NotNull Map map, @NotNull if (map.containsKey(KEY_ACTORS) && map.get(KEY_ACTORS) != null) { for (Object player : (List) map.get(KEY_ACTORS)) - actorList.add(serializer.deserialize(MapUtils.checkAndCastMap( - player, - String.class, - Object.class - ), PlayerBean.class)); + actorList.add(serializer.deserialize( + MapUtils.checkAndCastMap(player), PlayerBean.class)); } List entityList = new ArrayList<>(); if (map.containsKey(KEY_ENTITIES) && map.get(KEY_ENTITIES) != null) { for (Object entity : (List) map.get(KEY_ENTITIES)) - entityList.add(serializer.deserialize(MapUtils.checkAndCastMap( - entity, - String.class, - Object.class - ), EntityBean.class)); + entityList.add(serializer.deserialize( + MapUtils.checkAndCastMap(entity), EntityBean.class)); } StageBean world = null; if (map.containsKey(KEY_STAGE)) - world = serializer.deserialize(MapUtils.checkAndCastMap( - map.get(KEY_STAGE), - String.class, - Object.class - ), StageBean.class); + world = serializer.deserialize( + MapUtils.checkAndCastMap(map.get(KEY_STAGE)), StageBean.class); return new ContextBeanImpl( actorList, diff --git a/ScenamaticaScenarioFile/src/main/java/org/kunlab/scenamatica/scenariofile/beans/context/PlayerBeanImpl.java b/ScenamaticaScenarioFile/src/main/java/org/kunlab/scenamatica/scenariofile/beans/context/PlayerBeanImpl.java index a39925e72..a109b3722 100644 --- a/ScenamaticaScenarioFile/src/main/java/org/kunlab/scenamatica/scenariofile/beans/context/PlayerBeanImpl.java +++ b/ScenamaticaScenarioFile/src/main/java/org/kunlab/scenamatica/scenariofile/beans/context/PlayerBeanImpl.java @@ -183,11 +183,8 @@ public static PlayerBean deserialize(@NotNull Map map, @NotNull String playerListFooter = null; if (map.containsKey(KEY_PLAYER_LIST)) { - Map playerList = MapUtils.checkAndCastMap( - map.get(KEY_PLAYER_LIST), - String.class, - Object.class - ); + Map playerList = + MapUtils.checkAndCastMap(map.get(KEY_PLAYER_LIST)); playerListName = MapUtils.getOrNull(playerList, KEY_PLAYER_LIST_NAME); playerListHeader = MapUtils.getOrNull(playerList, KEY_PLAYER_LIST_HEADER); playerListFooter = MapUtils.getOrNull(playerList, KEY_PLAYER_LIST_FOOTER); diff --git a/ScenamaticaScenarioFile/src/main/java/org/kunlab/scenamatica/scenariofile/beans/entity/EntityBeanImpl.java b/ScenamaticaScenarioFile/src/main/java/org/kunlab/scenamatica/scenariofile/beans/entity/EntityBeanImpl.java index bff74402b..0cc176d55 100644 --- a/ScenamaticaScenarioFile/src/main/java/org/kunlab/scenamatica/scenariofile/beans/entity/EntityBeanImpl.java +++ b/ScenamaticaScenarioFile/src/main/java/org/kunlab/scenamatica/scenariofile/beans/entity/EntityBeanImpl.java @@ -176,11 +176,8 @@ private static void validatePotionEffectMap(@NotNull List> m for (Object o : map) { - Map effectMap = MapUtils.checkAndCastMap( - o, - String.class, - Object.class - ); + Map effectMap = + MapUtils.checkAndCastMap(o); MapUtils.checkType(effectMap, KEY_POTION_EFFECTS_TYPE, String.class); if (PotionEffectType.getByName((String) effectMap.get(KEY_POTION_EFFECTS_TYPE)) == null) throw new IllegalArgumentException("Invalid potion effect type."); @@ -295,11 +292,8 @@ public static EntityBean deserialize(@NotNull Map map, @NotNull Vector velocity = null; if (map.containsKey(KEY_VELOCITY)) - velocity = Vector.deserialize(MapUtils.checkAndCastMap( - map.get(KEY_VELOCITY), - String.class, - Object.class - )); + velocity = Vector.deserialize( + MapUtils.checkAndCastMap(map.get(KEY_VELOCITY))); String customName = MapUtils.getOrNull(map, KEY_CUSTOM_NAME); UUID uuid; @@ -317,10 +311,8 @@ public static EntityBean deserialize(@NotNull Map map, @NotNull List tags = MapUtils.getAsListOrEmpty(map, KEY_TAGS); DamageBean lastDamageCause = null; if (map.containsKey(KEY_LAST_DAMAGE)) - lastDamageCause = serializer.deserialize(MapUtils.checkAndCastMap( - map.get(KEY_LAST_DAMAGE), - String.class, Object.class - ), DamageBean.class); + lastDamageCause = serializer.deserialize( + MapUtils.checkAndCastMap(map.get(KEY_LAST_DAMAGE)), DamageBean.class); Integer maxHealth = MapUtils.getOrNull(map, KEY_MAX_HEALTH); Integer health = MapUtils.getOrNull(map, KEY_HEALTH); diff --git a/ScenamaticaScenarioFile/src/main/java/org/kunlab/scenamatica/scenariofile/beans/entity/entities/HumanEntityBeanImpl.java b/ScenamaticaScenarioFile/src/main/java/org/kunlab/scenamatica/scenariofile/beans/entity/entities/HumanEntityBeanImpl.java index d20683e75..ed4a997b1 100644 --- a/ScenamaticaScenarioFile/src/main/java/org/kunlab/scenamatica/scenariofile/beans/entity/entities/HumanEntityBeanImpl.java +++ b/ScenamaticaScenarioFile/src/main/java/org/kunlab/scenamatica/scenariofile/beans/entity/entities/HumanEntityBeanImpl.java @@ -116,17 +116,11 @@ public static void validate(@NotNull Map map, @NotNull BeanSeria validate(map); if (map.containsKey(KEY_INVENTORY)) - serializer.validate(MapUtils.checkAndCastMap( - map.get(KEY_INVENTORY), - String.class, - Object.class - ), PlayerInventoryBean.class); + serializer.validate( + MapUtils.checkAndCastMap(map.get(KEY_INVENTORY)), PlayerInventoryBean.class); if (map.containsKey(KEY_ENDER_CHEST)) - serializer.validate(MapUtils.checkAndCastMap( - map.get(KEY_ENDER_CHEST), - String.class, - Object.class - ), InventoryBean.class); + serializer.validate( + MapUtils.checkAndCastMap(map.get(KEY_ENDER_CHEST)), InventoryBean.class); } /** @@ -144,19 +138,13 @@ public static HumanEntityBean deserialize(@NotNull Map map, @Not PlayerInventoryBean inventory = null; if (map.containsKey(KEY_INVENTORY)) - inventory = serializer.deserialize(MapUtils.checkAndCastMap( - map.get(KEY_INVENTORY), - String.class, - Object.class - ), PlayerInventoryBean.class); + inventory = serializer.deserialize( + MapUtils.checkAndCastMap(map.get(KEY_INVENTORY)), PlayerInventoryBean.class); InventoryBean enderChest = null; if (map.containsKey(KEY_ENDER_CHEST)) - enderChest = serializer.deserialize(MapUtils.checkAndCastMap( - map.get(KEY_ENDER_CHEST), - String.class, - Object.class - ), InventoryBean.class); + enderChest = serializer.deserialize( + MapUtils.checkAndCastMap(map.get(KEY_ENDER_CHEST)), InventoryBean.class); MainHand mainHand = MapUtils.getAsEnumOrNull( map, diff --git a/ScenamaticaScenarioFile/src/main/java/org/kunlab/scenamatica/scenariofile/beans/entity/entities/ProjectileBeanImpl.java b/ScenamaticaScenarioFile/src/main/java/org/kunlab/scenamatica/scenariofile/beans/entity/entities/ProjectileBeanImpl.java index 99f700fc7..9012ed450 100644 --- a/ScenamaticaScenarioFile/src/main/java/org/kunlab/scenamatica/scenariofile/beans/entity/entities/ProjectileBeanImpl.java +++ b/ScenamaticaScenarioFile/src/main/java/org/kunlab/scenamatica/scenariofile/beans/entity/entities/ProjectileBeanImpl.java @@ -27,21 +27,21 @@ public ProjectileBeanImpl(@NotNull EntityBean original, @Nullable EntityBean sho public static Map serialize(@NotNull ProjectileBean bean, @NotNull BeanSerializer serializer) { - Map map = serializer.serializeEntity(bean); + Map map = serializer.serialize(bean, EntityBean.class); if (bean.getShooter() != null) - MapUtils.putIfNotNull(map, KEY_SHOOTER, serializer.serializeEntity(bean.getShooter())); + MapUtils.putMapIfNotEmpty(map, KEY_SHOOTER, serializer.serialize(bean.getShooter(), EntityBean.class)); return map; } public static void validate(@NotNull Map map, @NotNull BeanSerializer serializer) { - serializer.validateEntity(map); + serializer.validate(map, EntityBean.class); if (map.containsKey(KEY_SHOOTER)) { - Map shooterMap = MapUtils.checkAndCastMap(map.get(KEY_SHOOTER), String.class, Object.class); - serializer.validateEntity(shooterMap); + Map shooterMap = MapUtils.checkAndCastMap(map.get(KEY_SHOOTER)); + serializer.validate(shooterMap, EntityBean.class); } } @@ -50,17 +50,13 @@ public static ProjectileBean deserialize(@NotNull Map map, @NotN { validate(map, serializer); - EntityBean entity = serializer.deserializeEntity(map); + EntityBean entity = serializer.deserialize(map, EntityBean.class); EntityBean shooter = null; if (map.containsKey(KEY_SHOOTER)) { - Map shooterMap = MapUtils.checkAndCastMap( - map.get(KEY_SHOOTER), - String.class, - Object.class - ); - shooter = serializer.deserializeEntity(shooterMap); + Map shooterMap = MapUtils.checkAndCastMap(map.get(KEY_SHOOTER)); + shooter = serializer.deserialize(shooterMap, EntityBean.class); } return new ProjectileBeanImpl(entity, shooter); diff --git a/ScenamaticaScenarioFile/src/main/java/org/kunlab/scenamatica/scenariofile/beans/inventory/InventoryBeanImpl.java b/ScenamaticaScenarioFile/src/main/java/org/kunlab/scenamatica/scenariofile/beans/inventory/InventoryBeanImpl.java index 136f87b97..55788d79f 100644 --- a/ScenamaticaScenarioFile/src/main/java/org/kunlab/scenamatica/scenariofile/beans/inventory/InventoryBeanImpl.java +++ b/ScenamaticaScenarioFile/src/main/java/org/kunlab/scenamatica/scenariofile/beans/inventory/InventoryBeanImpl.java @@ -62,11 +62,7 @@ public static void validate(@NotNull Map map, @NotNull BeanSeria ); for (Map.Entry entry : contents.entrySet()) - serializer.validate(MapUtils.checkAndCastMap( - entry.getValue(), - String.class, - Object.class - ), ItemStackBean.class); + serializer.validate(MapUtils.checkAndCastMap(entry.getValue()), ItemStackBean.class); } @NotNull @@ -87,11 +83,7 @@ public static InventoryBean deserialize(@NotNull Map map, @NotNu for (Map.Entry entry : contents.entrySet()) mainContents.put( entry.getKey(), - serializer.deserialize(MapUtils.checkAndCastMap( - entry.getValue(), - String.class, - Object.class - ), ItemStackBean.class) + serializer.deserialize(MapUtils.checkAndCastMap(entry.getValue()), ItemStackBean.class) ); } diff --git a/ScenamaticaScenarioFile/src/main/java/org/kunlab/scenamatica/scenariofile/beans/inventory/ItemStackBeanImpl.java b/ScenamaticaScenarioFile/src/main/java/org/kunlab/scenamatica/scenariofile/beans/inventory/ItemStackBeanImpl.java index 84c52c023..47baa2b47 100644 --- a/ScenamaticaScenarioFile/src/main/java/org/kunlab/scenamatica/scenariofile/beans/inventory/ItemStackBeanImpl.java +++ b/ScenamaticaScenarioFile/src/main/java/org/kunlab/scenamatica/scenariofile/beans/inventory/ItemStackBeanImpl.java @@ -77,11 +77,7 @@ private static void validateEnchantments(Map map) { if (map.containsKey(KEY_ENCHANTMENTS)) { - Map enchtansMap = MapUtils.checkAndCastMap( - map.get(KEY_ENCHANTMENTS), - String.class, Object.class - ); - + Map enchtansMap = MapUtils.checkAndCastMap(map.get(KEY_ENCHANTMENTS)); for (Map.Entry entry : enchtansMap.entrySet()) { if (Enchantment.getByKey(NamespaceUtils.fromString(entry.getKey())) == null @@ -211,10 +207,7 @@ private static void validateAttributeModifiersMap(Map map) String name = entry.getKey(); for (Object obj : entry.getValue()) { - Map valuesMap = MapUtils.checkAndCastMap( - obj, - String.class, Object.class - ); + Map valuesMap = MapUtils.checkAndCastMap(obj); MapUtils.checkTypeIfContains(valuesMap, KEY_ATTRIBUTE_MODIFIER_AMOUNT, Number.class); // double MapUtils.checkTypeIfContains(valuesMap, KEY_ATTRIBUTE_MODIFIER_OPERATION, String.class); diff --git a/ScenamaticaScenarioFile/src/main/java/org/kunlab/scenamatica/scenariofile/beans/inventory/PlayerInventoryBeanImpl.java b/ScenamaticaScenarioFile/src/main/java/org/kunlab/scenamatica/scenariofile/beans/inventory/PlayerInventoryBeanImpl.java index 55c99046d..2b6b512e9 100644 --- a/ScenamaticaScenarioFile/src/main/java/org/kunlab/scenamatica/scenariofile/beans/inventory/PlayerInventoryBeanImpl.java +++ b/ScenamaticaScenarioFile/src/main/java/org/kunlab/scenamatica/scenariofile/beans/inventory/PlayerInventoryBeanImpl.java @@ -97,10 +97,7 @@ public static void validate(@NotNull Map map, @NotNull BeanSeria { if (map.containsKey(KEY_MAIN_INVENTORY)) { - Map mainInventory = new HashMap<>(MapUtils.checkAndCastMap( - map.get(KEY_MAIN_INVENTORY), - String.class, Object.class - )); + Map mainInventory = new HashMap<>(MapUtils.checkAndCastMap(map.get(KEY_MAIN_INVENTORY))); if (!mainInventory.containsKey(KEY_SIZE)) mainInventory.put(KEY_SIZE, 9 * 4); @@ -111,15 +108,9 @@ else if (!(mainInventory.get(KEY_SIZE) instanceof Integer || serializer.validate(mainInventory, InventoryBean.class); } if (map.containsKey(KEY_MAIN_HAND)) - serializer.validate(MapUtils.checkAndCastMap( - map.get(KEY_MAIN_HAND), - String.class, Object.class - ), ItemStackBean.class); + serializer.validate(MapUtils.checkAndCastMap(map.get(KEY_MAIN_HAND)), ItemStackBean.class); if (map.containsKey(KEY_OFF_HAND)) - serializer.validate(MapUtils.checkAndCastMap( - map.get(KEY_OFF_HAND), - String.class, Object.class - ), ItemStackBean.class); + serializer.validate(MapUtils.checkAndCastMap(map.get(KEY_OFF_HAND)), ItemStackBean.class); if (!map.containsKey(KEY_ARMOR_CONTENTS)) return; @@ -135,10 +126,7 @@ else if (!(mainInventory.get(KEY_SIZE) instanceof Integer || continue; serializer.validate( - MapUtils.checkAndCastMap( - armorContent, - String.class, Object.class - ), + MapUtils.checkAndCastMap(armorContent), ItemStackBean.class ); } @@ -158,10 +146,7 @@ public static PlayerInventoryBean deserialize(@NotNull Map map, if (armorContent == null) armorContentsList.add(null); else - armorContentsList.add(serializer.deserialize(MapUtils.checkAndCastMap( - armorContent, - String.class, Object.class - ), ItemStackBean.class)); + armorContentsList.add(serializer.deserialize(MapUtils.checkAndCastMap(armorContent), ItemStackBean.class)); } armorContents = armorContentsList.toArray(new ItemStackBean[0]); @@ -172,10 +157,7 @@ public static PlayerInventoryBean deserialize(@NotNull Map map, InventoryBean mainInventoryBean; if (map.containsKey(KEY_MAIN_INVENTORY)) { - Map mainInventory = new HashMap<>(MapUtils.checkAndCastMap( - map.get(KEY_MAIN_INVENTORY), - String.class, Object.class - )); + Map mainInventory = new HashMap<>(MapUtils.checkAndCastMap(map.get(KEY_MAIN_INVENTORY))); mainInventoryBean = serializer.deserialize(mainInventory, InventoryBean.class); } @@ -186,10 +168,7 @@ public static PlayerInventoryBean deserialize(@NotNull Map map, ItemStackBean mainHandItem; if (map.containsKey(KEY_MAIN_HAND)) mainHandItem = serializer.deserialize( - MapUtils.checkAndCastMap( - map.get(KEY_MAIN_HAND), - String.class, Object.class - ), + MapUtils.checkAndCastMap(map.get(KEY_MAIN_HAND)), ItemStackBean.class ); else @@ -198,10 +177,7 @@ public static PlayerInventoryBean deserialize(@NotNull Map map, ItemStackBean offHandItem; if (map.containsKey(KEY_OFF_HAND)) offHandItem = serializer.deserialize( - MapUtils.checkAndCastMap( - map.get(KEY_OFF_HAND), - String.class, Object.class - ), + MapUtils.checkAndCastMap(map.get(KEY_OFF_HAND)), ItemStackBean.class ); else diff --git a/ScenamaticaScenarioFile/src/main/java/org/kunlab/scenamatica/scenariofile/beans/misc/BlockBeanImpl.java b/ScenamaticaScenarioFile/src/main/java/org/kunlab/scenamatica/scenariofile/beans/misc/BlockBeanImpl.java index f9633f6d9..cffd9b98b 100644 --- a/ScenamaticaScenarioFile/src/main/java/org/kunlab/scenamatica/scenariofile/beans/misc/BlockBeanImpl.java +++ b/ScenamaticaScenarioFile/src/main/java/org/kunlab/scenamatica/scenariofile/beans/misc/BlockBeanImpl.java @@ -12,7 +12,6 @@ import org.kunlab.scenamatica.commons.utils.Utils; import org.kunlab.scenamatica.interfaces.scenariofile.misc.BlockBean; -import java.util.Collections; import java.util.HashMap; import java.util.Map; import java.util.Objects; @@ -86,18 +85,10 @@ public static void validate(@NotNull Map map) MapUtils.checkEnumNameIfContains(map, KEY_BIOME, Biome.class); if (map.containsKey(KEY_METADATA)) - MapUtils.checkAndCastMap( - map.get(KEY_METADATA), - String.class, - Object.class - ); + MapUtils.checkAndCastMap(map.get(KEY_METADATA)); if (map.containsKey(KEY_BLOCK_DATA)) - MapUtils.checkAndCastMap( - map.get(KEY_BLOCK_DATA), - String.class, - Object.class - ); + MapUtils.checkAndCastMap(map.get(KEY_BLOCK_DATA)); MapUtils.checkTypeIfContains(map, KEY_BLOCK_STATE, Byte.class); } @@ -112,18 +103,10 @@ public static BlockBean deserialize(@NotNull Map map) return new BlockBeanImpl( material, MapUtils.getAsLocationOrNull(map, KEY_BLOCK_LOCATION), - map.containsKey(KEY_METADATA) ? MapUtils.checkAndCastMap( - map.get(KEY_METADATA), - String.class, - Object.class - ): Collections.emptyMap(), + MapUtils.getAndCastOrEmptyMap(map, KEY_METADATA), MapUtils.getOrNull(map, KEY_LIGHT_LEVEL), MapUtils.getAsEnumOrNull(map, KEY_BIOME, Biome.class), - map.containsKey(KEY_BLOCK_DATA) ? MapUtils.checkAndCastMap( - map.get(KEY_BLOCK_DATA), - String.class, - Object.class - ): Collections.emptyMap(), + MapUtils.getAndCastOrEmptyMap(map, KEY_METADATA), MapUtils.getOrNull(map, KEY_BLOCK_STATE) ); } @@ -139,11 +122,12 @@ public boolean equals(Object o) BlockBeanImpl blockBean = (BlockBeanImpl) o; return this.getType() == blockBean.getType() + && Objects.equals(this.getLocation(), blockBean.getLocation()) && Objects.equals(this.getMetadata(), blockBean.getMetadata()) && Objects.equals(this.getLightLevel(), blockBean.getLightLevel()) && this.getBiome() == blockBean.getBiome() - && (this.getBlockData() == null || MapUtils.equals(this.getBlockData(), blockBean.getBlockData())) + && MapUtils.equals(this.getBlockData(), blockBean.getBlockData()) && Objects.equals(this.getBlockState(), blockBean.getBlockState()); } diff --git a/ScenamaticaScenarioFile/src/main/java/org/kunlab/scenamatica/scenariofile/beans/scenario/ActionBeanImpl.java b/ScenamaticaScenarioFile/src/main/java/org/kunlab/scenamatica/scenariofile/beans/scenario/ActionBeanImpl.java index b7a8412b5..0790e9fbe 100644 --- a/ScenamaticaScenarioFile/src/main/java/org/kunlab/scenamatica/scenariofile/beans/scenario/ActionBeanImpl.java +++ b/ScenamaticaScenarioFile/src/main/java/org/kunlab/scenamatica/scenariofile/beans/scenario/ActionBeanImpl.java @@ -33,11 +33,7 @@ public static void validate(@NotNull Map map) MapUtils.checkContainsKey(map, KEY_TYPE); if (map.containsKey(KEY_ARGUMENTS)) - MapUtils.checkAndCastMap( - map.get(KEY_ARGUMENTS), - String.class, - Object.class - ); + MapUtils.checkAndCastMap(map.get(KEY_ARGUMENTS)); } @@ -49,11 +45,7 @@ public static ActionBean deserialize(Map map) Map argumentsMap; if (map.containsKey(KEY_ARGUMENTS)) - argumentsMap = MapUtils.checkAndCastMap( - map.get(KEY_ARGUMENTS), - String.class, - Object.class - ); + argumentsMap = MapUtils.checkAndCastMap(map.get(KEY_ARGUMENTS)); else argumentsMap = null; diff --git a/ScenamaticaScenarioFile/src/main/java/org/kunlab/scenamatica/scenariofile/beans/scenario/ScenarioBeanImpl.java b/ScenamaticaScenarioFile/src/main/java/org/kunlab/scenamatica/scenariofile/beans/scenario/ScenarioBeanImpl.java index 67835d754..0c909624a 100644 --- a/ScenamaticaScenarioFile/src/main/java/org/kunlab/scenamatica/scenariofile/beans/scenario/ScenarioBeanImpl.java +++ b/ScenamaticaScenarioFile/src/main/java/org/kunlab/scenamatica/scenariofile/beans/scenario/ScenarioBeanImpl.java @@ -83,11 +83,7 @@ public static ScenarioBean deserialize(@NotNull Map map, @NotNul ActionBean runIf = null; if (map.containsKey(KEY_RUN_IF)) runIf = serializer.deserialize( - MapUtils.checkAndCastMap( - map.get(KEY_RUN_IF), - String.class, - Object.class - ), + MapUtils.checkAndCastMap(map.get(KEY_RUN_IF)), ActionBean.class ); diff --git a/ScenamaticaScenarioFile/src/main/java/org/kunlab/scenamatica/scenariofile/beans/trigger/TriggerBeanImpl.java b/ScenamaticaScenarioFile/src/main/java/org/kunlab/scenamatica/scenariofile/beans/trigger/TriggerBeanImpl.java index 8acc3bfc4..0172137ff 100644 --- a/ScenamaticaScenarioFile/src/main/java/org/kunlab/scenamatica/scenariofile/beans/trigger/TriggerBeanImpl.java +++ b/ScenamaticaScenarioFile/src/main/java/org/kunlab/scenamatica/scenariofile/beans/trigger/TriggerBeanImpl.java @@ -82,11 +82,7 @@ public static void validate(@NotNull Map map, @NotNull BeanSeria MapUtils.checkType(map, KEY_BEFORE_THAT, List.class); for (Object obj : (List) map.get(KEY_BEFORE_THAT)) serializer.validate( - MapUtils.checkAndCastMap( - obj, - String.class, - Object.class - ), + MapUtils.checkAndCastMap(obj), ScenarioBean.class ); } @@ -95,11 +91,7 @@ public static void validate(@NotNull Map map, @NotNull BeanSeria MapUtils.checkType(map, KEY_AFTER_THAT, List.class); for (Object obj : (List) map.get(KEY_AFTER_THAT)) serializer.validate( - MapUtils.checkAndCastMap( - obj, - String.class, - Object.class - ), + MapUtils.checkAndCastMap(obj), ScenarioBean.class ); } @@ -126,28 +118,19 @@ public static TriggerBean deserialize(@NotNull Map map, @NotNull List beforeThat = new LinkedList<>(); if (map.containsKey(KEY_BEFORE_THAT)) for (Object obj : (List) map.get(KEY_BEFORE_THAT)) - beforeThat.add(serializer.deserialize(MapUtils.checkAndCastMap( - obj, - String.class, - Object.class - ), ScenarioBean.class)); + beforeThat.add(serializer.deserialize( + MapUtils.checkAndCastMap(obj), ScenarioBean.class)); List afterThat = new LinkedList<>(); if (map.containsKey(KEY_AFTER_THAT)) for (Object obj : (List) map.get(KEY_AFTER_THAT)) - afterThat.add(serializer.deserialize(MapUtils.checkAndCastMap( - obj, - String.class, - Object.class - ), ScenarioBean.class)); + afterThat.add(serializer.deserialize( + MapUtils.checkAndCastMap(obj), ScenarioBean.class)); ActionBean runIf = null; if (map.containsKey(KEY_RUN_IF)) - runIf = serializer.deserialize(MapUtils.checkAndCastMap( - map.get(KEY_RUN_IF), - String.class, - Object.class - ), ActionBean.class); + runIf = serializer.deserialize( + MapUtils.checkAndCastMap(map.get(KEY_RUN_IF)), ActionBean.class); assert type != null; // validate() で検証済み return new TriggerBeanImpl( diff --git a/ScenamaticaScenarioFile/src/test/java/org/kunlab/scenamatica/scenariofile/beans/entity/ProjectileBeanSerializeTest.java b/ScenamaticaScenarioFile/src/test/java/org/kunlab/scenamatica/scenariofile/beans/entity/ProjectileBeanSerializeTest.java index 292b5dc7e..1a761ef72 100644 --- a/ScenamaticaScenarioFile/src/test/java/org/kunlab/scenamatica/scenariofile/beans/entity/ProjectileBeanSerializeTest.java +++ b/ScenamaticaScenarioFile/src/test/java/org/kunlab/scenamatica/scenariofile/beans/entity/ProjectileBeanSerializeTest.java @@ -26,7 +26,7 @@ public class ProjectileBeanSerializeTest public static final ProjectileBean EMPTY = new ProjectileBeanImpl( EntityBeanSerializeTest.EMPTY, - EntityBeanSerializeTest.EMPTY + null ); public static final Map EMPTY_MAP = new HashMap<>(EntityBeanSerializeTest.EMPTY_MAP);