|
| 1 | +package eu.pb4.placeholders.api.client; |
| 2 | + |
| 3 | +import com.google.common.collect.ImmutableMap; |
| 4 | +import eu.pb4.placeholders.api.*; |
| 5 | +import eu.pb4.placeholders.api.parsers.NodeParser; |
| 6 | +import eu.pb4.placeholders.api.parsers.TagLikeParser; |
| 7 | +import net.fabricmc.api.EnvType; |
| 8 | +import net.fabricmc.api.Environment; |
| 9 | +import net.minecraft.resources.Identifier; |
| 10 | +import org.jspecify.annotations.Nullable; |
| 11 | + |
| 12 | +import java.util.ArrayList; |
| 13 | +import java.util.HashMap; |
| 14 | +import java.util.List; |
| 15 | + |
| 16 | +@Environment(EnvType.CLIENT) |
| 17 | +public class ClientPlaceholders { |
| 18 | + private static final HashMap<Identifier, Placeholder<ClientPlaceholderContext, ?>> CLIENT_PLACEHOLDERS = new HashMap<>(); |
| 19 | + public static final Placeholders.PlaceholderGetter<ClientPlaceholderContext> CLIENT_PLACEHOLDER_GETTER = placeholder -> getClientPlaceholder(Identifier.tryParse(placeholder)); |
| 20 | + public static final NodeParser CLIENT_PLACEHOLDER_PARSER = TagLikeParser.placeholder(TagLikeParser.PLACEHOLDER, ClientPlaceholderContext.CLIENT_KEY, CLIENT_PLACEHOLDER_GETTER); |
| 21 | + private static final List<Placeholders.PlaceholderListChangedCallback> CLIENT_CHANGED_CALLBACKS = new ArrayList<>(); |
| 22 | + |
| 23 | + static { |
| 24 | + //noinspection ResultOfMethodCallIgnored |
| 25 | + Placeholders.COMMON_PLACEHOLDER_GETTER.getClass(); |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * Parses PlaceholderContext, can be used for parsing by hand |
| 30 | + * |
| 31 | + * @return PlaceholderResult |
| 32 | + */ |
| 33 | + public static PlaceholderResult parseClientPlaceholder(Identifier identifier, String argument, ClientPlaceholderContext context) { |
| 34 | + var placeholder = getClientPlaceholder(identifier); |
| 35 | + if (placeholder != null) { |
| 36 | + return placeholder.onPlaceholderRequest(context, argument); |
| 37 | + } else { |
| 38 | + return PlaceholderResult.invalid("Placeholder doesn't exist!"); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + @Nullable |
| 43 | + public static Placeholder<ClientPlaceholderContext, ?> getClientPlaceholder(Identifier identifier) { |
| 44 | + return CLIENT_PLACEHOLDERS.get(identifier); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Registers new placeholder for identifier |
| 49 | + */ |
| 50 | + public static <T> void registerClient(Identifier identifier, Placeholder.Handler<ClientPlaceholderContext, String> handler) { |
| 51 | + registerClient(identifier, ArgumentParser.STRING, handler); |
| 52 | + } |
| 53 | + |
| 54 | + public static <T> void registerClient(Identifier identifier, ArgumentParser<T> argumentParser, Placeholder.Handler<ClientPlaceholderContext, T> handler) { |
| 55 | + registerClient(new Placeholder<>(identifier, argumentParser, handler)); |
| 56 | + } |
| 57 | + |
| 58 | + public static void registerClient(Placeholder<ClientPlaceholderContext, ?> placeholder) { |
| 59 | + CLIENT_PLACEHOLDERS.put(placeholder.identifier(), placeholder); |
| 60 | + for (var e : CLIENT_CHANGED_CALLBACKS) { |
| 61 | + e.onPlaceholderListChange(placeholder.identifier(), false); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + public static ImmutableMap<Identifier, Placeholder<ClientPlaceholderContext, ?>> getClientPlaceholders() { |
| 66 | + return ImmutableMap.copyOf(CLIENT_PLACEHOLDERS); |
| 67 | + } |
| 68 | + |
| 69 | + public static void registerClientChangeEvent(Placeholders.PlaceholderListChangedCallback callback) { |
| 70 | + CLIENT_CHANGED_CALLBACKS.add(callback); |
| 71 | + } |
| 72 | +} |
0 commit comments