Add MaterialCommand
Signed-off-by: yoyosource <yoyosource@nidido.de>
Dieser Commit ist enthalten in:
Ursprung
a08bb3b46d
Commit
2a5df6d930
@ -20,19 +20,23 @@
|
|||||||
package de.steamwar.bausystem.features.gui.editor;
|
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 lombok.experimental.UtilityClass;
|
import lombok.experimental.UtilityClass;
|
||||||
import org.bukkit.entity.Player;
|
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;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
@UtilityClass
|
@UtilityClass
|
||||||
public class BauGuiImportExport {
|
public class BauGuiImportExport {
|
||||||
|
|
||||||
|
private static final String VERSION = "00000";
|
||||||
|
|
||||||
public String exportGui(BauGuiMapping mapping) {
|
public String exportGui(BauGuiMapping mapping) {
|
||||||
StringBuilder st = new StringBuilder();
|
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
|
append(st, Integer.toBinaryString(mapping.getSize() / 9), 3); // Menu Size
|
||||||
|
|
||||||
Map<Integer, Integer> map = new HashMap<>();
|
Map<Integer, Integer> map = new HashMap<>();
|
||||||
@ -41,27 +45,28 @@ public class BauGuiImportExport {
|
|||||||
List<Integer> integerList = map.keySet().stream().sorted().collect(Collectors.toList());
|
List<Integer> integerList = map.keySet().stream().sorted().collect(Collectors.toList());
|
||||||
for (int i = 0; i < integerList.size(); i++) {
|
for (int i = 0; i < integerList.size(); i++) {
|
||||||
int current = integerList.get(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) {
|
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];
|
byte[] bytes = new byte[st.length() / 8 + 1];
|
||||||
if (st.length() / 8 == (int) Math.ceil(st.length() / 8.0)) {
|
if (st.length() / 8 == (int) Math.ceil(st.length() / 8.0)) {
|
||||||
bytes = new byte[st.length() / 8];
|
bytes = new byte[st.length() / 8];
|
||||||
}
|
}
|
||||||
|
System.out.println(st.toString());
|
||||||
|
|
||||||
int index = 0;
|
int index = 0;
|
||||||
while (st.length() > 8) {
|
while (st.length() > 0) {
|
||||||
byte result = 0;
|
byte result = 0;
|
||||||
for (int i = 0; i < Math.min(8, st.length()); i++) {
|
for (int i = 0; i < Math.min(8, st.length()); i++) {
|
||||||
result |= (st.charAt(i) - '0') << (7 - i);
|
result |= (st.charAt(i) - '0') << (7 - i);
|
||||||
}
|
}
|
||||||
bytes[index] = result;
|
bytes[index] = result;
|
||||||
index++;
|
index++;
|
||||||
st.delete(0, 8);
|
st.delete(0, Math.min(8, st.length()));
|
||||||
}
|
}
|
||||||
|
|
||||||
return Base64.getEncoder().encodeToString(bytes);
|
return Base64.getEncoder().encodeToString(bytes);
|
||||||
@ -74,53 +79,40 @@ public class BauGuiImportExport {
|
|||||||
st.append(s);
|
st.append(s);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void binaryConvert(StringBuilder st, String s, int bitlet, char c) {
|
private static void binaryConvert(StringBuilder st, String s, char identifier) {
|
||||||
StringBuilder current = new StringBuilder(s);
|
StringBuilder current = new StringBuilder().append(identifier).append(s);
|
||||||
while (current.length() % bitlet != 0) {
|
while (current.length() % 4 != 0) {
|
||||||
current.insert(0, '0');
|
current.insert(0, '0');
|
||||||
}
|
}
|
||||||
while (current.length() > bitlet) {
|
while (current.length() > 4) {
|
||||||
st.append(c).append(current.substring(0, bitlet));
|
st.append('1').append(current.substring(0, 4));
|
||||||
current.delete(0, bitlet);
|
current.delete(0, 4);
|
||||||
}
|
}
|
||||||
st.append(c).append(current);
|
st.append('0').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) {
|
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);
|
BauSystem.MESSAGE.send("GUI_IMPORT_INVALID-CODE", p);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
int size = Integer.parseInt(st.substring(5, 8), 2);
|
||||||
|
if (size <= 0 || size > 5) {
|
||||||
final int size = bytes[bytes.length - 1];
|
|
||||||
if (size > 9 * 5) {
|
|
||||||
BauSystem.MESSAGE.send("GUI_IMPORT_INVALID-CODE", p);
|
BauSystem.MESSAGE.send("GUI_IMPORT_INVALID-CODE", p);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
size *= 9;
|
||||||
|
|
||||||
final Map<Integer, Integer> map = new HashMap<>();
|
st.delete(0, 8);
|
||||||
final int itemSize = BauGUI.getITEMS().size();
|
System.out.println(st.toString());
|
||||||
int head = 0;
|
// BauGUI.getITEMS().containsKey()
|
||||||
do {
|
// TODO: Implement this @yoyo
|
||||||
byte key = bytes[head];
|
|
||||||
head++;
|
|
||||||
byte value = bytes[head];
|
|
||||||
head++;
|
|
||||||
|
|
||||||
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;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
In neuem Issue referenzieren
Einen Benutzer sperren