Skip to content

Commit

Permalink
ExprItemDisplayItem - update syntax and code to support block display
Browse files Browse the repository at this point in the history
- also remove old syntax for block display item
  • Loading branch information
ShaneBeee committed Jul 13, 2024
1 parent 625cc5f commit 60f4d67
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 80 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
import ch.njol.skript.expressions.base.SimplePropertyExpression;
import ch.njol.util.coll.CollectionUtils;
import com.shanebeestudios.skbee.elements.display.types.Types;
import org.bukkit.Material;
import org.bukkit.block.data.BlockData;
import org.bukkit.entity.BlockDisplay;
import org.bukkit.entity.Entity;
import org.bukkit.entity.ItemDisplay;
import org.bukkit.event.Event;
Expand All @@ -17,29 +20,33 @@
import org.jetbrains.annotations.Nullable;

@Name("DisplayEntity - Display Item")
@Description({"Represents the display item of an Item Display Entity", Types.McWIKI})
@Description({"Represents the display ItemType of an Item/Block Display Entity", Types.McWIKI})
@Examples({"set display item of {_display} to diamond sword",
"set display item of {_display} to air",
"delete display item of {_display}"})
"set display item of {_display} to air",
"delete display item of {_display}"})
@Since("2.8.0")
public class ExprItemDisplayItem extends SimplePropertyExpression<Entity, ItemType> {

static {
register(ExprItemDisplayItem.class, ItemType.class, "display item", "entities");
register(ExprItemDisplayItem.class, ItemType.class, "display item[[ ]type]", "entities");
}

@SuppressWarnings("ConstantValue")
@Override
public @Nullable ItemType convert(Entity entity) {
ItemType itemType = null;
if (entity instanceof ItemDisplay itemDisplay) {
ItemStack itemStack = itemDisplay.getItemStack();
if (itemStack != null) return new ItemType(itemStack);
if (itemStack != null) itemType = new ItemType(itemStack);
} else if (entity instanceof BlockDisplay blockDisplay) {
itemType = new ItemType(blockDisplay.getBlock().getMaterial());
}
return null;
return itemType;
}

@SuppressWarnings("NullableProblems")
@Override
public @Nullable Class<?>[] acceptChange(ChangeMode mode) {
public Class<?> @Nullable [] acceptChange(ChangeMode mode) {
if (mode == ChangeMode.SET || mode == ChangeMode.DELETE) return CollectionUtils.array(ItemType.class);
return null;
}
Expand All @@ -49,9 +56,16 @@ public class ExprItemDisplayItem extends SimplePropertyExpression<Entity, ItemTy
public void change(Event event, @Nullable Object[] delta, ChangeMode mode) {
ItemType itemType = (delta != null && delta[0] instanceof ItemType it) ? it : null;
ItemStack itemStack = itemType != null ? itemType.getRandom() : null;
BlockData blockData = Material.AIR.createBlockData();
if (itemType != null) {
Material material = itemType.getMaterial();
if (material.isBlock()) blockData = material.createBlockData();
}
for (Entity entity : getExpr().getArray(event)) {
if (entity instanceof ItemDisplay itemDisplay) {
itemDisplay.setItemStack(itemStack);
} else if (entity instanceof BlockDisplay blockDisplay) {
blockDisplay.setBlock(blockData);
}
}
}
Expand Down

0 comments on commit 60f4d67

Please sign in to comment.