Command abstraction layers.

This commit is contained in:
Aust1n46 2022-01-23 20:56:06 -06:00
parent a87ad04856
commit 54a4504713
7 changed files with 329 additions and 111 deletions

View File

@ -1,4 +1,4 @@
#Sat Jan 15 01:48:59 CST 2022 #Tue Jan 18 02:01:06 CST 2022
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.source=1.8 org.eclipse.jdt.core.compiler.source=1.8
eclipse.preferences.version=1 eclipse.preferences.version=1

View File

@ -1,17 +1,22 @@
package venture.Aust1n46.chat.initiators.listeners; package venture.Aust1n46.chat.controllers;
import java.io.File;
import java.lang.reflect.Constructor; import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import org.bukkit.Server;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandMap;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.command.PluginCommand; import org.bukkit.command.PluginCommand;
import org.bukkit.command.SimpleCommandMap;
import org.bukkit.command.TabExecutor; import org.bukkit.command.TabExecutor;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
import com.google.inject.Inject; import com.google.inject.Inject;
@ -56,13 +61,16 @@ import venture.Aust1n46.chat.controllers.commands.VentureChatGui;
import venture.Aust1n46.chat.controllers.commands.Venturechat; import venture.Aust1n46.chat.controllers.commands.Venturechat;
import venture.Aust1n46.chat.initiators.application.VentureChat; import venture.Aust1n46.chat.initiators.application.VentureChat;
import venture.Aust1n46.chat.model.VentureCommand; import venture.Aust1n46.chat.model.VentureCommand;
import venture.Aust1n46.chat.utilities.FormatUtils;
/** /**
* Class that initializes and executes the plugin's commands. * Class that initializes and executes the plugin's commands.
*/ */
@Singleton @Singleton
public class CommandListener implements TabExecutor { public class CommandController implements TabExecutor {
private Map<String, VentureCommand> commands = new HashMap<String, VentureCommand>(); private static final String COMMAND_CONFIG_VERSION = "3.3.0";
private Map<String, VentureCommand> commandsOld = new HashMap<>();
@Inject @Inject
private VentureChat plugin; private VentureChat plugin;
@ -142,23 +150,58 @@ public class CommandListener implements TabExecutor {
@Inject @Inject
private Unmuteall unmuteall; private Unmuteall unmuteall;
private CommandMap commandMap;
private Constructor<PluginCommand> pluginCommandConstructor; private Constructor<PluginCommand> pluginCommandConstructor;
private final Map<String, Command> commands = new HashMap<>();
private Map<String, Command> knownCommands;
@Override @Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] parameters) { public boolean onCommand(CommandSender sender, Command command, String label, String[] parameters) {
commands.get(command.getName()).execute(sender, command.getName(), parameters); commandsOld.get(command.getName()).execute(sender, command.getName(), parameters);
return true; return true;
} }
@Override @Override
public List<String> onTabComplete(CommandSender sender, Command command, String label, String[] args) { public List<String> onTabComplete(CommandSender sender, Command command, String label, String[] args) {
return commands.get(command.getName()).onTabComplete(sender, command, label, args); return commandsOld.get(command.getName()).onTabComplete(sender, command, label, args);
} }
@SuppressWarnings("unchecked")
@Inject @Inject
public void postConstruct() { public void postConstruct() {
commandMap = plugin.getServer().getCommandMap(); final Server server = plugin.getServer();
final File commandsFile = new File(plugin.getDataFolder().getAbsolutePath(), "commands.yml");
if (!commandsFile.isFile()) {
plugin.saveResource("commands.yml", true);
}
FileConfiguration commandsFileConfiguration = YamlConfiguration.loadConfiguration(commandsFile);
final String fileVersion = commandsFileConfiguration.getString("Version", "null");
if (!fileVersion.equals(COMMAND_CONFIG_VERSION)) {
server.getConsoleSender()
.sendMessage(FormatUtils.FormatStringAll("&8[&eVentureChat&8]&e - Version Change Detected! Saving Old commands.yml and Generating Latest File"));
commandsFile.renameTo(new File(plugin.getDataFolder().getAbsolutePath(), "commands_old_" + fileVersion + ".yml"));
plugin.saveResource("commands.yml", true);
commandsFileConfiguration = YamlConfiguration.loadConfiguration(commandsFile);
}
try {
knownCommands = server.getCommandMap().getKnownCommands(); // Paper :)
}
// Spigot :(
catch (final NoSuchMethodError error) {
try {
final Field commandMapField = server.getClass().getDeclaredField("commandMap");
commandMapField.setAccessible(true);
final SimpleCommandMap simpleCommandMap = (SimpleCommandMap) commandMapField.get(server);
final Field knownCommandsField = SimpleCommandMap.class.getDeclaredField("knownCommands");
knownCommandsField.setAccessible(true);
knownCommands = (Map<String, Command>) knownCommandsField.get(simpleCommandMap);
} catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
server.getConsoleSender()
.sendMessage(FormatUtils.FormatStringAll("&8[&eVentureChat&8]&c - Unable to access CommandMap on Spigot. If this issue persists, try using Paper."));
e.printStackTrace();
}
}
try { try {
pluginCommandConstructor = PluginCommand.class.getDeclaredConstructor(String.class, Plugin.class); pluginCommandConstructor = PluginCommand.class.getDeclaredConstructor(String.class, Plugin.class);
pluginCommandConstructor.setAccessible(true); pluginCommandConstructor.setAccessible(true);
@ -166,52 +209,52 @@ public class CommandListener implements TabExecutor {
e.printStackTrace(); e.printStackTrace();
} }
commands.put("broadcast", broadcast); commandsOld.put("broadcast", broadcast);
commands.put("channel", channel); // commandsOld.put("channel", channel);
commands.put("join", channel); // commandsOld.put("join", channel);
commands.put("channelinfo", channelinfo); commandsOld.put("channelinfo", channelinfo);
commands.put("chatinfo", chatinfo); commandsOld.put("chatinfo", chatinfo);
commands.put("chatreload", chatreload); commandsOld.put("chatreload", chatreload);
commands.put("chlist", chlist); commandsOld.put("chlist", chlist);
commands.put("chwho", chwho); commandsOld.put("chwho", chwho);
commands.put("clearchat", clearchat); commandsOld.put("clearchat", clearchat);
commands.put("commandblock", commandblock); commandsOld.put("commandblock", commandblock);
commands.put("commandspy", commandspy); commandsOld.put("commandspy", commandspy);
commands.put("edit", edit); commandsOld.put("edit", edit);
commands.put("filter", filter); commandsOld.put("filter", filter);
commands.put("force", force); commandsOld.put("force", force);
commands.put("forceall", forceall); commandsOld.put("forceall", forceall);
commands.put("kickchannel", kickchannel); commandsOld.put("kickchannel", kickchannel);
commands.put("kickchannelall", kickchannelall); commandsOld.put("kickchannelall", kickchannelall);
commands.put("leave", leave); commandsOld.put("leave", leave);
commands.put("listen", listen); commandsOld.put("listen", listen);
commands.put("me", me); commandsOld.put("me", me);
commands.put("venturechat", venturechat); commandsOld.put("venturechat", venturechat);
commands.put("notifications", notifications); commandsOld.put("notifications", notifications);
commands.put("party", party); commandsOld.put("party", party);
commands.put("rangedspy", rangedSpy); commandsOld.put("rangedspy", rangedSpy);
commands.put("removemessage", removemessage); commandsOld.put("removemessage", removemessage);
commands.put("setchannel", setchannel); commandsOld.put("setchannel", setchannel);
commands.put("setchannelall", setchannelall); commandsOld.put("setchannelall", setchannelall);
commands.put("spy", spy); commandsOld.put("spy", spy);
commands.put("venturechatgui", ventureChatGui); commandsOld.put("venturechatgui", ventureChatGui);
commands.put("messagetoggle", messageToggle); commandsOld.put("messagetoggle", messageToggle);
commands.put("bungeetoggle", bungeeToggle); commandsOld.put("bungeetoggle", bungeeToggle);
for (String command : commands.keySet()) { for (String command : commandsOld.keySet()) {
registerCommand(command, this); registerCommand(command, this);
} }
plugin.getServer().getScheduler().runTaskLater(plugin, () -> { plugin.getServer().getScheduler().runTaskLater(plugin, () -> {
if (plugin.isEnabled()) { if (plugin.isEnabled()) {
commands.put("reply", reply); commandsOld.put("reply", reply);
commands.put("r", reply); commandsOld.put("r", reply);
registerCommand("reply", this); registerCommand("reply", this);
registerCommand("r", this); registerCommand("r", this);
commands.put("mute", mute); commandsOld.put("mute", mute);
commands.put("muteall", muteall); commandsOld.put("muteall", muteall);
commands.put("unmute", unmute); commandsOld.put("unmute", unmute);
commands.put("unmuteall", unmuteall); commandsOld.put("unmuteall", unmuteall);
registerCommand("mute", this); registerCommand("mute", this);
registerCommand("muteall", this); registerCommand("muteall", this);
registerCommand("unmute", this); registerCommand("unmute", this);
@ -225,15 +268,21 @@ public class CommandListener implements TabExecutor {
registerCommand("ignore", ignoreCommandExecutor); registerCommand("ignore", ignoreCommandExecutor);
} }
}, 0); }, 0);
registerCommand("channel", channel);
} }
private void registerCommand(final String command, final CommandExecutor commandExecutor) { private void registerCommand(final String command, final CommandExecutor commandExecutor) {
try { try {
final PluginCommand pluginCommand = pluginCommandConstructor.newInstance(command, plugin); final PluginCommand pluginCommand = pluginCommandConstructor.newInstance(command, plugin);
pluginCommand.setExecutor(commandExecutor); pluginCommand.setExecutor(commandExecutor);
commandMap.getKnownCommands().put(command, pluginCommand); knownCommands.put(command, pluginCommand);
} catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
private void registerCommand(final String commandLabel, final Command command) {
knownCommands.put(commandLabel, command);
}
} }

View File

@ -1,7 +1,6 @@
package venture.Aust1n46.chat.controllers.commands; package venture.Aust1n46.chat.controllers.commands;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import com.google.inject.Inject; import com.google.inject.Inject;
@ -11,13 +10,13 @@ import venture.Aust1n46.chat.api.events.ChannelJoinEvent;
import venture.Aust1n46.chat.controllers.PluginMessageController; import venture.Aust1n46.chat.controllers.PluginMessageController;
import venture.Aust1n46.chat.localization.LocalizedMessage; import venture.Aust1n46.chat.localization.LocalizedMessage;
import venture.Aust1n46.chat.model.ChatChannel; import venture.Aust1n46.chat.model.ChatChannel;
import venture.Aust1n46.chat.model.PlayerCommand;
import venture.Aust1n46.chat.model.VentureChatPlayer; import venture.Aust1n46.chat.model.VentureChatPlayer;
import venture.Aust1n46.chat.model.VentureCommand;
import venture.Aust1n46.chat.service.ConfigService; import venture.Aust1n46.chat.service.ConfigService;
import venture.Aust1n46.chat.service.VentureChatPlayerApiService; import venture.Aust1n46.chat.service.VentureChatPlayerApiService;
@Singleton @Singleton
public class Channel implements VentureCommand { public class Channel extends PlayerCommand {
@Inject @Inject
private PluginMessageController pluginMessageController; private PluginMessageController pluginMessageController;
@Inject @Inject
@ -25,31 +24,27 @@ public class Channel implements VentureCommand {
@Inject @Inject
private ConfigService configService; private ConfigService configService;
@Override @Inject
public void execute(CommandSender sender, String command, String[] args) { public Channel(String name) {
if (!(sender instanceof Player)) { super(name);
Bukkit.getServer().getConsoleSender().sendMessage(LocalizedMessage.COMMAND_MUST_BE_RUN_BY_PLAYER.toString());
return;
} }
VentureChatPlayer mcp = playerApiService.getOnlineMineverseChatPlayer((Player) sender);
@Override
public void execute(final Player player, final String commandLabel, final String[] args) {
final VentureChatPlayer mcp = playerApiService.getOnlineMineverseChatPlayer(player);
if (args.length > 0) { if (args.length > 0) {
if (!configService.isChannel(args[0])) { if (!configService.isChannel(args[0])) {
mcp.getPlayer().sendMessage(LocalizedMessage.INVALID_CHANNEL.toString() mcp.getPlayer().sendMessage(LocalizedMessage.INVALID_CHANNEL.toString().replace("{args}", args[0]));
.replace("{args}", args[0]));
return; return;
} }
ChatChannel channel = configService.getChannel(args[0]); ChatChannel channel = configService.getChannel(args[0]);
ChannelJoinEvent channelJoinEvent = new ChannelJoinEvent(mcp.getPlayer(), channel, LocalizedMessage.SET_CHANNEL.toString() ChannelJoinEvent channelJoinEvent = new ChannelJoinEvent(mcp.getPlayer(), channel,
.replace("{channel_color}", channel.getColor() + "") LocalizedMessage.SET_CHANNEL.toString().replace("{channel_color}", channel.getColor() + "").replace("{channel_name}", channel.getName()));
.replace("{channel_name}", channel.getName()));
Bukkit.getServer().getPluginManager().callEvent(channelJoinEvent); Bukkit.getServer().getPluginManager().callEvent(channelJoinEvent);
handleChannelJoinEvent(channelJoinEvent); handleChannelJoinEvent(channelJoinEvent);
return; return;
} }
mcp.getPlayer().sendMessage(LocalizedMessage.COMMAND_INVALID_ARGUMENTS.toString() mcp.getPlayer().sendMessage(LocalizedMessage.COMMAND_INVALID_ARGUMENTS.toString().replace("{command}", "/channel").replace("{args}", "[channel]"));
.replace("{command}", "/channel")
.replace("{args}", "[channel]"));
return;
} }
private void handleChannelJoinEvent(final ChannelJoinEvent event) { private void handleChannelJoinEvent(final ChannelJoinEvent event) {
@ -67,13 +62,12 @@ public class Channel implements VentureCommand {
if (mcp.hasConversation()) { if (mcp.hasConversation()) {
for (VentureChatPlayer p : playerApiService.getOnlineMineverseChatPlayers()) { for (VentureChatPlayer p : playerApiService.getOnlineMineverseChatPlayers()) {
if (p.isSpy()) { if (p.isSpy()) {
p.getPlayer().sendMessage(LocalizedMessage.EXIT_PRIVATE_CONVERSATION_SPY.toString() p.getPlayer().sendMessage(LocalizedMessage.EXIT_PRIVATE_CONVERSATION_SPY.toString().replace("{player_sender}", mcp.getName()).replace("{player_receiver}",
.replace("{player_sender}", mcp.getName()) playerApiService.getMineverseChatPlayer(mcp.getConversation()).getName()));
.replace("{player_receiver}", playerApiService.getMineverseChatPlayer(mcp.getConversation()).getName()));
} }
} }
mcp.getPlayer().sendMessage(LocalizedMessage.EXIT_PRIVATE_CONVERSATION.toString() mcp.getPlayer().sendMessage(
.replace("{player_receiver}", playerApiService.getMineverseChatPlayer(mcp.getConversation()).getName())); LocalizedMessage.EXIT_PRIVATE_CONVERSATION.toString().replace("{player_receiver}", playerApiService.getMineverseChatPlayer(mcp.getConversation()).getName()));
mcp.setConversation(null); mcp.setConversation(null);
} }
mcp.addListening(channel.getName()); mcp.addListening(channel.getName());

View File

@ -17,11 +17,11 @@ import com.google.inject.Singleton;
import net.milkbowl.vault.permission.Permission; import net.milkbowl.vault.permission.Permission;
import venture.Aust1n46.chat.VentureChatPlaceholders; import venture.Aust1n46.chat.VentureChatPlaceholders;
import venture.Aust1n46.chat.controllers.CommandController;
import venture.Aust1n46.chat.controllers.PluginMessageController; import venture.Aust1n46.chat.controllers.PluginMessageController;
import venture.Aust1n46.chat.controllers.VentureChatSpigotFlatFileController; import venture.Aust1n46.chat.controllers.VentureChatSpigotFlatFileController;
import venture.Aust1n46.chat.guice.VentureChatPluginModule; import venture.Aust1n46.chat.guice.VentureChatPluginModule;
import venture.Aust1n46.chat.initiators.listeners.ChatListener; import venture.Aust1n46.chat.initiators.listeners.ChatListener;
import venture.Aust1n46.chat.initiators.listeners.CommandListener;
import venture.Aust1n46.chat.initiators.listeners.LoginListener; import venture.Aust1n46.chat.initiators.listeners.LoginListener;
import venture.Aust1n46.chat.initiators.listeners.PacketListener; import venture.Aust1n46.chat.initiators.listeners.PacketListener;
import venture.Aust1n46.chat.initiators.listeners.PreProcessCommandListener; import venture.Aust1n46.chat.initiators.listeners.PreProcessCommandListener;
@ -64,7 +64,7 @@ public class VentureChat extends JavaPlugin implements PluginMessageListener {
final VentureChatPluginModule pluginModule = new VentureChatPluginModule(this); final VentureChatPluginModule pluginModule = new VentureChatPluginModule(this);
final Injector injector = Guice.createInjector(pluginModule); final Injector injector = Guice.createInjector(pluginModule);
injector.injectMembers(this); injector.injectMembers(this);
injector.injectMembers(new CommandListener()); injector.injectMembers(new CommandController());
injector.injectMembers(new UnmuteScheduler()); injector.injectMembers(new UnmuteScheduler());
try { try {

View File

@ -0,0 +1,24 @@
package venture.Aust1n46.chat.model;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import venture.Aust1n46.chat.localization.LocalizedMessage;
public abstract class PlayerCommand extends UniversalCommand {
protected PlayerCommand(final String name) {
super(name);
}
@Override
public void executeVoid(final CommandSender sender, final String commandLabel, final String[] args) {
if (sender instanceof Player) {
final Player player = (Player) sender;
execute(player, commandLabel, args);
} else {
plugin.getServer().getConsoleSender().sendMessage(LocalizedMessage.COMMAND_MUST_BE_RUN_BY_PLAYER.toString());
}
}
public abstract void execute(final Player player, final String commandLabel, final String[] args);
}

View File

@ -0,0 +1,32 @@
package venture.Aust1n46.chat.model;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.PluginIdentifiableCommand;
import org.bukkit.plugin.Plugin;
import com.google.inject.Inject;
import venture.Aust1n46.chat.initiators.application.VentureChat;
public abstract class UniversalCommand extends Command implements PluginIdentifiableCommand {
@Inject
protected VentureChat plugin;
protected UniversalCommand(final String name) {
super(name);
}
@Override
public boolean execute(final CommandSender sender, final String commandLabel, final String[] args) {
executeVoid(sender, commandLabel, args);
return true;
}
public abstract void executeVoid(final CommandSender sender, final String commandLabel, final String[] args);
@Override
public Plugin getPlugin() {
return plugin;
}
}

View File

@ -0,0 +1,119 @@
Version: 3.3.0
# Restart server after editing this file!!!
# Restart server after editing this file!!!
# Restart server after editing this file!!!
commands:
message:
aliases: [vmessage,msg,tell,whisper,pm]
enabled: true
ignore:
aliases: [vignore]
enabled: true
reply:
aliases: [vreply,r]
enabled: true
spy:
aliases: [vspy]
enabled: true
mute:
aliases: [mp,vmute]
enabled: true
unmute:
aliases: [ump,vunmute]
enabled: true
muteall:
aliases: [mpa,vmuteall]
enabled: true
unmuteall:
aliases: [umpa,vunmuteall]
enabled: true
channel:
aliases: [ch,vchannel,join]
enabled: true
listen:
aliases: [lis,vlisten]
enabled: true
leave:
aliases: [lev,vleave]
enabled: true
chlist:
aliases: [chl,vchlist]
enabled: true
chwho:
aliases: [chw,vchwho]
enabled: true
setchannel:
aliases: [sc,vsetchannel]
enabled: true
kickchannel:
aliases: [kc,vkickchannel]
enabled: true
kickchannelall:
aliases: [kca,vkickchannelall]
enabled: true
setchannelall:
aliases: [sca,vsetchannelall]
enabled: true
force:
aliases: [for,vforce]
enabled: true
forceall:
aliases: [fora,vforceall]
enabled: true
chatreload:
aliases: [cr,vchatreload]
enabled: true
commandspy:
aliases: [comspy,vcommandspy]
enabled: true
chatinfo:
aliases: [ci,vchatinfo]
enabled: true
channelinfo:
aliases: [chi,vchannelinfo]
enabled: true
venturechat:
aliases: [vc]
enabled: true
me:
aliases: [vme]
enabled: true
filter:
aliases: [fil,vfilter]
enabled: true
broadcast:
aliases: [bc,vbroadcast]
enabled: true
commandblock:
aliases: [cb,vcommandblock]
enabled: true
party:
aliases: [p,chatparty,cp,vparty]
enabled: true
clearchat:
aliases: [cc,vclearchat]
enabled: true
notifications:
aliases: [notify,vnotify]
enabled: true
removemessage:
aliases: [rm,vremovemessage]
enabled: true
rangedspy:
aliases: [rspy,vrangedspy]
enabled: true
venturechatgui:
aliases: [vchatgui]
enabled: true
messagetoggle:
aliases: [mtoggle,vmessagetoggle]
enabled: true
bungeetoggle:
aliases: [btoggle,vbungeetoggle]
enabled: true
config:
aliases: [vconfig]
enabled: false
edit:
aliases: [vedit]
enabled: false