|
| 1 | +package com.shanebeestudios.skbee.elements.text.expressions; |
| 2 | + |
| 3 | +import ch.njol.skript.Skript; |
| 4 | +import ch.njol.skript.classes.Changer.ChangeMode; |
| 5 | +import ch.njol.skript.doc.Description; |
| 6 | +import ch.njol.skript.doc.Examples; |
| 7 | +import ch.njol.skript.doc.Name; |
| 8 | +import ch.njol.skript.doc.Since; |
| 9 | +import ch.njol.util.coll.CollectionUtils; |
| 10 | +import com.shanebeestudios.skbee.api.skript.base.SimplePropertyExpression; |
| 11 | +import com.shanebeestudios.skbee.api.wrapper.ComponentWrapper; |
| 12 | +import net.kyori.adventure.text.Component; |
| 13 | +import org.bukkit.block.Block; |
| 14 | +import org.bukkit.block.BlockState; |
| 15 | +import org.bukkit.block.Container; |
| 16 | +import org.bukkit.block.Skull; |
| 17 | +import org.bukkit.event.Event; |
| 18 | +import org.jetbrains.annotations.Nullable; |
| 19 | + |
| 20 | +@Name("TextComponent - Block Name") |
| 21 | +@Description({"Get/set/delete the custom name of a block. This will work on container blocks and skulls.", |
| 22 | + "Skulls require Paper 1.21.4(Build 214)+", |
| 23 | + "Even though the custom name of any skull can be set, only a player head will retain its name when broken (Skeleton/creeper/etc skulls will not)."}) |
| 24 | +@Examples({"set {_name} to component block name of target block", |
| 25 | + "set component block name of target block to mini message from \"<rainbow>Mr Potato Head!\"", |
| 26 | + "delete component block name of target block"}) |
| 27 | +@Since("INSERT VERSION") |
| 28 | +public class ExprNameBlock extends SimplePropertyExpression<Block, ComponentWrapper> { |
| 29 | + |
| 30 | + private static final boolean HAS_SKULL_NAME = Skript.methodExists(Skull.class, "customName"); |
| 31 | + |
| 32 | + static { |
| 33 | + register(ExprNameBlock.class, ComponentWrapper.class, |
| 34 | + "component [custom] block name", "block"); |
| 35 | + } |
| 36 | + |
| 37 | + @Override |
| 38 | + public @Nullable ComponentWrapper convert(Block block) { |
| 39 | + BlockState state = block.getState(); |
| 40 | + Component component = null; |
| 41 | + if (state instanceof Container container) { |
| 42 | + component = container.customName(); |
| 43 | + } else if (state instanceof Skull skull && HAS_SKULL_NAME) { |
| 44 | + component = skull.customName(); |
| 45 | + } |
| 46 | + if (component != null) return ComponentWrapper.fromComponent(component); |
| 47 | + return null; |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public Class<?> @Nullable [] acceptChange(ChangeMode mode) { |
| 52 | + if (mode == ChangeMode.SET) return CollectionUtils.array(ComponentWrapper.class); |
| 53 | + else if (mode == ChangeMode.DELETE) return CollectionUtils.array(); |
| 54 | + return null; |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + public void change(Event event, Object @Nullable [] delta, ChangeMode mode) { |
| 59 | + Component name = null; |
| 60 | + if (delta != null && delta[0] instanceof ComponentWrapper cw) { |
| 61 | + name = cw.getComponent(); |
| 62 | + } |
| 63 | + for (Block block : getExpr().getArray(event)) { |
| 64 | + setName(block, name); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + private void setName(Block block, Component name) { |
| 69 | + BlockState state = block.getState(false); |
| 70 | + if (state instanceof Container container) { |
| 71 | + container.customName(name); |
| 72 | + } else if (state instanceof Skull skull && HAS_SKULL_NAME) { |
| 73 | + skull.customName(name); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + protected String getPropertyName() { |
| 79 | + return "component block custom name"; |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + public Class<? extends ComponentWrapper> getReturnType() { |
| 84 | + return ComponentWrapper.class; |
| 85 | + } |
| 86 | + |
| 87 | +} |
0 commit comments