From 2a5df6d930a8c709f59a1b7f48ce09a930d273da Mon Sep 17 00:00:00 2001 From: yoyosource Date: Tue, 25 May 2021 13:56:31 +0200 Subject: [PATCH] Add MaterialCommand Signed-off-by: yoyosource --- .../gui/editor/BauGuiImportExport.java | 72 +++++++++---------- 1 file changed, 32 insertions(+), 40 deletions(-) diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/gui/editor/BauGuiImportExport.java b/BauSystem_Main/src/de/steamwar/bausystem/features/gui/editor/BauGuiImportExport.java index d299b0e4..8346f4f1 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/gui/editor/BauGuiImportExport.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/gui/editor/BauGuiImportExport.java @@ -20,19 +20,23 @@ package de.steamwar.bausystem.features.gui.editor; import de.steamwar.bausystem.BauSystem; -import de.steamwar.bausystem.features.gui.BauGUI; import lombok.experimental.UtilityClass; import org.bukkit.entity.Player; -import java.util.*; +import java.util.Base64; +import java.util.HashMap; +import java.util.List; +import java.util.Map; import java.util.stream.Collectors; @UtilityClass public class BauGuiImportExport { + private static final String VERSION = "00000"; + public String exportGui(BauGuiMapping mapping) { StringBuilder st = new StringBuilder(); - st.append("00000"); // Version in Binary (0 - 32) + st.append(VERSION); // Version in Binary (0 - 32) append(st, Integer.toBinaryString(mapping.getSize() / 9), 3); // Menu Size Map map = new HashMap<>(); @@ -41,27 +45,28 @@ public class BauGuiImportExport { List integerList = map.keySet().stream().sorted().collect(Collectors.toList()); for (int i = 0; i < integerList.size(); i++) { int current = integerList.get(i); - int last = i == 0 ? 0 : integerList.get(i - 1); + int last = i == 0 ? -1 : integerList.get(i - 1); if (current - last > 1) { - binaryConvert(st, Integer.toBinaryString(current - last - 1), 3, '0'); + binaryConvert(st, Integer.toBinaryString(current - last - 1), '0'); } - binaryConvert(st, Integer.toBinaryString(map.get(current)), 4, '1'); + binaryConvert(st, Integer.toBinaryString(map.get(current)), '1'); } byte[] bytes = new byte[st.length() / 8 + 1]; if (st.length() / 8 == (int) Math.ceil(st.length() / 8.0)) { bytes = new byte[st.length() / 8]; } + System.out.println(st.toString()); int index = 0; - while (st.length() > 8) { + while (st.length() > 0) { byte result = 0; for (int i = 0; i < Math.min(8, st.length()); i++) { result |= (st.charAt(i) - '0') << (7 - i); } bytes[index] = result; index++; - st.delete(0, 8); + st.delete(0, Math.min(8, st.length())); } return Base64.getEncoder().encodeToString(bytes); @@ -74,53 +79,40 @@ public class BauGuiImportExport { st.append(s); } - private static void binaryConvert(StringBuilder st, String s, int bitlet, char c) { - StringBuilder current = new StringBuilder(s); - while (current.length() % bitlet != 0) { + private static void binaryConvert(StringBuilder st, String s, char identifier) { + StringBuilder current = new StringBuilder().append(identifier).append(s); + while (current.length() % 4 != 0) { current.insert(0, '0'); } - while (current.length() > bitlet) { - st.append(c).append(current.substring(0, bitlet)); - current.delete(0, bitlet); + while (current.length() > 4) { + st.append('1').append(current.substring(0, 4)); + current.delete(0, 4); } - st.append(c).append(current); + st.append('0').append(current); } public static boolean importGui(String str, Player p) { byte[] bytes = Base64.getDecoder().decode(str); - if (bytes.length % 2 != 1 || bytes.length < 2) { + StringBuilder st = new StringBuilder(); + for (byte b : bytes) { + append(st, Integer.toBinaryString(b & 0xFF), 8); + } + if (!st.substring(0, 5).equals(VERSION)) { BauSystem.MESSAGE.send("GUI_IMPORT_INVALID-CODE", p); return false; } - - - final int size = bytes[bytes.length - 1]; - if (size > 9 * 5) { + int size = Integer.parseInt(st.substring(5, 8), 2); + if (size <= 0 || size > 5) { BauSystem.MESSAGE.send("GUI_IMPORT_INVALID-CODE", p); return false; } + size *= 9; - final Map map = new HashMap<>(); - final int itemSize = BauGUI.getITEMS().size(); - int head = 0; - do { - byte key = bytes[head]; - head++; - byte value = bytes[head]; - head++; + st.delete(0, 8); + System.out.println(st.toString()); + // BauGUI.getITEMS().containsKey() + // TODO: Implement this @yoyo - if (map.containsKey((int) key) || map.containsValue((int) value) || - value >= size || key > itemSize) { - BauSystem.MESSAGE.send("GUI_IMPORT_INVALID-CODE", p); - return false; - } - - map.put((int) key, (int) value); - } while (head + 2 < bytes.length); - BauGuiMapping mapping = BauGuiMapping.getGuiMapping(p); - mapping.setMapping(map); - mapping.setSize(size); - mapping.save(); return true; } }