Skip to content

Commit

Permalink
feat: Migrating Item's UUIDs of owner/thrower to EntitySpecifier
Browse files Browse the repository at this point in the history
  • Loading branch information
PeyaPeyaPeyang committed May 11, 2024
1 parent 3f5d80d commit 024a99f
Show file tree
Hide file tree
Showing 11 changed files with 98 additions and 41 deletions.
3 changes: 2 additions & 1 deletion Scenamatica/Scenamatica.iml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<excludeFolder url="file://$MODULE_DIR$/scripts/build" />
<excludeFolder url="file://$MODULE_DIR$/scripts/dist" />
<excludeFolder url="file://$MODULE_DIR$/scripts/installer" />
<excludeFolder url="file://$MODULE_DIR$/scripts/Lib" />
</content>
</component>
<component name="FacetManager">
Expand All @@ -17,4 +18,4 @@
</configuration>
</facet>
</component>
</module>
</module>
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.kunlab.scenamatica.interfaces.structures.minecraft.entity.EntityStructure;
import org.kunlab.scenamatica.interfaces.structures.minecraft.inventory.ItemStackStructure;
import org.kunlab.scenamatica.interfaces.scenariofile.Mapped;
import org.kunlab.scenamatica.interfaces.structures.specifiers.PlayerSpecifier;

import java.util.UUID;

Expand All @@ -29,12 +30,12 @@ public interface EntityItemStructure extends EntityStructure, Mapped<Item>
/**
* アイテムを拾ったプレイヤの UUID です。
*/
UUID getOwner();
PlayerSpecifier getOwner();

/**
* アイテムを投げたプレイヤの UUID です。
*/
UUID getThrower();
PlayerSpecifier getThrower();

/**
* アイテムをモブが拾えるかどうかです。
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import java.util.List;
import java.util.Optional;
import java.util.UUID;

/**
* エンティティの指定子を提供します。
Expand Down Expand Up @@ -106,6 +107,18 @@ public interface EntitySpecifier<E extends Entity> extends Structure
*/
boolean hasStructure();

/**
* UUID を持っているかどうか取得します。
* @return UUID を持っているかどうか
*/
boolean hasUUID();

/**
* UUID を取得します。
* @return UUID
*/
UUID getSelectingUUID();

/**
* ターゲットの構造を取得します。
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ public static LivingEntityStructure ofLivingEntity(@NotNull LivingEntity entity)
entity.getMaximumNoDamageTicks(),
entity.getLastDamage(),
entity.getNoDamageTicks(),
entity.getKiller() == null ? PlayerSpecifierImpl.EMPTY: PlayerSpecifierImpl.of(entity.getKiller()),
entity.getKiller() == null ? PlayerSpecifierImpl.EMPTY: PlayerSpecifierImpl.ofPlayer(entity.getKiller()),
new ArrayList<>(entity.getActivePotionEffects()),
entity.getRemoveWhenFarAway(),
entity.getCanPickupItems(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import lombok.EqualsAndHashCode;
import lombok.Value;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Item;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.kunlab.scenamatica.commons.utils.MapUtils;
Expand All @@ -12,12 +14,16 @@
import org.kunlab.scenamatica.interfaces.structures.minecraft.entity.EntityStructure;
import org.kunlab.scenamatica.interfaces.structures.minecraft.entity.entities.EntityItemStructure;
import org.kunlab.scenamatica.interfaces.structures.minecraft.inventory.ItemStackStructure;
import org.kunlab.scenamatica.interfaces.structures.specifiers.EntitySpecifier;
import org.kunlab.scenamatica.interfaces.structures.specifiers.PlayerSpecifier;
import org.kunlab.scenamatica.structures.minecraft.entity.EntityStructureImpl;
import org.kunlab.scenamatica.structures.minecraft.inventory.ItemStackStructureImpl;
import org.kunlab.scenamatica.structures.specifiers.PlayerSpecifierImpl;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.function.Consumer;

@Value
@EqualsAndHashCode(callSuper = true)
Expand All @@ -26,20 +32,18 @@ public class EntityItemStructureImpl extends EntityStructureImpl implements Enti
@NotNull
ItemStackStructure itemStack;

@Nullable
Integer pickupDelay;
@Nullable
UUID owner;

@Nullable
UUID thrower;
@NotNull
EntitySpecifier<?> owner;
@NotNull
EntitySpecifier<?> thrower;

Boolean canMobPickup;

Boolean willAge;

public EntityItemStructureImpl(@NotNull EntityStructure original, @NotNull ItemStackStructure itemStack, @Nullable Integer pickupDelay,
@Nullable UUID owner, @Nullable UUID thrower, Boolean canMobPickup, Boolean willAge)
@NotNull EntitySpecifier<?> owner, @NotNull EntitySpecifier<?> thrower, Boolean canMobPickup, Boolean willAge)
{
super(EntityType.DROPPED_ITEM, original);
this.itemStack = itemStack;
Expand All @@ -59,10 +63,10 @@ public static Map<String, Object> serialize(@NotNull EntityItemStructure structu

if (structure.getPickupDelay() != null)
map.put(KEY_PICKUP_DELAY, structure.getPickupDelay());
if (structure.getOwner() != null)
map.put(KEY_OWNER, structure.getOwner().toString());
if (structure.getThrower() != null)
map.put(KEY_THROWER, structure.getThrower().toString());
if (structure.getOwner().canProvideTarget())
map.put(KEY_OWNER, structure.getOwner().getTargetRaw());
if (structure.getThrower().canProvideTarget())
map.put(KEY_THROWER, structure.getThrower().getTargetRaw());
if (structure.getCanMobPickup() != null)
map.put(KEY_CAN_MOB_PICKUP, structure.getCanMobPickup());
if (structure.getWillAge() != null)
Expand All @@ -89,13 +93,13 @@ public static EntityItemStructure deserialize(@NotNull Map<String, Object> map,
Number pickupDelayNum = MapUtils.getAsNumberOrNull(map, KEY_PICKUP_DELAY);
Integer pickupDelay = pickupDelayNum != null ? pickupDelayNum.intValue(): null;

UUID ownerUUID = null;
EntitySpecifier<?> owner = null;
if (map.containsKey(KEY_OWNER))
ownerUUID = UUIDUtil.toUUIDOrThrow((String) map.get(KEY_OWNER));
owner = serializer.tryDeserializeEntitySpecifier(map.get(KEY_OWNER));

UUID throwerUUID = null;
EntitySpecifier<?> thrower = null;
if (map.containsKey(KEY_THROWER))
throwerUUID = UUIDUtil.toUUIDOrThrow((String) map.get(KEY_THROWER));
thrower = serializer.tryDeserializeEntitySpecifier(map.get(KEY_THROWER));

ItemStackStructure itemStack = serializer.deserialize(map, ItemStackStructure.class);

Expand All @@ -109,8 +113,8 @@ public static EntityItemStructure deserialize(@NotNull Map<String, Object> map,
EntityStructureImpl.deserialize(copiedMap, serializer),
itemStack,
pickupDelay,
ownerUUID,
throwerUUID,
owner,
thrower,
MapUtils.getOrNull(copiedMap, KEY_CAN_MOB_PICKUP),
MapUtils.getOrNull(copiedMap, KEY_WILL_AGE)
);
Expand All @@ -123,8 +127,8 @@ public static EntityItemStructure of(@NotNull Item entity)
EntityStructureImpl.of(entity),
ItemStackStructureImpl.of(entity.getItemStack()),
entity.getPickupDelay(),
entity.getOwner(),
entity.getThrower(),
PlayerSpecifierImpl.ofPlayer(entity.getOwner()),
PlayerSpecifierImpl.ofPlayer(entity.getThrower()),
entity.canMobPickup(),
willAge(entity)
);
Expand All @@ -147,16 +151,26 @@ public void applyTo(Item entity)

if (this.pickupDelay != null)
entity.setPickupDelay(this.pickupDelay);
if (this.owner != null)
entity.setOwner(this.owner);
if (this.thrower != null)
entity.setThrower(this.thrower);
if (this.owner.canProvideTarget())
setUUIDByEntitySpecifier(this.owner, entity::setOwner);
if (this.thrower.canProvideTarget())
setUUIDByEntitySpecifier(this.thrower, entity::setThrower);
if (this.canMobPickup != null)
entity.setCanMobPickup(this.canMobPickup);
if (this.willAge != null)
setWillAge(entity, this.willAge);
}

private static void setUUIDByEntitySpecifier(@NotNull EntitySpecifier<?> specifier, @NotNull Consumer<UUID> setter)
{
if (specifier.hasUUID())
setter.accept(specifier.getSelectingUUID());
else
specifier.selectTarget(null)
.map(Entity::getUniqueId)
.ifPresent(setter);
}

@Override
public boolean canApplyTo(Object target)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,11 @@ public static <E extends Entity> EntitySpecifier<E> of(@NotNull E entity)
return new EntitySpecifierImpl<>(entity.getUniqueId());
}

public static <E extends Entity> EntitySpecifierImpl<E> of(@NotNull UUID entityUUID)
{
return new EntitySpecifierImpl<>(entityUUID);
}

protected static UUID tryConvertToUUID(String mayUUID)
{
try
Expand Down Expand Up @@ -178,15 +183,26 @@ public Object getTargetRaw()
{
if (this.selector != null)
return this.selector.getOriginal();
else
else if (this.mayUUID != null)
return this.mayUUID;
else if (this.targetStructure != null)
return this.targetStructure;
else
return null;
}

@Override
public boolean hasUUID()
{
return this.mayUUID != null;
}

@Override
public UUID getSelectingUUID()
{
return this.mayUUID;
}

@Override
public boolean canProvideTarget()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.UUID;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -96,9 +97,20 @@ else if (obj instanceof String)
throw new IllegalArgumentException("Cannot deserialize PlayerSpecifier from " + obj);
}

public static PlayerSpecifier of(@NotNull Player player)
public static PlayerSpecifier ofPlayer(@Nullable Player player)
{
return new PlayerSpecifierImpl(player.getUniqueId());
if (player == null)
return EMPTY;
else
return new PlayerSpecifierImpl(player.getUniqueId());
}

public static PlayerSpecifier ofPlayer(@Nullable UUID uuid)
{
if (uuid == null)
return EMPTY;
else
return new PlayerSpecifierImpl(uuid);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import org.kunlab.scenamatica.structures.minecraft.inventory.ItemStackStructureSerializeTest;
import org.kunlab.scenamatica.structures.minecraft.entity.entities.EntityItemStructureImpl;
import org.kunlab.scenamatica.structures.minecraft.utils.MapTestUtil;
import org.kunlab.scenamatica.structures.specifiers.EntitySpecifierImpl;
import org.kunlab.scenamatica.structures.specifiers.PlayerSpecifierImpl;

import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -36,8 +38,8 @@ public class EntityItemStructureSerializeTest
AEntityStructureSerializeTest.FULFILLED,
ItemStackStructureSerializeTest.FULFILLED,
1,
FULLFILLED_OWNER_UUID,
FULLFILLED_THROWER_UUID,
EntitySpecifierImpl.of(FULLFILLED_OWNER_UUID),
EntitySpecifierImpl.of(FULLFILLED_THROWER_UUID),
false,
false
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,7 @@ public class ItemStackStructureSerializeTest
"This is also a test lore.",
"This is literally a test lore."
),
new HashMap<Enchantment, Integer>()
{{/*
this.put(Enchantment.ARROW_DAMAGE, 1);
this.put(Enchantment.ARROW_FIRE, 1);
this.put(Enchantment.ARROW_INFINITE, 4);
this.put(Enchantment.ARROW_KNOCKBACK, 5);
this.put(Enchantment.LOOT_BONUS_BLOCKS, 1);
this.put(Enchantment.KNOCKBACK, 4);*/
}},
new HashMap<Enchantment, Integer>(),
Arrays.asList(
ItemFlag.HIDE_ATTRIBUTES,
ItemFlag.HIDE_ENCHANTS,
Expand Down
4 changes: 4 additions & 0 deletions Scenamatica/scripts/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@ events.json
build
dist
installer
Lib
Scripts

pyvenv.cfg
2 changes: 2 additions & 0 deletions Scenamatica/scripts/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pyqt5
pyinstaller

0 comments on commit 024a99f

Please sign in to comment.