Skip to content

Commit

Permalink
refactor(actions): Generics of EntityArgumentHolder
Browse files Browse the repository at this point in the history
  • Loading branch information
PeyaPeyaPeyang committed Nov 19, 2023
1 parent 60ca6ca commit 5acb9b9
Show file tree
Hide file tree
Showing 13 changed files with 60 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -49,11 +50,24 @@ protected boolean checkMatchedEntity(String specifier, @NotNull Entity actualEnt
.anyMatch(entity -> Objects.equals(entity.getUniqueId(), actualEntity.getUniqueId()));
}

protected EntityArgumentHolder deserializeTarget(Map<String, Object> map, BeanSerializer serializer)
protected EntityArgumentHolder<Entity> deserializeTarget(Map<String, Object> map, BeanSerializer serializer)
{
return EntityArgumentHolder.tryDeserialize(
map.get(AbstractEntityActionArgument.KEY_TARGET_ENTITY),
serializer
serializer,
EntityBean.class
);
}

protected <E extends Entity> EntityArgumentHolder<E> deserializeTarget(
Map<String, Object> map,
BeanSerializer serializer,
Class<? extends EntityBean> beanClass)
{
return EntityArgumentHolder.tryDeserialize(
map.get(AbstractEntityActionArgument.KEY_TARGET_ENTITY),
serializer,
beanClass
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@
import java.util.Objects;

@AllArgsConstructor
public abstract class AbstractEntityActionArgument extends AbstractActionArgument
public abstract class AbstractEntityActionArgument<E extends Entity> extends AbstractActionArgument
{
public static final String KEY_TARGET_ENTITY = "target";

@Nullable
private final EntityArgumentHolder entity;
private final EntityArgumentHolder<E> entity;

@Override
public boolean isSame(TriggerArgument argument)
Expand All @@ -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()
Expand Down Expand Up @@ -70,7 +70,7 @@ public String getArgumentString()
return this.entity.getArgumentString();
}

public EntityArgumentHolder getTargetHolder()
public EntityArgumentHolder<E> getTargetHolder()
{
return this.entity;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,13 @@ public Argument deserializeArgument(@NotNull Map<String, Object> map, @NotNull B

@Value
@EqualsAndHashCode(callSuper = true)
public static class Argument extends AbstractEntityActionArgument
public static class Argument extends AbstractEntityActionArgument<Entity>
{
public static final String KEY_ENTITY = "entity";

EntityBean entity;

public Argument(EntityArgumentHolder target, @Nullable EntityBean entity)
public Argument(EntityArgumentHolder<Entity> target, @Nullable EntityBean entity)
{
super(target);
this.entity = entity;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@

@Getter
@EqualsAndHashCode
public class EntityArgumentHolder
public class EntityArgumentHolder<E extends Entity>
{
public static final EntityArgumentHolder EMPTY = new EntityArgumentHolder(null);
public static final EntityArgumentHolder<?> EMPTY = new EntityArgumentHolder<>(null);

@Nullable
protected final String targetSpecifier;
Expand All @@ -38,36 +38,47 @@ public EntityArgumentHolder(@Nullable Object mayTarget)
}
}

public static EntityArgumentHolder tryDeserialize(Object obj, BeanSerializer serializer)
public static <E extends Entity> EntityArgumentHolder<E> tryDeserialize(
Object obj,
BeanSerializer serializer,
Class<? extends EntityBean> beanClass
)
{
if (obj == null)
return EMPTY;
// noinspection unchecked
return (EntityArgumentHolder<E>) EMPTY;

if (obj instanceof String || obj instanceof EntityBean)
return new EntityArgumentHolder(obj);
return new EntityArgumentHolder<>(obj);

if (obj instanceof Map)
{
// noinspection unchecked
Map<String, Object> map = (Map<String, Object>) 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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public A deserializeArgument(@NotNull Map<String, Object> map, @NotNull BeanSeri

@Getter
@EqualsAndHashCode(callSuper = true)
public static class Argument extends AbstractEntityActionArgument
public static class Argument extends AbstractEntityActionArgument<Entity>
{
public static final String KEY_DAMAGE_CAUSE = "cause";
public static final String KEY_AMOUNT = "amount"; // 最終ダメージ
Expand All @@ -122,7 +122,7 @@ public static class Argument extends AbstractEntityActionArgument
private final Double amount;
private final Map<EntityDamageEvent.DamageModifier, @NotNull Double> modifiers;

public Argument(EntityArgumentHolder target, EntityDamageEvent.DamageCause cause, Double amount, Map<EntityDamageEvent.DamageModifier, @NotNull Double> modifiers)
public Argument(EntityArgumentHolder<Entity> target, EntityDamageEvent.DamageCause cause, Double amount, Map<EntityDamageEvent.DamageModifier, @NotNull Double> modifiers)
{
super(target);
this.cause = cause;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<EntityDamageEvent.DamageModifier, @NotNull Double> modifiers, String damager)
public Argument(EntityArgumentHolder<Entity> target, EntityDamageEvent.DamageCause cause, Double amount, Map<EntityDamageEvent.DamageModifier, @NotNull Double> modifiers, String damager)
{
super(target, cause, amount, modifiers);
this.damager = damager;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public Argument deserializeArgument(@NotNull Map<String, Object> map, @NotNull B

@Value
@EqualsAndHashCode(callSuper = true)
public static class Argument extends AbstractEntityActionArgument
public static class Argument extends AbstractEntityActionArgument<Entity>
{
public static final String KEY_DROPS = "drops";
public static final String KEY_DROP_EXP = "dropExp";
Expand All @@ -139,7 +139,7 @@ public static class Argument extends AbstractEntityActionArgument
Float deathSoundVolume;
Float deathSoundPitch;

public Argument(@Nullable EntityArgumentHolder mayTarget, List<ItemStackBean> drops, Integer dropExp, Double reviveHealth, Boolean shouldPlayDeathSound, Sound deathSound, SoundCategory deathSoundCategory, Float deathSoundVolume, Float deathSoundPitch)
public Argument(@Nullable EntityArgumentHolder<Entity> mayTarget, List<ItemStackBean> drops, Integer dropExp, Double reviveHealth, Boolean shouldPlayDeathSound, Sound deathSound, SoundCategory deathSoundCategory, Float deathSoundVolume, Float deathSoundPitch)
{
super(mayTarget);
this.drops = drops;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,13 @@ public Argument deserializeArgument(@NotNull Map<String, Object> map, @NotNull B

@Value
@EqualsAndHashCode(callSuper = true)
public static class Argument extends AbstractEntityActionArgument
public static class Argument extends AbstractEntityActionArgument<Entity>
{
public static final String KEY_DROP_ITEM = "item";

EntityItemBean item;

public Argument(@Nullable EntityArgumentHolder mayTarget, EntityItemBean item)
public Argument(@Nullable EntityArgumentHolder<Entity> mayTarget, EntityItemBean item)
{
super(mayTarget);
this.item = item;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public Argument deserializeArgument(@NotNull Map<String, Object> map, @NotNull B

@Value
@EqualsAndHashCode(callSuper = true)
public static class Argument extends AbstractEntityActionArgument
public static class Argument extends AbstractEntityActionArgument<Entity>
{
public static final String KEY_FROM = "from";
public static final String KEY_TO = "to";
Expand All @@ -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<Entity> mayTarget, Location from, Location to, boolean useAI)
{
super(mayTarget);
this.from = from;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public Argument deserializeArgument(@NotNull Map<String, Object> map, @NotNull B

@Value
@EqualsAndHashCode(callSuper = true)
public static class Argument extends AbstractEntityActionArgument
public static class Argument extends AbstractEntityActionArgument<Entity>
{
public static final String KEY_REMAINING = "remaining";
public static final String KEY_ITEM = "item";
Expand All @@ -156,15 +156,15 @@ public static class Argument extends AbstractEntityActionArgument
EntityItemBean item;
String itemSelector;

public Argument(@Nullable EntityArgumentHolder mayTarget, Integer remaining, EntityItemBean item)
public Argument(@Nullable EntityArgumentHolder<Entity> mayTarget, Integer remaining, EntityItemBean item)
{
super(mayTarget);
this.remaining = remaining;
this.item = item;
this.itemSelector = null;
}

public Argument(@Nullable EntityArgumentHolder mayTarget, Integer remaining, String itemSelector)
public Argument(@Nullable EntityArgumentHolder<Entity> mayTarget, Integer remaining, String itemSelector)
{
super(mayTarget);
this.remaining = remaining;
Expand All @@ -182,7 +182,6 @@ public Item selectItem()
if (!this.isItemSelectable())
throw new IllegalArgumentException("Item is not selectable.");

assert this.itemSelector != null;
List<Entity> entities = EntityUtils.selectEntities(this.itemSelector);
if (entities.isEmpty())
throw new IllegalStateException("No entity found: " + this.itemSelector);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -170,7 +171,7 @@ public Argument deserializeArgument(@NotNull Map<String, Object> map, @NotNull B

@Value
@EqualsAndHashCode(callSuper = true)
public static class Argument extends AbstractEntityActionArgument
public static class Argument extends AbstractEntityActionArgument<Entity>
{
public static final String KEY_PLAYER = "player";
public static final String KEY_BLOCK = "block";
Expand All @@ -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<Entity> target, String playerSpecifier, BlockBean block, BlockFace blockFace)
{
super(target);
this.playerSpecifier = playerSpecifier;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 5acb9b9

Please sign in to comment.