SteamWar/BauSystem2.0
Archiviert
12
0

Add new BauGuiImportExport.exportGui

Signed-off-by: yoyosource <yoyosource@nidido.de>
Dieser Commit ist enthalten in:
yoyosource 2021-05-24 22:46:51 +02:00
Ursprung bb32c5ab0b
Commit 3b8d7f1316

Datei anzeigen

@ -21,31 +21,76 @@ package de.steamwar.bausystem.features.gui.editor;
import de.steamwar.bausystem.BauSystem; import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.features.gui.BauGUI; import de.steamwar.bausystem.features.gui.BauGUI;
import lombok.experimental.UtilityClass;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import java.util.Base64; import java.util.*;
import java.util.HashMap; import java.util.stream.Collectors;
import java.util.Map;
@UtilityClass
public class BauGuiImportExport { 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<Integer, Integer> map = new HashMap<>();
Map<Integer, Integer> map = mapping.getMapping(); mapping.getMapping().forEach((integer, integer2) -> {
map.put(integer2, integer);
});
int head = 0; List<Integer> integerList = map.keySet().stream().sorted().collect(Collectors.toList());
for (Map.Entry<Integer, Integer> e : map.entrySet()) { for (int i = 0; i < integerList.size(); i++) {
bytes[head] = e.getKey().byteValue(); int current = integerList.get(i);
head++; int last = 0;
bytes[head] = e.getValue().byteValue(); if (i != 0) {
head++; 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); 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) { public static boolean importGui(String str, Player p) {
byte[] bytes = Base64.getDecoder().decode(str); byte[] bytes = Base64.getDecoder().decode(str);
if (bytes.length % 2 != 1 || bytes.length < 2) { if (bytes.length % 2 != 1 || bytes.length < 2) {