Skip to content

Commit

Permalink
Migrate to Mojang mappings
Browse files Browse the repository at this point in the history
  • Loading branch information
Gegy committed Jul 21, 2024
1 parent 8d3ac11 commit 3330ed4
Show file tree
Hide file tree
Showing 57 changed files with 714 additions and 615 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,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
64 changes: 32 additions & 32 deletions src/main/java/eu/pb4/placeholders/api/PlaceholderContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,48 @@

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.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.world.entity.Entity;
import net.minecraft.world.phys.Vec2;
import net.minecraft.world.phys.Vec3;
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 Down Expand Up @@ -86,7 +86,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 @@ -95,45 +95,45 @@ public static PlaceholderContext of(GameProfile profile, MinecraftServer server)

public static PlaceholderContext of(GameProfile profile, MinecraftServer server, ViewObject view) {
var name = profile.getName() != null ? profile.getName() : profile.getId().toString();
return new PlaceholderContext(server, () -> new ServerCommandSource(CommandOutput.DUMMY, Vec3d.ZERO, Vec2f.ZERO, server.getOverworld(), server.getPermissionLevel(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(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.getServer(), player::getCommandSource, player.getServerWorld(), player, player, player.getGameProfile(), view);
public static PlaceholderContext of(ServerPlayer player, ViewObject view) {
return new PlaceholderContext(player.getServer(), player::createCommandSourceStack, player.serverLevel(), 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 {
return new PlaceholderContext(entity.getServer(), entity::getCommandSource, (ServerWorld) entity.getWorld(), null, entity, null, view);
return new PlaceholderContext(entity.getServer(), entity::createCommandSourceStack, (ServerLevel) entity.level(), 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
49 changes: 26 additions & 23 deletions src/main/java/eu/pb4/placeholders/api/Placeholders.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,18 @@
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 net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import org.jetbrains.annotations.Nullable;

import java.util.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Pattern;

public final class Placeholders {
Expand All @@ -29,14 +32,14 @@ public final class Placeholders {
@Deprecated
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 +55,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 +83,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 +109,7 @@ public static ParentNode parseNodes(TextNode node, Pattern pattern, PlaceholderG
return asSingleParent(PatternPlaceholderParser.of(pattern, contextKey, placeholderGetter).parseNodes(node));
}
@Deprecated
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
Expand Down Expand Up @@ -134,50 +137,50 @@ public boolean isContextOptional() {
}).parseNodes(node));
}
@Deprecated
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
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
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
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
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
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
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
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
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 +190,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 +207,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
8 changes: 4 additions & 4 deletions src/main/java/eu/pb4/placeholders/api/TextParserUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import eu.pb4.placeholders.api.node.parent.ParentNode;
import eu.pb4.placeholders.api.node.parent.ParentTextNode;
import eu.pb4.placeholders.api.parsers.TextParserV1;
import net.minecraft.text.Text;
import net.minecraft.network.chat.Component;


/**
Expand All @@ -15,15 +15,15 @@
public final class TextParserUtils {
private TextParserUtils() {}

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

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

public static Text formatText(String text, TextParserV1.TagParserGetter getter) {
public static Component formatText(String text, TextParserV1.TagParserGetter getter) {
return formatNodes(text, getter).toText(null, true);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package eu.pb4.placeholders.api.arguments;

import net.minecraft.util.function.CharPredicate;
import net.minecraft.CharPredicate;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
Expand Down
Loading

0 comments on commit 3330ed4

Please sign in to comment.