From 84f05e4f6849f03e0b8ab003ebb5cbc8ec592f30 Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Tue, 20 Apr 2021 09:14:44 +0200 Subject: [PATCH] Basic BauGUI Signed-off-by: Chaoscaot --- .../bausystem/features/gui/BauGUI.java | 60 +++++++++++++++++++ .../steamwar/bausystem/linkage/GuiItem.java | 42 +++++++++++++ .../bausystem/linkage/LinkageType.java | 7 ++- 3 files changed, 107 insertions(+), 2 deletions(-) create mode 100644 BauSystem_Main/src/de/steamwar/bausystem/features/gui/BauGUI.java create mode 100644 BauSystem_Main/src/de/steamwar/bausystem/linkage/GuiItem.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 new file mode 100644 index 00000000..ded935c2 --- /dev/null +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/gui/BauGUI.java @@ -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 . + */ + +package de.steamwar.bausystem.features.gui; + +import de.steamwar.bausystem.linkage.GuiItem; +import de.steamwar.inventory.SWInventory; +import lombok.experimental.UtilityClass; +import org.bukkit.entity.Player; + +import java.util.HashSet; +import java.util.Set; + +@UtilityClass +public class BauGUI { + + private static final Set ITEMS = new HashSet<>(); + + private static final Set OPEN_INVS = new HashSet<>(); + private static boolean updating = false; + + public static void addItem(GuiItem item) { + ITEMS.add(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(), item.getItem(p), item::click)); + inv.addCloseCallback(clickType -> { + if (!updating) { + OPEN_INVS.remove(p); + } + }); + } + + public static void update() { + updating = true; + OPEN_INVS.forEach(BauGUI::openBauGui); + updating = false; + } +} diff --git a/BauSystem_Main/src/de/steamwar/bausystem/linkage/GuiItem.java b/BauSystem_Main/src/de/steamwar/bausystem/linkage/GuiItem.java new file mode 100644 index 00000000..9288f2f9 --- /dev/null +++ b/BauSystem_Main/src/de/steamwar/bausystem/linkage/GuiItem.java @@ -0,0 +1,42 @@ +/* + * 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.linkage; + +import de.steamwar.bausystem.features.gui.BauGUI; +import lombok.AllArgsConstructor; +import lombok.Getter; +import org.bukkit.entity.Player; +import org.bukkit.event.inventory.ClickType; +import org.bukkit.inventory.ItemStack; + +@AllArgsConstructor +public abstract class GuiItem { + + @Getter + private final int slot; + + public abstract ItemStack getItem(Player player); + + public abstract boolean click(ClickType click); + + public void update() { + BauGUI.update(); + } +} diff --git a/BauSystem_Main/src/de/steamwar/bausystem/linkage/LinkageType.java b/BauSystem_Main/src/de/steamwar/bausystem/linkage/LinkageType.java index e9a57432..8a73906c 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/linkage/LinkageType.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/linkage/LinkageType.java @@ -20,6 +20,7 @@ package de.steamwar.bausystem.linkage; import de.steamwar.bausystem.BauSystem; +import de.steamwar.bausystem.features.gui.BauGUI; import de.steamwar.command.SWCommand; import lombok.AllArgsConstructor; import lombok.Getter; @@ -40,7 +41,8 @@ public enum LinkageType { DISABLE_LINK(0, true, Disable.class::isAssignableFrom, o -> ((Disable) o).disable()), PLAIN(1, false, clazz -> true), LISTENER(2, false, Listener.class::isAssignableFrom, o -> Bukkit.getPluginManager().registerEvents((Listener) o, BauSystem.getInstance())), - UNLINK_LISTENER(2, true, Listener.class::isAssignableFrom, o -> HandlerList.unregisterAll((Listener) o)); + UNLINK_LISTENER(2, true, Listener.class::isAssignableFrom, o -> HandlerList.unregisterAll((Listener) o)), + GUIITEM(3, false, GuiItem.class::isAssignableFrom, o -> BauGUI.addItem((GuiItem) o)); private final int order; @@ -49,5 +51,6 @@ public enum LinkageType { private final Predicate> linkagePredicate; @SuppressWarnings("FieldMayBeFinal") - private Consumer linkageConsumer = o -> {}; + private Consumer linkageConsumer = o -> { + }; } \ No newline at end of file