From 381e8cf7ff908f045c2228b5f454935311ba089e Mon Sep 17 00:00:00 2001 From: Jake Date: Sat, 1 Feb 2025 22:07:27 +0100 Subject: [PATCH] Improved ExprSpawnerEntityType --- .../it/jakegblp/lusk/api/BlockWrapper.java | 112 ++++++++++-------- .../expressions/ExprSpawnerEntityType.java | 25 +++- .../it/jakegblp/lusk/utils/Constants.java | 4 + 3 files changed, 87 insertions(+), 54 deletions(-) diff --git a/src/main/java/it/jakegblp/lusk/api/BlockWrapper.java b/src/main/java/it/jakegblp/lusk/api/BlockWrapper.java index f43ac6d5..f0faf671 100644 --- a/src/main/java/it/jakegblp/lusk/api/BlockWrapper.java +++ b/src/main/java/it/jakegblp/lusk/api/BlockWrapper.java @@ -10,13 +10,14 @@ import org.bukkit.block.data.Waterlogged; import org.bukkit.entity.Entity; import org.bukkit.entity.EntityType; +import org.bukkit.entity.minecart.SpawnerMinecart; 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.ApiStatus; import org.jetbrains.annotations.Nullable; import java.util.ArrayList; @@ -44,6 +45,12 @@ public class BlockWrapper { private final BlockDataMeta blockDataMeta; @Nullable private final BlockData blockData; + + // todo: implement for all methods and include all possible types (item frame, displays etc) + @Nullable + @ApiStatus.Experimental + private final Entity entity; + private final boolean shouldUpdate; public BlockWrapper(Object object) { @@ -51,50 +58,42 @@ public BlockWrapper(Object object) { } public BlockWrapper(Object object, boolean shouldUpdate) { + this.shouldUpdate = shouldUpdate; + + Block tempBlock = null; + BlockState tempBlockState = null; + ItemType tempItem = null; + BlockData tempBlockData = null; + BlockDataMeta tempBlockDataMeta = null; + BlockStateMeta tempBlockStateMeta = null; + Entity tempEntity = null; + if (object instanceof Block aBlock) { - this.block = aBlock; - this.blockState = null; - this.item = null; - this.blockData = null; - this.blockDataMeta = null; - this.blockStateMeta = null; + tempBlock = aBlock; } else if (object instanceof BlockState aBlockState) { - this.block = null; - this.blockState = aBlockState; - this.item = null; - this.blockData = null; - this.blockDataMeta = null; - this.blockStateMeta = null; + tempBlockState = aBlockState; } else if (object instanceof ItemType itemType) { - this.block = null; - this.blockState = null; - this.item = itemType; - this.blockData = null; - this.blockDataMeta = null; - this.blockStateMeta = null; + tempItem = itemType; } else if (object instanceof ItemMeta itemMeta) { - this.block = null; - this.blockState = null; - this.item = null; - this.blockData = null; - this.blockDataMeta = itemMeta instanceof BlockDataMeta meta ? meta : null; - this.blockStateMeta = itemMeta instanceof BlockStateMeta meta ? meta : null; + if (itemMeta instanceof BlockDataMeta meta) { + tempBlockDataMeta = meta; + } + if (itemMeta instanceof BlockStateMeta meta) { + tempBlockStateMeta = meta; + } } else if (object instanceof BlockData aBlockData) { - this.block = null; - this.blockState = null; - this.item = null; - this.blockData = aBlockData; - this.blockDataMeta = null; - this.blockStateMeta = null; - } else { - this.block = null; - this.blockState = null; - this.item = null; - this.blockData = null; - this.blockDataMeta = null; - this.blockStateMeta = null; + tempBlockData = aBlockData; + } else if (object instanceof Entity anEntity) { + tempEntity = anEntity; } - this.shouldUpdate = shouldUpdate; + + this.block = tempBlock; + this.blockState = tempBlockState; + this.item = tempItem; + this.blockData = tempBlockData; + this.blockDataMeta = tempBlockDataMeta; + this.blockStateMeta = tempBlockStateMeta; + this.entity = tempEntity; } @Nullable @@ -120,6 +119,11 @@ else if (itemMeta instanceof BlockDataMeta meta) return blockData; } + @Nullable + public Entity getEntity() { + return entity; + } + /** * Updates the inner Block with the given {@link BlockData}, * only works if {@link #shouldUpdate()} is true, @@ -484,13 +488,15 @@ public Float getBlastResistance() { public EntityData getSpawnerEntityType() { BlockState blockState = getBlockState(); EntityType entityType; - if (blockState instanceof Spawner spawner) + if (blockState instanceof CreatureSpawner 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 if (MINECRAFT_1_21) { + if (blockState instanceof TrialSpawner trialSpawner) { + if (trialSpawner.isOminous()) entityType = trialSpawner.getOminousConfiguration().getSpawnedType(); + else entityType = trialSpawner.getNormalConfiguration().getSpawnedType(); + } else if (getEntity() instanceof SpawnerMinecart spawnerMinecart) + entityType = spawnerMinecart.getSpawnedType(); + else return null; } else return null; return toEntityData(entityType); } @@ -502,13 +508,17 @@ public void setSpawnerEntityType(@Nullable EntityData entityData) { @SuppressWarnings("UnstableApiUsage") public void setSpawnerEntityType(@Nullable EntityType entityType) { BlockState blockState = getBlockState(); - if (blockState instanceof Spawner spawner) + if (entityType == null && !MINECRAFT_1_20) return; + if (blockState instanceof CreatureSpawner 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); + spawner.update(); + } else if (MINECRAFT_1_21) { + if (blockState instanceof TrialSpawner trialSpawner) { + if (trialSpawner.isOminous()) trialSpawner.getOminousConfiguration().setSpawnedType(entityType); + else trialSpawner.getNormalConfiguration().setSpawnedType(entityType); + trialSpawner.update(); + } else if (getEntity() instanceof SpawnerMinecart spawnerMinecart) + spawnerMinecart.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 index 2e6f179d..fef72b7f 100644 --- 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 @@ -10,14 +10,23 @@ import org.bukkit.entity.EntityType; import org.jetbrains.annotations.Nullable; +import static it.jakegblp.lusk.utils.Constants.MINECRAFT_1_20; + @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).") +@Description(""" +Gets the spawner entity type of the provided spawners or trial spawners (1.21+). + +**Works with items.** +**Works with Spawner Minecart (Requires 1.20+).** + +Can be set, reset (sets it to pig), and deleted (requires Minecraft 1.20+). +""") @Examples("set spawner entity type of {_block} to zombie") -@Since("1.3.4-beta1") +@Since("1.3.4") public class ExprSpawnerEntityType extends SimplerPropertyExpression { static { - register(ExprSpawnerEntityType.class, EntityData.class, "spawner (entity|creature) type", "itemtypes/blocks/blockstates"); + register(ExprSpawnerEntityType.class, EntityData.class, "spawner (entity|creature) type", "itemtypes/blocks/blockstates" + (MINECRAFT_1_20 ? "/entities" : "")); } @Override @@ -35,6 +44,16 @@ public boolean allowReset() { return true; } + @Override + public boolean allowDelete() { + return MINECRAFT_1_20; + } + + @Override + public void delete(Object from) { + super.delete(from); + } + @Override public void set(Object from, EntityData to) { new BlockWrapper(from).setSpawnerEntityType(to); diff --git a/src/main/java/it/jakegblp/lusk/utils/Constants.java b/src/main/java/it/jakegblp/lusk/utils/Constants.java index 67ca2563..ed9baa29 100644 --- a/src/main/java/it/jakegblp/lusk/utils/Constants.java +++ b/src/main/java/it/jakegblp/lusk/utils/Constants.java @@ -103,6 +103,10 @@ public class Constants { // * Whether the current server version is greater than or equal to 1.19.3 // */ //MINECRAFT_1_19_3 = VERSION_SERVER.isGreaterThanOrEqualTo(parseVersion("1.19.3")), + /** + * Whether the current server version is greater than or equal to 1.20 + */ + MINECRAFT_1_20 = VERSION_SERVER.isGreaterThanOrEqualTo(parseVersion("1.20")), /** * Whether the current server version is greater than or equal to 1.20.1 */