From 57b3b85fbce35f8b4b271f181be7559134e01b48 Mon Sep 17 00:00:00 2001 From: EpicKnarvik97 Date: Mon, 12 Jun 2023 23:17:48 +0200 Subject: [PATCH] Adds a new item to allow arena re-tries Prevents a NumberFormatException if the record number is invalid Updates Spigot to 1.20 --- pom.xml | 2 +- .../minigames/arena/AbstractArenaSession.java | 10 ++++- .../minigames/arena/ArenaSession.java | 5 +++ .../arena/parkour/ParkourArenaSession.java | 6 +++ .../parkour/JoinParkourArenaCommand.java | 2 +- .../net/knarcraft/minigames/gui/ArenaGUI.java | 44 +++++++++++++++---- .../knarcraft/minigames/gui/DropperGUI.java | 6 ++- .../knarcraft/minigames/gui/MiniGamesGUI.java | 4 +- .../knarcraft/minigames/gui/ParkourGUI.java | 8 ++-- .../minigames/listener/InteractListener.java | 16 +++++++ .../placeholder/RecordExpansion.java | 11 ++++- .../minigames/property/PersistentDataKey.java | 3 ++ 12 files changed, 97 insertions(+), 20 deletions(-) diff --git a/pom.xml b/pom.xml index bc3dac7..7f7c102 100644 --- a/pom.xml +++ b/pom.xml @@ -86,7 +86,7 @@ org.spigotmc spigot-api - 1.19.4-R0.1-SNAPSHOT + 1.20-R0.1-SNAPSHOT provided diff --git a/src/main/java/net/knarcraft/minigames/arena/AbstractArenaSession.java b/src/main/java/net/knarcraft/minigames/arena/AbstractArenaSession.java index c298e04..971eece 100644 --- a/src/main/java/net/knarcraft/minigames/arena/AbstractArenaSession.java +++ b/src/main/java/net/knarcraft/minigames/arena/AbstractArenaSession.java @@ -15,7 +15,7 @@ public abstract class AbstractArenaSession implements ArenaSession { private final @NotNull ArenaGameMode gameMode; private final @NotNull Player player; protected int deaths; - protected final long startTime; + protected long startTime; protected PlayerEntryState entryState; /** @@ -47,6 +47,14 @@ public abstract class AbstractArenaSession implements ArenaSession { player.sendMessage(Message.SUCCESS_ARENA_QUIT.getMessage()); } + @Override + public void reset() { + this.deaths = 0; + this.startTime = System.currentTimeMillis(); + PlayerTeleporter.teleportPlayer(this.player, this.arena.getSpawnLocation(), false, false); + this.entryState.setArenaState(); + } + /** * Announces a record set by this player * diff --git a/src/main/java/net/knarcraft/minigames/arena/ArenaSession.java b/src/main/java/net/knarcraft/minigames/arena/ArenaSession.java index 22737bb..8003b62 100644 --- a/src/main/java/net/knarcraft/minigames/arena/ArenaSession.java +++ b/src/main/java/net/knarcraft/minigames/arena/ArenaSession.java @@ -47,4 +47,9 @@ public interface ArenaSession { */ @NotNull ArenaGUI getGUI(); + /** + * Resets the session to allow a player to try again + */ + void reset(); + } diff --git a/src/main/java/net/knarcraft/minigames/arena/parkour/ParkourArenaSession.java b/src/main/java/net/knarcraft/minigames/arena/parkour/ParkourArenaSession.java index 1a1ae73..0f3d694 100644 --- a/src/main/java/net/knarcraft/minigames/arena/parkour/ParkourArenaSession.java +++ b/src/main/java/net/knarcraft/minigames/arena/parkour/ParkourArenaSession.java @@ -116,6 +116,12 @@ public class ParkourArenaSession extends AbstractArenaSession { return new ParkourGUI(player); } + @Override + public void reset() { + this.reachedCheckpoint = null; + super.reset(); + } + @Override protected void removeSession() { // Remove this session for game sessions to stop listeners from fiddling more with the player 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 1d536db..26f9792 100644 --- a/src/main/java/net/knarcraft/minigames/command/parkour/JoinParkourArenaCommand.java +++ b/src/main/java/net/knarcraft/minigames/command/parkour/JoinParkourArenaCommand.java @@ -101,7 +101,7 @@ public class JoinParkourArenaCommand implements CommandExecutor { } else { // Update the player's state to follow the arena's rules newSession.getEntryState().setArenaState(); - + player.getInventory().addItem(GUIHelper.getGUIOpenItem()); player.sendMessage(Message.SUCCESS_ARENA_JOINED.getMessage()); return true; diff --git a/src/main/java/net/knarcraft/minigames/gui/ArenaGUI.java b/src/main/java/net/knarcraft/minigames/gui/ArenaGUI.java index fdca5c8..a00dc43 100644 --- a/src/main/java/net/knarcraft/minigames/gui/ArenaGUI.java +++ b/src/main/java/net/knarcraft/minigames/gui/ArenaGUI.java @@ -21,18 +21,29 @@ import java.util.List; */ public abstract class ArenaGUI extends AbstractGUI { - protected final ArenaPlayerRegistry playerRegistry; - /** * Instantiates a new arena gui * - * @param inventorySize

The size of the GUI's inventory

- * @param inventoryName

The name of the inventory

- * @param playerRegistry

The player registry used for this GUI

+ * @param inventorySize

The size of the GUI's inventory

+ * @param inventoryName

The name of the inventory

*/ - public ArenaGUI(int inventorySize, String inventoryName, ArenaPlayerRegistry playerRegistry) { + public ArenaGUI(int inventorySize, String inventoryName) { super(inventorySize, inventoryName, null); - this.playerRegistry = playerRegistry; + } + + /** + * Gets an item describing a retry arena action + * + * @return

An arena restart item

+ */ + protected ItemStack getRestartItem() { + PlayerHeadGUIItemFactory restartItemFactory = new PlayerHeadGUIItemFactory(); + restartItemFactory.useSkin("e23b225ed0443c4eec7cf30a034490485904e6eb3d53ef2ab9e39c73bdf22c30"); + List loreLines = getLoreLines(); + loreLines.add(ChatColor.GRAY + "Use this item to retry the arena"); + restartItemFactory.setName(ChatColor.BLUE + "Retry arena"); + restartItemFactory.setLore(loreLines); + return restartItemFactory.build(); } /** @@ -108,7 +119,7 @@ public abstract class ArenaGUI extends AbstractGUI { * * @return

The leave action

*/ - protected GUIAction getLeaveAction() { + public static GUIAction getLeaveAction() { return (player) -> { ArenaSession session = MiniGames.getInstance().getSession(player.getUniqueId()); if (session != null) { @@ -117,12 +128,27 @@ public abstract class ArenaGUI extends AbstractGUI { }; } + /** + * Gets the action to run when triggering the restart action + * + * @return

The action for triggering a session restart

+ */ + public static GUIAction getRestartAction() { + return (player -> { + ArenaSession session = MiniGames.getInstance().getSession(player.getUniqueId()); + if (session != null) { + session.reset(); + } + }); + } + /** * Gets the action to run when triggering the toggle players action * + * @param playerRegistry

The registry containing relevant players

* @return

The action for triggering player visibility

*/ - protected GUIAction getTogglePlayersAction() { + public GUIAction getTogglePlayersAction(ArenaPlayerRegistry playerRegistry) { return (player) -> { PlayerVisibilityManager visibilityManager = MiniGames.getInstance().getPlayerVisibilityManager(); visibilityManager.toggleHidePlayers(playerRegistry, player); diff --git a/src/main/java/net/knarcraft/minigames/gui/DropperGUI.java b/src/main/java/net/knarcraft/minigames/gui/DropperGUI.java index a280166..acf2324 100644 --- a/src/main/java/net/knarcraft/minigames/gui/DropperGUI.java +++ b/src/main/java/net/knarcraft/minigames/gui/DropperGUI.java @@ -14,16 +14,18 @@ public class DropperGUI extends ArenaGUI { * @param player

The player the GUI is created for

*/ public DropperGUI(Player player) { - super(9, "Dropper", MiniGames.getInstance().getDropperArenaPlayerRegistry()); + super(9, "Dropper"); if (MiniGames.getInstance().getPlayerVisibilityManager().isHidingPlayers(player)) { setItem(0, getTogglePlayersItemEnabled()); } else { setItem(0, getTogglePlayersItemDisabled()); } setItem(2, getLeaveItem()); + setItem(4, getRestartItem()); - setAnyClickAction(0, getTogglePlayersAction()); + setAnyClickAction(0, getTogglePlayersAction(MiniGames.getInstance().getDropperArenaPlayerRegistry())); setAnyClickAction(2, getLeaveAction()); + setAnyClickAction(4, getRestartAction()); } } diff --git a/src/main/java/net/knarcraft/minigames/gui/MiniGamesGUI.java b/src/main/java/net/knarcraft/minigames/gui/MiniGamesGUI.java index 7b8f34e..4dca1f4 100644 --- a/src/main/java/net/knarcraft/minigames/gui/MiniGamesGUI.java +++ b/src/main/java/net/knarcraft/minigames/gui/MiniGamesGUI.java @@ -14,14 +14,14 @@ public class MiniGamesGUI extends ArenaGUI { * @param player

The player the GUI is created for

*/ public MiniGamesGUI(Player player) { - super(9, "MiniGames", null); + super(9, "MiniGames"); if (MiniGames.getInstance().getPlayerVisibilityManager().isHidingPlayers(player)) { setItem(0, getTogglePlayersItemEnabled()); } else { setItem(0, getTogglePlayersItemDisabled()); } - setAnyClickAction(0, getTogglePlayersAction()); + setAnyClickAction(0, getTogglePlayersAction(null)); } } diff --git a/src/main/java/net/knarcraft/minigames/gui/ParkourGUI.java b/src/main/java/net/knarcraft/minigames/gui/ParkourGUI.java index ac6d4d0..1783cc3 100644 --- a/src/main/java/net/knarcraft/minigames/gui/ParkourGUI.java +++ b/src/main/java/net/knarcraft/minigames/gui/ParkourGUI.java @@ -23,7 +23,7 @@ public class ParkourGUI extends ArenaGUI { * @param player

The player the GUI is created for

*/ public ParkourGUI(Player player) { - super(9, "Parkour", MiniGames.getInstance().getParkourArenaPlayerRegistry()); + super(9, "Parkour"); if (MiniGames.getInstance().getPlayerVisibilityManager().isHidingPlayers(player)) { setItem(0, getTogglePlayersItemEnabled()); } else { @@ -31,10 +31,12 @@ public class ParkourGUI extends ArenaGUI { } setItem(2, getGiveUpItem()); setItem(4, getLeaveItem()); + setItem(6, getRestartItem()); - setAnyClickAction(0, getTogglePlayersAction()); + setAnyClickAction(0, getTogglePlayersAction(MiniGames.getInstance().getParkourArenaPlayerRegistry())); setAnyClickAction(2, getGiveUpAction()); setAnyClickAction(4, getLeaveAction()); + setAnyClickAction(6, getRestartAction()); } /** @@ -57,7 +59,7 @@ public class ParkourGUI extends ArenaGUI { * * @return

The give up action

*/ - private GUIAction getGiveUpAction() { + public static GUIAction getGiveUpAction() { return (player) -> { ArenaSession session = MiniGames.getInstance().getSession(player.getUniqueId()); if (session instanceof ParkourArenaSession) { diff --git a/src/main/java/net/knarcraft/minigames/listener/InteractListener.java b/src/main/java/net/knarcraft/minigames/listener/InteractListener.java index f476b68..fe96318 100644 --- a/src/main/java/net/knarcraft/minigames/listener/InteractListener.java +++ b/src/main/java/net/knarcraft/minigames/listener/InteractListener.java @@ -1,6 +1,8 @@ package net.knarcraft.minigames.listener; import net.knarcraft.minigames.MiniGames; +import net.knarcraft.minigames.gui.ArenaGUI; +import net.knarcraft.minigames.gui.ParkourGUI; import net.knarcraft.minigames.property.PersistentDataKey; import net.knarcraft.minigames.util.GUIHelper; import org.bukkit.NamespacedKey; @@ -33,6 +35,20 @@ public class InteractListener implements Listener { if (persistentData != null && persistentData == PersistentDataKey.MENU_ITEM.getDataValue()) { event.setCancelled(true); GUIHelper.openGUI(event.getPlayer()); + return; + } + + persistentData = meta.getPersistentDataContainer().get(new NamespacedKey(MiniGames.getInstance(), + PersistentDataKey.LEAVE_ITEM.getKeyName()), PersistentDataType.INTEGER); + if (persistentData != null) { + event.setCancelled(true); + if (persistentData == PersistentDataKey.LEAVE_ITEM.getDataValue()) { + ArenaGUI.getLeaveAction().run(event.getPlayer()); + } else if (persistentData == PersistentDataKey.GIVE_UP_ITEM.getDataValue()) { + ParkourGUI.getGiveUpAction().run(event.getPlayer()); + } else if (persistentData == PersistentDataKey.TOGGLE_PLAYERS_ITEM.getDataValue()) { + //TODO: Figure out how in the world this should be done, as the existing code cannot be re-used + } } } diff --git a/src/main/java/net/knarcraft/minigames/placeholder/RecordExpansion.java b/src/main/java/net/knarcraft/minigames/placeholder/RecordExpansion.java index 9dca66c..93a0822 100644 --- a/src/main/java/net/knarcraft/minigames/placeholder/RecordExpansion.java +++ b/src/main/java/net/knarcraft/minigames/placeholder/RecordExpansion.java @@ -1,6 +1,7 @@ package net.knarcraft.minigames.placeholder; import me.clip.placeholderapi.expansion.PlaceholderExpansion; +import net.knarcraft.minigames.MiniGames; import net.knarcraft.minigames.arena.Arena; import net.knarcraft.minigames.arena.ArenaGameMode; import net.knarcraft.minigames.arena.ArenaGroup; @@ -26,6 +27,7 @@ import java.util.Objects; import java.util.Set; import java.util.UUID; import java.util.function.Supplier; +import java.util.logging.Level; /** * A placeholder expansion for parkour record placeholders @@ -71,7 +73,14 @@ public abstract class RecordExpansion extends PlaceholderExpansion { ArenaGameMode gameMode = parseGameMode(parts[2]); SelectionType selectionType = SelectionType.getFromString(parts[3]); String identifier = parts[4]; - int recordNumber = Integer.parseInt(parts[5]) - 1; + int recordNumber; + try { + recordNumber = Integer.parseInt(parts[5]) - 1; + } catch (NumberFormatException exception) { + MiniGames.log(Level.WARNING, "Invalid placeholder given. " + parts[5] + + " supplied instead of record placement."); + return parameters; + } InfoType infoType = InfoType.getFromString(parts[6]); if (recordType == null || infoType == null) { diff --git a/src/main/java/net/knarcraft/minigames/property/PersistentDataKey.java b/src/main/java/net/knarcraft/minigames/property/PersistentDataKey.java index 2965f89..a3343ab 100644 --- a/src/main/java/net/knarcraft/minigames/property/PersistentDataKey.java +++ b/src/main/java/net/knarcraft/minigames/property/PersistentDataKey.java @@ -6,6 +6,9 @@ package net.knarcraft.minigames.property; public enum PersistentDataKey { MENU_ITEM("MiniGamesMenu", 1799804), + LEAVE_ITEM("MiniGamesAction", 1799871), + GIVE_UP_ITEM("MiniGamesAction", 1799872), + TOGGLE_PLAYERS_ITEM("MiniGamesAction", 1799873), ; private final String keyName;