21
21
import net .minecraft .server .command .ServerCommandSource ;
22
22
import net .minecraft .text .TranslatableText ;
23
23
import net .minecraft .util .Util ;
24
+ import org .apache .logging .log4j .LogManager ;
25
+ import org .apache .logging .log4j .Logger ;
24
26
25
27
import java .io .File ;
26
28
import java .io .IOException ;
34
36
35
37
public class KitCommand {
36
38
39
+ private static final Logger logger = LogManager .getLogger ("clientcommands" );
40
+
37
41
private static final SimpleCommandExceptionType SAVE_FAILED_EXCEPTION = new SimpleCommandExceptionType (new TranslatableText ("commands.ckit.saveFile.failed" ));
38
- private static final SimpleCommandExceptionType LOAD_FAILED_EXCEPTION = new SimpleCommandExceptionType (new TranslatableText ("commands.ckit.loadFile.failed" ));
39
42
40
43
private static final DynamicCommandExceptionType ALREADY_EXISTS_EXCEPTION = new DynamicCommandExceptionType (arg -> new TranslatableText ("commands.ckit.create.alreadyExists" , arg ));
41
44
@@ -48,6 +51,14 @@ public class KitCommand {
48
51
49
52
private static final Map <String , ListTag > kits = new HashMap <>();
50
53
54
+ static {
55
+ try {
56
+ loadFile ();
57
+ } catch (IOException e ) {
58
+ logger .info ("Could not load kits file, hence /ckit will not work!" );
59
+ }
60
+ }
61
+
51
62
public static void register (CommandDispatcher <ServerCommandSource > dispatcher ) {
52
63
addClientSideCommand ("ckit" );
53
64
@@ -75,8 +86,6 @@ public static void register(CommandDispatcher<ServerCommandSource> dispatcher) {
75
86
}
76
87
77
88
private static int create (ServerCommandSource source , String name ) throws CommandSyntaxException {
78
- loadFile ();
79
-
80
89
if (kits .containsKey (name )) {
81
90
throw ALREADY_EXISTS_EXCEPTION .create (name );
82
91
}
@@ -87,8 +96,6 @@ private static int create(ServerCommandSource source, String name) throws Comman
87
96
}
88
97
89
98
private static int delete (ServerCommandSource source , String name ) throws CommandSyntaxException {
90
- loadFile ();
91
-
92
99
if (kits .remove (name ) == null ) {
93
100
throw NOT_FOUND_EXCEPTION .create (name );
94
101
}
@@ -98,8 +105,6 @@ private static int delete(ServerCommandSource source, String name) throws Comman
98
105
}
99
106
100
107
private static int edit (ServerCommandSource source , String name ) throws CommandSyntaxException {
101
- loadFile ();
102
-
103
108
if (!kits .containsKey (name )) {
104
109
throw NOT_FOUND_EXCEPTION .create (name );
105
110
}
@@ -114,8 +119,6 @@ private static int load(ServerCommandSource source, String name, boolean overrid
114
119
throw NOT_CREATIVE_EXCEPTION .create ();
115
120
}
116
121
117
- loadFile ();
118
-
119
122
ListTag kit = kits .get (name );
120
123
if (kit == null ) {
121
124
throw NOT_FOUND_EXCEPTION .create (name );
@@ -138,9 +141,7 @@ private static int load(ServerCommandSource source, String name, boolean overrid
138
141
return 0 ;
139
142
}
140
143
141
- private static int list (ServerCommandSource source ) throws CommandSyntaxException {
142
- loadFile ();
143
-
144
+ private static int list (ServerCommandSource source ) {
144
145
String list = String .join (", " , kits .keySet ());
145
146
sendFeedback (list .equals ("" ) ? "No available kits" : "Available kits: " + list );
146
147
return kits .size ();
@@ -163,31 +164,27 @@ private static void saveFile() throws CommandSyntaxException {
163
164
}
164
165
}
165
166
166
- private static void loadFile () throws CommandSyntaxException {
167
- try {
168
- kits .clear ();
169
- CompoundTag rootTag = NbtIo .read (new File (configPath .toFile (), "kits.dat" ));
170
- if (rootTag == null ) {
171
- return ;
172
- }
173
- final int currentVersion = SharedConstants .getGameVersion ().getWorldVersion ();
174
- final int fileVersion = rootTag .getInt ("DataVersion" );
175
- CompoundTag compoundTag = rootTag .getCompound ("Kits" );
176
- if (fileVersion >= currentVersion ) {
177
- compoundTag .getKeys ().forEach (key -> kits .put (key , compoundTag .getList (key , NbtType .COMPOUND )));
178
- } else {
179
- compoundTag .getKeys ().forEach (key -> {
180
- ListTag updatedListTag = new ListTag ();
181
- compoundTag .getList (key , NbtType .COMPOUND ).forEach (tag -> {
182
- Dynamic <Tag > oldTagDynamic = new Dynamic <>(NbtOps .INSTANCE , tag );
183
- Dynamic <Tag > newTagDynamic = client .getDataFixer ().update (TypeReferences .ITEM_STACK , oldTagDynamic , fileVersion , currentVersion );
184
- updatedListTag .add (newTagDynamic .getValue ());
185
- });
186
- kits .put (key , updatedListTag );
167
+ private static void loadFile () throws IOException {
168
+ kits .clear ();
169
+ CompoundTag rootTag = NbtIo .read (new File (configPath .toFile (), "kits.dat" ));
170
+ if (rootTag == null ) {
171
+ return ;
172
+ }
173
+ final int currentVersion = SharedConstants .getGameVersion ().getWorldVersion ();
174
+ final int fileVersion = rootTag .getInt ("DataVersion" );
175
+ CompoundTag compoundTag = rootTag .getCompound ("Kits" );
176
+ if (fileVersion >= currentVersion ) {
177
+ compoundTag .getKeys ().forEach (key -> kits .put (key , compoundTag .getList (key , NbtType .COMPOUND )));
178
+ } else {
179
+ compoundTag .getKeys ().forEach (key -> {
180
+ ListTag updatedListTag = new ListTag ();
181
+ compoundTag .getList (key , NbtType .COMPOUND ).forEach (tag -> {
182
+ Dynamic <Tag > oldTagDynamic = new Dynamic <>(NbtOps .INSTANCE , tag );
183
+ Dynamic <Tag > newTagDynamic = client .getDataFixer ().update (TypeReferences .ITEM_STACK , oldTagDynamic , fileVersion , currentVersion );
184
+ updatedListTag .add (newTagDynamic .getValue ());
187
185
});
188
- }
189
- } catch (IOException e ) {
190
- throw LOAD_FAILED_EXCEPTION .create ();
186
+ kits .put (key , updatedListTag );
187
+ });
191
188
}
192
189
}
193
190
}
0 commit comments