Adds a new item to allow arena re-tries

Prevents a NumberFormatException if the record number is invalid
Updates Spigot to 1.20
This commit is contained in:
Kristian Knarvik 2023-06-12 23:17:48 +02:00
parent 38839c0287
commit 57b3b85fbc
12 changed files with 97 additions and 20 deletions

View File

@ -86,7 +86,7 @@
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.19.4-R0.1-SNAPSHOT</version>
<version>1.20-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>

View File

@ -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
*

View File

@ -47,4 +47,9 @@ public interface ArenaSession {
*/
@NotNull ArenaGUI getGUI();
/**
* Resets the session to allow a player to try again
*/
void reset();
}

View File

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

View File

@ -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 <p>The size of the GUI's inventory</p>
* @param inventoryName <p>The name of the inventory</p>
* @param playerRegistry <p>The player registry used for this GUI</p>
*/
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 <p>An arena restart item</p>
*/
protected ItemStack getRestartItem() {
PlayerHeadGUIItemFactory restartItemFactory = new PlayerHeadGUIItemFactory();
restartItemFactory.useSkin("e23b225ed0443c4eec7cf30a034490485904e6eb3d53ef2ab9e39c73bdf22c30");
List<String> 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 <p>The leave action</p>
*/
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 <p>The action for triggering a session restart</p>
*/
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 <p>The registry containing relevant players</p>
* @return <p>The action for triggering player visibility</p>
*/
protected GUIAction getTogglePlayersAction() {
public GUIAction getTogglePlayersAction(ArenaPlayerRegistry<?> playerRegistry) {
return (player) -> {
PlayerVisibilityManager visibilityManager = MiniGames.getInstance().getPlayerVisibilityManager();
visibilityManager.toggleHidePlayers(playerRegistry, player);

View File

@ -14,16 +14,18 @@ public class DropperGUI extends ArenaGUI {
* @param player <p>The player the GUI is created for</p>
*/
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());
}
}

View File

@ -14,14 +14,14 @@ public class MiniGamesGUI extends ArenaGUI {
* @param player <p>The player the GUI is created for</p>
*/
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));
}
}

View File

@ -23,7 +23,7 @@ public class ParkourGUI extends ArenaGUI {
* @param player <p>The player the GUI is created for</p>
*/
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 <p>The give up action</p>
*/
private GUIAction getGiveUpAction() {
public static GUIAction getGiveUpAction() {
return (player) -> {
ArenaSession session = MiniGames.getInstance().getSession(player.getUniqueId());
if (session instanceof ParkourArenaSession) {

View File

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

View File

@ -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) {

View File

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