Skip to content

Commit

Permalink
Improved ExprSpawnerEntityType
Browse files Browse the repository at this point in the history
  • Loading branch information
JakeGBLP committed Feb 1, 2025
1 parent 7299bab commit 381e8cf
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 54 deletions.
112 changes: 61 additions & 51 deletions src/main/java/it/jakegblp/lusk/api/BlockWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -44,57 +45,55 @@ 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) {
this(object,true);
}

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
Expand All @@ -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,
Expand Down Expand Up @@ -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);
}
Expand All @@ -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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Object, EntityData> {

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
Expand All @@ -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);
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/it/jakegblp/lusk/utils/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down

0 comments on commit 381e8cf

Please sign in to comment.