SteamWar/BauSystem2.0
Archiviert
12
0

Update ConfigurationSerializableSerializer

Signed-off-by: yoyosource <yoyosource@nidido.de>
Dieser Commit ist enthalten in:
yoyosource 2021-05-24 12:46:19 +02:00
Ursprung 56d070bf16
Commit 2165682234
2 geänderte Dateien mit 43 neuen und 48 gelöschten Zeilen

Datei anzeigen

@ -21,11 +21,13 @@ package de.steamwar.bausystem.configplayer.serializer;
import org.bukkit.configuration.serialization.ConfigurationSerializable; import org.bukkit.configuration.serialization.ConfigurationSerializable;
import org.bukkit.configuration.serialization.ConfigurationSerialization; import org.bukkit.configuration.serialization.ConfigurationSerialization;
import yapion.hierarchy.api.groups.YAPIONAnyType;
import yapion.hierarchy.types.YAPIONObject; import yapion.hierarchy.types.YAPIONObject;
import yapion.serializing.api.SerializerObject; import yapion.serializing.api.SerializerObject;
import yapion.serializing.data.DeserializeData; import yapion.serializing.data.DeserializeData;
import yapion.serializing.data.SerializeData; import yapion.serializing.data.SerializeData;
import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -49,7 +51,14 @@ public class ConfigurationSerializableSerializer extends SerializerObject<Config
yapionObject.add(TYPE_IDENTIFIER, type().getTypeName()); yapionObject.add(TYPE_IDENTIFIER, type().getTypeName());
Map<String, Object> serializeDataMap = serializeData.object.serialize(); Map<String, Object> serializeDataMap = serializeData.object.serialize();
serializeDataMap.forEach((s, o) -> { serializeDataMap.forEach((s, o) -> {
yapionObject.add(s, serializeData.serialize(o)); YAPIONAnyType yapionAnyType = serializeData.serialize(o);
if (yapionAnyType instanceof YAPIONObject) {
YAPIONObject object = (YAPIONObject) yapionAnyType;
if (object.containsKey(TYPE_IDENTIFIER) && object.getPlainValue(TYPE_IDENTIFIER).equals("com.google.common.collect.RegularImmutableList")) {
object.put(TYPE_IDENTIFIER, ArrayList.class.getTypeName());
}
}
yapionObject.add(s, yapionAnyType);
}); });
return yapionObject; return yapionObject;
} }

Datei anzeigen

@ -20,71 +20,57 @@
package de.steamwar.bausystem.features.hotbar; package de.steamwar.bausystem.features.hotbar;
import de.steamwar.bausystem.configplayer.Config; import de.steamwar.bausystem.configplayer.Config;
import org.bukkit.configuration.file.YamlConfiguration; import lombok.experimental.UtilityClass;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import yapion.hierarchy.types.YAPIONArray;
import yapion.hierarchy.types.YAPIONObject;
import yapion.hierarchy.types.YAPIONValue;
import yapion.parser.YAPIONParser;
import yapion.serializing.YAPIONDeserializer;
import yapion.serializing.YAPIONSerializer;
import java.io.StringReader; @UtilityClass
// TODO: 16.05.2021 @Yoyo muss mal was Fixen, dann Item Save auf Yapion umstellen
public class DefaultHotbar { public class DefaultHotbar {
public static void updateHotbar(Player p) { private final YAPIONArray DEFAULT_HOTBAR = YAPIONParser.parse("{[{@type(org.bukkit.configuration.serialization.ConfigurationSerializable)v(2230)type(WOODEN_AXE)meta{@type(org.bukkit.configuration.serialization.ConfigurationSerializable)meta-type(UNSPECIFIC)display-name(WorldEdit Wand)lore{@type(java.util.ArrayList)values[Left click: select pos #1,Right click: select pos #2]}}},{@type(org.bukkit.configuration.serialization.ConfigurationSerializable)v(2230)type(COMPASS)meta{@type(org.bukkit.configuration.serialization.ConfigurationSerializable)meta-type(UNSPECIFIC)display-name(Navigation Wand)lore{@type(java.util.ArrayList)values[Left click: jump to location,Right click: pass through walls]}}},null,null,null,null,null,null,{@type(org.bukkit.configuration.serialization.ConfigurationSerializable)v(2230)type(NETHER_STAR)meta{@type(org.bukkit.configuration.serialization.ConfigurationSerializable)meta-type(UNSPECIFIC)display-name(§eBau GUI)}}]}").getArray("");
public void updateHotbar(Player p) {
ItemStack[] hotbar = new ItemStack[9]; ItemStack[] hotbar = new ItemStack[9];
System.arraycopy(p.getInventory().getContents(), 0, hotbar, 0, 9); System.arraycopy(p.getInventory().getContents(), 0, hotbar, 0, 9);
YamlConfiguration configuration = new YamlConfiguration(); YAPIONArray yapionArray = new YAPIONArray();
configuration.set("Items", hotbar); for (ItemStack itemStack : hotbar) {
Config.getInstance().get(p).add("hotbar", configuration.saveToString()); if (itemStack != null) {
yapionArray.add(YAPIONSerializer.serialize(itemStack));
} else {
yapionArray.add(new YAPIONValue<>(null));
}
}
Config.getInstance().get(p).add("hotbar", yapionArray);
Config.getInstance().save(p); Config.getInstance().save(p);
} }
public static void setHotbar(Player p) { public void setHotbar(Player p) {
ItemStack[] hotbar = getItems(p); ItemStack[] hotbar = getItems(p);
ItemStack[] inv = p.getInventory().getContents(); ItemStack[] inv = p.getInventory().getContents();
System.arraycopy(hotbar, 0, inv, 0, 9); System.arraycopy(hotbar, 0, inv, 0, 9);
p.getInventory().setContents(inv); p.getInventory().setContents(inv);
} }
public static ItemStack[] getItems(Player p) { public ItemStack[] getItems(Player p) {
YamlConfiguration hb = YamlConfiguration.loadConfiguration(new StringReader(Config.getInstance().get(p).getPlainValueOrDefault("hotbar", defaultHotbar()))); YAPIONArray yapionArray = Config.getInstance().get(p).getYAPIONArrayOrSetDefault("hotbar", defaultHotbar());;
ItemStack[] hotbar = hb.getList("Items").toArray(new ItemStack[9]); ItemStack[] hotbar = new ItemStack[9];
yapionArray.forEach((integer, yapionAnyType) -> {
if (yapionAnyType instanceof YAPIONValue) {
hotbar[integer] = null;
} else {
hotbar[integer] = (ItemStack) YAPIONDeserializer.deserialize((YAPIONObject) yapionAnyType);
}
});
return hotbar; return hotbar;
} }
public static String defaultHotbar() { public YAPIONArray defaultHotbar() {
return "Items:\n" + return DEFAULT_HOTBAR;
"- ==: org.bukkit.inventory.ItemStack\n" +
" v: 2230\n" +
" type: WOODEN_AXE\n" +
" meta:\n" +
" ==: ItemMeta\n" +
" meta-type: UNSPECIFIC\n" +
" display-name: WorldEdit Wand\n" +
" lore:\n" +
" - 'Left click: select pos #1'\n" +
" - 'Right click: select pos #2'\n" +
"- ==: org.bukkit.inventory.ItemStack\n" +
" v: 2230\n" +
" type: COMPASS\n" +
" meta:\n" +
" ==: ItemMeta\n" +
" meta-type: UNSPECIFIC\n" +
" display-name: Navigation Wand\n" +
" lore:\n" +
" - 'Left click: jump to location'\n" +
" - 'Right click: pass through walls'\n" +
"- null\n" +
"- null\n" +
"- null\n" +
"- null\n" +
"- null\n" +
"- null\n" +
"- ==: org.bukkit.inventory.ItemStack\n" +
" v: 2230\n" +
" type: NETHER_STAR\n" +
" meta:\n" +
" ==: ItemMeta\n" +
" meta-type: UNSPECIFIC\n" +
" display-name: §eBau GUI\n";
} }
} }