Implement Command System with Super's help

p.s. very messy
Also Implement Listener System
re-organize
This commit is contained in:
spacerocket62 2020-10-31 01:55:27 -07:00
parent 38ea672eb1
commit 2fa5afe40f
18 changed files with 330 additions and 125 deletions

View File

@ -18,15 +18,15 @@
<repositories> <repositories>
<repository> <repository>
<id>spigot-repo</id> <id>papermc</id>
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url> <url>https://papermc.io/repo/repository/maven-public/</url>
</repository> </repository>
</repositories> </repositories>
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>org.spigotmc</groupId> <groupId>com.destroystokyo.paper</groupId>
<artifactId>spigot-api</artifactId> <artifactId>paper-api</artifactId>
<version>1.16.3-R0.1-SNAPSHOT</version> <version>1.16.3-R0.1-SNAPSHOT</version>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>

View File

@ -5,8 +5,8 @@ import lombok.Setter;
import me.totalfreedom.plex.cache.MongoPlayerData; import me.totalfreedom.plex.cache.MongoPlayerData;
import me.totalfreedom.plex.cache.SQLPlayerData; import me.totalfreedom.plex.cache.SQLPlayerData;
import me.totalfreedom.plex.config.MainConfig; import me.totalfreedom.plex.config.MainConfig;
import me.totalfreedom.plex.listener.ChatListener; import me.totalfreedom.plex.handlers.CommandHandler;
import me.totalfreedom.plex.listener.PlayerListener; import me.totalfreedom.plex.handlers.ListenerHandler;
import me.totalfreedom.plex.rank.RankManager; import me.totalfreedom.plex.rank.RankManager;
import me.totalfreedom.plex.storage.MongoConnection; import me.totalfreedom.plex.storage.MongoConnection;
import me.totalfreedom.plex.storage.RedisConnection; import me.totalfreedom.plex.storage.RedisConnection;
@ -86,8 +86,8 @@ public class Plex extends JavaPlugin
sqlPlayerData = new SQLPlayerData(); sqlPlayerData = new SQLPlayerData();
} }
getServer().getPluginManager().registerEvents(new PlayerListener(), this); new ListenerHandler(); // this doesn't need a variable.
getServer().getPluginManager().registerEvents(new ChatListener(), this); new CommandHandler();
rankManager = new RankManager(); rankManager = new RankManager();
rankManager.generateDefaultRanks(); rankManager.generateDefaultRanks();

View File

@ -1,25 +0,0 @@
package me.totalfreedom.plex.command;
import lombok.Getter;
import me.totalfreedom.plex.player.PlexPlayer;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
public class CommandSource
{
@Getter
private CommandSender sender;
@Getter
private Player player;
private PlexPlayer plexPlayer;
public CommandSource(CommandSender sender)
{
this.sender = sender;
this.player = Bukkit.getPlayer(sender.getName());
this.plexPlayer = null;
}
}

View File

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

View File

@ -1,106 +1,157 @@
package me.totalfreedom.plex.command; package me.totalfreedom.plex.command;
import me.totalfreedom.plex.PlexBase; 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.source.RequiredCommandSource;
import me.totalfreedom.plex.player.PlexPlayer;
import me.totalfreedom.plex.rank.enums.Rank; import me.totalfreedom.plex.rank.enums.Rank;
import org.bukkit.Bukkit;
import org.bukkit.command.*; import org.bukkit.command.*;
import org.bukkit.entity.Player;
import java.lang.reflect.Field;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Objects;
public abstract class PlexCommand extends PlexBase implements CommandExecutor, TabCompleter public abstract class PlexCommand extends Command implements TabExecutor, IPlexCommand
{ {
private static final String COMMAND_PREFIX = "Command_";
private static CommandMap COMMAND_MAP;
private final CommandParameters params; private final CommandParameters params;
private final CommandPermissions perms; private final CommandPermissions perms;
private final String name;
private final String description;
private final String usage;
private final List<String> aliases;
private final Rank level; private final Rank level;
private final RequiredCommandSource source; private final RequiredCommandSource commandSource;
protected PlexCommand() public PlexCommand(String name)
{ {
this.params = this.getClass().getAnnotation(CommandParameters.class); super(name);
this.perms = this.getClass().getAnnotation(CommandPermissions.class); this.params = getClass().getAnnotation(CommandParameters.class);
this.name = this.getClass().getSimpleName().toLowerCase().replace(COMMAND_PREFIX.toLowerCase(), ""); this.perms = getClass().getAnnotation(CommandPermissions.class);
this.description = this.params.description();
this.usage = this.params.usage();
this.aliases = Arrays.asList(this.params.aliases().split(","));
this.level = this.perms.level();
this.source = this.perms.source();
}
public void register() setName(name);
{ setLabel(name);
PCommand command = new PCommand(this.name); setDescription(params.description());
command.setDescription(this.description); setUsage(params.usage());
command.setUsage(this.usage); if (params.aliases().split(",").length > 0)
command.setAliases(this.aliases);
this.getCommandMap().register("", command);
command.setExecutor(this);
}
protected CommandMap getCommandMap()
{
if (COMMAND_MAP == null)
{ {
try setAliases(Arrays.asList(params.aliases().split(",")));
}
this.level = perms.level();
this.commandSource = perms.source();
getMap().register("", this);
}
@Override
public boolean execute(CommandSender sender, String label, String[] args)
{
onCommand(sender, this, label, args);
return true;
}
@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)
{
if (!matches(label)) return false;
if (commandSource == RequiredCommandSource.CONSOLE)
{
if (sender instanceof Player)
{ {
final Field f = Bukkit.getServer().getClass().getDeclaredField("commandMap"); //TODO: Enter console only msg
f.setAccessible(true); return true;
COMMAND_MAP = (CommandMap) f.get(Bukkit.getServer());
return getCommandMap();
} }
catch (Exception e) execute(sender, args);
return true;
} else if (commandSource == RequiredCommandSource.IN_GAME)
{
if (!(sender instanceof Player))
{ {
e.printStackTrace(); //TODO: Enter player only msg
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);
return true;
} }
} }
else
return COMMAND_MAP;
return getCommandMap();
} }
private static class PCommand extends Command
@Override
public List<String> tabComplete(CommandSender sender, String alias, String[] args)
{ {
private PlexCommand command = null; if (!matches(alias)) return ImmutableList.of();
if (sender instanceof Player)
private PCommand(String name)
{ {
super(name); Player player = (Player) sender;
} PlexPlayer plexPlayer = PlayerCache.getPlexPlayerMap().get(player.getUniqueId());
if (plexPlayer.getRankFromString().isAtleast(getLevel()))
public void setExecutor(PlexCommand command) {
{ return onTabComplete(sender, args);
this.command = command; } else {
} return ImmutableList.of();
}
@Override } else {
public boolean execute(CommandSender sender, String c, String[] args) return onTabComplete(sender, args);
{
if (command == null)
return false;
return command.onCommand(sender, this, c, args);
}
@Override
public List<String> tabComplete(CommandSender sender, String alias, String[] args)
{
if (command == null)
return null;
return Objects.requireNonNull(command.onTabComplete(sender, this, alias, args));
} }
} }
@Override @Override
public boolean onCommand(CommandSender sender, Command cmd, String c, String[] args) public List<String> onTabComplete(CommandSender sender, Command cmd, String label, String[] args)
{ {
return tabComplete(sender, label, args);
}
private boolean matches(String label)
{
if (params.aliases().split(",").length > 0)
{
for (String alias : params.aliases().split(","))
{
if (alias.equalsIgnoreCase(label) || getName().equalsIgnoreCase(label))
{
return true;
}
}
} else if (params.aliases().split(",").length < 1)
{
return getName().equalsIgnoreCase(label);
}
return false; return false;
} }
public Rank getLevel()
{
return level;
}
public CommandMap getMap()
{
return Plex.get().getServer().getCommandMap();
}
} }

View File

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

View File

@ -1,5 +1,6 @@
package me.totalfreedom.plex.command; package me.totalfreedom.plex.command.annotations;
import me.totalfreedom.plex.command.source.RequiredCommandSource;
import me.totalfreedom.plex.rank.enums.Rank; import me.totalfreedom.plex.rank.enums.Rank;
import java.lang.annotation.Retention; import java.lang.annotation.Retention;

View File

@ -0,0 +1,31 @@
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.PlexCommand;
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;
@CommandPermissions(level = Rank.OP, source = RequiredCommandSource.ANY)
@CommandParameters(usage = "/<command>", aliases = "plexhelp", description = "Help with plex")
public class PlexCMD extends PlexCommand
{
public PlexCMD() {
super("plex");
}
@Override
public void execute(CommandSender sender, String[] args)
{
sender.sendMessage("HI");
}
@Override
public List<String> onTabComplete(CommandSender sender, String[] args) {
return Arrays.asList("Telesphoreo", "super", "Taahh");
}
}

View File

@ -0,0 +1,36 @@
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.PlexCommand;
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;
@CommandPermissions(level = Rank.OP, source = RequiredCommandSource.ANY)
@CommandParameters(usage = "/<command>", aliases = "tst,tast", description = "HELLO")
public class TestCMD extends PlexCommand
{
public TestCMD() {
super("test");
}
@Override
public void execute(CommandSender sender, String[] args) {
sender.sendMessage("HIIII BICH");
}
@Override
public List<String> onTabComplete(CommandSender sender, String[] args) {
if (args.length == 1)
{
return Arrays.asList("WHATTHEFAWK", "LUL");
}
return ImmutableList.of();
}
}

View File

@ -0,0 +1,25 @@
package me.totalfreedom.plex.command.source;
import lombok.Getter;
import me.totalfreedom.plex.cache.PlayerCache;
import me.totalfreedom.plex.player.PlexPlayer;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
@Getter
public class CommandSource
{
private CommandSender sender;
private final Player player;
private final PlexPlayer plexPlayer;
public CommandSource(CommandSender sender)
{
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;
}
}

View File

@ -1,4 +1,4 @@
package me.totalfreedom.plex.command; package me.totalfreedom.plex.command.source;
public enum RequiredCommandSource public enum RequiredCommandSource
{ {

View File

@ -0,0 +1,24 @@
package me.totalfreedom.plex.handlers;
import com.google.common.collect.Lists;
import me.totalfreedom.plex.command.PlexCommand;
import me.totalfreedom.plex.command.impl.PlexCMD;
import me.totalfreedom.plex.command.impl.TestCMD;
import me.totalfreedom.plex.util.PlexLog;
import java.util.List;
public class CommandHandler
{
private List<PlexCommand> commands = Lists.newArrayList();
public CommandHandler()
{
commands.add(new TestCMD());
commands.add(new PlexCMD());
PlexLog.log(String.format("Registered %s commands!", commands.size()));
}
}

View File

@ -0,0 +1,24 @@
package me.totalfreedom.plex.handlers;
import com.google.common.collect.Lists;
import me.totalfreedom.plex.listener.PlexListener;
import me.totalfreedom.plex.listener.impl.ChatListener;
import me.totalfreedom.plex.listener.impl.PlayerListener;
import me.totalfreedom.plex.util.PlexLog;
import java.util.List;
public class ListenerHandler
{
private List<PlexListener> listeners = Lists.newArrayList();
public ListenerHandler()
{
listeners.add(new ChatListener());
listeners.add(new PlayerListener());
PlexLog.log(String.format("Registered %s listeners!", listeners.size()));
}
}

View File

@ -0,0 +1,14 @@
package me.totalfreedom.plex.listener;
import me.totalfreedom.plex.PlexBase;
import org.bukkit.event.Listener;
public abstract class PlexListener extends PlexBase implements Listener
{
public PlexListener()
{
plugin.getServer().getPluginManager().registerEvents(this, plugin);
}
}

View File

@ -1,14 +1,15 @@
package me.totalfreedom.plex.listener; package me.totalfreedom.plex.listener.impl;
import me.totalfreedom.plex.Plex; import me.totalfreedom.plex.Plex;
import me.totalfreedom.plex.cache.PlayerCache; import me.totalfreedom.plex.cache.PlayerCache;
import me.totalfreedom.plex.listener.PlexListener;
import me.totalfreedom.plex.player.PlexPlayer; import me.totalfreedom.plex.player.PlexPlayer;
import net.md_5.bungee.api.ChatColor; import net.md_5.bungee.api.ChatColor;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.event.player.AsyncPlayerChatEvent; import org.bukkit.event.player.AsyncPlayerChatEvent;
public class ChatListener implements Listener public class ChatListener extends PlexListener
{ {
@EventHandler @EventHandler

View File

@ -1,10 +1,11 @@
package me.totalfreedom.plex.listener; package me.totalfreedom.plex.listener.impl;
import java.util.Arrays; import java.util.Arrays;
import me.totalfreedom.plex.Plex; import me.totalfreedom.plex.Plex;
import me.totalfreedom.plex.cache.MongoPlayerData; import me.totalfreedom.plex.cache.MongoPlayerData;
import me.totalfreedom.plex.cache.PlayerCache; import me.totalfreedom.plex.cache.PlayerCache;
import me.totalfreedom.plex.cache.SQLPlayerData; import me.totalfreedom.plex.cache.SQLPlayerData;
import me.totalfreedom.plex.listener.PlexListener;
import me.totalfreedom.plex.player.PlexPlayer; import me.totalfreedom.plex.player.PlexPlayer;
import me.totalfreedom.plex.player.PunishedPlayer; import me.totalfreedom.plex.player.PunishedPlayer;
import me.totalfreedom.plex.util.PlexLog; import me.totalfreedom.plex.util.PlexLog;
@ -16,10 +17,10 @@ import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent; import org.bukkit.event.player.PlayerQuitEvent;
public class PlayerListener implements Listener public class PlayerListener extends PlexListener
{ {
private final MongoPlayerData mongoPlayerData = Plex.get().getMongoPlayerData() != null ? Plex.get().getMongoPlayerData() : null; private final MongoPlayerData mongoPlayerData = plugin.getMongoPlayerData() != null ? plugin.getMongoPlayerData() : null;
private final SQLPlayerData sqlPlayerData = Plex.get().getSqlPlayerData() != null ? Plex.get().getSqlPlayerData() : null; private final SQLPlayerData sqlPlayerData = plugin.getSqlPlayerData() != null ? plugin.getSqlPlayerData() : null;
@EventHandler(priority = EventPriority.HIGHEST) @EventHandler(priority = EventPriority.HIGHEST)
public void onPlayerSetup(PlayerJoinEvent event) public void onPlayerSetup(PlayerJoinEvent event)
@ -47,13 +48,13 @@ public class PlayerListener implements Listener
} }
else if (sqlPlayerData != null) else if (sqlPlayerData != null)
{ {
if (!sqlPlayerData.exists(player.getUniqueId())) //okay, we're saving with mongo! now check if the player's document exists if (!sqlPlayerData.exists(player.getUniqueId())) //okay, we're saving with sql! now check if the player's document exists
{ {
PlexLog.log("AYO THIS MAN DONT EXIST"); // funi msg PlexLog.log("AYO THIS MAN DONT EXIST"); // funi msg
plexPlayer = new PlexPlayer(player.getUniqueId()); //it doesn't! okay so now create the object plexPlayer = new PlexPlayer(player.getUniqueId()); //it doesn't! okay so now create the object
plexPlayer.setName(player.getName()); //set the name of the player plexPlayer.setName(player.getName()); //set the name of the player
plexPlayer.setIps(Arrays.asList(player.getAddress().getAddress().getHostAddress().trim())); //set the arraylist of ips plexPlayer.setIps(Arrays.asList(player.getAddress().getAddress().getHostAddress().trim())); //set the arraylist of ips
sqlPlayerData.insert(plexPlayer); //and put their document in mongo collection sqlPlayerData.insert(plexPlayer); //and put their row into the sql table
} }
else else
{ {
@ -67,7 +68,7 @@ public class PlayerListener implements Listener
assert plexPlayer != null; assert plexPlayer != null;
if (Plex.get().getRankManager().isAdmin(plexPlayer)) if (plugin.getRankManager().isAdmin(plexPlayer))
{ {
if (!plexPlayer.getLoginMSG().isEmpty()) if (!plexPlayer.getLoginMSG().isEmpty())
{ {
@ -88,7 +89,7 @@ public class PlayerListener implements Listener
{ {
mongoPlayerData.update(plexPlayer); //update the player's document mongoPlayerData.update(plexPlayer); //update the player's document
} }
else if (sqlPlayerData != null) else if (sqlPlayerData != null) //sql checking
{ {
sqlPlayerData.update(plexPlayer); sqlPlayerData.update(plexPlayer);
} }

View File

@ -11,6 +11,8 @@ import lombok.AccessLevel;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
import me.totalfreedom.plex.rank.enums.Rank; import me.totalfreedom.plex.rank.enums.Rank;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
@Getter @Getter
@Setter @Setter
@ -59,6 +61,17 @@ public class PlexPlayer
public Rank getRankFromString() public Rank getRankFromString()
{ {
return Rank.valueOf(rank.toUpperCase()); OfflinePlayer player = Bukkit.getOfflinePlayer(UUID.fromString(uuid));
if (rank.isEmpty())
{
if (player.isOp())
{
return Rank.OP;
} else {
return Rank.NONOP;
}
} else {
return Rank.valueOf(rank.toUpperCase());
}
} }
} }

View File

@ -3,7 +3,3 @@ version: ${project.version}
author: Telesphoreo author: Telesphoreo
main: me.totalfreedom.plex.Plex main: me.totalfreedom.plex.Plex
api-version: 1.16 api-version: 1.16
commands:
plex:
description: Test
usage: /<command>