Adds a partially usable arena menu openable through a command

This commit is contained in:
Kristian Knarvik 2023-05-09 15:47:54 +02:00
parent 0704e138ec
commit 00ac0582f4
12 changed files with 371 additions and 44 deletions

View File

@ -41,6 +41,7 @@ The only permission normal players will need is `minigames.join` which is set to
|----------------------------------------|----------|-----------------------------|-------------------------------------------------------------------------------------| |----------------------------------------|----------|-----------------------------|-------------------------------------------------------------------------------------|
| /miniGamesReload | /mreload | | Reloads all data from disk. | | /miniGamesReload | /mreload | | Reloads all data from disk. |
| /miniGamesLeave | /mleave | | Leaves the current mini-game. | | /miniGamesLeave | /mleave | | Leaves the current mini-game. |
| /miniGamesMenu | /mmenu | | Shows a menu of actions if used while in an arena |
| /dropperList | /dlist | | Lists available dropper arenas. | | /dropperList | /dlist | | Lists available dropper arenas. |
| [/dropperJoin](#dropperjoin) | /djoin | \<arena> \[mode] | Joins the selected arena. | | [/dropperJoin](#dropperjoin) | /djoin | \<arena> \[mode] | Joins the selected arena. |
| /dropperCreate | /dcreate | \<name> | Creates a new dropper arena with the given name. The spawn is set to your location. | | /dropperCreate | /dcreate | \<name> | Creates a new dropper arena with the given name. The spawn is set to your location. |

24
pom.xml
View File

@ -40,6 +40,20 @@
</goals> </goals>
<configuration> <configuration>
<createDependencyReducedPom>false</createDependencyReducedPom> <createDependencyReducedPom>false</createDependencyReducedPom>
<filters>
<filter>
<artifact>net.knarcraft:knargui</artifact>
<includes>
<include>net/knarcraft/knargui/**</include>
</includes>
</filter>
<filter>
<excludes>
<exclude>*.MF</exclude>
<exclude>*.yml</exclude>
</excludes>
</filter>
</filters>
</configuration> </configuration>
</execution> </execution>
</executions> </executions>
@ -62,6 +76,10 @@
<id>placeholderapi</id> <id>placeholderapi</id>
<url>https://repo.extendedclip.com/content/repositories/placeholderapi/</url> <url>https://repo.extendedclip.com/content/repositories/placeholderapi/</url>
</repository> </repository>
<repository>
<id>knarcraft-repo</id>
<url>https://git.knarcraft.net/api/packages/EpicKnarvik97/maven</url>
</repository>
</repositories> </repositories>
<dependencies> <dependencies>
@ -89,5 +107,11 @@
<version>2.10.0</version> <version>2.10.0</version>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<dependency>
<groupId>net.knarcraft</groupId>
<artifactId>knargui</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
</dependencies> </dependencies>
</project> </project>

View File

@ -1,5 +1,6 @@
package net.knarcraft.minigames; package net.knarcraft.minigames;
import net.knarcraft.knargui.GUIListener;
import net.knarcraft.minigames.arena.ArenaPlayerRegistry; import net.knarcraft.minigames.arena.ArenaPlayerRegistry;
import net.knarcraft.minigames.arena.ArenaSession; import net.knarcraft.minigames.arena.ArenaSession;
import net.knarcraft.minigames.arena.dropper.DropperArena; import net.knarcraft.minigames.arena.dropper.DropperArena;
@ -21,6 +22,7 @@ import net.knarcraft.minigames.arena.parkour.ParkourPlayerEntryState;
import net.knarcraft.minigames.arena.record.IntegerRecord; import net.knarcraft.minigames.arena.record.IntegerRecord;
import net.knarcraft.minigames.arena.record.LongRecord; import net.knarcraft.minigames.arena.record.LongRecord;
import net.knarcraft.minigames.command.LeaveArenaCommand; import net.knarcraft.minigames.command.LeaveArenaCommand;
import net.knarcraft.minigames.command.MenuCommand;
import net.knarcraft.minigames.command.ReloadCommand; import net.knarcraft.minigames.command.ReloadCommand;
import net.knarcraft.minigames.command.dropper.CreateDropperArenaCommand; import net.knarcraft.minigames.command.dropper.CreateDropperArenaCommand;
import net.knarcraft.minigames.command.dropper.DropperGroupListCommand; import net.knarcraft.minigames.command.dropper.DropperGroupListCommand;
@ -228,6 +230,51 @@ public final class MiniGames extends JavaPlugin {
public void onEnable() { public void onEnable() {
// Plugin startup logic // Plugin startup logic
instance = this; instance = this;
// Load configuration
loadConfiguration();
// Register all listeners
registerListeners();
// Register all commands
registerCommands();
// Integrate with other plugins
doPluginIntegration();
}
@Override
public void onDisable() {
// Kill all sessions before exiting
for (DropperArena arena : dropperArenaHandler.getArenas().values()) {
dropperArenaPlayerRegistry.removeForArena(arena, true);
}
for (ParkourArena arena : parkourArenaHandler.getArenas().values()) {
parkourArenaPlayerRegistry.removeForArena(arena, true);
}
}
/**
* Sets up integration with third-party plugins
*/
private void doPluginIntegration() {
if (Bukkit.getPluginManager().getPlugin("PlaceholderAPI") != null) {
this.dropperRecordExpansion = new DropperRecordExpansion(this);
if (!this.dropperRecordExpansion.register()) {
log(Level.WARNING, "Unable to register PlaceholderAPI dropper expansion!");
}
this.parkourRecordExpansion = new ParkourRecordExpansion(this);
if (!this.parkourRecordExpansion.register()) {
log(Level.WARNING, "Unable to register PlaceholderAPI parkour expansion!");
}
}
}
/**
* Loads all configuration values used by this plugin
*/
private void loadConfiguration() {
this.saveDefaultConfig(); this.saveDefaultConfig();
getConfig().options().copyDefaults(true); getConfig().options().copyDefaults(true);
saveConfig(); saveConfig();
@ -241,56 +288,18 @@ public final class MiniGames extends JavaPlugin {
this.parkourArenaPlayerRegistry = new ParkourArenaPlayerRegistry(); this.parkourArenaPlayerRegistry = new ParkourArenaPlayerRegistry();
this.parkourArenaHandler = new ParkourArenaHandler(this.parkourArenaPlayerRegistry); this.parkourArenaHandler = new ParkourArenaHandler(this.parkourArenaPlayerRegistry);
this.parkourArenaHandler.load(); this.parkourArenaHandler.load();
}
/**
* Registers all listeners used by this plugin
*/
private void registerListeners() {
PluginManager pluginManager = getServer().getPluginManager(); PluginManager pluginManager = getServer().getPluginManager();
pluginManager.registerEvents(new DamageListener(), this); pluginManager.registerEvents(new DamageListener(), this);
pluginManager.registerEvents(new MoveListener(this.dropperConfiguration, this.parkourConfiguration), this); pluginManager.registerEvents(new MoveListener(this.dropperConfiguration, this.parkourConfiguration), this);
pluginManager.registerEvents(new PlayerStateChangeListener(), this); pluginManager.registerEvents(new PlayerStateChangeListener(), this);
pluginManager.registerEvents(new CommandListener(), this); pluginManager.registerEvents(new CommandListener(), this);
pluginManager.registerEvents(new GUIListener(false), this);
registerCommand("miniGamesReload", new ReloadCommand(), null);
registerCommand("miniGamesLeave", new LeaveArenaCommand(), null);
registerCommand("dropperCreate", new CreateDropperArenaCommand(), null);
registerCommand("dropperList", new ListDropperArenaCommand(), null);
registerCommand("dropperJoin", new JoinDropperArenaCommand(), new JoinDropperArenaTabCompleter());
registerCommand("dropperEdit", new EditDropperArenaCommand(this.dropperConfiguration), new EditDropperArenaTabCompleter());
registerCommand("dropperRemove", new RemoveDropperArenaCommand(), new RemoveDropperArenaTabCompleter());
registerCommand("dropperGroupSet", new DropperGroupSetCommand(), null);
registerCommand("dropperGroupSwap", new DropperGroupSwapCommand(), null);
registerCommand("dropperGroupList", new DropperGroupListCommand(), null);
registerCommand("parkourCreate", new CreateParkourArenaCommand(), null);
registerCommand("parkourList", new ListParkourArenaCommand(), null);
registerCommand("parkourJoin", new JoinParkourArenaCommand(), new JoinParkourArenaTabCompleter());
registerCommand("parkourEdit", new EditParkourArenaCommand(), new EditParkourArenaTabCompleter());
registerCommand("parkourRemove", new RemoveParkourArenaCommand(), new RemoveParkourArenaTabCompleter());
registerCommand("parkourGroupSet", new ParkourGroupSetCommand(), null);
registerCommand("parkourGroupSwap", new ParkourGroupSwapCommand(), null);
registerCommand("parkourGroupList", new ParkourGroupListCommand(), null);
registerCommand("parkourCheckpoint", new ParkourCheckpointCommand(), null);
if (Bukkit.getPluginManager().getPlugin("PlaceholderAPI") != null) {
this.dropperRecordExpansion = new DropperRecordExpansion(this);
if (!this.dropperRecordExpansion.register()) {
log(Level.WARNING, "Unable to register PlaceholderAPI dropper expansion!");
}
this.parkourRecordExpansion = new ParkourRecordExpansion(this);
if (!this.parkourRecordExpansion.register()) {
log(Level.WARNING, "Unable to register PlaceholderAPI parkour expansion!");
}
}
}
@Override
public void onDisable() {
// Kill all sessions before exiting
for (DropperArena arena : dropperArenaHandler.getArenas().values()) {
dropperArenaPlayerRegistry.removeForArena(arena, true);
}
for (ParkourArena arena : parkourArenaHandler.getArenas().values()) {
parkourArenaPlayerRegistry.removeForArena(arena, true);
}
} }
/** /**
@ -313,4 +322,45 @@ public final class MiniGames extends JavaPlugin {
} }
} }
/**
* Registers all commands used by this plugin
*/
private void registerCommands() {
registerCommand("miniGamesReload", new ReloadCommand(), null);
registerCommand("miniGamesLeave", new LeaveArenaCommand(), null);
registerCommand("miniGamesMenu", new MenuCommand(), null);
registerDropperCommands();
registerParkourCommands();
}
/**
* Registers all commands related to droppers
*/
private void registerDropperCommands() {
registerCommand("dropperCreate", new CreateDropperArenaCommand(), null);
registerCommand("dropperList", new ListDropperArenaCommand(), null);
registerCommand("dropperJoin", new JoinDropperArenaCommand(), new JoinDropperArenaTabCompleter());
registerCommand("dropperEdit", new EditDropperArenaCommand(this.dropperConfiguration), new EditDropperArenaTabCompleter());
registerCommand("dropperRemove", new RemoveDropperArenaCommand(), new RemoveDropperArenaTabCompleter());
registerCommand("dropperGroupSet", new DropperGroupSetCommand(), null);
registerCommand("dropperGroupSwap", new DropperGroupSwapCommand(), null);
registerCommand("dropperGroupList", new DropperGroupListCommand(), null);
}
/**
* Registers all commands related to parkour
*/
private void registerParkourCommands() {
registerCommand("parkourCreate", new CreateParkourArenaCommand(), null);
registerCommand("parkourList", new ListParkourArenaCommand(), null);
registerCommand("parkourJoin", new JoinParkourArenaCommand(), new JoinParkourArenaTabCompleter());
registerCommand("parkourEdit", new EditParkourArenaCommand(), new EditParkourArenaTabCompleter());
registerCommand("parkourRemove", new RemoveParkourArenaCommand(), new RemoveParkourArenaTabCompleter());
registerCommand("parkourGroupSet", new ParkourGroupSetCommand(), null);
registerCommand("parkourGroupSwap", new ParkourGroupSwapCommand(), null);
registerCommand("parkourGroupList", new ParkourGroupListCommand(), null);
registerCommand("parkourCheckpoint", new ParkourCheckpointCommand(), null);
}
} }

View File

@ -1,5 +1,6 @@
package net.knarcraft.minigames.arena; package net.knarcraft.minigames.arena;
import net.knarcraft.minigames.gui.ArenaGUI;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
/** /**
@ -38,4 +39,11 @@ public interface ArenaSession {
*/ */
@NotNull Arena getArena(); @NotNull Arena getArena();
/**
* Gets the GUI with this arena's options
*
* @return <p>This arena's GUI</p>
*/
@NotNull ArenaGUI getGUI();
} }

View File

@ -5,6 +5,8 @@ import net.knarcraft.minigames.arena.AbstractArenaSession;
import net.knarcraft.minigames.arena.PlayerEntryState; import net.knarcraft.minigames.arena.PlayerEntryState;
import net.knarcraft.minigames.config.DropperConfiguration; import net.knarcraft.minigames.config.DropperConfiguration;
import net.knarcraft.minigames.config.Message; import net.knarcraft.minigames.config.Message;
import net.knarcraft.minigames.gui.ArenaGUI;
import net.knarcraft.minigames.gui.DropperGUI;
import net.knarcraft.minigames.util.PlayerTeleporter; import net.knarcraft.minigames.util.PlayerTeleporter;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@ -16,6 +18,8 @@ import java.util.logging.Level;
*/ */
public class DropperArenaSession extends AbstractArenaSession { public class DropperArenaSession extends AbstractArenaSession {
private static final ArenaGUI gui = new DropperGUI();
private final @NotNull DropperArena arena; private final @NotNull DropperArena arena;
private final @NotNull Player player; private final @NotNull Player player;
private final @NotNull DropperArenaGameMode gameMode; private final @NotNull DropperArenaGameMode gameMode;
@ -101,6 +105,11 @@ public class DropperArenaSession extends AbstractArenaSession {
return this.arena; return this.arena;
} }
@Override
public @NotNull ArenaGUI getGUI() {
return gui;
}
@Override @Override
protected void removeSession() { protected void removeSession() {
// Remove this session for game sessions to stop listeners from fiddling more with the player // Remove this session for game sessions to stop listeners from fiddling more with the player

View File

@ -5,6 +5,8 @@ import net.knarcraft.minigames.arena.AbstractArenaSession;
import net.knarcraft.minigames.arena.PlayerEntryState; import net.knarcraft.minigames.arena.PlayerEntryState;
import net.knarcraft.minigames.config.Message; import net.knarcraft.minigames.config.Message;
import net.knarcraft.minigames.config.ParkourConfiguration; import net.knarcraft.minigames.config.ParkourConfiguration;
import net.knarcraft.minigames.gui.ArenaGUI;
import net.knarcraft.minigames.gui.ParkourGUI;
import net.knarcraft.minigames.util.PlayerTeleporter; import net.knarcraft.minigames.util.PlayerTeleporter;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@ -18,6 +20,8 @@ import java.util.logging.Level;
*/ */
public class ParkourArenaSession extends AbstractArenaSession { public class ParkourArenaSession extends AbstractArenaSession {
private static final ArenaGUI gui = new ParkourGUI();
private final @NotNull ParkourArena arena; private final @NotNull ParkourArena arena;
private final @NotNull Player player; private final @NotNull Player player;
private final @NotNull ParkourArenaGameMode gameMode; private final @NotNull ParkourArenaGameMode gameMode;
@ -112,6 +116,11 @@ public class ParkourArenaSession extends AbstractArenaSession {
return this.arena; return this.arena;
} }
@Override
public @NotNull ArenaGUI getGUI() {
return gui;
}
@Override @Override
protected void removeSession() { protected void removeSession() {
// Remove this session for game sessions to stop listeners from fiddling more with the player // Remove this session for game sessions to stop listeners from fiddling more with the player

View File

@ -0,0 +1,46 @@
package net.knarcraft.minigames.command;
import net.knarcraft.minigames.MiniGames;
import net.knarcraft.minigames.arena.ArenaSession;
import net.knarcraft.minigames.config.Message;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabExecutor;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.List;
/**
* The command for opening up an arena's menu
*/
public class MenuCommand implements TabExecutor {
@Override
public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s,
@NotNull String[] arguments) {
if (!(commandSender instanceof Player player)) {
commandSender.sendMessage(Message.ERROR_PLAYER_ONLY.getMessage());
return false;
}
ArenaSession existingSession = MiniGames.getInstance().getSession(player.getUniqueId());
if (existingSession == null) {
commandSender.sendMessage(Message.ERROR_NOT_IN_ARENA.getMessage());
return false;
}
existingSession.getGUI().openFor(player);
return true;
}
@Nullable
@Override
public List<String> onTabComplete(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s,
@NotNull String[] strings) {
return new ArrayList<>();
}
}

View File

@ -0,0 +1,96 @@
package net.knarcraft.minigames.gui;
import net.knarcraft.knargui.AbstractGUI;
import net.knarcraft.knargui.GUIAction;
import net.knarcraft.knargui.item.GUIItemFactory;
import net.knarcraft.minigames.MiniGames;
import net.knarcraft.minigames.arena.ArenaSession;
import net.md_5.bungee.api.ChatColor;
import org.bukkit.Material;
import org.bukkit.event.inventory.ClickType;
import org.bukkit.inventory.ItemStack;
import java.util.ArrayList;
import java.util.List;
/**
* A generic GUI for all arenas
*/
public abstract class ArenaGUI extends AbstractGUI {
/**
* 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>
*/
public ArenaGUI(int inventorySize, String inventoryName) {
super(inventorySize, inventoryName, null);
}
/**
* Gets an item describing player visibility toggling
*
* @return <p>A player toggle item</p>
*/
protected ItemStack getTogglePlayersItem() {
GUIItemFactory togglePlayersItemFactory = new GUIItemFactory(Material.PLAYER_HEAD);
List<String> loreLines = getLoreLines();
loreLines.add(ChatColor.GRAY + "Use this item to toggle the visibility");
loreLines.add(ChatColor.GRAY + "of other players");
togglePlayersItemFactory.setName(ChatColor.BLUE + "Toggle Players");
togglePlayersItemFactory.setLore(loreLines);
return togglePlayersItemFactory.build();
}
/**
* Gets an item describing a leave arena action
*
* @return <p>A leave item</p>
*/
protected ItemStack getLeaveItem() {
GUIItemFactory leaveItemFactory = new GUIItemFactory(Material.BARRIER);
List<String> loreLines = getLoreLines();
loreLines.add(ChatColor.GRAY + "Use this item to leave the arena");
leaveItemFactory.setName(ChatColor.DARK_RED + "Leave");
leaveItemFactory.setLore(loreLines);
return leaveItemFactory.build();
}
/**
* Gets an arraylist with one blank line lore-lines can be added to
*
* @return <p>An arraylist with one blank line</p>
*/
protected List<String> getLoreLines() {
List<String> loreLines = new ArrayList<>();
loreLines.add("");
return loreLines;
}
/**
* Sets a click action for both right-click and left-click
*
* @param inventorySlot <p>The inventory slot the action should be added to</p>
* @param action <p>The action to register</p>
*/
protected void setAnyClickAction(int inventorySlot, GUIAction action) {
setClickAction(inventorySlot, ClickType.LEFT, action);
setClickAction(inventorySlot, ClickType.RIGHT, action);
}
/**
* Gets the action to run when triggering the leave item
*
* @return <p>The leave action</p>
*/
protected GUIAction getLeaveAction() {
return (player) -> {
ArenaSession session = MiniGames.getInstance().getSession(player.getUniqueId());
if (session != null) {
session.triggerQuit(false);
}
};
}
}

View File

@ -0,0 +1,19 @@
package net.knarcraft.minigames.gui;
/**
* A GUI used in the dropper arena
*/
public class DropperGUI extends ArenaGUI {
/**
* Instantiates a new dropper gui
*/
public DropperGUI() {
super(9, "Dropper");
setItem(0, getTogglePlayersItem());
setItem(2, getLeaveItem());
setAnyClickAction(2, getLeaveAction());
}
}

View File

@ -0,0 +1,58 @@
package net.knarcraft.minigames.gui;
import net.knarcraft.knargui.GUIAction;
import net.knarcraft.knargui.item.GUIItemFactory;
import net.knarcraft.minigames.MiniGames;
import net.knarcraft.minigames.arena.ArenaSession;
import net.knarcraft.minigames.arena.parkour.ParkourArenaSession;
import net.md_5.bungee.api.ChatColor;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import java.util.List;
/**
* A GUI used in the parkour arena
*/
public class ParkourGUI extends ArenaGUI {
public ParkourGUI() {
super(9, "Parkour");
setItem(0, getTogglePlayersItem());
setItem(2, getGiveUpItem());
setItem(4, getLeaveItem());
setAnyClickAction(2, getGiveUpAction());
setAnyClickAction(4, getLeaveAction());
}
/**
* Gets an item describing a give up action
*
* @return <p>A give up item</p>
*/
private ItemStack getGiveUpItem() {
GUIItemFactory giveUpItemFactory = new GUIItemFactory(Material.SKELETON_SKULL);
List<String> loreLines = getLoreLines();
loreLines.add(ChatColor.GRAY + "Use this item to give up");
loreLines.add(ChatColor.GRAY + "and go to the last checkpoint");
giveUpItemFactory.setName(ChatColor.RED + "Give up");
giveUpItemFactory.setLore(loreLines);
return giveUpItemFactory.build();
}
/**
* Gets the action to run when triggering the give up item
*
* @return <p>The give up action</p>
*/
private GUIAction getGiveUpAction() {
return (player) -> {
ArenaSession session = MiniGames.getInstance().getSession(player.getUniqueId());
if (session instanceof ParkourArenaSession) {
session.triggerLoss();
}
};
}
}

View File

@ -32,6 +32,8 @@ public class CommandListener implements Listener {
allowedCommands.add("/parkourCheckpoint"); allowedCommands.add("/parkourCheckpoint");
allowedCommands.add("/pCheckpoint"); allowedCommands.add("/pCheckpoint");
allowedCommands.add("/pCheck"); allowedCommands.add("/pCheck");
allowedCommands.add("/miniGamesMenu");
allowedCommands.add("/mMenu");
String message = event.getMessage(); String message = event.getMessage();
if (!message.startsWith("/")) { if (!message.startsWith("/")) {

View File

@ -22,6 +22,11 @@ commands:
- pleave - pleave
usage: /<command> usage: /<command>
description: Used to leave the current dropper arena description: Used to leave the current dropper arena
miniGamesMenu:
aliases:
- mmenu
usage: /<command>
description: Used to display an actions menu while in an arena
dropperGroupSet: dropperGroupSet:
aliases: aliases:
- dgset - dgset