11package eu .pb4 .placeholders .api ;
22
33import com .mojang .authlib .GameProfile ;
4- import com . mojang . brigadier . context . CommandContext ;
4+ import eu . pb4 . placeholders . impl . placeholder . ViewObjectImpl ;
55import net .minecraft .entity .Entity ;
66import net .minecraft .server .MinecraftServer ;
77import net .minecraft .server .command .CommandOutput ;
88import net .minecraft .server .command .ServerCommandSource ;
99import net .minecraft .server .network .ServerPlayerEntity ;
1010import net .minecraft .server .world .ServerWorld ;
1111import net .minecraft .text .Text ;
12+ import net .minecraft .util .Identifier ;
1213import net .minecraft .util .math .Vec2f ;
1314import net .minecraft .util .math .Vec3d ;
1415
@@ -19,9 +20,18 @@ public record PlaceholderContext(MinecraftServer server,
1920 @ Nullable ServerWorld world ,
2021 @ Nullable ServerPlayerEntity player ,
2122 @ Nullable Entity entity ,
22- @ Nullable GameProfile gameProfile
23+ @ Nullable GameProfile gameProfile ,
24+ ViewObject view
2325) {
2426
27+ public PlaceholderContext (MinecraftServer server ,
28+ ServerCommandSource source ,
29+ @ Nullable ServerWorld world ,
30+ @ Nullable ServerPlayerEntity player ,
31+ @ Nullable Entity entity ,
32+ @ Nullable GameProfile gameProfile ) {
33+ this (server , source , world , player , entity , gameProfile , ViewObject .DEFAULT );
34+ }
2535
2636
2737 public static ParserContext .Key <PlaceholderContext > KEY = new ParserContext .Key <>("placeholder_context" , PlaceholderContext .class );
@@ -46,28 +56,68 @@ public ParserContext asParserContext() {
4656 return ParserContext .of (KEY , this );
4757 }
4858
59+ public PlaceholderContext withView (ViewObject view ) {
60+ return new PlaceholderContext (this .server , this .source , this .world , this .player , this .entity , this .gameProfile , view );
61+ }
62+
63+ public void addToContext (ParserContext context ) {
64+ context .with (KEY , this );
65+ }
66+
67+
4968 public static PlaceholderContext of (MinecraftServer server ) {
50- return new PlaceholderContext (server , server .getCommandSource (), null , null , null , null );
69+ return of (server , ViewObject .DEFAULT );
70+ }
71+
72+ public static PlaceholderContext of (MinecraftServer server , ViewObject view ) {
73+ return new PlaceholderContext (server , server .getCommandSource (), null , null , null , null , view );
5174 }
5275
5376 public static PlaceholderContext of (GameProfile profile , MinecraftServer server ) {
77+ return of (profile , server , ViewObject .DEFAULT );
78+ }
79+
80+ public static PlaceholderContext of (GameProfile profile , MinecraftServer server , ViewObject view ) {
5481 var name = profile .getName () != null ? profile .getName () : profile .getId ().toString ();
5582 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 );
5683 }
5784
5885 public static PlaceholderContext of (ServerPlayerEntity player ) {
59- return new PlaceholderContext (player .getServer (), player .getCommandSource (), player .getWorld (), player , player , player .getGameProfile ());
86+ return of (player , ViewObject .DEFAULT );
87+ }
88+
89+ public static PlaceholderContext of (ServerPlayerEntity player , ViewObject view ) {
90+ return new PlaceholderContext (player .getServer (), player .getCommandSource (), player .getWorld (), player , player , player .getGameProfile (), view );
6091 }
6192
6293 public static PlaceholderContext of (ServerCommandSource source ) {
63- return new PlaceholderContext (source .getServer (), source , source .getWorld (), source .getPlayer (), source .getEntity (), source .getPlayer () != null ? source .getPlayer ().getGameProfile () : null );
94+ return of (source , ViewObject .DEFAULT );
95+ }
96+
97+ public static PlaceholderContext of (ServerCommandSource source , ViewObject view ) {
98+ return new PlaceholderContext (source .getServer (), source , source .getWorld (), source .getPlayer (), source .getEntity (), source .getPlayer () != null ? source .getPlayer ().getGameProfile () : null , view );
6499 }
65100
66101 public static PlaceholderContext of (Entity entity ) {
102+ return of (entity , ViewObject .DEFAULT );
103+ }
104+
105+ public static PlaceholderContext of (Entity entity , ViewObject view ) {
67106 if (entity instanceof ServerPlayerEntity player ) {
68- return of (player );
107+ return of (player , view );
69108 } else {
70- return new PlaceholderContext (entity .getServer (), entity .getCommandSource (), (ServerWorld ) entity .getWorld (), null , entity , null );
109+ return new PlaceholderContext (entity .getServer (), entity .getCommandSource (), (ServerWorld ) entity .getWorld (), null , entity , null , view );
71110 }
72111 }
112+
113+
114+ public interface ViewObject {
115+ ViewObject DEFAULT = of (new Identifier ("placeholder_api" , "default" ));
116+
117+ static ViewObject of (Identifier identifier ) {
118+ return new ViewObjectImpl (identifier );
119+ }
120+
121+ Identifier identifier ();
122+ }
73123}
0 commit comments