diff --git a/plugin/src/main/java/com/denizenscript/denizen/objects/properties/material/MaterialSides.java b/plugin/src/main/java/com/denizenscript/denizen/objects/properties/material/MaterialSides.java index 7da3aeb2a3..bc185a51d7 100644 --- a/plugin/src/main/java/com/denizenscript/denizen/objects/properties/material/MaterialSides.java +++ b/plugin/src/main/java/com/denizenscript/denizen/objects/properties/material/MaterialSides.java @@ -2,53 +2,82 @@ import com.denizenscript.denizen.objects.MaterialTag; import com.denizenscript.denizencore.objects.Mechanism; -import com.denizenscript.denizencore.objects.ObjectTag; import com.denizenscript.denizencore.objects.core.ListTag; -import com.denizenscript.denizencore.objects.properties.Property; -import com.denizenscript.denizencore.objects.properties.PropertyParser; import com.denizenscript.denizencore.utilities.CoreUtilities; import org.bukkit.block.BlockFace; -import org.bukkit.block.data.*; +import org.bukkit.block.data.BlockData; import org.bukkit.block.data.type.RedstoneWire; import org.bukkit.block.data.type.Wall; -public class MaterialSides implements Property { +public class MaterialSides extends MaterialProperty { + + // <--[property] + // @object MaterialTag + // @name sides + // @input ListTag + // @description + // Controls the list of heights for a wall block, or connections for a redstone wire, in order North|East|South|West|Vertical. + // For wall blocks: For n/e/s/w, can be "tall", "low", or "none". For vertical, can be "tall" or "none". + // For redstone wires: For n/e/s/w, can be "none", "side", or "up". No vertical. + // --> + + public static boolean describes(MaterialTag material) { + BlockData data = material.getModernData(); + return data instanceof Wall || data instanceof RedstoneWire; + } - public static boolean describes(ObjectTag material) { - if (!(material instanceof MaterialTag)) { - return false; - } - MaterialTag mat = (MaterialTag) material; - if (!mat.hasModernData()) { - return false; - } - BlockData data = mat.getModernData(); - if (!(data instanceof Wall) && !(data instanceof RedstoneWire)) { - return false; - } - return true; + MaterialTag material; + + @Override + public ListTag getPropertyValue() { + return getSidesList(); } - public static MaterialSides getFrom(ObjectTag _material) { - if (!describes(_material)) { - return null; - } - else { - return new MaterialSides((MaterialTag) _material); - } + @Override + public String getPropertyId() { + return "sides"; } - public static final String[] handledMechs = new String[] { - "sides", "heights" - }; + @Override + public void setPropertyValue(ListTag list, Mechanism mechanism) { - public MaterialSides(MaterialTag _material) { - material = _material; + // <--[mechanism] + // @object MaterialTag + // @name heights + // @input ElementTag + // @deprecated Use 'sides' + // @description + // Deprecated in favor of <@link property MaterialTag.sides> + // @tags + // + // --> + if (isWall()) { + if (list.size() != 5) { + mechanism.echoError("Invalid sides list, size must be 5."); + return; + } + Wall wall = getWall(); + wall.setHeight(BlockFace.NORTH, Wall.Height.valueOf(list.get(0).toUpperCase())); + wall.setHeight(BlockFace.EAST, Wall.Height.valueOf(list.get(1).toUpperCase())); + wall.setHeight(BlockFace.SOUTH, Wall.Height.valueOf(list.get(2).toUpperCase())); + wall.setHeight(BlockFace.WEST, Wall.Height.valueOf(list.get(3).toUpperCase())); + wall.setUp(CoreUtilities.toLowerCase(list.get(4)).equals("tall")); + } + else if (isWire()) { + if (list.size() != 4) { + mechanism.echoError("Invalid sides list, size must be 4."); + return; + } + RedstoneWire wire = getWire(); + wire.setFace(BlockFace.NORTH, RedstoneWire.Connection.valueOf(list.get(0).toUpperCase())); + wire.setFace(BlockFace.EAST, RedstoneWire.Connection.valueOf(list.get(1).toUpperCase())); + wire.setFace(BlockFace.SOUTH, RedstoneWire.Connection.valueOf(list.get(2).toUpperCase())); + wire.setFace(BlockFace.WEST, RedstoneWire.Connection.valueOf(list.get(3).toUpperCase())); + } } - MaterialTag material; - public static void register() { + autoRegister("sides", MaterialSides.class, ListTag.class, true, "heights"); // <--[tag] // @attribute @@ -57,21 +86,8 @@ public static void register() { // @group properties // @deprecated Use 'sides' // @description - // Deprecated in favor of <@link tag MaterialTag.sides> + // Deprecated in favor of <@link property MaterialTag.sides> // --> - // <--[tag] - // @attribute - // @returns ListTag - // @mechanism MaterialTag.sides - // @group properties - // @description - // Returns the list of heights for a wall block, or connections for a redstone wire, in order North|East|South|West|Vertical. - // For wall blocks: For n/e/s/w, can be "tall", "low", or "none". For vertical, can be "tall" or "none". - // For redstone wires: For n/e/s/w, can be "none", "side", or "up". No vertical. - // --> - PropertyParser.registerStaticTag(MaterialSides.class, ListTag.class, "sides", (attribute, material) -> { - return material.getSidesList(); - }, "heights"); } public boolean isWall() { @@ -109,66 +125,4 @@ else if (isWire()) { } return list; } - - @Override - public String getPropertyString() { - return getSidesList().identify(); - } - - @Override - public String getPropertyId() { - return "sides"; - } - - @Override - public void adjust(Mechanism mechanism) { - - // <--[mechanism] - // @object MaterialTag - // @name heights - // @input ElementTag - // @deprecated Use 'sides' - // @description - // Deprecated in favor of <@link mechanism MaterialTag.sides> - // @tags - // - // --> - // <--[mechanism] - // @object MaterialTag - // @name sides - // @input ElementTag - // @description - // Sets the list of heights for a wall block, or connections for a redstone wire, in order North|East|South|West|Vertical. - // For wall blocks: For n/e/s/w, can be "tall", "low", or "none". For vertical, can be "tall" or "none". - // For redstone wires: For n/e/s/w, can be "none", "side", or "up". No vertical. - // @tags - // - // --> - if ((mechanism.matches("sides") || mechanism.matches("heights")) && mechanism.requireObject(ListTag.class)) { - ListTag list = mechanism.valueAsType(ListTag.class); - if (isWall()) { - if (list.size() != 5) { - mechanism.echoError("Invalid sides list, size must be 5."); - return; - } - Wall wall = getWall(); - wall.setHeight(BlockFace.NORTH, Wall.Height.valueOf(list.get(0).toUpperCase())); - wall.setHeight(BlockFace.EAST, Wall.Height.valueOf(list.get(1).toUpperCase())); - wall.setHeight(BlockFace.SOUTH, Wall.Height.valueOf(list.get(2).toUpperCase())); - wall.setHeight(BlockFace.WEST, Wall.Height.valueOf(list.get(3).toUpperCase())); - wall.setUp(CoreUtilities.toLowerCase(list.get(4)).equals("tall")); - } - else if (isWire()) { - if (list.size() != 4) { - mechanism.echoError("Invalid sides list, size must be 4."); - return; - } - RedstoneWire wire = getWire(); - wire.setFace(BlockFace.NORTH, RedstoneWire.Connection.valueOf(list.get(0).toUpperCase())); - wire.setFace(BlockFace.EAST, RedstoneWire.Connection.valueOf(list.get(1).toUpperCase())); - wire.setFace(BlockFace.SOUTH, RedstoneWire.Connection.valueOf(list.get(2).toUpperCase())); - wire.setFace(BlockFace.WEST, RedstoneWire.Connection.valueOf(list.get(3).toUpperCase())); - } - } - } } diff --git a/plugin/src/main/java/com/denizenscript/denizen/objects/properties/material/MaterialSnowable.java b/plugin/src/main/java/com/denizenscript/denizen/objects/properties/material/MaterialSnowable.java index bd9db7d5ae..52e8d5dfc0 100644 --- a/plugin/src/main/java/com/denizenscript/denizen/objects/properties/material/MaterialSnowable.java +++ b/plugin/src/main/java/com/denizenscript/denizen/objects/properties/material/MaterialSnowable.java @@ -2,52 +2,46 @@ import com.denizenscript.denizen.objects.MaterialTag; import com.denizenscript.denizencore.objects.Mechanism; -import com.denizenscript.denizencore.objects.ObjectTag; import com.denizenscript.denizencore.objects.core.ElementTag; -import com.denizenscript.denizencore.objects.properties.Property; -import com.denizenscript.denizencore.objects.properties.PropertyParser; +import org.bukkit.block.data.BlockData; import org.bukkit.block.data.Snowable; -public class MaterialSnowable implements Property { +public class MaterialSnowable extends MaterialProperty { - public static boolean describes(ObjectTag material) { - return material instanceof MaterialTag - && ((MaterialTag) material).hasModernData() - && ((MaterialTag) material).getModernData() instanceof Snowable; - } + // <--[tag] + // @object MaterialTag + // @name snowy + // @input ElementTag(Boolean) + // @description + // Controls whether this material is covered in snow. + // --> - public static MaterialSnowable getFrom(ObjectTag _material) { - if (!describes(_material)) { - return null; - } - else { - return new MaterialSnowable((MaterialTag) _material); - } + public static boolean describes(MaterialTag material) { + BlockData data = material.getModernData(); + return data instanceof Snowable; } - public static final String[] handledMechs = new String[] { - "snowy" - }; + MaterialTag material; - public MaterialSnowable(MaterialTag _material) { - material = _material; + @Override + public ElementTag getPropertyValue() { + return new ElementTag(isSnowy()); } - MaterialTag material; + @Override + public String getPropertyId() { + return "snowy"; + } - public static void register() { + @Override + public void setPropertyValue(ElementTag value, Mechanism mechanism) { + if (mechanism.requireBoolean()) { + getSnowable().setSnowy(value.asBoolean()); + } + } - // <--[tag] - // @attribute - // @returns ElementTag(Boolean) - // @mechanism MaterialTag.snowy - // @group properties - // @description - // Returns whether this material is covered in snow or not. - // --> - PropertyParser.registerStaticTag(MaterialSnowable.class, ElementTag.class, "snowy", (attribute, material) -> { - return new ElementTag(material.isSnowy()); - }); + public static void register() { + autoRegister("snowy", MaterialSnowable.class, ElementTag.class, true); } public Snowable getSnowable() { @@ -57,31 +51,4 @@ public Snowable getSnowable() { public boolean isSnowy() { return getSnowable().isSnowy(); } - - @Override - public String getPropertyString() { - return String.valueOf(isSnowy()); - } - - @Override - public String getPropertyId() { - return "snowy"; - } - - @Override - public void adjust(Mechanism mechanism) { - - // <--[mechanism] - // @object MaterialTag - // @name snowy - // @input ElementTag(Boolean) - // @description - // Sets this material to be covered in snow, or not. - // @tags - // - // --> - if (mechanism.matches("snowy") && mechanism.requireBoolean()) { - getSnowable().setSnowy(mechanism.getValue().asBoolean()); - } - } } diff --git a/plugin/src/main/java/com/denizenscript/denizen/objects/properties/material/MaterialSwitchable.java b/plugin/src/main/java/com/denizenscript/denizen/objects/properties/material/MaterialSwitchable.java index 997e53b285..fefd40fd0a 100644 --- a/plugin/src/main/java/com/denizenscript/denizen/objects/properties/material/MaterialSwitchable.java +++ b/plugin/src/main/java/com/denizenscript/denizen/objects/properties/material/MaterialSwitchable.java @@ -4,27 +4,36 @@ import com.denizenscript.denizen.nms.NMSVersion; import com.denizenscript.denizen.objects.MaterialTag; import com.denizenscript.denizencore.objects.Mechanism; -import com.denizenscript.denizencore.objects.ObjectTag; import com.denizenscript.denizencore.objects.core.ElementTag; -import com.denizenscript.denizencore.objects.properties.Property; -import com.denizenscript.denizencore.objects.properties.PropertyParser; import org.bukkit.block.data.BlockData; import org.bukkit.block.data.Lightable; -import org.bukkit.block.data.Powerable; import org.bukkit.block.data.Openable; +import org.bukkit.block.data.Powerable; import org.bukkit.block.data.type.*; -public class MaterialSwitchable implements Property { - - public static boolean describes(ObjectTag material) { - if (!(material instanceof MaterialTag)) { - return false; - } - MaterialTag mat = (MaterialTag) material; - if (!mat.hasModernData()) { - return false; - } - BlockData data = mat.getModernData(); +public class MaterialSwitchable extends MaterialProperty { + + // <--[property] + // @object MaterialTag + // @name switched + // @input ElementTag(Boolean) + // @synonyms MaterialTag.lit, MaterialTag.open, MaterialTag.active + // @description + // Controls whether a material is 'switched on', which has different semantic meaning depending on the material type. + // More specifically, this controls whether: + // - a Powerable material (like pressure plates) is activated + // - an Openable material (like doors) is open + // - a dispenser is powered and should dispense its contents + // - a daylight sensor is inverted (detects darkness instead of light) + // - a lightable block is lit + // - a piston block is extended + // - an end portal frame has an ender eye in it + // - a hopper is NOT being powered by redstone + // - a sculk_shrieker can summon a warden + // --> + + public static boolean describes(MaterialTag material) { + BlockData data = material.getModernData(); return data instanceof Powerable || data instanceof Openable || data instanceof Dispenser @@ -36,218 +45,85 @@ public static boolean describes(ObjectTag material) { || (NMSHandler.getVersion().isAtLeast(NMSVersion.v1_19) && data instanceof SculkShrieker); } - public static MaterialSwitchable getFrom(ObjectTag _material) { - if (!describes(_material)) { - return null; - } - else { - return new MaterialSwitchable((MaterialTag) _material); - } - } - - public static final String[] handledMechs = new String[] { - "switched" - }; - - public MaterialSwitchable(MaterialTag _material) { - material = _material; - } - - public MaterialTag material; - - public static void register() { - - // <--[tag] - // @attribute - // @returns ElementTag(Boolean) - // @mechanism MaterialTag.switched - // @synonyms MaterialTag.lit, MaterialTag.open, MaterialTag.active - // @group properties - // @description - // Returns whether a material is 'switched on', which has different semantic meaning depending on the material type. - // More specifically, this returns whether: - // - a Powerable material (like pressure plates) is activated - // - an Openable material (like doors) is open - // - a dispenser is powered and should dispense its contents - // - a daylight sensor is inverted (detects darkness instead of light) - // - a lightable block is lit - // - a piston block is extended - // - an end portal frame has an ender eye in it - // - a hopper is NOT being powered by redstone - // - a sculk_shrieker can summon a warden - // --> - PropertyParser.registerStaticTag(MaterialSwitchable.class, ElementTag.class, "switched", (attribute, material) -> { - return new ElementTag(material.getState()); - }); - } - - public boolean isPowerable() { - return material.getModernData() instanceof Powerable; - } - - public boolean isOpenable() { - return material.getModernData() instanceof Openable; - } - - public boolean isDisepnser() { - return material.getModernData() instanceof Dispenser; - } - - public boolean isDaylightDetector() { - return material.getModernData() instanceof DaylightDetector; - } - - public boolean isLightable() { - return material.getModernData() instanceof Lightable; - } - - public boolean isPiston() { - return material.getModernData() instanceof Piston; - } - - public boolean isEndFrame() { - return material.getModernData() instanceof EndPortalFrame; - } - - public boolean isHopper() { - return material.getModernData() instanceof Hopper; - } - - public boolean isSculkShrieker() { - return NMSHandler.getVersion().isAtLeast(NMSVersion.v1_19) && material.getModernData() instanceof SculkShrieker; - } - - public Powerable getPowerable() { - return (Powerable) material.getModernData(); - } - - public Openable getOpenable() { - return (Openable) material.getModernData(); - } - - public Dispenser getDispenser() { - return (Dispenser) material.getModernData(); - } - - public DaylightDetector getDaylightDetector() { - return (DaylightDetector) material.getModernData(); - } - - public Piston getPiston() { - return (Piston) material.getModernData(); - } - - public Lightable getLightable() { - return (Lightable) material.getModernData(); + public MaterialSwitchable(MaterialTag material) { + super(material); } - public EndPortalFrame getEndFrame() { - return (EndPortalFrame) material.getModernData(); - } - - public Hopper getHopper() { - return (Hopper) material.getModernData(); - } - - /*public SculkShrieker getSculkShrieker() { // TODO: 1.19 - return (SculkShrieker) material.getModernData(); - }*/ - - public boolean getState() { - if (isOpenable()) { - return getOpenable().isOpen(); + @Override + public ElementTag getPropertyValue() { + if (getBlockData() instanceof Openable openable) { + return new ElementTag(openable.isOpen()); + } + else if (getBlockData() instanceof Lightable lightable) { + return new ElementTag(lightable.isLit()); } - else if (isLightable()) { - return getLightable().isLit(); + else if (getBlockData() instanceof Powerable powerable) { + return new ElementTag(powerable.isPowered()); } - else if (isPowerable()) { - return getPowerable().isPowered(); + else if (getBlockData() instanceof Dispenser dispenser) { + return new ElementTag(dispenser.isTriggered()); } - else if (isDisepnser()) { - return getDispenser().isTriggered(); + else if (getBlockData() instanceof DaylightDetector daylightDetector) { + return new ElementTag(daylightDetector.isInverted()); } - else if (isDaylightDetector()) { - return getDaylightDetector().isInverted(); + else if (getBlockData() instanceof Piston piston) { + return new ElementTag(piston.isExtended()); } - else if (isPiston()) { - return getPiston().isExtended(); + else if (getBlockData() instanceof EndPortalFrame endPortalFrame) { + return new ElementTag(endPortalFrame.hasEye()); } - else if (isEndFrame()) { - return getEndFrame().hasEye(); + else if (getBlockData() instanceof Hopper hopper) { + return new ElementTag(hopper.isEnabled()); } - else if (isHopper()) { - return getHopper().isEnabled(); + else if (NMSHandler.getVersion().isAtLeast(NMSVersion.v1_19) && getBlockData() instanceof SculkShrieker sculkShrieker) { + return new ElementTag(sculkShrieker.isCanSummon()); } - else if (isSculkShrieker()) { - return ((SculkShrieker) material.getModernData()).isCanSummon(); + return null; + } + + @Override + public String getPropertyId() { + return "switched"; + } + + @Override + public void setPropertyValue(ElementTag value, Mechanism mechanism) { + if (mechanism.requireBoolean()) { + setState(value.asBoolean()); } - return false; // Unreachable } public void setState(boolean state) { - if (isOpenable()) { - getOpenable().setOpen(state); + if (getBlockData() instanceof Openable openable) { + openable.setOpen(state); } - else if (isLightable()) { - getLightable().setLit(state); + else if (getBlockData() instanceof Lightable lightable) { + lightable.setLit(state); } - else if (isPowerable()) { - getPowerable().setPowered(state); + else if (getBlockData() instanceof Powerable powerable) { + powerable.setPowered(state); } - else if (isDisepnser()) { - getDispenser().setTriggered(state); + else if (getBlockData() instanceof Dispenser dispenser) { + dispenser.setTriggered(state); } - else if (isDaylightDetector()) { - getDaylightDetector().setInverted(state); + else if (getBlockData() instanceof DaylightDetector daylightDetector) { + daylightDetector.setInverted(state); } - else if (isPiston()) { - getPiston().setExtended(state); + else if (getBlockData() instanceof Piston piston) { + piston.setExtended(state); } - else if (isEndFrame()) { - getEndFrame().setEye(state); + else if (getBlockData() instanceof EndPortalFrame endPortalFrame) { + endPortalFrame.setEye(state); } - else if (isHopper()) { - getHopper().setEnabled(state); + else if (getBlockData() instanceof Hopper hopper) { + hopper.setEnabled(state); } - else if (isSculkShrieker()) { - ((SculkShrieker) material.getModernData()).setCanSummon(state); + else if (NMSHandler.getVersion().isAtLeast(NMSVersion.v1_19) && getBlockData() instanceof SculkShrieker sculkShrieker) { + sculkShrieker.setCanSummon(state); } } - @Override - public String getPropertyString() { - return String.valueOf(getState()); - } - - @Override - public String getPropertyId() { - return "switched"; - } - - @Override - public void adjust(Mechanism mechanism) { - - // <--[mechanism] - // @object MaterialTag - // @name switched - // @input ElementTag(Boolean) - // @description - // Sets whether a material is 'switched on', which has different semantic meaning depending on the material type. - // More specifically, this sets whether: - // - a Powerable material (like pressure plates) is activated - // - an Openable material (like doors) is open - // - a dispenser is powered and should dispense its contents - // - a daylight sensor can see the sun - // - a lightable block is lit - // - a piston block is extended - // - an end portal frame has an ender eye in it - // - a hopper is NOT being powered by redstone - // - a sculk_shrieker can summon a warden - // @tags - // - // --> - if (mechanism.matches("switched") && mechanism.requireBoolean()) { - setState(mechanism.getValue().asBoolean()); - } + public static void register() { + autoRegister("switched", MaterialSwitchable.class, ElementTag.class, true); } } diff --git a/plugin/src/main/java/com/denizenscript/denizen/scripts/commands/world/SwitchCommand.java b/plugin/src/main/java/com/denizenscript/denizen/scripts/commands/world/SwitchCommand.java index 92923d7018..2dfde9ed69 100644 --- a/plugin/src/main/java/com/denizenscript/denizen/scripts/commands/world/SwitchCommand.java +++ b/plugin/src/main/java/com/denizenscript/denizen/scripts/commands/world/SwitchCommand.java @@ -152,11 +152,8 @@ public void execute(final ScriptEntry scriptEntry) { } public static boolean switchState(Block b) { - MaterialSwitchable switchable = MaterialSwitchable.getFrom(new MaterialTag(b.getBlockData())); - if (switchable == null) { - return false; - } - return switchable.getState(); + MaterialSwitchable switchable = new MaterialSwitchable(new MaterialTag(b)); + return switchable.getPropertyValue().asBoolean(); } // Break off this portion of the code from execute() so it can be used in both execute and the delayed runnable @@ -164,20 +161,16 @@ public void switchBlock(ScriptEntry scriptEntry, Location interactLocation, Swit Block block = interactLocation.getBlock(); BlockData data1 = block.getBlockData(); MaterialTag materialTag = new MaterialTag(data1); - MaterialSwitchable switchable = MaterialSwitchable.getFrom(materialTag); - if (switchable == null) { - Debug.echoError("Cannot switch block of type '" + materialTag.getMaterial().name() + "'"); - return; - } + MaterialSwitchable switchable = new MaterialSwitchable(materialTag); if (materialTag.getMaterial() == Material.BELL) { NMSHandler.blockHelper.ringBell((Bell) block.getState()); return; } - boolean currentState = switchable.getState(); + boolean currentState = switchable.getPropertyValue().asBoolean(); if ((switchState.equals(SwitchState.ON) && !currentState) || (switchState.equals(SwitchState.OFF) && currentState) || switchState.equals(SwitchState.TOGGLE)) { switchable.setState(!currentState); if (physics) { - block.setBlockData(switchable.material.getModernData()); + block.setBlockData(switchable.getBlockData()); } else { ModifyBlockCommand.setBlock(block.getLocation(), materialTag, false, null); @@ -192,9 +185,9 @@ public void switchBlock(ScriptEntry scriptEntry, Location interactLocation, Swit } BlockData data2 = other.getBlock().getBlockData(); if (data2.getMaterial() == data1.getMaterial()) { - MaterialSwitchable switchable2 = MaterialSwitchable.getFrom(new MaterialTag(data2)); + MaterialSwitchable switchable2 = new MaterialSwitchable(new MaterialTag(data2)); switchable2.setState(!currentState); - other.getBlock().setBlockData(switchable2.material.getModernData()); + other.getBlock().setBlockData(switchable2.getBlockData()); if (physics) { AdjustBlockCommand.applyPhysicsAt(other); }