Skip to content

Commit

Permalink
MaterialTag.distance: leaves support
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed May 5, 2022
1 parent 225fe71 commit 943506c
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 16 deletions.
Expand Up @@ -3758,14 +3758,16 @@ else if (PolygonTag.matches(attribute.getParam())) {
// @attribute <LocationTag.tree_distance>
// @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;
});
Expand Down
Expand Up @@ -6,14 +6,16 @@
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 {

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) {
Expand Down Expand Up @@ -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.<MaterialDistance, ElementTag>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());
Expand All @@ -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
// <MaterialTag.distance>
// -->
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);
}
}
}
Expand Down
Expand Up @@ -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());
Expand Down
Expand Up @@ -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.

Expand Down

0 comments on commit 943506c

Please sign in to comment.