|
1 | 1 | package dev.dfonline.codeclient.dev.menu.DevInventory;
|
2 | 2 |
|
| 3 | +import dev.dfonline.codeclient.FileManager; |
| 4 | +import dev.dfonline.codeclient.Utility; |
3 | 5 | import dev.dfonline.codeclient.hypercube.actiondump.*;
|
4 | 6 | import net.minecraft.item.ItemStack;
|
5 | 7 | import net.minecraft.item.Items;
|
6 | 8 | import net.minecraft.nbt.NbtHelper;
|
7 | 9 | import net.minecraft.text.Text;
|
| 10 | +import net.minecraft.util.Formatting; |
8 | 11 |
|
| 12 | +import java.io.IOException; |
| 13 | +import java.nio.file.Files; |
| 14 | +import java.nio.file.Path; |
9 | 15 | import java.util.ArrayList;
|
| 16 | +import java.util.Base64; |
10 | 17 | import java.util.List;
|
11 | 18 |
|
12 | 19 | public class DevInventoryGroup {
|
@@ -111,7 +118,7 @@ public int getDisplayY(int originY, int menuHeight) {
|
111 | 118 | public static final DevInventoryGroup POTIONS = new DevInventoryGroup("potion", "Potions", Items.DRAGON_BREATH.getDefaultStack(), false);
|
112 | 119 | public static final DevInventoryGroup GAME_VALUES = new DevInventoryGroup("game_value", "Game Values", Items.NAME_TAG.getDefaultStack(), false);
|
113 | 120 | public static final DevInventoryGroup OTHERS = new DevInventoryGroup("others", "Other Items", Items.IRON_INGOT.getDefaultStack(), false).disableSearch();
|
114 |
| - public static final DevInventoryGroup CODE_VAULT = new DevInventoryGroup("code_vault", "Code Vault", Items.ENDER_CHEST.getDefaultStack(), false); |
| 121 | + public static final DevInventoryGroup SAVED_TEMPLATES = new DevInventoryGroup("saved_templates", "Saved Templates", Items.ENDER_CHEST.getDefaultStack(), false); |
115 | 122 | public static final DevInventoryGroup INVENTORY = new DevInventoryGroup("inventory", "Inventory", Items.CHEST.getDefaultStack(), false).disableSearch();
|
116 | 123 |
|
117 | 124 | interface ItemsProvider {
|
@@ -211,6 +218,18 @@ private DevInventoryGroup useCategory(Searchable[] categoryValues) {
|
211 | 218 | POTIONS.useCategory(actionDump.potions);
|
212 | 219 | PARTICLES.useCategory(actionDump.particles);
|
213 | 220 |
|
| 221 | + SAVED_TEMPLATES.setItemsProvider(query -> { |
| 222 | + ArrayList<Searchable> items = new ArrayList<>(); |
| 223 | + if(query == null) query = ""; |
| 224 | + query = query.toLowerCase(); |
| 225 | + for (Searchable template: readTemplates(FileManager.templatesPath())) { |
| 226 | + for (String term : template.getTerms()) if(term.toLowerCase().contains(query)) { |
| 227 | + items.add(template); |
| 228 | + break; |
| 229 | + } |
| 230 | + } |
| 231 | + return items; |
| 232 | + }); |
214 | 233 | SEARCH.setItemsProvider(query -> {
|
215 | 234 | ArrayList<Searchable> items = new ArrayList<>();
|
216 | 235 | if(query == null) query = "";
|
@@ -246,4 +265,26 @@ private DevInventoryGroup useCategory(Searchable[] categoryValues) {
|
246 | 265 | catch (Exception ignored) {
|
247 | 266 | }
|
248 | 267 | }
|
| 268 | + |
| 269 | + static private ArrayList<Searchable> readTemplates(Path path) { |
| 270 | + return readTemplates(path,path); |
| 271 | + } |
| 272 | + static private ArrayList<Searchable> readTemplates(Path path, Path root) { |
| 273 | + var list = new ArrayList<Searchable>(); |
| 274 | + try { |
| 275 | + var dir = Files.list(path); |
| 276 | + for (var file : dir.toList()) { |
| 277 | + if(Files.isDirectory(file)) list.addAll(readTemplates(file,root)); |
| 278 | + if(file.getFileName().toString().endsWith(".dft")) { |
| 279 | + byte[] data = Files.readAllBytes(file); |
| 280 | + ItemStack template = Utility.makeTemplate(new String(Base64.getEncoder().encode(data))); |
| 281 | + template.setCustomName(Text.empty().formatted(Formatting.RED).append("Saved Template").append(Text.literal(" » ").formatted(Formatting.DARK_RED, Formatting.BOLD)).append(String.valueOf(root.relativize(file)))); |
| 282 | + list.add(new Searchable.StaticSearchable(template)); |
| 283 | + } |
| 284 | + } |
| 285 | + dir.close(); |
| 286 | + } catch (IOException ignored) { |
| 287 | + } |
| 288 | + return list; |
| 289 | + } |
249 | 290 | }
|
0 commit comments