From 9c91e11780424cd91999c090efaceec68a5adc48 Mon Sep 17 00:00:00 2001 From: EpicKnarvik97 Date: Thu, 11 May 2023 17:52:49 +0200 Subject: [PATCH] Gives players an item to open the arena menu --- .../net/knarcraft/minigames/MiniGames.java | 2 + .../arena/AbstractPlayerEntryState.java | 34 +++++++++++ .../minigames/command/MenuCommand.java | 14 +---- .../dropper/JoinDropperArenaCommand.java | 2 + .../parkour/JoinParkourArenaCommand.java | 2 + .../minigames/listener/InteractListener.java | 39 +++++++++++++ .../minigames/property/PersistentDataKey.java | 43 ++++++++++++++ .../knarcraft/minigames/util/GUIHelper.java | 58 +++++++++++++++++++ 8 files changed, 183 insertions(+), 11 deletions(-) create mode 100644 src/main/java/net/knarcraft/minigames/listener/InteractListener.java create mode 100644 src/main/java/net/knarcraft/minigames/property/PersistentDataKey.java create mode 100644 src/main/java/net/knarcraft/minigames/util/GUIHelper.java diff --git a/src/main/java/net/knarcraft/minigames/MiniGames.java b/src/main/java/net/knarcraft/minigames/MiniGames.java index baea40d..2a34c74 100644 --- a/src/main/java/net/knarcraft/minigames/MiniGames.java +++ b/src/main/java/net/knarcraft/minigames/MiniGames.java @@ -54,6 +54,7 @@ import net.knarcraft.minigames.container.SerializableMaterial; import net.knarcraft.minigames.container.SerializableUUID; import net.knarcraft.minigames.listener.CommandListener; import net.knarcraft.minigames.listener.DamageListener; +import net.knarcraft.minigames.listener.InteractListener; import net.knarcraft.minigames.listener.MoveListener; import net.knarcraft.minigames.listener.PlayerStateChangeListener; import net.knarcraft.minigames.placeholder.DropperRecordExpansion; @@ -311,6 +312,7 @@ public final class MiniGames extends JavaPlugin { pluginManager.registerEvents(new PlayerStateChangeListener(), this); pluginManager.registerEvents(new CommandListener(), this); pluginManager.registerEvents(new GUIListener(true), this); + pluginManager.registerEvents(new InteractListener(), this); } /** diff --git a/src/main/java/net/knarcraft/minigames/arena/AbstractPlayerEntryState.java b/src/main/java/net/knarcraft/minigames/arena/AbstractPlayerEntryState.java index 0823387..27e6ec9 100644 --- a/src/main/java/net/knarcraft/minigames/arena/AbstractPlayerEntryState.java +++ b/src/main/java/net/knarcraft/minigames/arena/AbstractPlayerEntryState.java @@ -2,14 +2,21 @@ package net.knarcraft.minigames.arena; import net.knarcraft.minigames.MiniGames; import net.knarcraft.minigames.container.SerializableUUID; +import net.knarcraft.minigames.property.PersistentDataKey; import org.bukkit.Bukkit; import org.bukkit.GameMode; import org.bukkit.Location; +import org.bukkit.NamespacedKey; import org.bukkit.entity.Player; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.ItemMeta; +import org.bukkit.persistence.PersistentDataType; import org.jetbrains.annotations.NotNull; import java.util.HashMap; +import java.util.HashSet; import java.util.Map; +import java.util.Set; import java.util.UUID; import java.util.logging.Level; @@ -86,6 +93,7 @@ public abstract class AbstractPlayerEntryState implements PlayerEntryState { player.setAllowFlight(this.originalAllowFlight); player.setInvulnerable(this.originalInvulnerable); player.setSwimming(this.originalIsSwimming); + removeMenuItem(player); } @Override @@ -121,4 +129,30 @@ public abstract class AbstractPlayerEntryState implements PlayerEntryState { return data; } + /** + * Removes the menu item from the given player's inventory + * + * @param player

The player to remove the menu item from

+ */ + private void removeMenuItem(Player player) { + Set itemsToRemove = new HashSet<>(); + player.getInventory().forEach((item) -> { + if (item == null) { + return; + } + ItemMeta meta = item.getItemMeta(); + if (meta == null) { + return; + } + Integer persistentData = meta.getPersistentDataContainer().get(new NamespacedKey(MiniGames.getInstance(), + PersistentDataKey.MENU_ITEM.getKeyName()), PersistentDataType.INTEGER); + if (persistentData != null && persistentData == PersistentDataKey.MENU_ITEM.getDataValue()) { + itemsToRemove.add(item); + } + }); + for (ItemStack toRemove : itemsToRemove) { + player.getInventory().remove(toRemove); + } + } + } diff --git a/src/main/java/net/knarcraft/minigames/command/MenuCommand.java b/src/main/java/net/knarcraft/minigames/command/MenuCommand.java index 9a9135e..ac7a0e4 100644 --- a/src/main/java/net/knarcraft/minigames/command/MenuCommand.java +++ b/src/main/java/net/knarcraft/minigames/command/MenuCommand.java @@ -1,9 +1,7 @@ package net.knarcraft.minigames.command; -import net.knarcraft.minigames.MiniGames; -import net.knarcraft.minigames.arena.ArenaSession; import net.knarcraft.minigames.config.Message; -import net.knarcraft.minigames.gui.MiniGamesGUI; +import net.knarcraft.minigames.util.GUIHelper; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.bukkit.command.TabExecutor; @@ -27,14 +25,8 @@ public class MenuCommand implements TabExecutor { return false; } - ArenaSession existingSession = MiniGames.getInstance().getSession(player.getUniqueId()); - if (existingSession == null) { - new MiniGamesGUI(player).openFor(player); - return false; - } else { - existingSession.getGUI().openFor(player); - return true; - } + GUIHelper.openGUI(player); + return true; } @Nullable diff --git a/src/main/java/net/knarcraft/minigames/command/dropper/JoinDropperArenaCommand.java b/src/main/java/net/knarcraft/minigames/command/dropper/JoinDropperArenaCommand.java index 05637f0..192b569 100644 --- a/src/main/java/net/knarcraft/minigames/command/dropper/JoinDropperArenaCommand.java +++ b/src/main/java/net/knarcraft/minigames/command/dropper/JoinDropperArenaCommand.java @@ -8,6 +8,7 @@ import net.knarcraft.minigames.arena.dropper.DropperArenaGroup; import net.knarcraft.minigames.arena.dropper.DropperArenaSession; import net.knarcraft.minigames.config.DropperConfiguration; import net.knarcraft.minigames.config.Message; +import net.knarcraft.minigames.util.GUIHelper; import net.knarcraft.minigames.util.PlayerTeleporter; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; @@ -101,6 +102,7 @@ public class JoinDropperArenaCommand implements CommandExecutor { newSession.getEntryState().setArenaState(); // Update visibility for the player MiniGames.getInstance().getPlayerVisibilityManager().updateHiddenPlayers(playerRegistry, player); + player.getInventory().addItem(GUIHelper.getGUIOpenItem()); player.sendMessage(Message.SUCCESS_ARENA_JOINED.getMessage()); return true; } diff --git a/src/main/java/net/knarcraft/minigames/command/parkour/JoinParkourArenaCommand.java b/src/main/java/net/knarcraft/minigames/command/parkour/JoinParkourArenaCommand.java index 1c3aa7e..2d78d1c 100644 --- a/src/main/java/net/knarcraft/minigames/command/parkour/JoinParkourArenaCommand.java +++ b/src/main/java/net/knarcraft/minigames/command/parkour/JoinParkourArenaCommand.java @@ -8,6 +8,7 @@ import net.knarcraft.minigames.arena.parkour.ParkourArenaGroup; import net.knarcraft.minigames.arena.parkour.ParkourArenaSession; import net.knarcraft.minigames.config.Message; import net.knarcraft.minigames.config.ParkourConfiguration; +import net.knarcraft.minigames.util.GUIHelper; import net.knarcraft.minigames.util.PlayerTeleporter; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; @@ -99,6 +100,7 @@ public class JoinParkourArenaCommand implements CommandExecutor { newSession.getEntryState().setArenaState(); // Update visibility for the player MiniGames.getInstance().getPlayerVisibilityManager().updateHiddenPlayers(playerRegistry, player); + player.getInventory().addItem(GUIHelper.getGUIOpenItem()); player.sendMessage(Message.SUCCESS_ARENA_JOINED.getMessage()); return true; } diff --git a/src/main/java/net/knarcraft/minigames/listener/InteractListener.java b/src/main/java/net/knarcraft/minigames/listener/InteractListener.java new file mode 100644 index 0000000..f476b68 --- /dev/null +++ b/src/main/java/net/knarcraft/minigames/listener/InteractListener.java @@ -0,0 +1,39 @@ +package net.knarcraft.minigames.listener; + +import net.knarcraft.minigames.MiniGames; +import net.knarcraft.minigames.property.PersistentDataKey; +import net.knarcraft.minigames.util.GUIHelper; +import org.bukkit.NamespacedKey; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerInteractEvent; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.ItemMeta; +import org.bukkit.persistence.PersistentDataType; + +/** + * A listener that listens for player interactions + */ +public class InteractListener implements Listener { + + @EventHandler + public void menuInteractListener(PlayerInteractEvent event) { + ItemStack item = event.getItem(); + if (item == null || !item.hasItemMeta()) { + return; + } + ItemMeta meta = item.getItemMeta(); + if (meta == null) { + return; + } + + Integer persistentData = meta.getPersistentDataContainer().get(new NamespacedKey(MiniGames.getInstance(), + PersistentDataKey.MENU_ITEM.getKeyName()), PersistentDataType.INTEGER); + + if (persistentData != null && persistentData == PersistentDataKey.MENU_ITEM.getDataValue()) { + event.setCancelled(true); + GUIHelper.openGUI(event.getPlayer()); + } + } + +} diff --git a/src/main/java/net/knarcraft/minigames/property/PersistentDataKey.java b/src/main/java/net/knarcraft/minigames/property/PersistentDataKey.java new file mode 100644 index 0000000..2965f89 --- /dev/null +++ b/src/main/java/net/knarcraft/minigames/property/PersistentDataKey.java @@ -0,0 +1,43 @@ +package net.knarcraft.minigames.property; + +/** + * An enum for all persistent data keys used by this plugin + */ +public enum PersistentDataKey { + + MENU_ITEM("MiniGamesMenu", 1799804), + ; + + private final String keyName; + private final int dataValue; + + /** + * Instantiates a new persistent data key + * + * @param keyName

The name of this key

+ * @param dataValue

The integer data value of this key

+ */ + PersistentDataKey(String keyName, int dataValue) { + this.keyName = keyName; + this.dataValue = dataValue; + } + + /** + * Gets the name of this persistent data key + * + * @return

The name of this key

+ */ + public String getKeyName() { + return this.keyName; + } + + /** + * Gets the integer data value of this persistent data key + * + * @return

The integer data value

+ */ + public int getDataValue() { + return this.dataValue; + } + +} diff --git a/src/main/java/net/knarcraft/minigames/util/GUIHelper.java b/src/main/java/net/knarcraft/minigames/util/GUIHelper.java new file mode 100644 index 0000000..90e4ecc --- /dev/null +++ b/src/main/java/net/knarcraft/minigames/util/GUIHelper.java @@ -0,0 +1,58 @@ +package net.knarcraft.minigames.util; + +import net.knarcraft.knargui.item.PlayerHeadGUIItemFactory; +import net.knarcraft.minigames.MiniGames; +import net.knarcraft.minigames.arena.ArenaSession; +import net.knarcraft.minigames.gui.MiniGamesGUI; +import net.knarcraft.minigames.property.PersistentDataKey; +import org.bukkit.ChatColor; +import org.bukkit.NamespacedKey; +import org.bukkit.entity.Player; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.ItemMeta; +import org.bukkit.persistence.PersistentDataType; + +/** + * A helper class for the in-arena GUI + */ +public final class GUIHelper { + + private GUIHelper() { + + } + + /** + * Gets the item used for opening the mini-games menu + * + * @return

The item used for opening the GUI

+ */ + public static ItemStack getGUIOpenItem() { + PlayerHeadGUIItemFactory factory = new PlayerHeadGUIItemFactory(); + factory.useSkin("3fdab40434ed5d01f58c45ca0c9fada4662e1772ff43e2974979440a5cfe15c9"); + factory.setName(ChatColor.AQUA + "§ MiniGames Menu §"); + ItemStack item = factory.build(); + ItemMeta meta = item.getItemMeta(); + if (meta != null) { + meta.getPersistentDataContainer().set(new NamespacedKey(MiniGames.getInstance(), + PersistentDataKey.MENU_ITEM.getKeyName()), + PersistentDataType.INTEGER, PersistentDataKey.MENU_ITEM.getDataValue()); + } + item.setItemMeta(meta); + return item; + } + + /** + * Opens the correct GUI for the given player + * + * @param player

The player to show a GUI for

+ */ + public static void openGUI(Player player) { + ArenaSession existingSession = MiniGames.getInstance().getSession(player.getUniqueId()); + if (existingSession == null) { + new MiniGamesGUI(player).openFor(player); + } else { + existingSession.getGUI().openFor(player); + } + } + +}