From 943506c7adcabd5195ba9c5f0c4fcea02adce680 Mon Sep 17 00:00:00 2001 From: "Alex \"mcmonkey\" Goodwin" Date: Thu, 5 May 2022 07:47:02 -0700 Subject: [PATCH] MaterialTag.distance: leaves support --- .../denizen/objects/LocationTag.java | 10 ++-- .../properties/material/MaterialDistance.java | 46 +++++++++++++++---- .../material/MaterialPersistent.java | 4 -- .../utilities/BukkitImplDeprecations.java | 3 ++ 4 files changed, 47 insertions(+), 16 deletions(-) diff --git a/plugin/src/main/java/com/denizenscript/denizen/objects/LocationTag.java b/plugin/src/main/java/com/denizenscript/denizen/objects/LocationTag.java index 5591e33f46..b4a481ed92 100644 --- a/plugin/src/main/java/com/denizenscript/denizen/objects/LocationTag.java +++ b/plugin/src/main/java/com/denizenscript/denizen/objects/LocationTag.java @@ -3758,14 +3758,16 @@ else if (PolygonTag.matches(attribute.getParam())) { // @attribute // @returns ElementTag(Number) // @group world + // @deprecated Use MaterialTag.distance // @description - // Returns a number of how many blocks away from a connected tree leaves are. - // Defaults to 7 if not connected to a tree. + // Deprecated in favor of <@link tag MaterialTag.distance> + // Used like <[location].material.distance> // --> tagProcessor.registerTag(ElementTag.class, "tree_distance", (attribute, object) -> { + BukkitImplDeprecations.locationDistanceTag.warn(attribute.context); MaterialTag material = new MaterialTag(object.getBlockDataForTag(attribute)); - if (MaterialPersistent.describes(material)) { - return new ElementTag(MaterialPersistent.getFrom(material).getDistance()); + if (MaterialDistance.describes(material)) { + return new ElementTag(MaterialDistance.getFrom(material).getDistance()); } return null; }); diff --git a/plugin/src/main/java/com/denizenscript/denizen/objects/properties/material/MaterialDistance.java b/plugin/src/main/java/com/denizenscript/denizen/objects/properties/material/MaterialDistance.java index c7e6fee2a9..221ea288e8 100644 --- a/plugin/src/main/java/com/denizenscript/denizen/objects/properties/material/MaterialDistance.java +++ b/plugin/src/main/java/com/denizenscript/denizen/objects/properties/material/MaterialDistance.java @@ -6,6 +6,7 @@ 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.type.Leaves; import org.bukkit.block.data.type.Scaffolding; public class MaterialDistance implements Property { @@ -13,7 +14,8 @@ public class MaterialDistance implements Property { public static boolean describes(Object material) { return material instanceof MaterialTag && ((MaterialTag) material).hasModernData() - && ((MaterialTag) material).getModernData() instanceof Scaffolding; + && (((MaterialTag) material).getModernData() instanceof Scaffolding + || ((MaterialTag) material).getModernData() instanceof Leaves); } public static MaterialDistance getFrom(ObjectTag _material) { @@ -43,17 +45,40 @@ public static void registerTags() { // @mechanism MaterialTag.distance // @group properties // @description - // Returns the horizontal distance between a scaffolding block and the nearest scaffolding block placed above a 'bottom' scaffold. + // Returns the horizontal distance between a scaffolding block and the nearest scaffolding block placed above a 'bottom' scaffold, + // or between a leaves block and the nearest log (a distance of 7 will cause a leaf to decay if 'persistent' is also false, less than 7 will prevent decay). // --> PropertyParser.registerStaticTag(ElementTag.class, "distance", (attribute, material) -> { - return new ElementTag(material.getScaffolding().getDistance()); + return new ElementTag(material.getDistance()); }); } + public int getDistance() { + if (isScaffolding()) { + return getScaffolding().getDistance(); + } + else if (isLeaves()) { + return getLeaves().getDistance(); + } + throw new UnsupportedOperationException(); + } + public Scaffolding getScaffolding() { return (Scaffolding) material.getModernData(); } + public Leaves getLeaves() { + return (Leaves) material.getModernData(); + } + + public boolean isScaffolding() { + return material.getModernData() instanceof Scaffolding; + } + + public boolean isLeaves() { + return material.getModernData() instanceof Leaves; + } + @Override public String getPropertyString() { return String.valueOf(getScaffolding().getDistance()); @@ -72,17 +97,22 @@ public void adjust(Mechanism mechanism) { // @name distance // @input ElementTag(Number) // @description - // Sets the horizontal distance between a scaffolding block and the nearest scaffolding block placed above a 'bottom' scaffold. + // Sets the horizontal distance between a scaffolding block and the nearest scaffolding block placed above a 'bottom' scaffold, or between a leaves block and the nearest log. // @tags // // --> if (mechanism.matches("distance") && mechanism.requireInteger()) { int distance = mechanism.getValue().asInt(); - if (distance >= 0 && distance <= getScaffolding().getMaximumDistance()) { - getScaffolding().setDistance(distance); + if (isScaffolding()) { + if (distance >= 0 && distance <= getScaffolding().getMaximumDistance()) { + getScaffolding().setDistance(distance); + } + else { + mechanism.echoError("Distance must be between 0 and " + getScaffolding().getMaximumDistance()); + } } - else { - mechanism.echoError("Distance must be between 0 and " + getScaffolding().getMaximumDistance()); + else if (isLeaves()) { + getLeaves().setDistance(distance); } } } diff --git a/plugin/src/main/java/com/denizenscript/denizen/objects/properties/material/MaterialPersistent.java b/plugin/src/main/java/com/denizenscript/denizen/objects/properties/material/MaterialPersistent.java index e6e8928000..0f116c4167 100644 --- a/plugin/src/main/java/com/denizenscript/denizen/objects/properties/material/MaterialPersistent.java +++ b/plugin/src/main/java/com/denizenscript/denizen/objects/properties/material/MaterialPersistent.java @@ -54,10 +54,6 @@ public Leaves getLeaves() { return (Leaves) material.getModernData(); } - public int getDistance() { - return getLeaves().getDistance(); - } - @Override public String getPropertyString() { return String.valueOf(getLeaves().isPersistent()); diff --git a/plugin/src/main/java/com/denizenscript/denizen/utilities/BukkitImplDeprecations.java b/plugin/src/main/java/com/denizenscript/denizen/utilities/BukkitImplDeprecations.java index d549a65efa..315a71873f 100644 --- a/plugin/src/main/java/com/denizenscript/denizen/utilities/BukkitImplDeprecations.java +++ b/plugin/src/main/java/com/denizenscript/denizen/utilities/BukkitImplDeprecations.java @@ -164,6 +164,9 @@ public class BukkitImplDeprecations { // Added 2019/11/11, made slow 2021/11/2021. public static Warning entityLocationCursorOnTag = new SlowWarning("entityLocationCursorOnTag", "entity.location.cursor_on tags should be replaced by entity.cursor_on (be careful with the slight differences though)."); + // Added 2021/05/05. + public static Warning locationDistanceTag = new SlowWarning("locationDistanceTag", "locationtag.tree_distance is deprecated in favor of location.material.distance"); + // ==================== VERY SLOW deprecations ==================== // These are only shown minimally, so server owners are aware of them but not bugged by them. Only servers with active scripters (using 'ex reload') will see them often.