From fb2856430fd048c7157f3d428c4cfe969c23e6fe Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Sun, 2 May 2021 15:29:29 +0200 Subject: [PATCH] Renaming Slot and add Gui Edit with Tempclass for default generation Signed-off-by: Chaoscaot --- .../bausystem/features/gui/BauGUI.java | 38 +-- .../bausystem/features/gui/BauGUICommand.java | 7 + .../features/gui/editor/BauGuiEditor.java | 220 ++++++++++++++++++ .../features/gui/editor/BauGuiMapping.java | 104 +++++++++ .../features/gui/editor/TempClass.java | 56 +++++ .../features/region/items/ColorGuiItem.java | 3 +- .../features/world/InventoryListener.java | 4 +- .../steamwar/bausystem/linkage/GuiItem.java | 2 +- 8 files changed, 413 insertions(+), 21 deletions(-) create mode 100644 BauSystem_Main/src/de/steamwar/bausystem/features/gui/editor/BauGuiEditor.java create mode 100644 BauSystem_Main/src/de/steamwar/bausystem/features/gui/editor/BauGuiMapping.java create mode 100644 BauSystem_Main/src/de/steamwar/bausystem/features/gui/editor/TempClass.java diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/gui/BauGUI.java b/BauSystem_Main/src/de/steamwar/bausystem/features/gui/BauGUI.java index 33695d54..710d2695 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/gui/BauGUI.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/gui/BauGUI.java @@ -23,6 +23,7 @@ import de.steamwar.bausystem.BauSystem; import de.steamwar.bausystem.Permission; import de.steamwar.bausystem.SWUtils; import de.steamwar.bausystem.config.ColorConfig; +import de.steamwar.bausystem.features.gui.editor.BauGuiMapping; import de.steamwar.bausystem.linkage.GuiItem; import de.steamwar.inventory.SWInventory; import lombok.Getter; @@ -32,15 +33,13 @@ import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.ItemMeta; -import java.util.Collections; -import java.util.HashSet; -import java.util.List; -import java.util.Set; +import java.util.*; @UtilityClass public class BauGUI { - private static final Set ITEMS = new HashSet<>(); + @Getter + private static final HashMap ITEMS = new HashMap<>(); private static final Set OPEN_INVS = new HashSet<>(); private static boolean updating = false; @@ -56,24 +55,31 @@ public class BauGUI { } public static void addItem(GuiItem item) { - ITEMS.add(item); + ITEMS.put(item.getId(), item); } public static void openBauGui(Player p) { if (!updating) { OPEN_INVS.add(p); } - SWInventory inv = new SWInventory(p, 5 * 9, "Bau GUI"); - ITEMS.forEach(item -> inv.setItem(item.getSlot(), permissionLore(item.getItem(p), item.permission(), p), clickType -> { - if (item.permission().hasPermission(p)) { - if (item.click(clickType, p)) { - update(); - } - } else { - p.closeInventory(); - p.sendMessage(BauSystem.PREFIX + ColorConfig.ERROR + "Du hast nicht genug rechte um dies zu tun"); + BauGuiMapping mapping = BauGuiMapping.getGuiMapping(p); + SWInventory inv = new SWInventory(p, mapping.getSize(), "Bau GUI"); + ITEMS.values().forEach(item -> { + if (!mapping.isShown(item.getId())) { + return; } - })); + + inv.setItem(mapping.getSlot(item.getId()), permissionLore(item.getItem(p), item.permission(), p), clickType -> { + if (item.permission().hasPermission(p)) { + if (item.click(clickType, p)) { + update(); + } + } else { + p.closeInventory(); + p.sendMessage(BauSystem.PREFIX + ColorConfig.ERROR + "Du hast nicht genug rechte um dies zu tun"); + } + }); + }); inv.addCloseCallback(clickType -> { if (!updating) { OPEN_INVS.remove(p); diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/gui/BauGUICommand.java b/BauSystem_Main/src/de/steamwar/bausystem/features/gui/BauGUICommand.java index 475331d3..344937cd 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/gui/BauGUICommand.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/gui/BauGUICommand.java @@ -19,9 +19,11 @@ package de.steamwar.bausystem.features.gui; +import de.steamwar.bausystem.features.gui.editor.BauGuiEditor; import de.steamwar.bausystem.linkage.LinkageType; import de.steamwar.bausystem.linkage.Linked; import de.steamwar.command.SWCommand; +import de.steamwar.inventory.SWItem; import org.bukkit.entity.Player; @Linked(LinkageType.COMMAND) @@ -40,4 +42,9 @@ public class BauGUICommand extends SWCommand { public void giveItem(Player p) { BauGUI.giveItem(p); } + + @Register("editor") + public void openEditor(Player p) { + BauGuiEditor.openGuiEditor(p, new SWItem().getItemStack()); + } } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/gui/editor/BauGuiEditor.java b/BauSystem_Main/src/de/steamwar/bausystem/features/gui/editor/BauGuiEditor.java new file mode 100644 index 00000000..0e1a30d8 --- /dev/null +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/gui/editor/BauGuiEditor.java @@ -0,0 +1,220 @@ +/* + * 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.gui.editor; + +import de.steamwar.bausystem.BauSystem; +import de.steamwar.bausystem.config.ColorConfig; +import de.steamwar.bausystem.features.gui.BauGUI; +import de.steamwar.bausystem.linkage.GuiItem; +import de.steamwar.bausystem.linkage.LinkageType; +import de.steamwar.bausystem.linkage.Linked; +import de.steamwar.inventory.SWItem; +import de.steamwar.inventory.SWListInv; +import org.bukkit.Bukkit; +import org.bukkit.Material; +import org.bukkit.NamespacedKey; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.entity.EntityDamageEvent; +import org.bukkit.event.inventory.InventoryClickEvent; +import org.bukkit.event.inventory.InventoryCloseEvent; +import org.bukkit.event.inventory.InventoryDragEvent; +import org.bukkit.event.player.PlayerMoveEvent; +import org.bukkit.event.player.PlayerQuitEvent; +import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.ItemMeta; +import org.bukkit.persistence.PersistentDataType; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +@Linked(LinkageType.LISTENER) +public class BauGuiEditor implements Listener { + + private static final List open_Edits = new ArrayList<>(); + + public static void openGuiEditor(Player p, ItemStack cursor) { + BauGuiMapping mapping = BauGuiMapping.getGuiMapping(p); + Inventory inv = Bukkit.createInventory(null, mapping.getSize() + 9, "Bau Gui Editor"); + for (Map.Entry e : mapping.getMapping().entrySet()) { + if (e.getValue() >= 0) { + if (e.getValue() < mapping.getSize()) { + inv.setItem(e.getValue(), addId(BauGUI.getITEMS().get(e.getKey()).getItem(p), e.getKey())); + } + } + } + + for (int j = mapping.getSize(); j < mapping.getSize() + 9; j++) { + inv.setItem(j, new SWItem(Material.WHITE_STAINED_GLASS_PANE, ColorConfig.HIGHLIGHT + "").getItemStack()); + } + + inv.setItem(mapping.getSize() + 3, new SWItem(mapping.getSize() == 9 * 5 ? Material.GRAY_STAINED_GLASS_PANE : Material.LIME_STAINED_GLASS_PANE, ColorConfig.HIGHLIGHT + "+1 Zeile").getItemStack()); + inv.setItem(mapping.getSize() + 2, new SWItem(mapping.getSize() == 9 ? Material.GRAY_STAINED_GLASS_PANE : Material.RED_STAINED_GLASS_PANE, ColorConfig.HIGHLIGHT + "-1 Zeile").getItemStack()); + + inv.setItem(mapping.getSize() + 5, new SWItem(Material.BARRIER, ColorConfig.ERROR + "Trashcan").getItemStack()); + inv.setItem(mapping.getSize() + 6, new SWItem(Material.SCUTE, ColorConfig.HIGHLIGHT + "Mehr Items").getItemStack()); + + p.openInventory(inv); + p.getOpenInventory().setCursor(cursor == null ? new SWItem().getItemStack() : cursor); + open_Edits.add(p); + } + + private static ItemStack addId(ItemStack itemStack, int id) { + ItemMeta meta = itemStack.getItemMeta(); + meta.getPersistentDataContainer().set(new NamespacedKey(BauSystem.getInstance(), "gui-item-id"), PersistentDataType.INTEGER, id); + itemStack.setItemMeta(meta); + return itemStack; + } + + private static int getId(ItemStack itemStack) { + ItemMeta meta = itemStack.getItemMeta(); + return meta.getPersistentDataContainer().get(new NamespacedKey(BauSystem.getInstance(), "gui-item-id"), PersistentDataType.INTEGER); + } + + @EventHandler + public void onInventoryClick(InventoryClickEvent event) { + if (!open_Edits.contains(event.getWhoClicked())) { + return; + } + + ItemStack i = event.getCurrentItem(); + Player p = (Player) event.getWhoClicked(); + BauGuiMapping mapping = BauGuiMapping.getGuiMapping(p); + if (event.getClickedInventory() == p.getInventory()) { + event.setCancelled(true); + } + + if (event.getHotbarButton() != -1) { + event.setCancelled(true); + } + + if (i == null) { + return; + } + + switch (i.getType()) { + case RED_STAINED_GLASS_PANE: + event.setCancelled(true); + saveMapping(p); + mapping.setSize(mapping.getSize() - 9); + openGuiEditor(p, event.getCursor()); + break; + case LIME_STAINED_GLASS_PANE: + event.setCancelled(true); + saveMapping(p); + mapping.setSize(mapping.getSize() + 9); + openGuiEditor(p, event.getCursor()); + break; + case BARRIER: + event.setCancelled(true); + event.getView().setCursor(new SWItem().getItemStack()); + break; + case SCUTE: + event.setCancelled(true); + saveMapping(p); + List> items = new ArrayList<>(); + for (GuiItem item : BauGUI.getITEMS().values()) { + if (mapping.isShown(item.getId())) { + continue; + } + SWItem ip = new SWItem(); + ip.setItemStack(item.getItem(p)); + items.add(new SWListInv.SWListEntry(ip, item)); + } + if (items.isEmpty()) { + return; + } + SWListInv inv = new SWListInv<>(p, "Item auswählen", items, (clickType, item) -> { + openGuiEditor(p, addId(item.getItem(p), item.getId())); + }); + inv.open(); + break; + case WHITE_STAINED_GLASS_PANE: + case GRAY_STAINED_GLASS_PANE: + event.setCancelled(true); + break; + default: + } + } + + @EventHandler + public void onEntityDamage(EntityDamageEvent event) { + if (open_Edits.contains(event.getEntity())) { + event.setCancelled(true); + } + } + + @EventHandler + public void onPlayerMove(PlayerMoveEvent event) { + if (open_Edits.contains(event.getPlayer())) { + event.setCancelled(true); + } + } + + @EventHandler + public void onPlayerQuit(PlayerQuitEvent event) { + open_Edits.remove(event.getPlayer()); + } + + @EventHandler + public void onInventoryDrag(InventoryDragEvent event) { + if (open_Edits.contains(event.getWhoClicked())) { + event.setCancelled(true); + } + } + + private void saveMapping(Player p) { + BauGuiMapping mapping = BauGuiMapping.getGuiMapping(p); + if (mapping.isSaved()) { + return; + } + HashMap newMapping = new HashMap<>(); + + for (int i = 0; i < p.getOpenInventory().getTopInventory().getContents().length; i++) { + ItemStack itemStack = p.getOpenInventory().getTopInventory().getContents()[i]; + if (itemStack == null || itemStack.getType() == Material.AIR || i >= mapping.getSize()) continue; + newMapping.put(getId(itemStack), i); + } + + for (Map.Entry e : BauGUI.getITEMS().entrySet()) { + if (!newMapping.containsKey(e.getKey())) { + newMapping.put(e.getKey(), -1); + } + } + + mapping.setMapping(newMapping); + } + + @EventHandler + public void onInventoryClose(InventoryCloseEvent event) { + if (!open_Edits.contains(event.getPlayer())) { + return; + } + + Player p = (Player) event.getPlayer(); + + saveMapping(p); + open_Edits.remove(p); + } +} diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/gui/editor/BauGuiMapping.java b/BauSystem_Main/src/de/steamwar/bausystem/features/gui/editor/BauGuiMapping.java new file mode 100644 index 00000000..36bd3146 --- /dev/null +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/gui/editor/BauGuiMapping.java @@ -0,0 +1,104 @@ +/* + * 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.gui.editor; + +import de.steamwar.bausystem.BauSystem; +import lombok.Getter; +import org.bukkit.Bukkit; +import org.bukkit.entity.Player; + +import java.util.*; + +public class BauGuiMapping { + + private static final HashMap fromUUID = new HashMap<>(); + private static final List mappings = new ArrayList<>(); + + static { + Bukkit.getScheduler().runTaskTimer(BauSystem.getInstance(), () -> { + mappings.forEach(BauGuiMapping::tick); + }, 1, 1); + } + + @Getter + private final UUID owner; + @Getter + private final Map mapping; + @Getter + private int size; + @Getter + private boolean saved; + + protected BauGuiMapping(UUID owner) { + this.owner = owner; + this.mapping = new HashMap<>(); + fromUUID.put(owner, this); + mappings.add(this); + } + + protected BauGuiMapping(UUID owner, int size, Map mapping) { + this.owner = owner; + this.size = size; + this.mapping = mapping; + fromUUID.put(owner, this); + mappings.add(this); + } + + public static BauGuiMapping getGuiMapping(Player p) { + BauGuiMapping mapping = fromUUID.get(p.getUniqueId()); + + if (mapping == null) { + mapping = TempClass.DEFAULT_GUI.apply(p.getUniqueId()); + } + + return mapping; + } + + public boolean isShown(int id) { + if (!mapping.containsKey(id)) { + return false; + } + + return mapping.get(id) >= 0; + } + + public int getSlot(int id) { + return mapping.get(id); + } + + public void setMapping(Map mapping) { + this.mapping.clear(); + this.mapping.putAll(mapping); + this.saved = true; + } + + public boolean setSize(int size) { + if (size % 9 != 0) { + return false; + } + + this.size = size; + return true; + } + + private void tick() { + this.saved = false; + } +} diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/gui/editor/TempClass.java b/BauSystem_Main/src/de/steamwar/bausystem/features/gui/editor/TempClass.java new file mode 100644 index 00000000..95ba462a --- /dev/null +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/gui/editor/TempClass.java @@ -0,0 +1,56 @@ +/* + * 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.gui.editor; + +import de.steamwar.bausystem.features.gui.BauGUI; +import de.steamwar.bausystem.linkage.GuiItem; + +import java.util.HashMap; +import java.util.Random; +import java.util.UUID; +import java.util.function.Function; + +// TODO: 01.05.2021 remove class +public class TempClass { + + public static final Function DEFAULT_GUI; + + static { + DEFAULT_GUI = player -> { + Random rnd = new Random(); + int size = (rnd.nextInt(5) + 1) * 9; + HashMap mapping = new HashMap<>(); + for (GuiItem item : BauGUI.getITEMS().values()) { + int slot; + int i = 0; + do { + if (i > 5) { + slot = -1; + break; + } + slot = rnd.nextInt(size + 1) - 1; + i++; + } while (mapping.containsValue(slot)); + mapping.put(item.getId(), slot); + } + return new BauGuiMapping(player, size, mapping); + }; + } +} diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/region/items/ColorGuiItem.java b/BauSystem_Main/src/de/steamwar/bausystem/features/region/items/ColorGuiItem.java index 6e86f23f..63964374 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/region/items/ColorGuiItem.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/region/items/ColorGuiItem.java @@ -96,12 +96,13 @@ public class ColorGuiItem extends GuiItem { ColorMode current = Region.getRegion(p.getLocation()).getPlain(Flag.COLOR, ColorMode.class); List> items = new ArrayList<>(); for (ColorMode value : ColorMode.values()) { - items.add(new SWListInv.SWListEntry<>(new SWItem(mapColor(value.getColor()), (byte) 0, value.getChatValue(), Collections.emptyList(), value == current, clickType -> { + items.add(new SWListInv.SWListEntry<>(new SWItem(mapColor(value.getColor()), (byte) 0, "§f" + value.getChatValue(), Collections.emptyList(), value == current, clickType -> { }), value)); } SWListInv inv = new SWListInv(p, "Farbe Wählen", items, (clickType, colorMode) -> { p.closeInventory(); p.performCommand("color " + colorMode.getChatValue()); + update(); }); inv.open(); return false; diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/world/InventoryListener.java b/BauSystem_Main/src/de/steamwar/bausystem/features/world/InventoryListener.java index a4e2490d..a06ed3c1 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/world/InventoryListener.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/world/InventoryListener.java @@ -19,8 +19,6 @@ package de.steamwar.bausystem.features.world; -import de.steamwar.bausystem.linkage.LinkageType; -import de.steamwar.bausystem.linkage.Linked; import de.steamwar.core.Core; import org.bukkit.Material; import org.bukkit.attribute.Attribute; @@ -31,7 +29,7 @@ import org.bukkit.event.inventory.InventoryClickEvent; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.ItemMeta; -@Linked(LinkageType.LISTENER) +//@Linked(LinkageType.LISTENER) public class InventoryListener implements Listener { @EventHandler diff --git a/BauSystem_Main/src/de/steamwar/bausystem/linkage/GuiItem.java b/BauSystem_Main/src/de/steamwar/bausystem/linkage/GuiItem.java index 7e13c9ec..5981da75 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/linkage/GuiItem.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/linkage/GuiItem.java @@ -31,7 +31,7 @@ import org.bukkit.inventory.ItemStack; public abstract class GuiItem { @Getter - private final int slot; + private final int id; public abstract ItemStack getItem(Player player);