From 3b8d7f1316405978959ec812ce6139227a09ab76 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Mon, 24 May 2021 22:46:51 +0200 Subject: [PATCH] Add new BauGuiImportExport.exportGui Signed-off-by: yoyosource --- .../gui/editor/BauGuiImportExport.java | 71 +++++++++++++++---- 1 file changed, 58 insertions(+), 13 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 2f1a3fd4..d8aa5f5f 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 @@ -21,31 +21,76 @@ 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.Base64; -import java.util.HashMap; -import java.util.Map; +import java.util.*; +import java.util.stream.Collectors; +@UtilityClass public class BauGuiImportExport { - public static String exportGui(BauGuiMapping mapping) { + public String exportGui(BauGuiMapping mapping) { + StringBuilder st = new StringBuilder(); + st.append("00000"); // Version in Binary (0 - 32) + append(st, Integer.toBinaryString(mapping.getSize() / 9), 3); // Menu Size - byte[] bytes = new byte[(mapping.getMapping().size() * 2) + 1]; - Map map = mapping.getMapping(); + Map map = new HashMap<>(); + mapping.getMapping().forEach((integer, integer2) -> { + map.put(integer2, integer); + }); - int head = 0; - for (Map.Entry e : map.entrySet()) { - bytes[head] = e.getKey().byteValue(); - head++; - bytes[head] = e.getValue().byteValue(); - head++; + List integerList = map.keySet().stream().sorted().collect(Collectors.toList()); + for (int i = 0; i < integerList.size(); i++) { + int current = integerList.get(i); + int last = 0; + if (i != 0) { + last = integerList.get(i - 1); + } + if (current - last > 1) { + binaryConvert(st, Integer.toBinaryString(current - last), 3, '0'); + } + binaryConvert(st, Integer.toBinaryString(map.get(current)), 4, '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]; + } + + int index = 0; + while (st.length() > 8) { + 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); } - bytes[head] = (byte) mapping.getSize(); return Base64.getEncoder().encodeToString(bytes); } + private static void append(StringBuilder st, String s, int length) { + for (int i = 0; i < length - s.length(); i++) { + st.append("0"); + } + 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) { + current.insert(0, '0'); + } + while (current.length() > bitlet) { + st.append(c).append(current.substring(0, bitlet)); + current.delete(0, bitlet); + } + st.append(c).append(current); + } + public static boolean importGui(String str, Player p) { byte[] bytes = Base64.getDecoder().decode(str); if (bytes.length % 2 != 1 || bytes.length < 2) {