Skip to content

Commit 140d369

Browse files
authored
Greatly improve efficiency of IO (#230)
* Greatly improve efficiency of IO * Fix encapsulation breakage * Implement requested changes
1 parent 4c7fc2f commit 140d369

File tree

2 files changed

+33
-37
lines changed

2 files changed

+33
-37
lines changed

src/main/java/net/earthcomputer/clientcommands/command/KitCommand.java

Lines changed: 33 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
import net.minecraft.server.command.ServerCommandSource;
2222
import net.minecraft.text.TranslatableText;
2323
import net.minecraft.util.Util;
24+
import org.apache.logging.log4j.LogManager;
25+
import org.apache.logging.log4j.Logger;
2426

2527
import java.io.File;
2628
import java.io.IOException;
@@ -34,8 +36,9 @@
3436

3537
public class KitCommand {
3638

39+
private static final Logger logger = LogManager.getLogger("clientcommands");
40+
3741
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"));
3942

4043
private static final DynamicCommandExceptionType ALREADY_EXISTS_EXCEPTION = new DynamicCommandExceptionType(arg -> new TranslatableText("commands.ckit.create.alreadyExists", arg));
4144

@@ -48,6 +51,14 @@ public class KitCommand {
4851

4952
private static final Map<String, ListTag> kits = new HashMap<>();
5053

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+
5162
public static void register(CommandDispatcher<ServerCommandSource> dispatcher) {
5263
addClientSideCommand("ckit");
5364

@@ -75,8 +86,6 @@ public static void register(CommandDispatcher<ServerCommandSource> dispatcher) {
7586
}
7687

7788
private static int create(ServerCommandSource source, String name) throws CommandSyntaxException {
78-
loadFile();
79-
8089
if (kits.containsKey(name)) {
8190
throw ALREADY_EXISTS_EXCEPTION.create(name);
8291
}
@@ -87,8 +96,6 @@ private static int create(ServerCommandSource source, String name) throws Comman
8796
}
8897

8998
private static int delete(ServerCommandSource source, String name) throws CommandSyntaxException {
90-
loadFile();
91-
9299
if (kits.remove(name) == null) {
93100
throw NOT_FOUND_EXCEPTION.create(name);
94101
}
@@ -98,8 +105,6 @@ private static int delete(ServerCommandSource source, String name) throws Comman
98105
}
99106

100107
private static int edit(ServerCommandSource source, String name) throws CommandSyntaxException {
101-
loadFile();
102-
103108
if (!kits.containsKey(name)) {
104109
throw NOT_FOUND_EXCEPTION.create(name);
105110
}
@@ -114,8 +119,6 @@ private static int load(ServerCommandSource source, String name, boolean overrid
114119
throw NOT_CREATIVE_EXCEPTION.create();
115120
}
116121

117-
loadFile();
118-
119122
ListTag kit = kits.get(name);
120123
if (kit == null) {
121124
throw NOT_FOUND_EXCEPTION.create(name);
@@ -138,9 +141,7 @@ private static int load(ServerCommandSource source, String name, boolean overrid
138141
return 0;
139142
}
140143

141-
private static int list(ServerCommandSource source) throws CommandSyntaxException {
142-
loadFile();
143-
144+
private static int list(ServerCommandSource source) {
144145
String list = String.join(", ", kits.keySet());
145146
sendFeedback(list.equals("") ? "No available kits" : "Available kits: " + list);
146147
return kits.size();
@@ -163,31 +164,27 @@ private static void saveFile() throws CommandSyntaxException {
163164
}
164165
}
165166

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());
187185
});
188-
}
189-
} catch (IOException e) {
190-
throw LOAD_FAILED_EXCEPTION.create();
186+
kits.put(key, updatedListTag);
187+
});
191188
}
192189
}
193190
}

src/main/resources/assets/clientcommands/lang/en_us.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@
8181

8282
"commands.ckit.notFound": "Kit \"%s\" not found",
8383
"commands.ckit.saveFile.failed": "Could not save kits file",
84-
"commands.ckit.loadFile.failed": "Could not load kits file",
8584
"commands.ckit.load.success": "Successfully gave kit \"%s\" to self",
8685
"commands.ckit.load.notCreative": "Player must be in creative mode to give items to self",
8786
"commands.ckit.create.success": "Successfully created kit \"%s\"",

0 commit comments

Comments
 (0)