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>
<repository>
<id>spigot-repo</id>
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
<id>papermc</id>
<url>https://papermc.io/repo/repository/maven-public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<groupId>com.destroystokyo.paper</groupId>
<artifactId>paper-api</artifactId>
<version>1.16.3-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>

View File

@ -5,8 +5,8 @@ import lombok.Setter;
import me.totalfreedom.plex.cache.MongoPlayerData;
import me.totalfreedom.plex.cache.SQLPlayerData;
import me.totalfreedom.plex.config.MainConfig;
import me.totalfreedom.plex.listener.ChatListener;
import me.totalfreedom.plex.listener.PlayerListener;
import me.totalfreedom.plex.handlers.CommandHandler;
import me.totalfreedom.plex.handlers.ListenerHandler;
import me.totalfreedom.plex.rank.RankManager;
import me.totalfreedom.plex.storage.MongoConnection;
import me.totalfreedom.plex.storage.RedisConnection;
@ -86,8 +86,8 @@ public class Plex extends JavaPlugin
sqlPlayerData = new SQLPlayerData();
}
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
getServer().getPluginManager().registerEvents(new ChatListener(), this);
new ListenerHandler(); // this doesn't need a variable.
new CommandHandler();
rankManager = new RankManager();
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;
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 org.bukkit.Bukkit;
import org.bukkit.command.*;
import org.bukkit.entity.Player;
import java.lang.reflect.Field;
import java.util.Arrays;
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 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 RequiredCommandSource source;
private final RequiredCommandSource commandSource;
protected PlexCommand()
public PlexCommand(String name)
{
this.params = this.getClass().getAnnotation(CommandParameters.class);
this.perms = this.getClass().getAnnotation(CommandPermissions.class);
this.name = this.getClass().getSimpleName().toLowerCase().replace(COMMAND_PREFIX.toLowerCase(), "");
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();
}
super(name);
this.params = getClass().getAnnotation(CommandParameters.class);
this.perms = getClass().getAnnotation(CommandPermissions.class);
public void register()
{
PCommand command = new PCommand(this.name);
command.setDescription(this.description);
command.setUsage(this.usage);
command.setAliases(this.aliases);
this.getCommandMap().register("", command);
command.setExecutor(this);
}
protected CommandMap getCommandMap()
{
if (COMMAND_MAP == null)
setName(name);
setLabel(name);
setDescription(params.description());
setUsage(params.usage());
if (params.aliases().split(",").length > 0)
{
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");
f.setAccessible(true);
COMMAND_MAP = (CommandMap) f.get(Bukkit.getServer());
return getCommandMap();
//TODO: Enter console only msg
return true;
}
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;
private PCommand(String name)
if (!matches(alias)) return ImmutableList.of();
if (sender instanceof Player)
{
super(name);
}
public void setExecutor(PlexCommand command)
{
this.command = command;
}
@Override
public boolean execute(CommandSender sender, String c, String[] 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));
Player player = (Player) sender;
PlexPlayer plexPlayer = PlayerCache.getPlexPlayerMap().get(player.getUniqueId());
if (plexPlayer.getRankFromString().isAtleast(getLevel()))
{
return onTabComplete(sender, args);
} else {
return ImmutableList.of();
}
} else {
return onTabComplete(sender, args);
}
}
@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;
}
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.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 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
{

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.cache.PlayerCache;
import me.totalfreedom.plex.listener.PlexListener;
import me.totalfreedom.plex.player.PlexPlayer;
import net.md_5.bungee.api.ChatColor;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.AsyncPlayerChatEvent;
public class ChatListener implements Listener
public class ChatListener extends PlexListener
{
@EventHandler

View File

@ -1,10 +1,11 @@
package me.totalfreedom.plex.listener;
package me.totalfreedom.plex.listener.impl;
import java.util.Arrays;
import me.totalfreedom.plex.Plex;
import me.totalfreedom.plex.cache.MongoPlayerData;
import me.totalfreedom.plex.cache.PlayerCache;
import me.totalfreedom.plex.cache.SQLPlayerData;
import me.totalfreedom.plex.listener.PlexListener;
import me.totalfreedom.plex.player.PlexPlayer;
import me.totalfreedom.plex.player.PunishedPlayer;
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.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 SQLPlayerData sqlPlayerData = Plex.get().getSqlPlayerData() != null ? Plex.get().getSqlPlayerData() : null;
private final MongoPlayerData mongoPlayerData = plugin.getMongoPlayerData() != null ? plugin.getMongoPlayerData() : null;
private final SQLPlayerData sqlPlayerData = plugin.getSqlPlayerData() != null ? plugin.getSqlPlayerData() : null;
@EventHandler(priority = EventPriority.HIGHEST)
public void onPlayerSetup(PlayerJoinEvent event)
@ -47,13 +48,13 @@ public class PlayerListener implements Listener
}
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
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.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
{
@ -67,7 +68,7 @@ public class PlayerListener implements Listener
assert plexPlayer != null;
if (Plex.get().getRankManager().isAdmin(plexPlayer))
if (plugin.getRankManager().isAdmin(plexPlayer))
{
if (!plexPlayer.getLoginMSG().isEmpty())
{
@ -88,7 +89,7 @@ public class PlayerListener implements Listener
{
mongoPlayerData.update(plexPlayer); //update the player's document
}
else if (sqlPlayerData != null)
else if (sqlPlayerData != null) //sql checking
{
sqlPlayerData.update(plexPlayer);
}

View File

@ -11,6 +11,8 @@ import lombok.AccessLevel;
import lombok.Getter;
import lombok.Setter;
import me.totalfreedom.plex.rank.enums.Rank;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
@Getter
@Setter
@ -59,6 +61,17 @@ public class PlexPlayer
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

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