From 640143a0e67f1ce7d97db6a06f4ff5fc6646e8d1 Mon Sep 17 00:00:00 2001 From: "Alex \"mcmonkey\" Goodwin" Date: Thu, 23 Dec 2021 06:13:46 -0800 Subject: [PATCH] redstone wire blockdata support --- .../objects/properties/PropertyRegistry.java | 6 +- .../properties/material/MaterialHeights.java | 117 ------------ .../properties/material/MaterialSides.java | 173 ++++++++++++++++++ .../utilities/blocks/FullBlockData.java | 9 + 4 files changed, 185 insertions(+), 120 deletions(-) delete mode 100644 plugin/src/main/java/com/denizenscript/denizen/objects/properties/material/MaterialHeights.java create mode 100644 plugin/src/main/java/com/denizenscript/denizen/objects/properties/material/MaterialSides.java diff --git a/plugin/src/main/java/com/denizenscript/denizen/objects/properties/PropertyRegistry.java b/plugin/src/main/java/com/denizenscript/denizen/objects/properties/PropertyRegistry.java index 8d66d2fc2d..a47f08b57e 100644 --- a/plugin/src/main/java/com/denizenscript/denizen/objects/properties/PropertyRegistry.java +++ b/plugin/src/main/java/com/denizenscript/denizen/objects/properties/PropertyRegistry.java @@ -201,9 +201,6 @@ public static void registerMainProperties() { PropertyParser.registerProperty(MaterialDrags.class, MaterialTag.class); PropertyParser.registerProperty(MaterialFaces.class, MaterialTag.class); PropertyParser.registerProperty(MaterialHalf.class, MaterialTag.class); - if (NMSHandler.getVersion().isAtLeast(NMSVersion.v1_16)) { - PropertyParser.registerProperty(MaterialHeights.class, MaterialTag.class); - } PropertyParser.registerProperty(MaterialHinge.class, MaterialTag.class); PropertyParser.registerProperty(MaterialInstrument.class, MaterialTag.class); PropertyParser.registerProperty(MaterialLocked.class, MaterialTag.class); @@ -215,6 +212,9 @@ public static void registerMainProperties() { PropertyParser.registerProperty(MaterialPersistent.class, MaterialTag.class); PropertyParser.registerProperty(MaterialPower.class, MaterialTag.class); PropertyParser.registerProperty(MaterialShape.class, MaterialTag.class); + if (NMSHandler.getVersion().isAtLeast(NMSVersion.v1_16)) { + PropertyParser.registerProperty(MaterialSides.class, MaterialTag.class); + } PropertyParser.registerProperty(MaterialSnowable.class, MaterialTag.class); PropertyParser.registerProperty(MaterialSwitchable.class, MaterialTag.class); PropertyParser.registerProperty(MaterialSwitchFace.class, MaterialTag.class); diff --git a/plugin/src/main/java/com/denizenscript/denizen/objects/properties/material/MaterialHeights.java b/plugin/src/main/java/com/denizenscript/denizen/objects/properties/material/MaterialHeights.java deleted file mode 100644 index b5f2969340..0000000000 --- a/plugin/src/main/java/com/denizenscript/denizen/objects/properties/material/MaterialHeights.java +++ /dev/null @@ -1,117 +0,0 @@ -package com.denizenscript.denizen.objects.properties.material; - -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 org.bukkit.block.BlockFace; -import org.bukkit.block.data.*; -import org.bukkit.block.data.type.Wall; - -public class MaterialHeights 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(); - if (!(data instanceof Wall)) { - return false; - } - return true; - } - - public static MaterialHeights getFrom(ObjectTag _material) { - if (!describes(_material)) { - return null; - } - else { - return new MaterialHeights((MaterialTag) _material); - } - } - - public static final String[] handledMechs = new String[] { - "heights" - }; - - private MaterialHeights(MaterialTag _material) { - material = _material; - } - - MaterialTag material; - - public static void registerTags() { - - // <--[tag] - // @attribute - // @returns ListTag - // @mechanism MaterialTag.heights - // @group properties - // @description - // Returns the list of heights for a wall block, in order North|East|South|West|Vertical. - // For n/e/s/w, can be "tall", "low", or "none". For vertical, can be "tall" or "none". - // --> - PropertyParser.registerStaticTag(ListTag.class, "heights", (attribute, material) -> { - return material.getHeightsList(); - }); - } - - public Wall getWall() { - return (Wall) material.getModernData(); - } - - public ListTag getHeightsList() { - ListTag list = new ListTag(5); - Wall wall = getWall(); - list.add(wall.getHeight(BlockFace.NORTH).name()); - list.add(wall.getHeight(BlockFace.EAST).name()); - list.add(wall.getHeight(BlockFace.SOUTH).name()); - list.add(wall.getHeight(BlockFace.WEST).name()); - list.add(wall.isUp() ? "TALL" : "NONE"); - return list; - } - - @Override - public String getPropertyString() { - return getHeightsList().identify(); - } - - @Override - public String getPropertyId() { - return "heights"; - } - - @Override - public void adjust(Mechanism mechanism) { - - // <--[mechanism] - // @object MaterialTag - // @name heights - // @input ElementTag - // @description - // Sets the list of heights for a wall block, in order North|East|South|West|Vertical. - // For n/e/s/w, can be "tall", "low", or "none". For vertical, can be "tall" or "none". - // @tags - // - // --> - if (mechanism.matches("heights") && mechanism.requireObject(ListTag.class)) { - ListTag list = mechanism.valueAsType(ListTag.class); - if (list.size() != 5) { - mechanism.echoError("Invalid heights 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(list.get(4).toUpperCase().equals("TALL")); - } - } -} 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 new file mode 100644 index 0000000000..3063f3bb52 --- /dev/null +++ b/plugin/src/main/java/com/denizenscript/denizen/objects/properties/material/MaterialSides.java @@ -0,0 +1,173 @@ +package com.denizenscript.denizen.objects.properties.material; + +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 org.bukkit.block.BlockFace; +import org.bukkit.block.data.*; +import org.bukkit.block.data.type.RedstoneWire; +import org.bukkit.block.data.type.Wall; + +public class MaterialSides 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(); + if (!(data instanceof Wall) && !(data instanceof RedstoneWire)) { + return false; + } + return true; + } + + public static MaterialSides getFrom(ObjectTag _material) { + if (!describes(_material)) { + return null; + } + else { + return new MaterialSides((MaterialTag) _material); + } + } + + public static final String[] handledMechs = new String[] { + "sides", "heights" + }; + + private MaterialSides(MaterialTag _material) { + material = _material; + } + + MaterialTag material; + + public static void registerTags() { + + // <--[tag] + // @attribute + // @returns ListTag + // @mechanism MaterialTag.heights + // @group properties + // @deprecated Use 'sides' + // @description + // Deprecated in favor of <@link tag 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(ListTag.class, "sides", (attribute, material) -> { + return material.getSidesList(); + }, "heights"); + } + + public boolean isWall() { + return material.getModernData() instanceof Wall; + } + + public Wall getWall() { + return (Wall) material.getModernData(); + } + + public boolean isWire() { + return material.getModernData() instanceof RedstoneWire; + } + + public RedstoneWire getWire() { + return (RedstoneWire) material.getModernData(); + } + + public ListTag getSidesList() { + ListTag list = new ListTag(5); + if (isWall()) { + Wall wall = getWall(); + list.add(wall.getHeight(BlockFace.NORTH).name()); + list.add(wall.getHeight(BlockFace.EAST).name()); + list.add(wall.getHeight(BlockFace.SOUTH).name()); + list.add(wall.getHeight(BlockFace.WEST).name()); + list.add(wall.isUp() ? "TALL" : "NONE"); + } + else if (isWire()) { + RedstoneWire wire = getWire(); + list.add(wire.getFace(BlockFace.NORTH).name()); + list.add(wire.getFace(BlockFace.EAST).name()); + list.add(wire.getFace(BlockFace.SOUTH).name()); + list.add(wire.getFace(BlockFace.WEST).name()); + } + 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(list.get(4).toUpperCase().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/utilities/blocks/FullBlockData.java b/plugin/src/main/java/com/denizenscript/denizen/utilities/blocks/FullBlockData.java index 484478837a..ee64bd4ee3 100644 --- a/plugin/src/main/java/com/denizenscript/denizen/utilities/blocks/FullBlockData.java +++ b/plugin/src/main/java/com/denizenscript/denizen/utilities/blocks/FullBlockData.java @@ -13,6 +13,7 @@ import org.bukkit.block.Block; import org.bukkit.block.BlockFace; import org.bukkit.block.data.*; +import org.bukkit.block.data.type.RedstoneWire; import org.bukkit.block.data.type.Wall; import java.util.Map; @@ -151,6 +152,14 @@ else if (data instanceof MultipleFacing) { } return new FullBlockData(newData, tileEntityData, flags); } + else if (data instanceof RedstoneWire) { + RedstoneWire newData = (RedstoneWire) data.clone(); + newData.setFace(BlockFace.NORTH, ((RedstoneWire) data).getFace(BlockFace.EAST)); + newData.setFace(BlockFace.WEST, ((RedstoneWire) data).getFace(BlockFace.NORTH)); + newData.setFace(BlockFace.EAST, ((RedstoneWire) data).getFace(BlockFace.SOUTH)); + newData.setFace(BlockFace.SOUTH, ((RedstoneWire) data).getFace(BlockFace.WEST)); + return new FullBlockData(newData, tileEntityData, flags); + } else if (NMSHandler.getVersion().isAtLeast(NMSVersion.v1_16) && data instanceof Wall) { Wall newData = (Wall) data.clone(); newData.setHeight(BlockFace.NORTH, ((Wall) data).getHeight(BlockFace.EAST));