| 1 | package thunder.hack.features.cmd.impl;
|
| 2 |
|
| 3 | import com.google.gson.JsonObject;
|
| 4 | import com.google.gson.JsonParser;
|
| 5 | import com.mojang.brigadier.arguments.StringArgumentType;
|
| 6 | import com.mojang.brigadier.builder.LiteralArgumentBuilder;
|
| 7 | import org.jetbrains.annotations.NotNull;
|
| 8 | import thunder.hack.features.cmd.Command;
|
| 9 | import thunder.hack.core.manager.client.ModuleManager;
|
| 10 |
|
| 11 | import java.io.*;
|
| 12 | import net.minecraft.ChatFormatting;
|
| 13 | import net.minecraft.commands.SharedSuggestionProvider;
|
| 14 | import net.minecraft.core.component.DataComponents;
|
| 15 | import net.minecraft.world.item.ItemStack;
|
| 16 | import net.minecraft.world.item.PotionItem;
|
| 17 |
|
| 18 | import static com.mojang.brigadier.Command.SINGLE_SUCCESS;
|
| 19 | import static thunder.hack.features.modules.client.ClientSettings.isRu;
|
| 20 |
|
| 21 | public class KitCommand extends Command {
|
| 22 | final static private String PATH = "ThunderHackRecode/misc/AutoGear.json";
|
| 23 |
|
| 24 | public KitCommand() {
|
| 25 | super("kit");
|
| 26 | }
|
| 27 |
|
| 28 | @Override
|
| 29 | public void executeBuild(@NotNull LiteralArgumentBuilder<SharedSuggestionProvider> builder) {
|
| 30 | builder.then(literal("list").executes(context -> {
|
| 31 | listMessage();
|
| 32 |
|
| 33 | return SINGLE_SUCCESS;
|
| 34 | }));
|
| 35 |
|
| 36 | builder.then(literal("create").then(arg("name", StringArgumentType.word()).executes(context -> {
|
| 37 | save(context.getArgument("name", String.class));
|
| 38 | return SINGLE_SUCCESS;
|
| 39 | })));
|
| 40 |
|
| 41 | builder.then(literal("set").then(arg("name", StringArgumentType.word()).executes(context -> {
|
| 42 | set(context.getArgument("name", String.class));
|
| 43 | return SINGLE_SUCCESS;
|
| 44 | })));
|
| 45 |
|
| 46 | builder.then(literal("del").then(arg("name", StringArgumentType.word()).executes(context -> {
|
| 47 | delete(context.getArgument("name", String.class));
|
| 48 | return SINGLE_SUCCESS;
|
| 49 | })));
|
| 50 |
|
| 51 | builder.executes(context -> {
|
| 52 | sendMessage("kit <create/set/del/list> <name>");
|
| 53 | return SINGLE_SUCCESS;
|
| 54 | });
|
| 55 | }
|
| 56 |
|
| 57 | public static String getSelectedKit() {
|
| 58 | try {
|
| 59 | JsonObject json = JsonParser.parseReader(new FileReader(PATH)).getAsJsonObject();
|
| 60 | if (!json.get("selected").getAsString().equals("none"))
|
| 61 | return json.get("selected").getAsString();
|
| 62 | } catch (Exception ignored) {
|
| 63 | }
|
| 64 | sendMessage(isRu() ? "Кит не найден" : "Kit not found");
|
| 65 | return "";
|
| 66 | }
|
| 67 |
|
| 68 | public static String getKitItems(String kit) {
|
| 69 | try {
|
| 70 | JsonObject json = JsonParser.parseReader(new FileReader(PATH)).getAsJsonObject();
|
| 71 | return json.get(kit).getAsString();
|
| 72 | } catch (Exception ignored) {
|
| 73 | }
|
| 74 | sendMessage(isRu() ? "Кит не найден" : "Kit not found");
|
| 75 | return "";
|
| 76 | }
|
| 77 |
|
| 78 | private void listMessage() {
|
| 79 | try {
|
| 80 | JsonObject json = JsonParser.parseReader(new FileReader(PATH)).getAsJsonObject();
|
| 81 | sendMessage(isRu() ? "Доступные киты:" : "Available kits:");
|
| 82 | for (int i = 0; i < json.entrySet().size(); i++) {
|
| 83 | String item = json.entrySet().toArray()[i].toString().split("=")[0];
|
| 84 | sendMessage(ChatFormatting.GRAY + "-> " + item + (item.equals("selected") ? (isRu() ? "(Выбран)" : " (Selected)") : ""));
|
| 85 | }
|
| 86 | } catch (Exception e) {
|
| 87 | sendMessage(isRu() ? "Проблема с конфигурацией китов!" : "Error with kit cfg!");
|
| 88 | }
|
| 89 | }
|
| 90 |
|
| 91 | private void delete(String name) {
|
| 92 | try {
|
| 93 | JsonObject json = JsonParser.parseReader(new FileReader(PATH)).getAsJsonObject();
|
| 94 | if (json.get(name) != null && !name.equals("selected")) {
|
| 95 | json.remove(name);
|
| 96 | if (json.get("selected").getAsString().equals(name))
|
| 97 | json.addProperty("selected", "none");
|
| 98 | saveFile(json, name, isRu() ? "удален" : "deleted");
|
| 99 | } else sendMessage("Kit not found");
|
| 100 |
|
| 101 | } catch (Exception e) {
|
| 102 | sendMessage(isRu() ? "Кит не найден" : "Kit not found");
|
| 103 | }
|
| 104 | }
|
| 105 |
|
| 106 | private void set(String name) {
|
| 107 | try {
|
| 108 | JsonObject json = JsonParser.parseReader(new FileReader(PATH)).getAsJsonObject();
|
| 109 | if (json.get(name) != null && !name.equals("selected")) {
|
| 110 | json.addProperty("selected", name);
|
| 111 | saveFile(json, name, isRu() ? "выбран" : "selected");
|
| 112 | ModuleManager.autoGear.setup();
|
| 113 | } else sendMessage(isRu() ? "Кит не найден" : "Kit not found");
|
| 114 | } catch (Exception e) {
|
| 115 | sendMessage(isRu() ? "Кит не найден" : "Kit not found");
|
| 116 | }
|
| 117 | }
|
| 118 |
|
| 119 | private void save(String name) {
|
| 120 | JsonObject json = new JsonObject();
|
| 121 | try {
|
| 122 | json = JsonParser.parseReader(new FileReader(PATH)).getAsJsonObject();
|
| 123 | if (json.get(name) != null && !name.equals("selected")) {
|
| 124 | sendMessage(isRu() ? "Этот кит уже существует" : "This kit arleady exist");
|
| 125 | return;
|
| 126 | }
|
| 127 | } catch (IOException e) {
|
| 128 | json.addProperty("selected", "none");
|
| 129 | }
|
| 130 |
|
| 131 | StringBuilder jsonInventory = new StringBuilder();
|
| 132 |
|
| 133 | for (ItemStack item : mc.player.getInventory().getNonEquipmentItems())
|
| 134 | jsonInventory.append(item.getItem() instanceof PotionItem ? item.getItem().getDescriptionId() + item.getItem().components().get(DataComponents.POTION_CONTENTS).getColor() : item.getItem().getDescriptionId()).append(" ");
|
| 135 |
|
| 136 | json.addProperty(name, jsonInventory.toString());
|
| 137 | saveFile(json, name, isRu() ? "сохранен" : "saved");
|
| 138 | }
|
| 139 |
|
| 140 | private void saveFile(@NotNull JsonObject completeJson, String name, String operation) {
|
| 141 | try {
|
| 142 | File file = new File(PATH);
|
| 143 | try {
|
| 144 | file.createNewFile();
|
| 145 | } catch (Exception ignored) {
|
| 146 | }
|
| 147 |
|
| 148 | BufferedWriter bw = new BufferedWriter(new FileWriter(PATH));
|
| 149 | bw.write(completeJson.toString());
|
| 150 | bw.close();
|
| 151 | sendMessage((isRu() ? "Кит " : "Kit ") + ChatFormatting.AQUA + name + ChatFormatting.RESET + " " + operation);
|
| 152 | } catch (IOException e) {
|
| 153 | sendMessage(isRu() ? "Ошибка сохранения файла" : "Error saving the file");
|
| 154 | }
|
| 155 | }
|
| 156 | }
|
| 157 |
|