6
6
import com .mojang .brigadier .context .CommandContext ;
7
7
import com .mojang .brigadier .context .CommandContextBuilder ;
8
8
import com .mojang .brigadier .context .ParsedCommandNode ;
9
- import com .mt1006 .mocap .mocap .playing .PlayerData ;
10
- import com .mt1006 .mocap .utils .Utils ;
11
9
import net .minecraft .commands .CommandSourceStack ;
12
10
import net .minecraft .commands .Commands ;
13
- import org .apache .commons .lang3 .function .TriFunction ;
14
11
import org .jetbrains .annotations .Nullable ;
15
12
16
13
import java .util .List ;
@@ -27,114 +24,81 @@ public static RequiredArgumentBuilder<CommandSourceStack, String> withPlayerArgu
27
24
.then (Commands .literal ("from_mineskin" ).then (Commands .argument ("mineskinURL" , StringArgumentType .greedyString ()).executes (command )));
28
25
}
29
26
30
- public static @ Nullable String getString ( CommandContext <?> ctx , String name )
27
+ public static Command < CommandSourceStack > command ( Function < CommandInfo , Boolean > function )
31
28
{
32
- try { return StringArgumentType .getString (ctx , name ); }
33
- catch (Exception exception ) { return null ; }
29
+ return (ctx ) -> (function .apply (new CommandInfo (ctx )) ? 1 : 0 );
34
30
}
35
31
36
- public static PlayerData getPlayerData (CommandContext <?> ctx )
37
- {
38
- String playerName = getString (ctx , "playerName" );
39
- if (playerName == null ) { return new PlayerData ((String )null ); }
40
-
41
- String fromPlayer = getString (ctx , "skinPlayerName" );
42
- if (fromPlayer != null ) { return new PlayerData (playerName , PlayerData .SkinSource .FROM_PLAYER , fromPlayer ); }
43
-
44
- String fromFile = getString (ctx , "skinFilename" );
45
- if (fromFile != null ) { return new PlayerData (playerName , PlayerData .SkinSource .FROM_FILE , fromFile ); }
46
-
47
- String fromMineskin = getString (ctx , "mineskinURL" );
48
- if (fromMineskin != null ) { return new PlayerData (playerName , PlayerData .SkinSource .FROM_MINESKIN , fromMineskin ); }
49
-
50
- return new PlayerData (playerName );
51
- }
52
-
53
- public static Command <CommandSourceStack > simpleCommand (Function <CommandSourceStack , Boolean > function )
54
- {
55
- return (ctx ) -> (function .apply (ctx .getSource ()) ? 1 : 0 );
56
- }
57
-
58
- public static RequiredArgumentBuilder <CommandSourceStack , String > withStringArgument (BiFunction <CommandSourceStack , String , Boolean > function , String arg )
32
+ public static RequiredArgumentBuilder <CommandSourceStack , String > withStringArgument (BiFunction <CommandInfo , String , Boolean > function , String arg )
59
33
{
60
34
return Commands .argument (arg , StringArgumentType .string ()).executes ((ctx ) -> stringCommand (function , ctx , arg ));
61
35
}
62
36
63
- public static RequiredArgumentBuilder <CommandSourceStack , String > withTwoStringArguments (TriFunction <CommandSourceStack , String , String , Boolean > function , String arg1 , String arg2 )
37
+ public static RequiredArgumentBuilder <CommandSourceStack , String > withTwoStringArguments (TriFunction <CommandInfo , String , String , Boolean > function , String arg1 , String arg2 )
64
38
{
65
39
return Commands .argument (arg1 , StringArgumentType .string ())
66
40
.then (Commands .argument (arg2 , StringArgumentType .string ())
67
41
.executes ((ctx ) -> twoStringCommand (function , ctx , arg1 , arg2 )));
68
42
}
69
43
70
- private static int stringCommand (BiFunction <CommandSourceStack , String , Boolean > function , CommandContext <CommandSourceStack > ctx , String arg )
44
+ private static int stringCommand (BiFunction <CommandInfo , String , Boolean > function , CommandContext <CommandSourceStack > ctx , String arg )
71
45
{
46
+ CommandInfo commandInfo = new CommandInfo (ctx );
72
47
try
73
48
{
74
49
String name = StringArgumentType .getString (ctx , arg );
75
- return function .apply (ctx . getSource () , name ) ? 1 : 0 ;
50
+ return function .apply (commandInfo , name ) ? 1 : 0 ;
76
51
}
77
52
catch (Exception exception )
78
53
{
79
- Utils .sendException (exception , ctx . getSource () , "mocap.error.unable_to_get_argument" );
54
+ commandInfo .sendException (exception , "mocap.error.unable_to_get_argument" );
80
55
return 0 ;
81
56
}
82
57
}
83
58
84
- private static int twoStringCommand (TriFunction <CommandSourceStack , String , String , Boolean > function , CommandContext <CommandSourceStack > ctx , String arg1 , String arg2 )
59
+ private static int twoStringCommand (TriFunction <CommandInfo , String , String , Boolean > function , CommandContext <CommandSourceStack > ctx , String arg1 , String arg2 )
85
60
{
61
+ CommandInfo commandInfo = new CommandInfo (ctx );
86
62
try
87
63
{
88
64
String name1 = StringArgumentType .getString (ctx , arg1 );
89
65
String name2 = StringArgumentType .getString (ctx , arg2 );
90
- return function .apply (ctx . getSource () , name1 , name2 ) ? 1 : 0 ;
66
+ return function .apply (commandInfo , name1 , name2 ) ? 1 : 0 ;
91
67
}
92
68
catch (Exception exception )
93
69
{
94
- Utils .sendException (exception , ctx . getSource () , "mocap.error.unable_to_get_argument" );
70
+ commandInfo .sendException (exception , "mocap.error.unable_to_get_argument" );
95
71
return 0 ;
96
72
}
97
73
}
98
74
99
- public static <T > @ Nullable CommandContext <T > getFinalCommandContext (CommandContext <T > ctx )
100
- {
101
- while (true )
102
- {
103
- String command = getCommandNode (ctx , 0 );
104
- if (command != null && (command .equals ("mocap" ) || command .equals ("mocap:mocap" ))) { return ctx ; }
105
-
106
- ctx = ctx .getChild ();
107
- if (ctx == null ) { return null ; }
108
- }
109
- }
110
-
111
75
public static <T > @ Nullable CommandContextBuilder <T > getFinalCommandContext (CommandContextBuilder <T > ctx )
112
76
{
113
77
while (true )
114
78
{
115
- String command = getCommandNode (ctx , 0 );
79
+ String command = getNode (ctx , 0 );
116
80
if (command != null && (command .equals ("mocap" ) || command .equals ("mocap:mocap" ))) { return ctx ; }
117
81
118
82
ctx = ctx .getChild ();
119
83
if (ctx == null ) { return null ; }
120
84
}
121
85
}
122
86
123
- public static @ Nullable String getCommandNode ( CommandContext <?> ctx , int pos )
87
+ public static @ Nullable String getNode ( CommandContextBuilder <?> ctx , int pos )
124
88
{
125
- return getCommandNode (ctx .getNodes (), pos );
89
+ return getNode (ctx .getNodes (), pos );
126
90
}
127
91
128
- public static @ Nullable String getCommandNode (CommandContextBuilder <?> ctx , int pos )
129
- {
130
- return getCommandNode (ctx .getNodes (), pos );
131
- }
132
-
133
- private static @ Nullable String getCommandNode (List <? extends ParsedCommandNode <?>> nodes , int pos )
92
+ public static @ Nullable String getNode (List <? extends ParsedCommandNode <?>> nodes , int pos )
134
93
{
135
94
int size = nodes .size ();
136
95
if (pos < 0 ) { pos += size ; }
137
96
if (pos >= size || pos < 0 ) { return null ; }
138
97
return nodes .get (pos ).getNode ().getName ();
139
98
}
99
+
100
+ public interface TriFunction <T , U , V , R >
101
+ {
102
+ R apply (T t , U u , V v );
103
+ }
140
104
}
0 commit comments