Skip to content

Commit

Permalink
LocationTag.format
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Feb 5, 2023
1 parent 150d3eb commit f57cf67
Showing 1 changed file with 39 additions and 0 deletions.
Expand Up @@ -54,6 +54,7 @@
import org.bukkit.util.Vector;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

public class LocationTag extends org.bukkit.Location implements ObjectTag, Notable, Adjustable, FlaggableObject {
Expand Down Expand Up @@ -3069,6 +3070,33 @@ else if (attribute.startsWith("unexplored_structure", 2) && attribute.hasContext
return list;
});

// <--[tag]
// @attribute <LocationTag.format[<format>]>
// @returns ElementTag
// @group identity
// @description
// Formats a LocationTag according to a specified format input.
// You can request x, y, z, yaw, or pitch as full "f", short "s", or block "b" format.
// eg "fx" for "full X coordinate" (like 1.051234), "syaw" for "short yaw" (like 1.03), or "bz" for "block Z coordinate" (like 1).
// "world" is also available to get the world name.
// If "$" is in the format, only codes prefixed with "$" will be replaced, eg "$bx but not bx" will become "5 but not bx" (allowing the safe use of unpredictable text inside the format block).
// @example
// # This example does a simple format, like "1, 2, 3".
// - narrate "You are at <player.location.format[bx, by, bz]>"
// @example
// # This example adds colors, uses the "$" prefix to be safe from bugs, and formats like "You are at y = 32 in Space".
// - narrate "<&[base]>You are at <player.location.format[y = <&[emphasis]>$by <&[base]>in <&[emphasis]>$world]>"
// -->
tagProcessor.registerTag(ElementTag.class, ElementTag.class, "format", (attribute, object, formatElem) -> {
String format = formatElem.asString();
String prefix = format.contains("$") ? "$" : "";
format = format.replace(prefix + "world", object.getWorldName() == null ? "" : object.getWorldName());
format = object.formatHelper(format, prefix + "b", (d) -> String.valueOf((long) Math.floor(d)));
format = object.formatHelper(format, prefix + "f", CoreUtilities::doubleToString);
format = object.formatHelper(format, prefix + "s", (d) -> CoreUtilities.doubleToString(Math.round(d * 100) / 100.0));
return new ElementTag(format, true);
});

// <--[tag]
// @attribute <LocationTag.formatted>
// @returns ElementTag
Expand Down Expand Up @@ -5406,6 +5434,17 @@ else if (state instanceof Dropper) {
tagProcessor.processMechanism(this, mechanism);
}

public String formatHelper(String format, String prefix, Function<Double, String> formatNum) {
if (!format.contains(prefix)) {
return format;
}
return format.replace(prefix + "yaw", formatNum.apply((double) getYaw()))
.replace(prefix + "pitch", formatNum.apply((double) getPitch()))
.replace(prefix + "x", formatNum.apply(getX()))
.replace(prefix + "y", formatNum.apply(getY()))
.replace(prefix + "z", formatNum.apply(getZ()));
}

@Override
public boolean advancedMatches(String matcher) {
String matcherLow = CoreUtilities.toLowerCase(matcher);
Expand Down

0 comments on commit f57cf67

Please sign in to comment.