Gives players an item to open the arena menu

This commit is contained in:
Kristian Knarvik 2023-05-11 17:52:49 +02:00
parent 4513fc2de7
commit 9c91e11780
8 changed files with 183 additions and 11 deletions

View File

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

View File

@ -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 <p>The player to remove the menu item from</p>
*/
private void removeMenuItem(Player player) {
Set<ItemStack> 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);
}
}
}

View File

@ -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,15 +25,9 @@ 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);
GUIHelper.openGUI(player);
return true;
}
}
@Nullable
@Override

View File

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

View File

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

View File

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

View File

@ -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 <p>The name of this key</p>
* @param dataValue <p>The integer data value of this key</p>
*/
PersistentDataKey(String keyName, int dataValue) {
this.keyName = keyName;
this.dataValue = dataValue;
}
/**
* Gets the name of this persistent data key
*
* @return <p>The name of this key</p>
*/
public String getKeyName() {
return this.keyName;
}
/**
* Gets the integer data value of this persistent data key
*
* @return <p>The integer data value</p>
*/
public int getDataValue() {
return this.dataValue;
}
}

View File

@ -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 <p>The item used for opening the GUI</p>
*/
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 <p>The player to show a GUI for</p>
*/
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);
}
}
}