1+ package io .github .c20c01 .command ;
2+
3+ import com .mojang .brigadier .CommandDispatcher ;
4+ import com .mojang .brigadier .context .CommandContext ;
5+ import io .github .c20c01 .CCMain ;
6+ import net .minecraft .commands .CommandSourceStack ;
7+ import net .minecraft .commands .Commands ;
8+ import net .minecraft .network .chat .ChatType ;
9+ import net .minecraft .network .chat .TextComponent ;
10+ import net .minecraft .server .level .ServerPlayer ;
11+ import net .minecraft .world .entity .Entity ;
12+ import net .minecraftforge .event .RegisterCommandsEvent ;
13+ import net .minecraftforge .eventbus .api .SubscribeEvent ;
14+ import net .minecraftforge .fml .common .Mod ;
15+
16+ @ Mod .EventBusSubscriber
17+ public class CommandEventHandler extends ModSettings {
18+
19+ @ SubscribeEvent
20+ public static void registerCommands (RegisterCommandsEvent event ) {
21+ CommandDispatcher <CommandSourceStack > dispatcher = event .getDispatcher ();
22+ dispatcher .register (
23+ Commands .literal (CCMain .ID )
24+ .requires ((sourceStack ) -> sourceStack .hasPermission (2 ))
25+ .executes (context -> print (context , "❤ Floo Powder ❤" ))
26+ .then (Commands .literal (HAND_USE_FP ).executes (context -> print (context , HAND_USE_FP + ": " + MAP .get (HAND_USE_FP )))
27+ .then (Commands .literal (TRUE ).executes (context -> {
28+ put (HAND_USE_FP , true );
29+ return print (context , HAND_USE_FP + " ← " + TRUE );
30+ }))
31+ .then (Commands .literal (FALSE ).executes (context -> {
32+ put (HAND_USE_FP , false );
33+ return print (context , HAND_USE_FP + " ← " + FALSE );
34+ }))
35+ )
36+ );
37+ }
38+
39+ private static int print (CommandContext <CommandSourceStack > commandContext , String s ) {
40+ Entity entity = commandContext .getSource ().getEntity ();
41+ if (entity instanceof ServerPlayer serverPlayer ) {
42+ var text = new TextComponent (s );
43+ serverPlayer .sendMessage (text , ChatType .CHAT , entity .getUUID ());
44+ }
45+ return 0 ;
46+ }
47+ }
0 commit comments