6 changes: 3 additions & 3 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ org.gradle.jvmargs=-Xmx1G

# Fabric Properties
# check these on https://fabricmc.net/use
minecraft_version=1.21.5-rc1
yarn_mappings=1.21.5-rc1+build.1
minecraft_version=1.21.5
yarn_mappings=1.21.5+build.1
loader_version=0.16.10

#Fabric api
fabric_version=0.119.1+1.21.5

# Mod Properties
mod_version = 2.6.1+1.21.5
mod_version = 2.6.2+1.21.5
maven_group = eu.pb4
archives_base_name = placeholder-api
47 changes: 39 additions & 8 deletions src/main/java/eu/pb4/placeholders/api/ParserContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,16 @@
public final class ParserContext {
private Map<Key<?>, Object> map;
private boolean copyOnWrite;
private boolean hasNodeContext;

private ParserContext(Map<Key<?>, Object> map, boolean copyOnWrite) {
private ParserContext(Map<Key<?>, Object> map, boolean copyOnWrite, boolean hasNodeContext) {
this.map = map;
this.copyOnWrite = copyOnWrite;
this.hasNodeContext = hasNodeContext;
}

public static ParserContext of() {
return new ParserContext(new HashMap<>(), false);
return new ParserContext(new HashMap<>(), false, false);
}

public static <T> ParserContext of(Key<T> key, T object) {
Expand All @@ -34,6 +36,7 @@ public <T> ParserContext with(Key<T> key, T object) {
this.copyOnWrite = false;
}
this.map.put(key, object);
this.hasNodeContext |= key.nodeContext();
return this;
}

Expand Down Expand Up @@ -68,24 +71,52 @@ public boolean contains(Key<?> key) {

public ParserContext copy() {
this.copyOnWrite = true;
return new ParserContext(this.map, true);
return new ParserContext(this.map, true, this.hasNodeContext);
}

public ParserContext copyWithoutNodeContext() {
if (this.hasNodeContext) {
var map = new HashMap<Key<?>, Object>();
for (var key : this.map.keySet()) {
if (!key.nodeContext()) {
map.put(key, this.map.get(key));
}
}

return new ParserContext(map, false, false);
}
return this.copy();
}

public record Key<T>(String key, @Nullable Class<T> type, boolean nodeContext) {
public Key(String key, @Nullable Class<T> type) {
this(key, type, false);
}


public record Key<T>(String key, @Nullable Class<T> type) {
public static final Key<Boolean> COMPACT_TEXT = new Key<>("compact_text", Boolean.class);
public static final Key<RegistryWrapper.WrapperLookup> WRAPPER_LOOKUP = new Key<>("wrapper_lookup", RegistryWrapper.WrapperLookup.class);
public static final Key<DynamicShadowNode.Transformer> DEFAULT_SHADOW_STYLER = Key.of("default_shadow_styler");
public static final Key<DynamicShadowNode.Transformer> DEFAULT_SHADOW_STYLER = Key.ofNode("default_shadow_styler");

public static <T> Key<T> of(String key, T type) {
//noinspection unchecked
return new Key<T>(key, (Class<T>) type.getClass());
return new Key<T>(key, (Class<T>) type.getClass(), false);
}


public static <T> Key<T> of(String key) {
//noinspection unchecked
return new Key<T>(key, null);
return new Key<T>(key, null, false);
}

public static <T> Key<T> ofNode(String key, T type) {
//noinspection unchecked
return new Key<T>(key, (Class<T>) type.getClass(), true);
}


public static <T> Key<T> ofNode(String key) {
//noinspection unchecked
return new Key<T>(key, null, true);
}
};
}
32 changes: 16 additions & 16 deletions src/main/java/eu/pb4/placeholders/api/Placeholders.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,31 +89,31 @@ public static Text parseText(TextNode textNode, PlaceholderContext context) {
}


@Deprecated
@Deprecated(forRemoval = true)
public static ParentNode parseNodes(TextNode node, Pattern pattern) {
return parseNodes(node, pattern, PlaceholderContext.KEY);
}
@Deprecated
@Deprecated(forRemoval = true)
public static ParentNode parseNodes(TextNode node, Pattern pattern, ParserContext.Key<PlaceholderContext> contextKey) {
return asSingleParent(PatternPlaceholderParser.of(pattern, contextKey, DEFAULT_PLACEHOLDER_GETTER).parseNodes(node));
}
@Deprecated
@Deprecated(forRemoval = true)
public static ParentNode parseNodes(TextNode node, Pattern pattern, PlaceholderGetter placeholderGetter) {
return parseNodes(node, pattern, placeholderGetter, PlaceholderContext.KEY);
}
@Deprecated
@Deprecated(forRemoval = true)
public static ParentNode parseNodes(TextNode node, Pattern pattern, PlaceholderGetter placeholderGetter, ParserContext.Key<PlaceholderContext> contextKey) {
return asSingleParent(PatternPlaceholderParser.of(pattern, contextKey, placeholderGetter).parseNodes(node));
}
@Deprecated
@Deprecated(forRemoval = true)
public static ParentNode parseNodes(TextNode node, Pattern pattern, Map<String, Text> placeholders) {
return asSingleParent(PatternPlaceholderParser.ofTextMap(pattern, placeholders).parseNodes(node));
}
@Deprecated
@Deprecated(forRemoval = true)
public static ParentNode parseNodes(TextNode node, Pattern pattern, Set<String> placeholders, ParserContext.Key<PlaceholderGetter> key) {
return parseNodes(node, pattern, placeholders, key, PlaceholderContext.KEY);
}
@Deprecated
@Deprecated(forRemoval = true)
public static ParentNode parseNodes(TextNode node, Pattern pattern, Set<String> placeholders, ParserContext.Key<PlaceholderGetter> key, ParserContext.Key<PlaceholderContext> contextKey) {
return asSingleParent(PatternPlaceholderParser.of(pattern, contextKey, new PlaceholderGetter() {
@Override
Expand All @@ -133,43 +133,43 @@ public boolean isContextOptional() {
}
}).parseNodes(node));
}
@Deprecated
@Deprecated(forRemoval = true)
public static Text parseText(Text text, PlaceholderContext context, Pattern pattern) {
return parseNodes(TextNode.convert(text), pattern).toText(ParserContext.of(PlaceholderContext.KEY, context));
}
@Deprecated
@Deprecated(forRemoval = true)
public static Text parseText(Text text, PlaceholderContext context, Pattern pattern, PlaceholderGetter placeholderGetter) {
return parseNodes(TextNode.convert(text), pattern, placeholderGetter).toText(ParserContext.of(PlaceholderContext.KEY, context));
}
@Deprecated
@Deprecated(forRemoval = true)
public static Text parseText(Text text, Pattern pattern, Map<String, Text> placeholders) {
return parseNodes(TextNode.convert(text), pattern, placeholders).toText(ParserContext.of());
}
@Deprecated
@Deprecated(forRemoval = true)
public static Text parseText(Text text, Pattern pattern, Set<String> placeholders, ParserContext.Key<PlaceholderGetter> key) {
return parseNodes(TextNode.convert(text), pattern, placeholders, key).toText(ParserContext.of());
}

@Deprecated
@Deprecated(forRemoval = true)
public static Text parseText(TextNode textNode, PlaceholderContext context, Pattern pattern) {
return parseNodes(textNode, pattern).toText(ParserContext.of(PlaceholderContext.KEY, context));
}

@Deprecated
@Deprecated(forRemoval = true)
public static Text parseText(TextNode textNode, PlaceholderContext context, Pattern pattern, PlaceholderGetter placeholderGetter) {
return parseNodes(textNode, pattern, placeholderGetter).toText(ParserContext.of(PlaceholderContext.KEY, context));
}

@Deprecated
@Deprecated(forRemoval = true)
public static Text parseText(TextNode textNode, PlaceholderContext context, Pattern pattern, Map<String, Text> placeholders) {
return parseNodes(textNode, pattern, placeholders).toText(ParserContext.of(PlaceholderContext.KEY, context));
}
@Deprecated
@Deprecated(forRemoval = true)
public static Text parseText(TextNode textNode, Pattern pattern, Map<String, Text> placeholders) {
return parseNodes(textNode, pattern, placeholders).toText();
}

@Deprecated
@Deprecated(forRemoval = true)
public static Text parseText(TextNode textNode, Pattern pattern, Set<String> placeholders, ParserContext.Key<PlaceholderGetter> key) {
return parseNodes(textNode, pattern, placeholders, key).toText();
}
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/eu/pb4/placeholders/api/TextParserUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@
* You should use {@link eu.pb4.placeholders.api.parsers.ParserBuilder} for stacked parsing
* or {@link eu.pb4.placeholders.api.parsers.TagParser} for only tags to text.
*/
@Deprecated
@Deprecated(forRemoval = true)
public final class TextParserUtils {
private TextParserUtils() {}

public static Text formatText(String text) {
return formatNodes(text).toText(null, true);
return formatNodes(text).toText(ParserContext.of(), true);
}

public static Text formatTextSafe(String text) {
return formatNodesSafe(text).toText(null, true);
return formatNodesSafe(text).toText(ParserContext.of(), true);
}

public static Text formatText(String text, TextParserV1.TagParserGetter getter) {
Expand Down
64 changes: 32 additions & 32 deletions src/main/java/eu/pb4/placeholders/api/node/parent/HoverNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,28 +41,7 @@ public T value() {
@SuppressWarnings("unchecked")
@Override
protected Style style(ParserContext context) {
if (this.action == Action.TEXT_NODE) {
return Style.EMPTY.withHoverEvent(new HoverEvent.ShowText(((TextNode) this.value).toText(context, true)));
} else if (this.action == Action.ENTITY_NODE) {
return Style.EMPTY.withHoverEvent(new HoverEvent.ShowEntity(((EntityNodeContent) this.value).toVanilla(context)));
} else if (this.action == Action.LAZY_ITEM_STACK) {
RegistryWrapper.WrapperLookup wrapper;
if (context.contains(ParserContext.Key.WRAPPER_LOOKUP)) {
wrapper = context.getOrThrow(ParserContext.Key.WRAPPER_LOOKUP);
} else if (context.contains(PlaceholderContext.KEY)) {
wrapper = context.getOrThrow(PlaceholderContext.KEY).server().getRegistryManager();
} else {
return Style.EMPTY;
}

return Style.EMPTY.withHoverEvent(new HoverEvent.ShowItem(((LazyItemStackNodeContent<T>) this.value).toVanilla(wrapper)));
} else if (this.action == Action.VANILLA_ITEM_STACK) {
return Style.EMPTY.withHoverEvent(new HoverEvent.ShowItem(((HoverEvent.ShowItem) this.value).item()));
} else if (this.action == Action.VANILLA_ENTITY) {
return Style.EMPTY.withHoverEvent(new HoverEvent.ShowEntity(((HoverEvent.ShowEntity) this.value).entity()));
} else {
return Style.EMPTY;
}
return Style.EMPTY.withHoverEvent(toVanilla(this.action, this.value, context));
}

@Override
Expand Down Expand Up @@ -111,6 +90,33 @@ public ParentTextNode copyWith(TextNode[] children, NodeParser parser) {
} return this.copyWith(children);
}


@Nullable
public static <T> HoverEvent toVanilla(HoverNode.Action<T, ?> action, T value, ParserContext context) {
if (action == Action.TEXT_NODE) {
return new HoverEvent.ShowText(((TextNode) value).toText(context.copyWithoutNodeContext(), true));
} else if (action == Action.ENTITY_NODE) {
return new HoverEvent.ShowEntity(((EntityNodeContent) value).toVanilla(context.copyWithoutNodeContext()));
} else if (action == Action.LAZY_ITEM_STACK) {
RegistryWrapper.WrapperLookup wrapper;
if (context.contains(ParserContext.Key.WRAPPER_LOOKUP)) {
wrapper = context.getOrThrow(ParserContext.Key.WRAPPER_LOOKUP);
} else if (context.contains(PlaceholderContext.KEY)) {
wrapper = context.getOrThrow(PlaceholderContext.KEY).server().getRegistryManager();
} else {
return null;
}

return new HoverEvent.ShowItem(((LazyItemStackNodeContent<T>) value).toVanilla(wrapper));
} else if (action == Action.VANILLA_ITEM_STACK) {
return new HoverEvent.ShowItem(((HoverEvent.ShowItem) value).item());
} else if (action == Action.VANILLA_ENTITY) {
return new HoverEvent.ShowEntity(((HoverEvent.ShowEntity) value).entity());
} else {
return null;
}
}

@Override
public String toString() {
return "HoverNode{" +
Expand Down Expand Up @@ -139,14 +145,11 @@ public String toString()
}
}

public record EntityNodeContent(EntityType<?>entityType, UUID uuid, @Nullable TextNode name) implements HoverEvent {
public EntityContent toVanilla(ParserContext context) {
return new EntityContent(this.entityType, this.uuid, Optional.ofNullable(this.name != null ? this.name.toText(context, true) : null));
public record EntityNodeContent(EntityType<?>entityType, UUID uuid, @Nullable TextNode name) {
public HoverEvent.EntityContent toVanilla(ParserContext context) {
return new HoverEvent.EntityContent(this.entityType, this.uuid, Optional.ofNullable(this.name != null ? this.name.toText(context, true) : null));
}

@Override
public Action getAction() { return Action.SHOW_ENTITY; }

@Override
public String toString() {
return "HoverNode$EntityNodeContent{id="+
Expand All @@ -159,7 +162,7 @@ public String toString() {
}
}

public record LazyItemStackNodeContent<T>(Identifier identifier, int count, DynamicOps<T> ops, T componentMap) implements HoverEvent {
public record LazyItemStackNodeContent<T>(Identifier identifier, int count, DynamicOps<T> ops, T componentMap) {
public ItemStack toVanilla(RegistryWrapper.WrapperLookup lookup) {
var stack = new ItemStack(lookup.getOrThrow(RegistryKeys.ITEM).getOrThrow(RegistryKey.of(RegistryKeys.ITEM, identifier)));
stack.setCount(count);
Expand All @@ -169,9 +172,6 @@ public ItemStack toVanilla(RegistryWrapper.WrapperLookup lookup) {
return stack;
}

@Override
public Action getAction() { return Action.SHOW_ITEM; }

@Override
public String toString() {
return "HoverNode$LazyItemStackNodeContent{id="
Expand Down
Loading