|
1 | 1 | package eu.pb4.placeholders.api; |
2 | 2 |
|
3 | 3 | import com.mojang.authlib.GameProfile; |
| 4 | +import eu.pb4.placeholders.impl.PlaceholderContextImpl; |
4 | 5 | import eu.pb4.placeholders.impl.placeholder.ViewObjectImpl; |
5 | | -import net.minecraft.commands.CommandSource; |
6 | 6 | import net.minecraft.commands.CommandSourceStack; |
7 | | -import net.minecraft.network.chat.Component; |
| 7 | +import net.minecraft.core.BlockPos; |
| 8 | +import net.minecraft.core.HolderLookup; |
8 | 9 | import net.minecraft.resources.Identifier; |
9 | 10 | import net.minecraft.server.MinecraftServer; |
10 | 11 | import net.minecraft.server.level.ServerLevel; |
11 | 12 | import net.minecraft.server.level.ServerPlayer; |
12 | 13 | import net.minecraft.server.players.NameAndId; |
13 | 14 | import net.minecraft.world.entity.Entity; |
14 | | -import net.minecraft.world.phys.Vec2; |
| 15 | +import net.minecraft.world.entity.player.Player; |
| 16 | +import net.minecraft.world.level.Level; |
15 | 17 | import net.minecraft.world.phys.Vec3; |
16 | | -import org.jetbrains.annotations.Contract; |
17 | | -import org.jetbrains.annotations.Nullable; |
| 18 | +import org.jspecify.annotations.Nullable; |
18 | 19 |
|
19 | | -import java.util.function.Supplier; |
| 20 | +import javax.tools.Diagnostic; |
20 | 21 |
|
| 22 | +public interface PlaceholderContext { |
| 23 | + ParserContext.Key<PlaceholderContext> COMMON_KEY = ParserContext.Key.of("placeholder_context", PlaceholderContext.class); |
21 | 24 |
|
22 | | -public record PlaceholderContext(MinecraftServer server, |
23 | | - Supplier<CommandSourceStack> lazySource, |
24 | | - @Nullable ServerLevel world, |
25 | | - @Nullable ServerPlayer player, |
26 | | - @Nullable Entity entity, |
27 | | - @Nullable GameProfile gameProfile, |
28 | | - ViewObject view |
29 | | -) { |
30 | | - |
31 | | - public CommandSourceStack source() { |
32 | | - return this.lazySource.get(); |
| 25 | + default boolean hasLevel() { |
| 26 | + return this.level() != null; |
33 | 27 | } |
34 | 28 |
|
35 | | - public static ParserContext.Key<PlaceholderContext> KEY = new ParserContext.Key<>("placeholder_context", PlaceholderContext.class); |
36 | | - |
37 | | - public boolean hasWorld() { |
38 | | - return this.world != null; |
| 29 | + default boolean hasPlayer() { |
| 30 | + return this.player() != null; |
39 | 31 | } |
40 | 32 |
|
41 | | - public boolean hasPlayer() { |
42 | | - return this.player != null; |
| 33 | + default boolean hasNameAndId() { |
| 34 | + return this.nameAndId() != null; |
43 | 35 | } |
44 | 36 |
|
45 | | - public boolean hasGameProfile() { |
46 | | - return this.gameProfile != null; |
| 37 | + default boolean hasGameProfile() { |
| 38 | + return this.gameProfile() != null; |
47 | 39 | } |
48 | 40 |
|
49 | | - public boolean hasEntity() { |
50 | | - return this.entity != null; |
| 41 | + default boolean hasEntity() { |
| 42 | + return this.entity() != null; |
51 | 43 | } |
52 | 44 |
|
53 | | - public ParserContext asParserContext() { |
54 | | - return ParserContext.of(KEY, this).with(ParserContext.Key.WRAPPER_LOOKUP, this.server.registryAccess()); |
| 45 | + default boolean hasHolderLookup() { |
| 46 | + return this.holderLookup() != null; |
55 | 47 | } |
56 | 48 |
|
57 | | - public PlaceholderContext withView(ViewObject view) { |
58 | | - return new PlaceholderContext(this.server, this.lazySource, this.world, this.player, this.entity, this.gameProfile, view); |
| 49 | + default boolean hasBlockPosition() { |
| 50 | + return this.blockPosition() != null; |
59 | 51 | } |
60 | 52 |
|
61 | | - public void addToContext(ParserContext context) { |
62 | | - context.with(KEY, this); |
63 | | - context.with(ParserContext.Key.WRAPPER_LOOKUP, this.server.registryAccess()); |
| 53 | + default boolean hasPosition() { |
| 54 | + return this.position() != null; |
64 | 55 | } |
65 | 56 |
|
| 57 | + PlaceholderContext withView(PlaceholderContextImpl.ViewObject view); |
66 | 58 |
|
67 | | - public static PlaceholderContext of(MinecraftServer server) { |
68 | | - return of(server, ViewObject.DEFAULT); |
69 | | - } |
70 | | - |
71 | | - public static PlaceholderContext of(MinecraftServer server, ViewObject view) { |
72 | | - return new PlaceholderContext(server, server::createCommandSourceStack, null, null, null, null, view); |
73 | | - } |
74 | 59 |
|
75 | | - public static PlaceholderContext of(GameProfile profile, MinecraftServer server) { |
76 | | - return of(profile, server, ViewObject.DEFAULT); |
| 60 | + default ParserContext asParserContext() { |
| 61 | + return ParserContext.of(COMMON_KEY, this).with(ParserContext.Key.HOLDER_LOOKUP, this.holderLookup()); |
77 | 62 | } |
78 | 63 |
|
79 | | - public static PlaceholderContext of(GameProfile profile, MinecraftServer server, ViewObject view) { |
80 | | - var name = profile.name() != null ? profile.name() : profile.id().toString(); |
81 | | - 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); |
| 64 | + default void addToContext(ParserContext context) { |
| 65 | + context.with(COMMON_KEY, this); |
| 66 | + context.withIfNotSet(ParserContext.Key.HOLDER_LOOKUP, this.holderLookup()); |
82 | 67 | } |
83 | 68 |
|
84 | | - public static PlaceholderContext of(ServerPlayer player) { |
85 | | - return of(player, ViewObject.DEFAULT); |
86 | | - } |
| 69 | + HolderLookup.@Nullable Provider holderLookup(); |
| 70 | + @Nullable |
| 71 | + Level level(); |
87 | 72 |
|
88 | | - public static PlaceholderContext of(ServerPlayer player, ViewObject view) { |
89 | | - return new PlaceholderContext(player.level().getServer(), player::createCommandSourceStack, player.level(), player, player, player.getGameProfile(), view); |
90 | | - } |
| 73 | + @Nullable |
| 74 | + Player player(); |
| 75 | + @Nullable |
| 76 | + Entity entity(); |
| 77 | + @Nullable |
| 78 | + NameAndId nameAndId(); |
| 79 | + @Nullable |
| 80 | + GameProfile gameProfile(); |
91 | 81 |
|
92 | | - public static PlaceholderContext of(CommandSourceStack source) { |
93 | | - return of(source, ViewObject.DEFAULT); |
94 | | - } |
95 | | - |
96 | | - public static PlaceholderContext of(CommandSourceStack source, ViewObject view) { |
97 | | - return new PlaceholderContext(source.getServer(), () -> source, source.getLevel(), source.getPlayer(), source.getEntity(), source.getPlayer() != null ? source.getPlayer().getGameProfile() : null, view); |
98 | | - } |
99 | | - |
100 | | - public static PlaceholderContext of(Entity entity) { |
101 | | - return of(entity, ViewObject.DEFAULT); |
102 | | - } |
103 | | - |
104 | | - public static PlaceholderContext of(Entity entity, ViewObject view) { |
105 | | - if (entity instanceof ServerPlayer player) { |
106 | | - return of(player, view); |
107 | | - } else { |
108 | | - var world = (ServerLevel) entity.level(); |
109 | | - return new PlaceholderContext(world.getServer(), () -> entity.createCommandSourceStackForNameResolution(world), world, null, entity, null, view); |
110 | | - } |
111 | | - } |
| 82 | + PlaceholderContext.ViewObject view(); |
112 | 83 |
|
| 84 | + @Nullable |
| 85 | + BlockPos blockPosition(); |
| 86 | + @Nullable |
| 87 | + Vec3 position(); |
113 | 88 |
|
114 | | - public interface ViewObject { |
| 89 | + interface ViewObject { |
115 | 90 | ViewObject DEFAULT = of(Identifier.fromNamespaceAndPath("placeholder_api", "default")); |
116 | 91 |
|
117 | 92 | static ViewObject of(Identifier identifier) { |
|
0 commit comments