diff --git a/src/main/java/it/jakegblp/lusk/api/BlockWrapper.java b/src/main/java/it/jakegblp/lusk/api/BlockWrapper.java index b884a5a2..f43ac6d5 100644 --- a/src/main/java/it/jakegblp/lusk/api/BlockWrapper.java +++ b/src/main/java/it/jakegblp/lusk/api/BlockWrapper.java @@ -1,6 +1,7 @@ package it.jakegblp.lusk.api; import ch.njol.skript.aliases.ItemType; +import ch.njol.skript.entity.EntityData; import it.jakegblp.lusk.utils.Constants; import org.bukkit.Material; import org.bukkit.block.*; @@ -8,10 +9,12 @@ import org.bukkit.block.data.Levelled; import org.bukkit.block.data.Waterlogged; import org.bukkit.entity.Entity; +import org.bukkit.entity.EntityType; import org.bukkit.inventory.BrewerInventory; import org.bukkit.inventory.meta.BlockDataMeta; import org.bukkit.inventory.meta.BlockStateMeta; import org.bukkit.inventory.meta.ItemMeta; +import org.bukkit.spawner.Spawner; import org.bukkit.util.BoundingBox; import org.bukkit.util.VoxelShape; import org.jetbrains.annotations.Nullable; @@ -20,8 +23,9 @@ import java.util.List; import static ch.njol.skript.paperlib.PaperLib.isPaper; -import static it.jakegblp.lusk.utils.Constants.MINECRAFT_1_20_1; -import static it.jakegblp.lusk.utils.Constants.PAPER_HAS_1_18_2_EXTENDED_ENTITY_API; +import static it.jakegblp.lusk.utils.Constants.*; +import static it.jakegblp.lusk.utils.EntityUtils.toEntityData; +import static it.jakegblp.lusk.utils.EntityUtils.toEntityType; import static it.jakegblp.lusk.utils.ItemUtils.getNullableItemStack; import static it.jakegblp.lusk.utils.ItemUtils.getNullableItemType; import static it.jakegblp.lusk.utils.LuskUtils.warning; @@ -474,4 +478,38 @@ public Float getBlastResistance() { if (material != null && material.isBlock()) return material.getBlastResistance(); return null; } + + @Nullable + @SuppressWarnings("UnstableApiUsage") + public EntityData getSpawnerEntityType() { + BlockState blockState = getBlockState(); + EntityType entityType; + if (blockState instanceof Spawner spawner) + entityType = spawner.getSpawnedType(); + else if (MINECRAFT_1_21 && blockState instanceof TrialSpawner trialSpawner) { + if (trialSpawner.isOminous()) + entityType = trialSpawner.getOminousConfiguration().getSpawnedType(); + else + entityType = trialSpawner.getNormalConfiguration().getSpawnedType(); + } else return null; + return toEntityData(entityType); + } + + public void setSpawnerEntityType(@Nullable EntityData entityData) { + setSpawnerEntityType(toEntityType(entityData)); + } + + @SuppressWarnings("UnstableApiUsage") + public void setSpawnerEntityType(@Nullable EntityType entityType) { + BlockState blockState = getBlockState(); + if (blockState instanceof Spawner spawner) + spawner.setSpawnedType(entityType); + else if (MINECRAFT_1_21 && blockState instanceof TrialSpawner trialSpawner) { + if (trialSpawner.isOminous()) + trialSpawner.getOminousConfiguration().setSpawnedType(entityType); + else + trialSpawner.getNormalConfiguration().setSpawnedType(entityType); + } + } + } diff --git a/src/main/java/it/jakegblp/lusk/elements/minecraft/blocks/spawner/expressions/ExprSpawnerEntityType.java b/src/main/java/it/jakegblp/lusk/elements/minecraft/blocks/spawner/expressions/ExprSpawnerEntityType.java new file mode 100644 index 00000000..2e6f179d --- /dev/null +++ b/src/main/java/it/jakegblp/lusk/elements/minecraft/blocks/spawner/expressions/ExprSpawnerEntityType.java @@ -0,0 +1,57 @@ +package it.jakegblp.lusk.elements.minecraft.blocks.spawner.expressions; + +import ch.njol.skript.doc.Description; +import ch.njol.skript.doc.Examples; +import ch.njol.skript.doc.Name; +import ch.njol.skript.doc.Since; +import ch.njol.skript.entity.EntityData; +import it.jakegblp.lusk.api.BlockWrapper; +import it.jakegblp.lusk.api.skript.SimplerPropertyExpression; +import org.bukkit.entity.EntityType; +import org.jetbrains.annotations.Nullable; + +@Name("Spawner/Trial Spawner - Entity Type") +@Description("Gets the spawner entity type of the provided spawners or trial spawners (1.21+).\n**Works with items.**\nCan be set and reset (sets it to pig).") +@Examples("set spawner entity type of {_block} to zombie") +@Since("1.3.4-beta1") +public class ExprSpawnerEntityType extends SimplerPropertyExpression { + + static { + register(ExprSpawnerEntityType.class, EntityData.class, "spawner (entity|creature) type", "itemtypes/blocks/blockstates"); + } + + @Override + public @Nullable EntityData convert(Object from) { + return new BlockWrapper(from).getSpawnerEntityType(); + } + + @Override + public boolean allowSet() { + return true; + } + + @Override + public boolean allowReset() { + return true; + } + + @Override + public void set(Object from, EntityData to) { + new BlockWrapper(from).setSpawnerEntityType(to); + } + + @Override + public void reset(Object from) { + new BlockWrapper(from).setSpawnerEntityType(EntityType.PIG); + } + + @Override + protected String getPropertyName() { + return "spawner entity type"; + } + + @Override + public Class getReturnType() { + return EntityData.class; + } +} diff --git a/src/main/java/it/jakegblp/lusk/utils/EntityUtils.java b/src/main/java/it/jakegblp/lusk/utils/EntityUtils.java index 9e77b850..c885e0c6 100644 --- a/src/main/java/it/jakegblp/lusk/utils/EntityUtils.java +++ b/src/main/java/it/jakegblp/lusk/utils/EntityUtils.java @@ -1,5 +1,6 @@ package it.jakegblp.lusk.utils; +import ch.njol.skript.entity.EntityData; import org.bukkit.entity.*; import org.bukkit.inventory.EntityEquipment; import org.bukkit.inventory.EquipmentSlot; @@ -259,6 +260,17 @@ public static void setEntityEquipmentSlot(LivingEntity livingEntity, EquipmentSl } } } + + @Nullable + public static EntityData toEntityData(EntityType entityType) { + return entityType == null ? null : ch.njol.skript.bukkitutil.EntityUtils.toSkriptEntityData(entityType); + } + + @Nullable + public static EntityType toEntityType(EntityData entityType) { + return entityType == null ? null : ch.njol.skript.bukkitutil.EntityUtils.toBukkitEntityType(entityType); + } + //todo: add can pickup items for pre 2.8 //todo: add isImmuneToZombification for hoglins and piglins } diff --git a/src/test/scripts/expressions/ExprSpawnerEntityType.sk b/src/test/scripts/expressions/ExprSpawnerEntityType.sk new file mode 100644 index 00000000..dd140690 --- /dev/null +++ b/src/test/scripts/expressions/ExprSpawnerEntityType.sk @@ -0,0 +1,14 @@ +test "ExprSpawnerEntityType": + set {_location} to location(10, 11, 10) + set block at {_location} to spawner + set {_spawner} to block at {_location} + assert spawner entity type of {_spawner} is pig with "spawner entity type of spawner at location %{_location}% is not pig (default)" + set spawner entity type of {_spawner} to zombie + assert spawner entity type of {_spawner} is zombie with "spawner entity type of spawner at location %{_location}% is not zombie" + + server version >= 1.21.0 + + set block at {_location} to trial spawner + set {_spawner} to block at {_location} + set spawner entity type of {_spawner} to skeleton + assert spawner entity type of {_spawner} is zombie with "spawner entity type of spawner at location %{_location}% is not skeleton"