command stuff

- exceptions used now
- use CommandSource over CommandSender as it will generally be more versatile
This commit is contained in:
Super_ 2020-11-02 19:19:26 -05:00
parent 3517c05054
commit 57dbafb385
16 changed files with 195 additions and 100 deletions

View File

@ -1,13 +1,13 @@
package me.totalfreedom.plex.command;
import org.bukkit.command.CommandSender;
import me.totalfreedom.plex.command.source.CommandSource;
import java.util.List;
public interface IPlexCommand
{
void execute(CommandSender sender, String[] args);
List<String> onTabComplete(CommandSender sender, String[] args);
void execute(CommandSource sender, String[] args);
List<String> onTabComplete(CommandSource sender, String[] args);
}

View File

@ -3,16 +3,26 @@ package me.totalfreedom.plex.command;
import com.google.common.collect.ImmutableList;
import me.totalfreedom.plex.Plex;
import me.totalfreedom.plex.cache.PlayerCache;
import me.totalfreedom.plex.command.annotations.CommandParameters;
import me.totalfreedom.plex.command.annotations.CommandPermissions;
import me.totalfreedom.plex.command.annotation.CommandParameters;
import me.totalfreedom.plex.command.annotation.CommandPermissions;
import me.totalfreedom.plex.command.exception.CommandArgumentException;
import me.totalfreedom.plex.command.exception.CommandFailException;
import me.totalfreedom.plex.command.exception.PlayerNotFoundException;
import me.totalfreedom.plex.command.source.CommandSource;
import me.totalfreedom.plex.command.source.RequiredCommandSource;
import me.totalfreedom.plex.player.PlexPlayer;
import me.totalfreedom.plex.rank.enums.Rank;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.World;
import org.bukkit.command.*;
import org.bukkit.entity.Player;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;
import static me.totalfreedom.plex.util.PlexUtils.tl;
public abstract class PlexCommand extends Command implements TabExecutor, IPlexCommand
{
@ -22,6 +32,7 @@ public abstract class PlexCommand extends Command implements TabExecutor, IPlexC
private final CommandPermissions perms;
private final Rank level;
private CommandSource sender;
private final RequiredCommandSource commandSource;
public PlexCommand(String name)
@ -56,69 +67,61 @@ public abstract class PlexCommand extends Command implements TabExecutor, IPlexC
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)
{
if (!matches(label)) return false;
if (commandSource == RequiredCommandSource.CONSOLE)
if (this.sender == null)
this.sender = new CommandSource(sender);
if (commandSource == RequiredCommandSource.CONSOLE && sender instanceof Player)
{
if (sender instanceof Player)
{
//TODO: Enter console only msg
return true;
}
execute(sender, args);
send(tl("noPermissionInGame"));
return true;
} else if (commandSource == RequiredCommandSource.IN_GAME)
}
if (commandSource == RequiredCommandSource.IN_GAME)
{
if (!(sender instanceof Player))
if (sender instanceof ConsoleCommandSender)
{
//TODO: Enter player only msg
send(tl("noPermissionConsole"));
return true;
}
Player player = (Player) sender;
PlexPlayer plexPlayer = PlayerCache.getPlexPlayerMap().get(player.getUniqueId());
if (!plexPlayer.getRankFromString().isAtLeast(getLevel()))
{
//TODO: Enter <insert level> only and higher msg
return true;
}
execute(sender, args);
return true;
} else {
if (!(sender instanceof Player))
{
execute(sender, args);
return true;
} else {
Player player = (Player) sender;
PlexPlayer plexPlayer = PlayerCache.getPlexPlayerMap().get(player.getUniqueId());
if (!plexPlayer.getRankFromString().isAtLeast(getLevel()))
{
//TODO: Enter <insert level> only and higher msg
return true;
}
execute(sender, args);
send(tl("noPermissionRank", ChatColor.stripColor(getLevel().getLoginMSG())));
return true;
}
}
try
{
execute(this.sender, args);
}
catch (CommandArgumentException ex)
{
send(getUsage().replace("<command>", getLabel()));
}
catch (PlayerNotFoundException | CommandFailException ex)
{
send(ex.getMessage());
}
return true;
}
@Override
public List<String> tabComplete(CommandSender sender, String alias, String[] args)
{
if (!matches(alias)) return ImmutableList.of();
if (this.sender == null)
this.sender = new CommandSource(sender);
if (sender instanceof Player)
{
Player player = (Player) sender;
PlexPlayer plexPlayer = PlayerCache.getPlexPlayerMap().get(player.getUniqueId());
if (plexPlayer.getRankFromString().isAtLeast(getLevel()))
{
return onTabComplete(sender, args);
return onTabComplete(this.sender, args);
} else {
return ImmutableList.of();
}
} else {
return onTabComplete(sender, args);
return onTabComplete(this.sender, args);
}
}
@ -146,6 +149,55 @@ public abstract class PlexCommand extends Command implements TabExecutor, IPlexC
return false;
}
protected void send(String s, CommandSource sender)
{
sender.send(s);
}
protected void send(String s, Player player)
{
player.sendMessage(s);
}
protected void send(String s)
{
if (sender == null)
return;
send(s, sender);
}
protected Player getNonNullPlayer(String name)
{
Player player = Bukkit.getPlayer(name);
if (player == null)
throw new PlayerNotFoundException();
return player;
}
protected PlexPlayer getOnlinePlexPlayer(String name)
{
Player player = getNonNullPlayer(name);
PlexPlayer plexPlayer = PlayerCache.getPlexPlayer(player.getUniqueId());
if (plexPlayer == null)
throw new PlayerNotFoundException();
return plexPlayer;
}
protected PlexPlayer getOfflinePlexPlayer(UUID uuid)
{
PlexPlayer plexPlayer = PlayerCache.getPlexPlayer(uuid);
if (plexPlayer == null)
throw new PlayerNotFoundException();
return plexPlayer;
}
protected World getNonNullWorld(String name)
{
World world = Bukkit.getWorld(name);
if (world == null)
throw new CommandFailException(tl("worldNotFound"));
return world;
}
public Rank getLevel()
{

View File

@ -1,4 +1,4 @@
package me.totalfreedom.plex.command.annotations;
package me.totalfreedom.plex.command.annotation;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

View File

@ -1,4 +1,4 @@
package me.totalfreedom.plex.command.annotations;
package me.totalfreedom.plex.command.annotation;
import me.totalfreedom.plex.command.source.RequiredCommandSource;
import me.totalfreedom.plex.rank.enums.Rank;

View File

@ -0,0 +1,3 @@
package me.totalfreedom.plex.command.exception;
public class CommandArgumentException extends RuntimeException {} // lolololol

View File

@ -0,0 +1,9 @@
package me.totalfreedom.plex.command.exception;
public class CommandFailException extends RuntimeException // this is literally just a runtime exception lol
{
public CommandFailException(String s)
{
super(s);
}
}

View File

@ -0,0 +1,11 @@
package me.totalfreedom.plex.command.exception;
import static me.totalfreedom.plex.util.PlexUtils.tl;
public class PlayerNotFoundException extends RuntimeException
{
public PlayerNotFoundException()
{
super(tl("playerNotFound"));
}
}

View File

@ -2,19 +2,23 @@ package me.totalfreedom.plex.command.impl;
import me.totalfreedom.plex.cache.PlayerCache;
import me.totalfreedom.plex.command.PlexCommand;
import me.totalfreedom.plex.command.annotations.CommandParameters;
import me.totalfreedom.plex.command.annotations.CommandPermissions;
import me.totalfreedom.plex.command.annotation.CommandParameters;
import me.totalfreedom.plex.command.annotation.CommandPermissions;
import me.totalfreedom.plex.command.exception.CommandArgumentException;
import me.totalfreedom.plex.command.exception.CommandFailException;
import me.totalfreedom.plex.command.source.CommandSource;
import me.totalfreedom.plex.command.source.RequiredCommandSource;
import me.totalfreedom.plex.util.PlexUtils;
import me.totalfreedom.plex.world.BlockMapChunkGenerator;
import me.totalfreedom.plex.world.CustomWorld;
import org.bukkit.*;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.*;
import org.bukkit.scheduler.BukkitRunnable;
import java.util.*;
import static me.totalfreedom.plex.util.PlexUtils.tl;
@CommandParameters(description = "Subliminal message.")
@CommandPermissions(source = RequiredCommandSource.IN_GAME)
public class FionnCMD extends PlexCommand
@ -28,14 +32,12 @@ public class FionnCMD extends PlexCommand
}
@Override
public void execute(CommandSender sender, String[] args)
public void execute(CommandSource sender, String[] args)
{
Player player = (Player) sender;
if (!player.getUniqueId().equals(UUID.fromString("9aa3eda6-c271-440a-a578-a952ee9aee2f")))
{
sender.sendMessage(ChatColor.RED + "You cannot run this command!");
return;
}
if (!sender.getPlayer().getUniqueId().equals(UUID.fromString("9aa3eda6-c271-440a-a578-a952ee9aee2f")))
throw new CommandFailException(tl("noPermission"));
if (args.length != 0)
throw new CommandArgumentException();
String name = "fionn";
LinkedHashMap<Material, Integer> map = new LinkedHashMap<>();
map.put(Material.CRIMSON_NYLIUM, 1);
@ -115,7 +117,7 @@ public class FionnCMD extends PlexCommand
}
@Override
public List<String> onTabComplete(CommandSender sender, String[] args)
public List<String> onTabComplete(CommandSource sender, String[] args)
{
return null;
}

View File

@ -1,12 +1,12 @@
package me.totalfreedom.plex.command.impl;
import me.totalfreedom.plex.command.PlexCommand;
import me.totalfreedom.plex.command.annotations.CommandParameters;
import me.totalfreedom.plex.command.annotations.CommandPermissions;
import me.totalfreedom.plex.command.annotation.CommandParameters;
import me.totalfreedom.plex.command.annotation.CommandPermissions;
import me.totalfreedom.plex.command.source.CommandSource;
import me.totalfreedom.plex.rank.enums.Rank;
import me.totalfreedom.plex.util.PlexUtils;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import java.util.List;
@ -23,7 +23,7 @@ public class OpAllCMD extends PlexCommand
}
@Override
public void execute(CommandSender sender, String[] args)
public void execute(CommandSource sender, String[] args)
{
for (Player player : Bukkit.getOnlinePlayers())
player.setOp(true);
@ -31,7 +31,7 @@ public class OpAllCMD extends PlexCommand
}
@Override
public List<String> onTabComplete(CommandSender sender, String[] args) {
public List<String> onTabComplete(CommandSource sender, String[] args) {
return null;
}
}

View File

@ -1,12 +1,12 @@
package me.totalfreedom.plex.command.impl;
import me.totalfreedom.plex.command.PlexCommand;
import me.totalfreedom.plex.command.annotations.CommandParameters;
import me.totalfreedom.plex.command.annotations.CommandPermissions;
import me.totalfreedom.plex.command.annotation.CommandParameters;
import me.totalfreedom.plex.command.annotation.CommandPermissions;
import me.totalfreedom.plex.command.exception.CommandArgumentException;
import me.totalfreedom.plex.command.source.CommandSource;
import me.totalfreedom.plex.rank.enums.Rank;
import me.totalfreedom.plex.util.PlexUtils;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import java.util.List;
@ -23,19 +23,17 @@ public class OpCMD extends PlexCommand
}
@Override
public void execute(CommandSender sender, String[] args)
public void execute(CommandSource sender, String[] args)
{
Player player = Bukkit.getPlayer(args[0]);
if (player == null)
{
sender.sendMessage(tl("playerNotFound"));
return;
}
if (args.length != 1)
throw new CommandArgumentException();
Player player = getNonNullPlayer(args[0]);
PlexUtils.broadcast(tl("oppedPlayer", sender.getName(), player.getName()));
}
@Override
public List<String> onTabComplete(CommandSender sender, String[] args) {
return null;
public List<String> onTabComplete(CommandSource sender, String[] args)
{
return args.length == 1 ? PlexUtils.getPlayerNameList() : null;
}
}

View File

@ -1,8 +1,9 @@
package me.totalfreedom.plex.command.impl;
import me.totalfreedom.plex.command.annotations.CommandParameters;
import me.totalfreedom.plex.command.annotations.CommandPermissions;
import me.totalfreedom.plex.command.annotation.CommandParameters;
import me.totalfreedom.plex.command.annotation.CommandPermissions;
import me.totalfreedom.plex.command.PlexCommand;
import me.totalfreedom.plex.command.source.CommandSource;
import me.totalfreedom.plex.command.source.RequiredCommandSource;
import me.totalfreedom.plex.rank.enums.Rank;
import org.bukkit.command.CommandSender;
@ -19,13 +20,13 @@ public class PlexCMD extends PlexCommand
}
@Override
public void execute(CommandSender sender, String[] args)
public void execute(CommandSource sender, String[] args)
{
sender.sendMessage("HI");
send("HI");
}
@Override
public List<String> onTabComplete(CommandSender sender, String[] args) {
public List<String> onTabComplete(CommandSource sender, String[] args) {
return Arrays.asList("Telesphoreo", "super", "Taahh");
}
}

View File

@ -1,12 +1,12 @@
package me.totalfreedom.plex.command.impl;
import com.google.common.collect.ImmutableList;
import me.totalfreedom.plex.command.annotations.CommandParameters;
import me.totalfreedom.plex.command.annotations.CommandPermissions;
import me.totalfreedom.plex.command.annotation.CommandParameters;
import me.totalfreedom.plex.command.annotation.CommandPermissions;
import me.totalfreedom.plex.command.PlexCommand;
import me.totalfreedom.plex.command.source.CommandSource;
import me.totalfreedom.plex.command.source.RequiredCommandSource;
import me.totalfreedom.plex.rank.enums.Rank;
import org.bukkit.command.CommandSender;
import java.util.Arrays;
import java.util.List;
@ -22,13 +22,13 @@ public class TestCMD extends PlexCommand
}
@Override
public void execute(CommandSender sender, String[] args)
public void execute(CommandSource sender, String[] args)
{
sender.sendMessage(tl("variableTest", sender.getName()));
send(tl("variableTest", sender.getName()));
}
@Override
public List<String> onTabComplete(CommandSender sender, String[] args) {
public List<String> onTabComplete(CommandSource sender, String[] args) {
if (args.length == 1)
{
return Arrays.asList("WHATTHEFAWK", "LUL");

View File

@ -1,20 +1,18 @@
package me.totalfreedom.plex.command.impl;
import com.google.common.collect.ImmutableList;
import me.totalfreedom.plex.Plex;
import me.totalfreedom.plex.command.PlexCommand;
import me.totalfreedom.plex.command.annotations.CommandParameters;
import me.totalfreedom.plex.command.annotations.CommandPermissions;
import me.totalfreedom.plex.command.annotation.CommandParameters;
import me.totalfreedom.plex.command.annotation.CommandPermissions;
import me.totalfreedom.plex.command.exception.CommandArgumentException;
import me.totalfreedom.plex.command.source.CommandSource;
import me.totalfreedom.plex.command.source.RequiredCommandSource;
import me.totalfreedom.plex.rank.enums.Rank;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import static me.totalfreedom.plex.util.PlexUtils.tl;
@ -28,16 +26,17 @@ public class WorldCMD extends PlexCommand
}
@Override
public void execute(CommandSender sender, String[] args)
public void execute(CommandSource sender, String[] args)
{
Player player = (Player) sender;
World world = Bukkit.getWorld(args[0]);
player.teleport(new Location(world, 0, world.getHighestBlockYAt(0, 0) + 1, 0, 0, 0));
sender.sendMessage(tl("playerWorldTeleport", world.getName()));
if (args.length != 1)
throw new CommandArgumentException();
World world = getNonNullWorld(args[0]);
sender.getPlayer().teleport(new Location(world, 0, world.getHighestBlockYAt(0, 0) + 1, 0, 0, 0));
send(tl("playerWorldTeleport", world.getName()));
}
@Override
public List<String> onTabComplete(CommandSender sender, String[] args)
public List<String> onTabComplete(CommandSource sender, String[] args)
{
List<String> worlds = new ArrayList<>();
for (World world : Bukkit.getWorlds())

View File

@ -18,6 +18,16 @@ public class CommandSource
{
this.sender = sender;
this.player = sender instanceof Player ? Bukkit.getPlayer(sender.getName()) : null;
this.plexPlayer = sender instanceof Player ? PlayerCache.getPlexPlayerMap().get(((Player)sender).getUniqueId()) : null;
this.plexPlayer = sender instanceof Player ? PlayerCache.getPlexPlayerMap().get(((Player) sender).getUniqueId()) : null;
}
public void send(String s)
{
sender.sendMessage(s);
}
public String getName()
{
return sender.getName();
}
}

View File

@ -1,6 +1,7 @@
package me.totalfreedom.plex.util;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
@ -100,11 +101,6 @@ public class PlexUtils
return color;
}
public static void warpToWorld(Player player, World world)
{
player.teleport(new Location(world, 0, world.getHighestBlockYAt(0, 0), 0));
}
public static void setBlocks(Location c1, Location c2, Material material)
{
if (!c1.getWorld().getName().equals(c1.getWorld().getName()))
@ -128,6 +124,14 @@ public class PlexUtils
}
}
public static List<String> getPlayerNameList()
{
List<String> names = new ArrayList<>();
for (Player player : Bukkit.getOnlinePlayers())
names.add(player.getName());
return names;
}
public static void broadcast(String s)
{
Bukkit.broadcastMessage(s);

View File

@ -23,6 +23,7 @@ test: this is a test message!
# 1: the command sender's username
variableTest: variable test with <v>!
playerNotFound: Player not found!
worldNotFound: World not found!
noAdminWorldBlockPlace: <e>You are not allowed to place blocks in the admin world!
noAdminWorldBlockBreak: <e>You are not allowed to break blocks in the admin world!
# 1: the world you have been teleported to
@ -31,4 +32,9 @@ playerWorldTeleport: You have been teleported to <v>.
oppedAllPlayers: <b><v> has opped all players on the server
# 1: the person who is opping
# 2: the person who has been opped
oppedPlayer: <v> has opped <v>
oppedPlayer: <b><v> has opped <v>
noPermission: <e>You cannot use this command!
# 1: the login message (uncolored) of the rank required to use the command
noPermissionRank: <e>You must be at least <v> rank to use this command!
noPermissionInGame: <e>You must be in console to use this command!
noPermissionConsole: <e>You must be in-game to use this command!