Skip to content

Commit

Permalink
refactor(commons): Refactor map controlling operations with util method
Browse files Browse the repository at this point in the history
  • Loading branch information
PeyaPeyaPeyang committed Nov 15, 2023
1 parent eeefdba commit adbecb5
Show file tree
Hide file tree
Showing 44 changed files with 161 additions and 376 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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()
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,7 @@ public BlockBean deserializeBlockOrNull(@NotNull Map<String, Object> 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
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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();
Expand All @@ -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.");

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,7 @@ public Argument deserializeArgument(@NotNull Map<String, Object> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public static EntityArgumentHolder tryDeserialize(Object obj, BeanSerializer ser
// noinspection unchecked
Map<String, Object> map = (Map<String, Object>) obj;

return new EntityArgumentHolder(serializer.deserializeEntity(map));
return new EntityArgumentHolder(serializer.deserialize(map, EntityBean.class));
}

throw new IllegalArgumentException("Cannot deserialize EntityArgumentHolder from " + obj);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public Argument deserializeArgument(@NotNull Map<String, Object> map, @NotNull B
String damager = MapUtils.getOrNull(map, Argument.KEY_DAMAGER);

return new Argument(
base.getTargetRaw(),
base.getTargetHolder(),
base.getCause(),
base.getAmount(),
base.getModifiers(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public Argument deserializeArgument(@NotNull Map<String, Object> map, @NotNull B
drops = new ArrayList<>();
List<Map<String, Object>> dropMaps = MapUtils.getAsList(map, Argument.KEY_DROPS);
for (Map<String, Object> dropMap : dropMaps)
drops.add(serializer.deserializeItemStack(dropMap));
drops.add(serializer.deserialize(dropMap, ItemStackBean.class));
}

return new Argument(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,7 @@ public Argument deserializeArgument(@NotNull Map<String, Object> 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
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,9 @@ public Argument deserializeArgument(@NotNull Map<String, Object> 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
)
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,11 +156,7 @@ public Argument deserializeArgument(@NotNull Map<String, Object> 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
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,7 @@ public Argument deserializeArgument(@NotNull Map<String, Object> 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
));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,7 @@ protected InventoryBean deserializeInventoryIfContains(Map<String, Object> map,
return null;

return serializer.deserialize(
MapUtils.checkAndCastMap(
map.get(AbstractInventoryArgument.KEY_INVENTORY),
String.class,
Object.class
),
MapUtils.checkAndCastMap(AbstractInventoryArgument.KEY_INVENTORY),
InventoryBean.class
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,22 +116,14 @@ public T deserializeArgument(@NotNull Map<String, Object> 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
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,7 @@ public Argument deserializeArgument(@NotNull Map<String, Object> 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
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,21 +117,17 @@ public Argument deserializeArgument(@NotNull Map<String, Object> 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),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,7 @@ public Argument deserializeArgument(@NotNull Map<String, Object> 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
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,20 +91,17 @@ public Argument deserializeArgument(@NotNull Map<String, Object> 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<ItemStackBean> items = new ArrayList<>();
if (map.containsKey(Argument.KEY_ITEMS_HARVESTED))
{
List<Map<String, Object>> itemMaps = MapUtils.getAsList(map, Argument.KEY_ITEMS_HARVESTED);
for (Map<String, Object> itemMap : itemMaps)
items.add(serializer.deserializeItemStack(itemMap));
items.add(serializer.deserialize(itemMap, ItemStackBean.class));
}

return new Argument(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,8 @@ public Argument deserializeArgument(@NotNull Map<String, Object> map, @NotNull B
ItemStackBean item = null;
if (map.containsKey(Argument.KEY_CURRENT_ITEM))
{
Map<String, Object> itemMap = MapUtils.checkAndCastMap(
map.get(Argument.KEY_CURRENT_ITEM),
String.class,
Object.class
);
Map<String, Object> itemMap =
MapUtils.checkAndCastMap(Argument.KEY_CURRENT_ITEM);

serializer.validate(itemMap, ItemStackBean.class);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,7 @@ public Argument deserializeArgument(@NotNull Map<String, Object> 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
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,7 @@ public Argument deserializeArgument(@NotNull Map<String, Object> 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
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,22 +90,14 @@ public Argument deserializeArgument(@NotNull Map<String, Object> 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
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,9 @@ public Argument deserializeArgument(@NotNull Map<String, Object> 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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,7 @@ public Argument deserializeArgument(@NotNull Map<String, Object> map, @NotNull B
MapUtils.checkTypeIfContains(map, Argument.KEY_PROJECTILE_EPSILON, Double.class);
// ItemStackBeanImpl.checkMap(map, PlayerLaunchProjectileActionArgument.KEY_PROJECTILE_SHOOTER);

Map<String, Object> velocityMap = MapUtils.checkAndCastMap(map.get(Argument.KEY_PROJECTILE_VELOCITY),
String.class, Object.class
);
Map<String, Object> 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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,33 +60,21 @@ public static BucketActionArgument deserialize(Map<String, Object> 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
);

Expand Down
Loading

0 comments on commit adbecb5

Please sign in to comment.