Renaming Slot and add Gui Edit with Tempclass for default generation

Signed-off-by: Chaoscaot <chaoscaot444@gmail.com>
Dieser Commit ist enthalten in:
Chaoscaot 2021-05-02 15:29:29 +02:00
Ursprung a99542bd86
Commit fb2856430f
8 geänderte Dateien mit 413 neuen und 21 gelöschten Zeilen

Datei anzeigen

@ -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<GuiItem> ITEMS = new HashSet<>();
@Getter
private static final HashMap<Integer, GuiItem> ITEMS = new HashMap<>();
private static final Set<Player> 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);

Datei anzeigen

@ -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());
}
}

Datei anzeigen

@ -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 <https://www.gnu.org/licenses/>.
*/
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<Player> 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<Integer, Integer> 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<SWListInv.SWListEntry<GuiItem>> 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<GuiItem> 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<Integer, Integer> 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<Integer, GuiItem> 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);
}
}

Datei anzeigen

@ -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 <https://www.gnu.org/licenses/>.
*/
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<UUID, BauGuiMapping> fromUUID = new HashMap<>();
private static final List<BauGuiMapping> mappings = new ArrayList<>();
static {
Bukkit.getScheduler().runTaskTimer(BauSystem.getInstance(), () -> {
mappings.forEach(BauGuiMapping::tick);
}, 1, 1);
}
@Getter
private final UUID owner;
@Getter
private final Map<Integer, Integer> 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<Integer, Integer> 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<Integer, Integer> 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;
}
}

Datei anzeigen

@ -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 <https://www.gnu.org/licenses/>.
*/
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<UUID, BauGuiMapping> DEFAULT_GUI;
static {
DEFAULT_GUI = player -> {
Random rnd = new Random();
int size = (rnd.nextInt(5) + 1) * 9;
HashMap<Integer, Integer> 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);
};
}
}

Datei anzeigen

@ -96,12 +96,13 @@ public class ColorGuiItem extends GuiItem {
ColorMode current = Region.getRegion(p.getLocation()).getPlain(Flag.COLOR, ColorMode.class);
List<SWListInv.SWListEntry<ColorMode>> 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<ColorMode> inv = new SWListInv<ColorMode>(p, "Farbe Wählen", items, (clickType, colorMode) -> {
p.closeInventory();
p.performCommand("color " + colorMode.getChatValue());
update();
});
inv.open();
return false;

Datei anzeigen

@ -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

Datei anzeigen

@ -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);