Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ loom {
dependencies {
// To change the versions see the gradle.properties file
minecraft "com.mojang:minecraft:${project.minecraft_version}"
mappings "net.fabricmc:yarn:${project.yarn_mappings}:v2"
mappings loom.officialMojangMappings()
modImplementation "net.fabricmc:fabric-loader:${project.loader_version}"

// Fabric API. This is technically optional, but you probably want it anyway.
Expand Down
6 changes: 2 additions & 4 deletions src/main/java/eu/pb4/placeholders/api/ParserContext.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package eu.pb4.placeholders.api;

import eu.pb4.placeholders.api.node.parent.DynamicShadowNode;
import net.minecraft.registry.RegistryWrapper;
import net.minecraft.text.Text;
import org.jetbrains.annotations.Nullable;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.function.Supplier;
import net.minecraft.core.HolderLookup;

public final class ParserContext {
private Map<Key<?>, Object> map;
Expand Down Expand Up @@ -95,7 +93,7 @@ public Key(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<HolderLookup.Provider> WRAPPER_LOOKUP = new Key<>("wrapper_lookup", HolderLookup.Provider.class);
public static final Key<DynamicShadowNode.Transformer> DEFAULT_SHADOW_STYLER = Key.ofNode("default_shadow_styler");

public static <T> Key<T> of(String key, T type) {
Expand Down
72 changes: 36 additions & 36 deletions src/main/java/eu/pb4/placeholders/api/PlaceholderContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,51 +2,51 @@

import com.mojang.authlib.GameProfile;
import eu.pb4.placeholders.impl.placeholder.ViewObjectImpl;
import net.minecraft.entity.Entity;
import net.minecraft.commands.CommandSource;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.PlayerConfigEntry;
import net.minecraft.server.command.CommandOutput;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.Vec2f;
import net.minecraft.util.math.Vec3d;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.server.players.NameAndId;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.phys.Vec2;
import net.minecraft.world.phys.Vec3;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.Nullable;

import java.util.function.Supplier;


public record PlaceholderContext(MinecraftServer server,
Supplier<ServerCommandSource> lazySource,
@Nullable ServerWorld world,
@Nullable ServerPlayerEntity player,
Supplier<CommandSourceStack> lazySource,
@Nullable ServerLevel world,
@Nullable ServerPlayer player,
@Nullable Entity entity,
@Nullable GameProfile gameProfile,
ViewObject view
) {

public PlaceholderContext(MinecraftServer server,
ServerCommandSource source,
@Nullable ServerWorld world,
@Nullable ServerPlayerEntity player,
CommandSourceStack source,
@Nullable ServerLevel world,
@Nullable ServerPlayer player,
@Nullable Entity entity,
@Nullable GameProfile gameProfile,
ViewObject view
) {
this(server, () -> source, world, player, entity, gameProfile, view);
}

public ServerCommandSource source() {
public CommandSourceStack source() {
return this.lazySource.get();
}

public PlaceholderContext(MinecraftServer server,
ServerCommandSource source,
@Nullable ServerWorld world,
@Nullable ServerPlayerEntity player,
CommandSourceStack source,
@Nullable ServerLevel world,
@Nullable ServerPlayer player,
@Nullable Entity entity,
@Nullable GameProfile gameProfile) {
this(server, source, world, player, entity, gameProfile, ViewObject.DEFAULT);
Expand All @@ -72,7 +72,7 @@ public boolean hasEntity() {
}

public ParserContext asParserContext() {
return ParserContext.of(KEY, this).with(ParserContext.Key.WRAPPER_LOOKUP, this.server.getRegistryManager());
return ParserContext.of(KEY, this).with(ParserContext.Key.WRAPPER_LOOKUP, this.server.registryAccess());
}

public PlaceholderContext withView(ViewObject view) {
Expand All @@ -81,7 +81,7 @@ public PlaceholderContext withView(ViewObject view) {

public void addToContext(ParserContext context) {
context.with(KEY, this);
context.with(ParserContext.Key.WRAPPER_LOOKUP, this.server.getRegistryManager());
context.with(ParserContext.Key.WRAPPER_LOOKUP, this.server.registryAccess());
}


Expand All @@ -90,7 +90,7 @@ public static PlaceholderContext of(MinecraftServer server) {
}

public static PlaceholderContext of(MinecraftServer server, ViewObject view) {
return new PlaceholderContext(server, server::getCommandSource, null, null, null, null, view);
return new PlaceholderContext(server, server::createCommandSourceStack, null, null, null, null, view);
}

public static PlaceholderContext of(GameProfile profile, MinecraftServer server) {
Expand All @@ -99,46 +99,46 @@ public static PlaceholderContext of(GameProfile profile, MinecraftServer server)

public static PlaceholderContext of(GameProfile profile, MinecraftServer server, ViewObject view) {
var name = profile.name() != null ? profile.name() : profile.id().toString();
return new PlaceholderContext(server, () -> new ServerCommandSource(CommandOutput.DUMMY, Vec3d.ZERO, Vec2f.ZERO, server.getOverworld(), server.getPermissionLevel(new PlayerConfigEntry(profile)), name, Text.literal(name), server, null), null, null, null, profile, view);
return new PlaceholderContext(server, () -> new CommandSourceStack(CommandSource.NULL, Vec3.ZERO, Vec2.ZERO, server.overworld(), server.getProfilePermissions(new NameAndId(profile)), name, Component.literal(name), server, null), null, null, null, profile, view);
}

public static PlaceholderContext of(ServerPlayerEntity player) {
public static PlaceholderContext of(ServerPlayer player) {
return of(player, ViewObject.DEFAULT);
}

public static PlaceholderContext of(ServerPlayerEntity player, ViewObject view) {
return new PlaceholderContext(player.getEntityWorld().getServer(), player::getCommandSource, player.getEntityWorld(), player, player, player.getGameProfile(), view);
public static PlaceholderContext of(ServerPlayer player, ViewObject view) {
return new PlaceholderContext(player.level().getServer(), player::createCommandSourceStack, player.level(), player, player, player.getGameProfile(), view);
}

public static PlaceholderContext of(ServerCommandSource source) {
public static PlaceholderContext of(CommandSourceStack source) {
return of(source, ViewObject.DEFAULT);
}

public static PlaceholderContext of(ServerCommandSource source, ViewObject view) {
return new PlaceholderContext(source.getServer(), source, source.getWorld(), source.getPlayer(), source.getEntity(), source.getPlayer() != null ? source.getPlayer().getGameProfile() : null, view);
public static PlaceholderContext of(CommandSourceStack source, ViewObject view) {
return new PlaceholderContext(source.getServer(), source, source.getLevel(), source.getPlayer(), source.getEntity(), source.getPlayer() != null ? source.getPlayer().getGameProfile() : null, view);
}

public static PlaceholderContext of(Entity entity) {
return of(entity, ViewObject.DEFAULT);
}

public static PlaceholderContext of(Entity entity, ViewObject view) {
if (entity instanceof ServerPlayerEntity player) {
if (entity instanceof ServerPlayer player) {
return of(player, view);
} else {
var world = (ServerWorld) entity.getEntityWorld();
return new PlaceholderContext(world.getServer(), () -> entity.getCommandSource(world), world, null, entity, null, view);
var world = (ServerLevel) entity.level();
return new PlaceholderContext(world.getServer(), () -> entity.createCommandSourceStackForNameResolution(world), world, null, entity, null, view);
}
}


public interface ViewObject {
ViewObject DEFAULT = of(Identifier.of("placeholder_api", "default"));
ViewObject DEFAULT = of(ResourceLocation.fromNamespaceAndPath("placeholder_api", "default"));

static ViewObject of(Identifier identifier) {
static ViewObject of(ResourceLocation identifier) {
return new ViewObjectImpl(identifier);
}

Identifier identifier();
ResourceLocation identifier();
}
}
16 changes: 8 additions & 8 deletions src/main/java/eu/pb4/placeholders/api/PlaceholderResult.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
package eu.pb4.placeholders.api;

import eu.pb4.placeholders.api.parsers.TextParserV1;
import net.minecraft.text.Style;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;
import net.minecraft.ChatFormatting;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.Style;

public final class PlaceholderResult {
private final Text text;
private final Component text;
private String string;
private final boolean valid;

private PlaceholderResult(Text text, String reason) {
private PlaceholderResult(Component text, String reason) {
if (text != null) {
this.text = text;
this.valid = true;
} else {
this.text = Text.literal("[" + (reason != null ? reason : "Invalid placeholder!") + "]").setStyle(Style.EMPTY.withColor(Formatting.GRAY).withItalic(true));
this.text = Component.literal("[" + (reason != null ? reason : "Invalid placeholder!") + "]").setStyle(Style.EMPTY.withColor(ChatFormatting.GRAY).withItalic(true));
this.valid = false;
}
}
Expand All @@ -25,7 +25,7 @@ private PlaceholderResult(Text text, String reason) {
*
* @return Text
*/
public Text text() {
public Component text() {
return this.text;
}

Expand Down Expand Up @@ -76,7 +76,7 @@ public static PlaceholderResult invalid() {
*
* @return PlaceholderResult
*/
public static PlaceholderResult value(Text text) {
public static PlaceholderResult value(Component text) {
return new PlaceholderResult(text, null);
}

Expand Down
43 changes: 21 additions & 22 deletions src/main/java/eu/pb4/placeholders/api/Placeholders.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,15 @@
import eu.pb4.placeholders.api.parsers.NodeParser;
import eu.pb4.placeholders.api.parsers.PatternPlaceholderParser;
import eu.pb4.placeholders.api.parsers.TagLikeParser;
import eu.pb4.placeholders.impl.placeholder.PlaceholderNode;
import eu.pb4.placeholders.impl.placeholder.builtin.PlayerPlaceholders;
import eu.pb4.placeholders.impl.placeholder.builtin.ServerPlaceholders;
import eu.pb4.placeholders.impl.placeholder.builtin.WorldPlaceholders;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;
import org.jetbrains.annotations.Nullable;

import java.util.*;
import java.util.regex.Pattern;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;

public final class Placeholders {
@Deprecated(forRemoval = true)
Expand All @@ -29,14 +28,14 @@ public final class Placeholders {
@Deprecated(forRemoval = true)
public static final Pattern PREDEFINED_PLACEHOLDER_PATTERN = PatternPlaceholderParser.PREDEFINED_PLACEHOLDER_PATTERN;

private static final HashMap<Identifier, PlaceholderHandler> PLACEHOLDERS = new HashMap<>();
private static final HashMap<ResourceLocation, PlaceholderHandler> PLACEHOLDERS = new HashMap<>();

private static final List<PlaceholderListChangedCallback> CHANGED_CALLBACKS = new ArrayList<>();

public static final PlaceholderGetter DEFAULT_PLACEHOLDER_GETTER = new PlaceholderGetter() {
@Override
public PlaceholderHandler getPlaceholder(String placeholder) {
return PLACEHOLDERS.get(Identifier.tryParse(placeholder));
return PLACEHOLDERS.get(ResourceLocation.tryParse(placeholder));
}

@Override
Expand All @@ -52,7 +51,7 @@ public boolean isContextOptional() {
*
* @return PlaceholderResult
*/
public static PlaceholderResult parsePlaceholder(Identifier identifier, String argument, PlaceholderContext context) {
public static PlaceholderResult parsePlaceholder(ResourceLocation identifier, String argument, PlaceholderContext context) {
if (PLACEHOLDERS.containsKey(identifier)) {
return PLACEHOLDERS.get(identifier).onPlaceholderRequest(context, argument);
} else {
Expand Down Expand Up @@ -80,11 +79,11 @@ public static ParentNode parseNodes(TextNode node, ParserContext.Key<Placeholder
*
* @return Text
*/
public static Text parseText(Text text, PlaceholderContext context) {
public static Component parseText(Component text, PlaceholderContext context) {
return parseNodes(TextNode.convert(text)).toText(ParserContext.of(PlaceholderContext.KEY, context));
}

public static Text parseText(TextNode textNode, PlaceholderContext context) {
public static Component parseText(TextNode textNode, PlaceholderContext context) {
return parseNodes(textNode).toText(ParserContext.of(PlaceholderContext.KEY, context));
}

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

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

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

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

@Deprecated(forRemoval = true)
public static Text parseText(TextNode textNode, Pattern pattern, Set<String> placeholders, ParserContext.Key<PlaceholderGetter> key) {
public static Component parseText(TextNode textNode, Pattern pattern, Set<String> placeholders, ParserContext.Key<PlaceholderGetter> key) {
return parseNodes(textNode, pattern, placeholders, key).toText();
}

/**
* Registers new placeholder for identifier
*/
public static void register(Identifier identifier, PlaceholderHandler handler) {
public static void register(ResourceLocation identifier, PlaceholderHandler handler) {
PLACEHOLDERS.put(identifier, handler);
for (var e : CHANGED_CALLBACKS) {
e.onPlaceholderListChange(identifier, false);
Expand All @@ -187,15 +186,15 @@ public static void register(Identifier identifier, PlaceholderHandler handler) {
/**
* Removes placeholder
*/
public static void remove(Identifier identifier) {
public static void remove(ResourceLocation identifier) {
if (PLACEHOLDERS.remove(identifier) != null) {
for (var e : CHANGED_CALLBACKS) {
e.onPlaceholderListChange(identifier, true);
}
}
}

public static ImmutableMap<Identifier, PlaceholderHandler> getPlaceholders() {
public static ImmutableMap<ResourceLocation, PlaceholderHandler> getPlaceholders() {
return ImmutableMap.copyOf(PLACEHOLDERS);
}

Expand All @@ -204,7 +203,7 @@ public static void registerChangeEvent(PlaceholderListChangedCallback callback)
}

public interface PlaceholderListChangedCallback {
void onPlaceholderListChange(Identifier identifier, boolean removed);
void onPlaceholderListChange(ResourceLocation identifier, boolean removed);
}

public interface PlaceholderGetter {
Expand Down
Loading