Move gamemode stuff to listener

- Fix messages in messages.yml not being used
- Fixed checkRank not working
- Added permissions support for tab handing
This commit is contained in:
Telesphoreo 2022-02-13 23:55:50 -06:00
parent bd8c772228
commit f2db87624f
21 changed files with 197 additions and 109 deletions

View File

@ -95,7 +95,6 @@ public abstract class PlexCommand extends Command implements PluginIdentifiableC
*/
protected abstract Component execute(@NotNull CommandSender sender, @Nullable Player playerSender, @NotNull String[] args);
/**
* @hidden
*/
@ -112,6 +111,7 @@ public abstract class PlexCommand extends Command implements PluginIdentifiableC
sender.sendMessage(tl("noPermissionInGame"));
return true;
}
if (commandSource == RequiredCommandSource.IN_GAME)
{
if (sender instanceof ConsoleCommandSender)
@ -119,8 +119,10 @@ public abstract class PlexCommand extends Command implements PluginIdentifiableC
send(sender, tl("noPermissionConsole"));
return true;
}
Player player = (Player)sender;
}
if (sender instanceof Player player)
{
PlexPlayer plexPlayer = PlayerCache.getPlexPlayerMap().get(player.getUniqueId());
if (plugin.getSystem().equalsIgnoreCase("ranks"))
@ -146,37 +148,6 @@ public abstract class PlexCommand extends Command implements PluginIdentifiableC
return true;
}
}
if (commandSource == RequiredCommandSource.ANY)
{
if (sender instanceof Player player)
{
PlexPlayer plexPlayer = PlayerCache.getPlexPlayerMap().get(player.getUniqueId());
if (plugin.getSystem().equalsIgnoreCase("ranks"))
{
if (!plexPlayer.getRankFromString().isAtLeast(getLevel()))
{
send(sender, tl("noPermissionRank", ChatColor.stripColor(getLevel().getLoginMSG())));
return true;
}
}
else if (plugin.getSystem().equalsIgnoreCase("permissions"))
{
if (!player.hasPermission(perms.permission()))
{
send(sender, tl("noPermissionNode", perms.permission()));
return true;
}
}
else
{
PlexLog.error("Neither permissions or ranks were selected to be used in the configuration file!");
send(sender, "There is a server misconfiguration. Please alert a developer or the owner");
return true;
}
}
}
try
{
Component component = this.execute(sender, isConsole(sender) ? null : (Player)sender, args);
@ -194,7 +165,6 @@ public abstract class PlexCommand extends Command implements PluginIdentifiableC
return true;
}
/**
* Checks if the string given is a command string
*
@ -268,7 +238,6 @@ public abstract class PlexCommand extends Command implements PluginIdentifiableC
if (!isConsole(sender))
{
return checkRank((Player)sender, rank, permission);
// return true;
}
return true;
}
@ -284,10 +253,14 @@ public abstract class PlexCommand extends Command implements PluginIdentifiableC
*/
protected boolean checkRank(Player player, Rank rank, String permission)
{
if (player instanceof ConsoleCommandSender)
{
return true;
}
PlexPlayer plexPlayer = getPlexPlayer(player);
if (plugin.getSystem().equalsIgnoreCase("ranks"))
{
if (!plexPlayer.getRank().equals(rank.toString()))
if (!plexPlayer.getRankFromString().isAtLeast(getLevel()))
{
throw new CommandFailException(PlexUtils.tl("noPermissionRank", ChatColor.stripColor(rank.getLoginMSG())));
}
@ -302,6 +275,48 @@ public abstract class PlexCommand extends Command implements PluginIdentifiableC
return true;
}
/**
* Checks whether a sender has enough permissions or is high enough a rank
*
* @param sender The player object
* @param rank The rank to check (if the server is using ranks)
* @param permission The permission to check (if the server is using permissions)
* @return true if the sender has enough permissions
* @see Rank
*/
protected boolean checkTab(CommandSender sender, Rank rank, String permission)
{
if (!isConsole(sender))
{
return checkTab((Player)sender, rank, permission);
}
return true;
}
/**
* Checks whether a player has enough permissions or is high enough a rank
*
* @param player The player object
* @param rank The rank to check (if the server is using ranks)
* @param permission The permission to check (if the server is using permissions)
* @return true if the sender has enough permissions
* @see Rank
*/
protected boolean checkTab(Player player, Rank rank, String permission)
{
PlexPlayer plexPlayer = getPlexPlayer(player);
if (plugin.getSystem().equalsIgnoreCase("ranks"))
{
return rank.isAtLeast(plexPlayer.getRankFromString());
}
else if (plugin.getSystem().equalsIgnoreCase("permissions"))
{
return player.hasPermission(permission);
}
return true;
}
/**
* Checks if a player is an admin
*
@ -412,12 +427,22 @@ public abstract class PlexCommand extends Command implements PluginIdentifiableC
/**
* Converts usage to a component
*
* @param s The usage to convert
* @return A kyori component stating the usage
* @return A Kyori component stating the usage
*/
protected Component usage()
{
return componentFromString(ChatColor.YELLOW + "Correct Usage: " + ChatColor.GRAY + this.getUsage());
}
/**
* Converts usage to a component
*
* s The usage to convert
* @return A Kyori component stating the usage
*/
protected Component usage(String s)
{
return componentFromString(ChatColor.YELLOW + "Correct Usage: " + ChatColor.GRAY + s);
return componentFromString(ChatColor.YELLOW + "Correct Usage: " + ChatColor.GRAY + this.getUsage());
}
protected Player getNonNullPlayer(String name)

View File

@ -36,7 +36,7 @@ public class AdminCMD extends PlexCommand
{
if (args.length == 0)
{
return usage(getUsage());
return usage();
}
if (args[0].equalsIgnoreCase("add"))
@ -64,8 +64,6 @@ public class AdminCMD extends PlexCommand
return tl("playerIsAdmin");
}
plexPlayer.setRank(Rank.ADMIN.name());
DataUtils.update(plexPlayer);
Bukkit.getServer().getPluginManager().callEvent(new AdminAddEvent(sender, plexPlayer));
return null;
}
@ -94,8 +92,6 @@ public class AdminCMD extends PlexCommand
return tl("playerNotAdmin");
}
plexPlayer.setRank("");
DataUtils.update(plexPlayer);
Bukkit.getServer().getPluginManager().callEvent(new AdminRemoveEvent(sender, plexPlayer));
return null;
}
@ -138,9 +134,6 @@ public class AdminCMD extends PlexCommand
return tl("playerNotAdmin");
}
plexPlayer.setRank(rank.name().toLowerCase());
DataUtils.update(plexPlayer);
Bukkit.getServer().getPluginManager().callEvent(new AdminSetRankEvent(sender, plexPlayer, rank));
return null;

View File

@ -6,6 +6,7 @@ import dev.plex.command.annotation.CommandParameters;
import dev.plex.command.annotation.CommandPermissions;
import dev.plex.command.exception.CommandFailException;
import dev.plex.command.source.RequiredCommandSource;
import dev.plex.event.GameModeUpdateEvent;
import dev.plex.rank.enums.Rank;
import dev.plex.util.PlexUtils;
import java.util.List;
@ -21,7 +22,6 @@ import org.jetbrains.annotations.Nullable;
@CommandParameters(name = "adventure", aliases = "gma", description = "Set your own or another player's gamemode to adventure mode")
public class AdventureCMD extends PlexCommand
{
@Override
protected Component execute(@NotNull CommandSender sender, @Nullable Player playerSender, String[] args)
{
@ -29,28 +29,27 @@ public class AdventureCMD extends PlexCommand
{
if (isConsole(sender))
{
throw new CommandFailException("You must define a player when using the console!");
throw new CommandFailException(PlexUtils.tl("consoleMustDefinePlayer"));
}
playerSender.setGameMode(GameMode.ADVENTURE);
return tl("gameModeSetTo", "adventure");
Bukkit.getServer().getPluginManager().callEvent(new GameModeUpdateEvent(sender, playerSender, GameMode.ADVENTURE));
return null;
}
if (checkRank(playerSender, Rank.ADMIN, "plex.gamemode.adventure.others"))
if (checkRank(sender, Rank.ADMIN, "plex.gamemode.adventure.others"))
{
if (args[0].equals("-a"))
{
for (Player targetPlayer : Bukkit.getServer().getOnlinePlayers())
{
targetPlayer.setGameMode(GameMode.ADVENTURE);
tl("gameModeSetTo", "adventure");
}
return tl("gameModeSetTo", "adventure");
PlexUtils.broadcast(tl("setEveryoneGameMode", sender.getName(), "adventure"));
return null;
}
Player nPlayer = getNonNullPlayer(args[0]);
// use send
send(nPlayer, tl("playerSetOtherGameMode", sender.getName(), "adventure"));
nPlayer.setGameMode(GameMode.ADVENTURE);
return tl("setOtherPlayerGameModeTo", nPlayer.getName(), "adventure");
Bukkit.getServer().getPluginManager().callEvent(new GameModeUpdateEvent(sender, nPlayer, GameMode.ADVENTURE));
}
return null;
}
@ -58,7 +57,7 @@ public class AdventureCMD extends PlexCommand
@Override
public @NotNull List<String> tabComplete(@NotNull CommandSender sender, @NotNull String alias, @NotNull String[] args) throws IllegalArgumentException
{
if (isAdmin(sender))
if (checkTab(sender, Rank.ADMIN, "plex.gamemode.adventure.others"))
{
return PlexUtils.getPlayerNameList();
}

View File

@ -36,7 +36,7 @@ public class BanCMD extends PlexCommand
{
if (args.length == 0)
{
return usage(getUsage());
return usage();
}
UUID targetUUID = PlexUtils.getFromName(args[0]);

View File

@ -6,6 +6,7 @@ import dev.plex.command.annotation.CommandParameters;
import dev.plex.command.annotation.CommandPermissions;
import dev.plex.command.exception.CommandFailException;
import dev.plex.command.source.RequiredCommandSource;
import dev.plex.event.GameModeUpdateEvent;
import dev.plex.rank.enums.Rank;
import dev.plex.util.PlexUtils;
import java.util.List;
@ -28,28 +29,31 @@ public class CreativeCMD extends PlexCommand
{
if (isConsole(sender))
{
throw new CommandFailException("You must define a player when using the console!");
throw new CommandFailException(PlexUtils.tl("consoleMustDefinePlayer"));
}
playerSender.setGameMode(GameMode.CREATIVE);
return tl("gameModeSetTo", "creative");
if (!(playerSender == null))
{
Bukkit.getServer().getPluginManager().callEvent(new GameModeUpdateEvent(sender, playerSender.getPlayer(), GameMode.CREATIVE));
}
return null;
}
if (checkRank(playerSender, Rank.ADMIN, "plex.gamemode.creative.others"))
if (checkRank(sender, Rank.ADMIN, "plex.gamemode.creative.others"))
{
if (args[0].equals("-a"))
{
for (Player targetPlayer : Bukkit.getServer().getOnlinePlayers())
{
targetPlayer.setGameMode(GameMode.CREATIVE);
tl("gameModeSetTo", "creative");
}
return tl("gameModeSetTo", "creative");
PlexUtils.broadcast(tl("setEveryoneGameMode", sender.getName(), "creative"));
return null;
}
Player nPlayer = getNonNullPlayer(args[0]);
// use send
send(nPlayer, tl("playerSetOtherGameMode", sender.getName(), "creative"));
nPlayer.setGameMode(GameMode.CREATIVE);
return tl("setOtherPlayerGameModeTo", nPlayer.getName(), "creative");
Bukkit.getServer().getPluginManager().callEvent(new GameModeUpdateEvent(sender, nPlayer, GameMode.CREATIVE));
return null;
}
return null;
}
@ -57,7 +61,7 @@ public class CreativeCMD extends PlexCommand
@Override
public @NotNull List<String> tabComplete(@NotNull CommandSender sender, @NotNull String alias, @NotNull String[] args) throws IllegalArgumentException
{
if (isAdmin(sender))
if (checkTab(sender, Rank.ADMIN, "plex.gamemode.creative.others"))
{
return PlexUtils.getPlayerNameList();
}

View File

@ -22,7 +22,7 @@ public class DeopCMD extends PlexCommand
{
if (args.length != 1)
{
return usage(getUsage());
return usage();
}
Player player = getNonNullPlayer(args[0]);
player.setOp(false);

View File

@ -28,7 +28,7 @@ public class FreezeCMD extends PlexCommand
{
if (args.length != 1)
{
return usage(getUsage());
return usage();
}
Player player = getNonNullPlayer(args[0]);
PunishedPlayer punishedPlayer = PlayerCache.getPunishedPlayer(player.getUniqueId());

View File

@ -32,7 +32,7 @@ public class NameHistoryCMD extends PlexCommand
{
if (args.length != 1)
{
return usage(getUsage());
return usage();
}
String username = args[0];

View File

@ -22,7 +22,7 @@ public class OpCMD extends PlexCommand
{
if (args.length != 1)
{
return usage(getUsage());
return usage();
}
Player player = getNonNullPlayer(args[0]);
player.setOp(true);

View File

@ -53,7 +53,7 @@ public class PlexCMD extends PlexCommand
}
else
{
return usage(getUsage());
return usage();
}
return null;
}

View File

@ -6,6 +6,7 @@ import dev.plex.command.annotation.CommandParameters;
import dev.plex.command.annotation.CommandPermissions;
import dev.plex.command.exception.CommandFailException;
import dev.plex.command.source.RequiredCommandSource;
import dev.plex.event.GameModeUpdateEvent;
import dev.plex.rank.enums.Rank;
import dev.plex.util.PlexUtils;
import java.util.List;
@ -28,28 +29,27 @@ public class SpectatorCMD extends PlexCommand
{
if (isConsole(sender))
{
throw new CommandFailException("You must define a player when using the console!");
throw new CommandFailException(PlexUtils.tl("consoleMustDefinePlayer"));
}
playerSender.setGameMode(GameMode.SPECTATOR);
return tl("gameModeSetTo", "spectator");
Bukkit.getServer().getPluginManager().callEvent(new GameModeUpdateEvent(sender, playerSender, GameMode.SPECTATOR));
return null;
}
if (checkRank(playerSender, Rank.ADMIN, "plex.gamemode.spectator.others"))
if (checkRank(sender, Rank.ADMIN, "plex.gamemode.spectator.others"))
{
if (args[0].equals("-a"))
{
for (Player targetPlayer : Bukkit.getServer().getOnlinePlayers())
{
targetPlayer.setGameMode(GameMode.SPECTATOR);
tl("gameModeSetTo", "spectator");
}
return tl("gameModeSetTo", "spectator");
PlexUtils.broadcast(tl("setEveryoneGameMode", sender.getName(), "spectator"));
return null;
}
Player nPlayer = getNonNullPlayer(args[0]);
// use send
send(nPlayer, tl("playerSetOtherGameMode", sender.getName(), "spectator"));
nPlayer.setGameMode(GameMode.SPECTATOR);
return tl("setOtherPlayerGameModeTo", nPlayer.getName(), "spectator");
Bukkit.getServer().getPluginManager().callEvent(new GameModeUpdateEvent(sender, nPlayer, GameMode.SPECTATOR));
}
return null;
}
@ -57,7 +57,7 @@ public class SpectatorCMD extends PlexCommand
@Override
public @NotNull List<String> tabComplete(@NotNull CommandSender sender, @NotNull String alias, @NotNull String[] args) throws IllegalArgumentException
{
if (isAdmin(sender))
if (checkTab(sender, Rank.ADMIN, "plex.gamemode.spectator.others"))
{
return PlexUtils.getPlayerNameList();
}

View File

@ -6,6 +6,7 @@ import dev.plex.command.annotation.CommandParameters;
import dev.plex.command.annotation.CommandPermissions;
import dev.plex.command.exception.CommandFailException;
import dev.plex.command.source.RequiredCommandSource;
import dev.plex.event.GameModeUpdateEvent;
import dev.plex.rank.enums.Rank;
import dev.plex.util.PlexUtils;
import java.util.List;
@ -28,28 +29,27 @@ public class SurvivalCMD extends PlexCommand
{
if (isConsole(sender))
{
throw new CommandFailException("You must define a player when using the console!");
throw new CommandFailException(PlexUtils.tl("consoleMustDefinePlayer"));
}
playerSender.setGameMode(GameMode.SURVIVAL);
return tl("gameModeSetTo", "survival");
Bukkit.getServer().getPluginManager().callEvent(new GameModeUpdateEvent(sender, playerSender, GameMode.SURVIVAL));
}
if (checkRank(playerSender, Rank.ADMIN, "plex.gamemode.survival.others"))
if (checkRank(sender, Rank.ADMIN, "plex.gamemode.survival.others"))
{
if (args[0].equals("-a"))
{
for (Player targetPlayer : Bukkit.getServer().getOnlinePlayers())
{
targetPlayer.setGameMode(GameMode.SURVIVAL);
send(targetPlayer, tl("gameModeSetTo", "survival"));
}
return tl("gameModeSetTo", "survival");
PlexUtils.broadcast(tl("setEveryoneGameMode", sender.getName(), "survival"));
return null;
}
Player nPlayer = getNonNullPlayer(args[0]);
// use send
send(nPlayer, tl("playerSetOtherGameMode", sender.getName(), "survival"));
nPlayer.setGameMode(GameMode.SURVIVAL);
return tl("setOtherPlayerGameModeTo", nPlayer.getName(), "survival");
Bukkit.getServer().getPluginManager().callEvent(new GameModeUpdateEvent(sender, nPlayer, GameMode.SURVIVAL));
return null;
}
return null;
}
@ -57,7 +57,7 @@ public class SurvivalCMD extends PlexCommand
@Override
public @NotNull List<String> tabComplete(@NotNull CommandSender sender, @NotNull String alias, @NotNull String[] args) throws IllegalArgumentException
{
if (isAdmin(sender))
if (checkTab(sender, Rank.ADMIN, "plex.gamemode.survival.others"))
{
return PlexUtils.getPlayerNameList();
}

View File

@ -28,7 +28,7 @@ public class UnbanCMD extends PlexCommand
{
if (args.length == 0)
{
return usage(getUsage());
return usage();
}
if (args.length == 1)

View File

@ -25,7 +25,7 @@ public class UnfreezeCMD extends PlexCommand
{
if (args.length != 1)
{
return usage(getUsage());
return usage();
}
Player player = getNonNullPlayer(args[0]);
PunishedPlayer punishedPlayer = PlayerCache.getPunishedPlayer(player.getUniqueId());
@ -40,6 +40,6 @@ public class UnfreezeCMD extends PlexCommand
@Override
public @NotNull List<String> tabComplete(@NotNull CommandSender sender, @NotNull String alias, @NotNull String[] args) throws IllegalArgumentException
{
return args.length == 1 && isAdmin(sender) ? PlexUtils.getPlayerNameList() : ImmutableList.of();
return args.length == 1 && checkTab(sender, Rank.ADMIN, "plex.unfreeze") ? PlexUtils.getPlayerNameList() : ImmutableList.of();
}
}

View File

@ -26,7 +26,7 @@ public class WorldCMD extends PlexCommand
{
if (args.length != 1)
{
return usage(getUsage());
return usage();
}
World world = getNonNullWorld(args[0]);
playerSender.teleportAsync(new Location(world, 0, world.getHighestBlockYAt(0, 0) + 1, 0, 0, 0));

View File

@ -30,7 +30,6 @@ public class AdminSetRankEvent extends Event
*/
private final Rank rank;
@Override
public HandlerList getHandlers()
{

View File

@ -0,0 +1,32 @@
package dev.plex.event;
import lombok.Data;
import org.bukkit.GameMode;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
import org.jetbrains.annotations.NotNull;
@Data
public class GameModeUpdateEvent extends Event
{
private static final HandlerList handlers = new HandlerList();
private final CommandSender sender;
private final Player player;
private final GameMode gameMode;
@Override
public @NotNull HandlerList getHandlers()
{
return handlers;
}
public static HandlerList getHandlerList()
{
return handlers;
}
}

View File

@ -7,6 +7,7 @@ import dev.plex.listener.impl.BanListener;
import dev.plex.listener.impl.ChatListener;
import dev.plex.listener.impl.CommandListener;
import dev.plex.listener.impl.FreezeListener;
import dev.plex.listener.impl.GameModeListener;
import dev.plex.listener.impl.PlayerListener;
import dev.plex.listener.impl.ServerListener;
import dev.plex.listener.impl.WorldListener;
@ -19,14 +20,15 @@ public class ListenerHandler
public ListenerHandler()
{
List<PlexListener> listeners = Lists.newArrayList();
listeners.add(new ServerListener());
listeners.add(new ChatListener());
listeners.add(new CommandListener());
listeners.add(new PlayerListener());
listeners.add(new WorldListener());
listeners.add(new FreezeListener());
listeners.add(new AdminListener());
listeners.add(new BanListener());
listeners.add(new ChatListener());
listeners.add(new CommandListener());
listeners.add(new FreezeListener());
listeners.add(new GameModeListener());
listeners.add(new PlayerListener());
listeners.add(new ServerListener());
listeners.add(new WorldListener());
PlexLog.log(String.format("Registered %s listeners!", listeners.size()));
}
}

View File

@ -1,5 +1,6 @@
package dev.plex.listener.impl;
import dev.plex.cache.DataUtils;
import dev.plex.event.AdminAddEvent;
import dev.plex.event.AdminRemoveEvent;
import dev.plex.event.AdminSetRankEvent;
@ -17,7 +18,8 @@ public class AdminListener extends PlexListener
{
String userSender = event.getSender().getName();
PlexPlayer target = event.getPlexPlayer();
target.setRank(Rank.ADMIN.name());
DataUtils.update(target);
PlexUtils.broadcast(tl("newAdminAdded", userSender, target.getName()));
}
@ -26,7 +28,8 @@ public class AdminListener extends PlexListener
{
String userSender = event.getSender().getName();
PlexPlayer target = event.getPlexPlayer();
target.setRank("");
DataUtils.update(target);
PlexUtils.broadcast(tl("adminRemoved", userSender, target.getName()));
}
@ -36,7 +39,8 @@ public class AdminListener extends PlexListener
String userSender = event.getSender().getName();
PlexPlayer target = event.getPlexPlayer();
Rank newRank = event.getRank();
PlexUtils.broadcast(tl("adminSetRank", userSender, target.getName(), newRank.name().toUpperCase()));
target.setRank(newRank.name().toLowerCase());
DataUtils.update(target);
PlexUtils.broadcast(tl("adminSetRank", userSender, target.getName(), newRank.getReadableString()));
}
}

View File

@ -0,0 +1,29 @@
package dev.plex.listener.impl;
import dev.plex.event.GameModeUpdateEvent;
import dev.plex.listener.PlexListener;
import dev.plex.util.PlexUtils;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
public class GameModeListener extends PlexListener
{
@EventHandler
public void onGameModeUpdate(GameModeUpdateEvent event)
{
CommandSender userSender = event.getSender();
Player target = event.getPlayer();
if (userSender.getName().equals(target.getName()))
{
target.setGameMode(event.getGameMode());
userSender.sendMessage(PlexUtils.tl("gameModeSetTo", event.getGameMode().toString().toLowerCase()));
}
else
{
target.sendMessage(PlexUtils.tl("playerSetOtherGameMode", userSender.getName(), event.getGameMode().toString().toLowerCase()));
target.setGameMode(event.getGameMode());
userSender.sendMessage(PlexUtils.tl("setOtherPlayerGameModeTo", target.getName(), event.getGameMode().toString().toLowerCase()));
}
}
}

View File

@ -62,6 +62,7 @@ nameHistoryDoesntExist: "<e>Couldn't find this user! Please check if your spelli
gameModeSetTo: "Your gamemode has been set to <v>."
setOtherPlayerGameModeTo: "You set <v>'s gamemode to <v>."
playerSetOtherGameMode: "<v> set your gamemode to <v>."
setEveryoneGameMode: "<b><v> - Changing everyone's gamemode to <v>"
consoleMustDefinePlayer: "You must define a player since you are running this command from console."
newAdminAdded: "<b><v> - Adding <v> to the admin list"
adminRemoved: "<e><v> - Removing <v> from the admin list"