diff --git a/BauSystem_Main/src/BauSystem.properties b/BauSystem_Main/src/BauSystem.properties index 4ac81c96..a5491fd4 100644 --- a/BauSystem_Main/src/BauSystem.properties +++ b/BauSystem_Main/src/BauSystem.properties @@ -127,6 +127,11 @@ COUNTINGWAND_MESSAGE_R-CLICK = §7Erste Position bei: §8[§7{0}§8, §7{1}§8, COUNTINGWAND_MESSAGE_L-CLICK = §7Zweite Position bei: §8[§7{0}§8, §7{1}§8, §7{2}§8] ({3}) COUNTINGWAND_MESSAGE_DIMENSION = §e{0}§8, §e{1}§8, §e{2} +# Hotbar +HOTBAR_SAVED = §7Deine Hotbar wurde als Standard gespeichert +HOTBAR_LOADED = §7Deine Standard Hotbar wurde geladen +HOTBAR_INVENTORY = Standard Hotbar + # GUI GUI_EDITOR_ITEM-NAME=§eGui Editor GUI_EXPORT_CODE=§eDein Gui-Code: diff --git a/BauSystem_Main/src/de/steamwar/bausystem/configplayer/Config.java b/BauSystem_Main/src/de/steamwar/bausystem/configplayer/Config.java index a855c710..7ad889c4 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/configplayer/Config.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/configplayer/Config.java @@ -1,5 +1,25 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2021 SteamWar.de-Serverteam + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + package de.steamwar.bausystem.configplayer; +import de.steamwar.bausystem.configplayer.serializer.ConfigurationSerializableSerializer; import de.steamwar.bausystem.linkage.LinkageType; import de.steamwar.bausystem.linkage.Linked; import de.steamwar.sql.UserConfig; @@ -13,6 +33,7 @@ import org.bukkit.event.player.PlayerQuitEvent; import yapion.hierarchy.output.StringOutput; import yapion.hierarchy.types.YAPIONObject; import yapion.parser.YAPIONParser; +import yapion.serializing.SerializeManager; import java.util.HashMap; import java.util.Map; @@ -22,6 +43,10 @@ import java.util.logging.Level; @Linked(LinkageType.LISTENER) public class Config implements Listener { + static { + SerializeManager.add(new ConfigurationSerializableSerializer()); + } + @Getter private static Config instance; diff --git a/BauSystem_Main/src/de/steamwar/bausystem/configplayer/ConfigCreator.java b/BauSystem_Main/src/de/steamwar/bausystem/configplayer/ConfigCreator.java index b7c13d11..f2d8a4a8 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/configplayer/ConfigCreator.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/configplayer/ConfigCreator.java @@ -19,6 +19,7 @@ package de.steamwar.bausystem.configplayer; +import de.steamwar.bausystem.features.hotbar.DefaultHotbar; import lombok.experimental.UtilityClass; import yapion.hierarchy.types.YAPIONObject; @@ -34,6 +35,9 @@ public class ConfigCreator { // Any initialising goes into here yapionObject.add("baugui", defaultBauGui()); + + // Default Hotbar Gui + yapionObject.add("hotbar", DefaultHotbar.defaultHotbar()); return yapionObject; } @@ -54,5 +58,4 @@ public class ConfigCreator { yapionObject.add("size", 45); return yapionObject; } - } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/configplayer/serializer/ConfigurationSerializableSerializer.java b/BauSystem_Main/src/de/steamwar/bausystem/configplayer/serializer/ConfigurationSerializableSerializer.java new file mode 100644 index 00000000..50599ff8 --- /dev/null +++ b/BauSystem_Main/src/de/steamwar/bausystem/configplayer/serializer/ConfigurationSerializableSerializer.java @@ -0,0 +1,66 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2021 SteamWar.de-Serverteam + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package de.steamwar.bausystem.configplayer.serializer; + +import org.bukkit.configuration.serialization.ConfigurationSerializable; +import org.bukkit.configuration.serialization.ConfigurationSerialization; +import yapion.hierarchy.types.YAPIONObject; +import yapion.serializing.api.SerializerObject; +import yapion.serializing.data.DeserializeData; +import yapion.serializing.data.SerializeData; + +import java.util.HashMap; +import java.util.Map; + +import static yapion.utils.IdentifierUtils.TYPE_IDENTIFIER; + +public class ConfigurationSerializableSerializer extends SerializerObject { + + @Override + public Class type() { + return ConfigurationSerializable.class; + } + + @Override + public boolean isInterface() { + return true; + } + + @Override + public YAPIONObject serialize(SerializeData serializeData) { + YAPIONObject yapionObject = new YAPIONObject(); + yapionObject.add(TYPE_IDENTIFIER, type().getTypeName()); + Map serializeDataMap = serializeData.object.serialize(); + serializeDataMap.forEach((s, o) -> { + yapionObject.add(s, serializeData.serialize(o)); + }); + return yapionObject; + } + + @Override + public ConfigurationSerializable deserialize(DeserializeData deserializeData) { + Map deserializeDataMap = new HashMap<>(); + deserializeData.object.forEach((s, yapionAnyType) -> { + if (s.equals(TYPE_IDENTIFIER)) return; + deserializeDataMap.put(s, deserializeData.deserialize(yapionAnyType)); + }); + return ConfigurationSerialization.deserializeObject(deserializeDataMap); + } +} \ No newline at end of file diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/hotbar/DefaultHotbar.java b/BauSystem_Main/src/de/steamwar/bausystem/features/hotbar/DefaultHotbar.java new file mode 100644 index 00000000..abbec165 --- /dev/null +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/hotbar/DefaultHotbar.java @@ -0,0 +1,90 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2021 SteamWar.de-Serverteam + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package de.steamwar.bausystem.features.hotbar; + +import de.steamwar.bausystem.configplayer.Config; +import org.bukkit.configuration.file.YamlConfiguration; +import org.bukkit.entity.Player; +import org.bukkit.inventory.ItemStack; + +import java.io.StringReader; + +// TODO: 16.05.2021 @Yoyo muss mal was Fixen, dann Item Save auf Yapion umstellen +public class DefaultHotbar { + + public static void updateHotbar(Player p) { + ItemStack[] hotbar = new ItemStack[9]; + System.arraycopy(p.getInventory().getContents(), 0, hotbar, 0, 9); + YamlConfiguration configuration = new YamlConfiguration(); + configuration.set("Items", hotbar); + Config.getInstance().get(p).add("hotbar", configuration.saveToString()); + Config.getInstance().save(p); + } + + public static void setHotbar(Player p) { + ItemStack[] hotbar = getItems(p); + ItemStack[] inv = p.getInventory().getContents(); + System.arraycopy(hotbar, 0, inv, 0, 9); + p.getInventory().setContents(inv); + } + + public static ItemStack[] getItems(Player p) { + YamlConfiguration hb = YamlConfiguration.loadConfiguration(new StringReader(Config.getInstance().get(p).getPlainValueOrDefault("hotbar", defaultHotbar()))); + ItemStack[] hotbar = hb.getList("Items").toArray(new ItemStack[9]); + return hotbar; + } + + public static String defaultHotbar() { + return "Items:\n" + + "- ==: 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"; + } +} diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/hotbar/HotbarCommand.java b/BauSystem_Main/src/de/steamwar/bausystem/features/hotbar/HotbarCommand.java new file mode 100644 index 00000000..0a7ab976 --- /dev/null +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/hotbar/HotbarCommand.java @@ -0,0 +1,70 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2021 SteamWar.de-Serverteam + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package de.steamwar.bausystem.features.hotbar; + +import de.steamwar.bausystem.BauSystem; +import de.steamwar.bausystem.config.ColorConfig; +import de.steamwar.bausystem.linkage.LinkageType; +import de.steamwar.bausystem.linkage.Linked; +import de.steamwar.command.SWCommand; +import de.steamwar.inventory.SWInventory; +import org.bukkit.entity.Player; +import org.bukkit.inventory.ItemStack; + +@Linked(LinkageType.COMMAND) +public class HotbarCommand extends SWCommand { + + protected HotbarCommand() { + super("hotbar", "hb"); + } + + @Register(help = true) + public void genericHelp(Player p, String... args) { + p.sendMessage(ColorConfig.BASE + "---=== (" + ColorConfig.HIGHLIGHT + "Hotbar" + ColorConfig.BASE + ") ===---"); + p.sendMessage(ColorConfig.BASE + "Speichert eine Hotbar. Diese wird beim Joinen eines Bauserver, wo du ein Leeres Inventar hast geladen."); + p.sendMessage(ColorConfig.OTHER + "/" + ColorConfig.HIGHLIGHT + "hotbar save" + ColorConfig.OTHER + " - " + ColorConfig.BASE + "Speicher deine Aktuelle Hotbar"); + p.sendMessage(ColorConfig.OTHER + "/" + ColorConfig.HIGHLIGHT + "hotbar load" + ColorConfig.OTHER + " - " + ColorConfig.BASE + "Lade deine Standard Hotbar"); + p.sendMessage(ColorConfig.OTHER + "/" + ColorConfig.HIGHLIGHT + "hotbar show" + ColorConfig.OTHER + " - " + ColorConfig.BASE + "Zeigt dir deine Standard Hotbar"); + } + + @Register("load") + public void loadHotbar(Player p) { + DefaultHotbar.setHotbar(p); + BauSystem.MESSAGE.send("HOTBAR_SAVED", p); + } + + @Register("save") + public void saveHotbar(Player p) { + DefaultHotbar.updateHotbar(p); + BauSystem.MESSAGE.send("HOTBAR_LOADED", p); + } + + @Register("show") + public void showHotbar(Player p) { + SWInventory inv = new SWInventory(p, 9, BauSystem.MESSAGE.parse("HOTBAR_INVENTORY", p)); + ItemStack[] hotbar = DefaultHotbar.getItems(p); + for (int i = 0; i < hotbar.length; i++) { + if (hotbar[i] == null) continue; + inv.setItem(i, hotbar[i], clickType -> { + }); + } + inv.open(); + } +} diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/hotbar/HotbarListener.java b/BauSystem_Main/src/de/steamwar/bausystem/features/hotbar/HotbarListener.java new file mode 100644 index 00000000..5772c878 --- /dev/null +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/hotbar/HotbarListener.java @@ -0,0 +1,48 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2021 SteamWar.de-Serverteam + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package de.steamwar.bausystem.features.hotbar; + +import de.steamwar.bausystem.linkage.LinkageType; +import de.steamwar.bausystem.linkage.Linked; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerJoinEvent; + +@Linked(LinkageType.LISTENER) +public class HotbarListener implements Listener { + + @EventHandler(priority = EventPriority.LOWEST) + public void onPlayerJoin(PlayerJoinEvent event) { + System.out.println(event.getPlayer().getInventory().getContents()); + if (allNull(event.getPlayer().getInventory().getContents())) { + DefaultHotbar.setHotbar(event.getPlayer()); + } + } + + private boolean allNull(Object[] arr) { + for (Object o : arr) { + if (o != null) { + return false; + } + } + return true; + } +}