Add BauSystem module

Fix ci java version
Fix LinkageProcessor
This commit is contained in:
2024-08-05 13:28:50 +02:00
parent 41d31e6c9c
commit 3366a30b0c
526 changed files with 43550 additions and 149479 deletions
@@ -0,0 +1,120 @@
/*
* 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;
import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.Permission;
import de.steamwar.bausystem.SWUtils;
import de.steamwar.bausystem.features.gui.editor.BauGuiMapping;
import de.steamwar.bausystem.linkage.LinkageUtils;
import de.steamwar.bausystem.linkage.specific.BauGuiItem;
import de.steamwar.inventory.SWInventory;
import lombok.experimental.UtilityClass;
import net.md_5.bungee.api.ChatColor;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import java.util.*;
@UtilityClass
public class BauGUI {
private static final Map<Integer, BauGuiItem> ITEMS = new HashMap<>();
public static Map<Integer, BauGuiItem> getITEMS() {
if (ITEMS.isEmpty()) LinkageUtils.linkGUIItems();
return ITEMS;
}
private static final Set<Player> OPEN_INVS = new HashSet<>();
private static boolean updating = false;
public static ItemStack getGUI_ITEM(Player p) {
ItemStack GUI_ITEM = new ItemStack(Material.NETHER_STAR);
ItemMeta meta = GUI_ITEM.getItemMeta();
meta.setDisplayName(ChatColor.YELLOW + BauSystem.MESSAGE.parse("GUI_NAME", p));
GUI_ITEM.setItemMeta(meta);
return GUI_ITEM;
}
public static void addItem(BauGuiItem item) {
if (ITEMS.containsKey(item.getId())) {
throw new IllegalArgumentException("Duplicate Id Key: " + item.getId() + ". From Classes: " + ITEMS.get(item.getId()).getClass().getName() + " and " + item.getClass().getName());
}
ITEMS.put(item.getId(), item);
}
public static void openBauGui(Player p) {
if (!updating) {
OPEN_INVS.add(p);
}
BauGuiMapping mapping = BauGuiMapping.getGuiMapping(p);
SWInventory inv = new SWInventory(p, mapping.getSize(), BauSystem.MESSAGE.parse("GUI_NAME", p));
getITEMS().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();
BauSystem.MESSAGE.send("NO_PERMISSION", p);
}
});
});
inv.addCloseCallback(clickType -> {
if (!updating) {
OPEN_INVS.remove(p);
}
});
inv.open();
}
public static void update() {
updating = true;
OPEN_INVS.forEach(BauGUI::openBauGui);
updating = false;
}
public static void giveItem(Player p) {
SWUtils.giveItemToPlayer(p, getGUI_ITEM(p));
}
private static ItemStack permissionLore(ItemStack itemStack, Permission permission, Player p) {
ItemMeta meta = itemStack.getItemMeta();
if (!permission.hasPermission(p)) {
List<String> lore = meta.getLore();
if (lore == null) {
lore = Collections.singletonList(BauSystem.MESSAGE.parse("NO_PERMISSION", p));
} else {
lore.add(BauSystem.MESSAGE.parse("NO_PERMISSION", p));
}
meta.setLore(lore);
}
itemStack.setItemMeta(meta);
return itemStack;
}
}
@@ -0,0 +1,49 @@
/*
* 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;
import de.steamwar.bausystem.features.gui.editor.BauGuiEditor;
import de.steamwar.command.SWCommand;
import de.steamwar.inventory.SWItem;
import de.steamwar.linkage.Linked;
import org.bukkit.entity.Player;
@Linked
public class BauGUICommand extends SWCommand {
public BauGUICommand() {
super("gui", "baugui", "g");
}
@Register(help = true)
public void genericHelp(Player p, String... args) {
BauGUI.openBauGui(p);
}
@Register("item")
public void giveItem(Player p) {
BauGUI.giveItem(p);
}
@Register("editor")
public void openEditor(Player p) {
BauGuiEditor.openGuiEditor(p, new SWItem().getItemStack());
}
}
@@ -0,0 +1,60 @@
/*
* 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;
import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.Permission;
import de.steamwar.bausystem.linkage.specific.BauGuiItem;
import de.steamwar.inventory.SWItem;
import de.steamwar.linkage.Linked;
import net.md_5.bungee.api.ChatColor;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.ClickType;
import org.bukkit.inventory.ItemStack;
import java.util.Arrays;
@Linked
public class BauGuiBauGuiItem extends BauGuiItem {
public BauGuiBauGuiItem() {
super(14);
}
@Override
public ItemStack getItem(Player player) {
return new SWItem(Material.NETHER_STAR, ChatColor.YELLOW + BauSystem.MESSAGE.parse("GUI_NAME", player),
Arrays.asList(BauSystem.MESSAGE.parse("GUI_ITEM_LORE1", player), BauSystem.MESSAGE.parse("GUI_ITEM_LORE2", player)), false, clickType -> {
}).getItemStack();
}
@Override
public boolean click(ClickType click, Player p) {
p.closeInventory();
BauGUI.giveItem(p);
return false;
}
@Override
public Permission permission() {
return Permission.MEMBER;
}
}
@@ -0,0 +1,43 @@
/*
* 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;
import de.steamwar.linkage.Linked;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.player.PlayerInteractEvent;
@Linked
public class BauGuiListener implements Listener {
@EventHandler
public void onPlayerInteract(PlayerInteractEvent event) {
if (event.getItem() == null) {
return;
}
if (event.getAction() == Action.RIGHT_CLICK_BLOCK || event.getAction() == Action.RIGHT_CLICK_AIR) {
if (event.getItem().isSimilar(BauGUI.getGUI_ITEM(event.getPlayer()))) {
BauGUI.openBauGui(event.getPlayer());
}
}
}
}
@@ -0,0 +1,233 @@
/*
* 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.features.gui.BauGUI;
import de.steamwar.bausystem.linkage.specific.BauGuiItem;
import de.steamwar.inventory.SWItem;
import de.steamwar.inventory.SWListInv;
import de.steamwar.linkage.Linked;
import lombok.Getter;
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.InventoryView;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.persistence.PersistentDataType;
import java.util.*;
@Linked
public class BauGuiEditor implements Listener {
@Getter
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, BauSystem.MESSAGE.parse("GUI_EDITOR_TITLE", p));
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, "").getItemStack());
}
inv.setItem(mapping.getSize() + 3, new SWItem(mapping.getSize() == 9 * 5 ? Material.GRAY_STAINED_GLASS_PANE : Material.LIME_STAINED_GLASS_PANE, BauSystem.MESSAGE.parse("GUI_EDITOR_ITEM_ROW_P", p)).getItemStack());
inv.setItem(mapping.getSize() + 2, new SWItem(mapping.getSize() == 9 ? Material.GRAY_STAINED_GLASS_PANE : Material.RED_STAINED_GLASS_PANE, BauSystem.MESSAGE.parse("GUI_EDITOR_ITEM_ROW_M", p)).getItemStack());
inv.setItem(mapping.getSize() + 5, new SWItem(Material.BARRIER, BauSystem.MESSAGE.parse("GUI_EDITOR_ITEM_TRASH", p), Arrays.asList(BauSystem.MESSAGE.parse("GUI_EDITOR_ITEM_TRASH_LORE", p)), false, clickType -> {
}).getItemStack());
inv.setItem(mapping.getSize() + 6, new SWItem(Material.SCUTE, BauSystem.MESSAGE.parse("GUI_EDITOR_ITEM_MORE", p)).getItemStack());
inv.setItem(mapping.getSize() + 8, new SWItem(Material.ARROW, BauSystem.MESSAGE.parse("GUI_EDITOR_ITEM_CLOSE", p)).getItemStack());
p.openInventory(inv);
p.getOpenInventory().setCursor(cursor == null ? new SWItem().getItemStack() : cursor);
open_Edits.add(p);
BauGuiMapping.startWatchdog();
}
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;
}
if (event.getSlot() - mapping.getSize() >= 0) {
event.setCancelled(true);
}
switch (event.getSlot() - mapping.getSize()) {
case 2:
if (mapping.getSize() == 9) {
return;
}
saveMapping(p);
mapping.setSize(mapping.getSize() - 9);
openGuiEditor(p, event.getCursor());
break;
case 3:
if (mapping.getSize() == 9 * 5) {
return;
}
saveMapping(p);
mapping.setSize(mapping.getSize() + 9);
openGuiEditor(p, event.getCursor());
break;
case 5:
event.getView().setCursor(new SWItem().getItemStack());
break;
case 6:
saveMapping(p);
List<SWListInv.SWListEntry<BauGuiItem>> items = new ArrayList<>();
for (BauGuiItem 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<BauGuiItem> inv = new SWListInv<>(p, BauSystem.MESSAGE.parse("GUI_EDITOR_TITLE_MORE", p), items, (clickType, item) -> {
openGuiEditor(p, addId(item.getItem(p), item.getId()));
});
inv.open();
break;
case 8:
saveMapping(p);
BauGUI.openBauGui(p);
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())) {
if (event.getRawSlots().stream().anyMatch(integer -> event.getView().getInventory(integer) == event.getWhoClicked().getInventory())) {
event.setCancelled(true);
}
}
}
private void saveMapping(Player p) {
saveMapping(p.getOpenInventory(), BauGuiMapping.getGuiMapping(p));
}
private void saveMapping(InventoryView view, BauGuiMapping mapping) {
if (mapping.isSaved()) {
return;
}
HashMap<Integer, Integer> newMapping = new HashMap<>();
for (int i = 0; i < view.getTopInventory().getContents().length; i++) {
ItemStack itemStack = view.getTopInventory().getContents()[i];
if (itemStack == null || itemStack.getType() == Material.AIR || i >= mapping.getSize()) continue;
newMapping.put(getId(itemStack), i);
}
for (Map.Entry<Integer, BauGuiItem> e : BauGUI.getITEMS().entrySet()) {
if (!newMapping.containsKey(e.getKey())) {
newMapping.put(e.getKey(), -1);
}
}
mapping.setMapping(newMapping);
mapping.save();
}
@EventHandler
public void onInventoryClose(InventoryCloseEvent event) {
if (!open_Edits.contains(event.getPlayer())) {
return;
}
Player p = (Player) event.getPlayer();
saveMapping(event.getView(), BauGuiMapping.getGuiMapping(p));
open_Edits.remove(p);
}
}
@@ -0,0 +1,54 @@
/*
* 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.Permission;
import de.steamwar.bausystem.linkage.specific.BauGuiItem;
import de.steamwar.inventory.SWItem;
import de.steamwar.linkage.Linked;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.ClickType;
import org.bukkit.inventory.ItemStack;
@Linked
public class BauGuiEditorGuiItem extends BauGuiItem {
public BauGuiEditorGuiItem() {
super(25);
}
@Override
public Permission permission() {
return Permission.MEMBER;
}
@Override
public ItemStack getItem(Player player) {
return new SWItem(Material.IRON_PICKAXE, BauSystem.MESSAGE.parse("GUI_EDITOR_ITEM_NAME", player)).getItemStack();
}
@Override
public boolean click(ClickType click, Player p) {
p.performCommand("gui editor");
return false;
}
}
@@ -0,0 +1,138 @@
/*
* 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.configplayer.Config;
import de.steamwar.bausystem.features.gui.BauGUI;
import de.steamwar.bausystem.linkage.specific.BauGuiItem;
import lombok.Getter;
import lombok.Setter;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitTask;
import yapion.hierarchy.types.YAPIONObject;
import java.util.*;
public class BauGuiMapping {
private static final HashMap<UUID, BauGuiMapping> fromUUID = new HashMap<>();
private static final List<BauGuiMapping> mappings = new ArrayList<>();
private static BukkitTask task;
public static void startWatchdog() {
if (task == null) {
Bukkit.getScheduler().runTaskTimer(BauSystem.getInstance(), bukkitTask -> {
mappings.forEach(BauGuiMapping::tick);
if (BauGuiEditor.getOpen_Edits().isEmpty()) {
bukkitTask.cancel();
task = null;
}
}, 1, 1);
}
}
@Getter
private final YAPIONObject object;
@Getter
private final Player owner;
@Getter
@Setter
private boolean saved;
protected BauGuiMapping(YAPIONObject object, Player p) {
this.object = object;
this.owner = p;
fromUUID.put(p.getUniqueId(), this);
mappings.add(this);
}
public static BauGuiMapping getGuiMapping(Player p) {
BauGuiMapping mapping = fromUUID.get(p.getUniqueId());
if (mapping == null) {
YAPIONObject yapionObject = Config.getInstance().get(p);
mapping = new BauGuiMapping(yapionObject.getObject("baugui"), p);
}
return mapping;
}
public boolean isShown(int id) {
return object.getPlainValueOrDefault(Integer.toString(id), -1) >= 0;
}
public int getSlot(int id) {
return object.getPlainValue(Integer.toString(id));
}
public Map<Integer, Integer> getMapping() {
return internalReadMap();
}
public void setMapping(Map<Integer, Integer> mapping) {
internalWriteMap(mapping);
}
public int getSize() {
return object.getPlainValueOrDefault("size", 45);
}
public void setSize(int size) {
if (size % 9 != 0) {
return;
}
object.add("size", size);
}
private void tick() {
this.saved = false;
}
public void save() {
if (saved) return;
YAPIONObject config = Config.getInstance().get(owner);
config.add("baugui", object);
Config.getInstance().save(owner);
saved = true;
}
private Map<Integer, Integer> internalReadMap() {
Map<Integer, Integer> map = new HashMap<>();
for (Map.Entry<Integer, BauGuiItem> e : BauGUI.getITEMS().entrySet()) {
Integer value = object.getPlainValueOrDefault(e.getKey().toString(), -1);
if (value == -1) continue;
map.put(e.getKey(), value);
}
return map;
}
private void internalWriteMap(Map<Integer, Integer> map) {
for (Map.Entry<Integer, BauGuiItem> e : BauGUI.getITEMS().entrySet()) {
object.remove(e.getKey().toString());
Integer value = map.getOrDefault(e.getKey(), -1);
if (value == -1) continue;
object.add(e.getKey().toString(), value);
}
}
}