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.source=1.8
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.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.bukkit.Server;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandMap;
import org.bukkit.command.CommandSender;
import org.bukkit.command.PluginCommand;
import org.bukkit.command.SimpleCommandMap;
import org.bukkit.command.TabExecutor;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.plugin.Plugin;
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.initiators.application.VentureChat;
import venture.Aust1n46.chat.model.VentureCommand;
import venture.Aust1n46.chat.utilities.FormatUtils;
/**
* Class that initializes and executes the plugin's commands.
*/
@Singleton
public class CommandListener implements TabExecutor {
private Map<String, VentureCommand> commands = new HashMap<String, VentureCommand>();
public class CommandController implements TabExecutor {
private static final String COMMAND_CONFIG_VERSION = "3.3.0";
private Map<String, VentureCommand> commandsOld = new HashMap<>();
@Inject
private VentureChat plugin;
@ -142,23 +150,58 @@ public class CommandListener implements TabExecutor {
@Inject
private Unmuteall unmuteall;
private CommandMap commandMap;
private Constructor<PluginCommand> pluginCommandConstructor;
private final Map<String, Command> commands = new HashMap<>();
private Map<String, Command> knownCommands;
@Override
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;
}
@Override
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
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 {
pluginCommandConstructor = PluginCommand.class.getDeclaredConstructor(String.class, Plugin.class);
pluginCommandConstructor.setAccessible(true);
@ -166,52 +209,52 @@ public class CommandListener implements TabExecutor {
e.printStackTrace();
}
commands.put("broadcast", broadcast);
commands.put("channel", channel);
commands.put("join", channel);
commands.put("channelinfo", channelinfo);
commands.put("chatinfo", chatinfo);
commands.put("chatreload", chatreload);
commands.put("chlist", chlist);
commands.put("chwho", chwho);
commands.put("clearchat", clearchat);
commands.put("commandblock", commandblock);
commands.put("commandspy", commandspy);
commands.put("edit", edit);
commands.put("filter", filter);
commands.put("force", force);
commands.put("forceall", forceall);
commands.put("kickchannel", kickchannel);
commands.put("kickchannelall", kickchannelall);
commands.put("leave", leave);
commands.put("listen", listen);
commands.put("me", me);
commands.put("venturechat", venturechat);
commands.put("notifications", notifications);
commands.put("party", party);
commands.put("rangedspy", rangedSpy);
commands.put("removemessage", removemessage);
commands.put("setchannel", setchannel);
commands.put("setchannelall", setchannelall);
commands.put("spy", spy);
commands.put("venturechatgui", ventureChatGui);
commands.put("messagetoggle", messageToggle);
commands.put("bungeetoggle", bungeeToggle);
for (String command : commands.keySet()) {
commandsOld.put("broadcast", broadcast);
// commandsOld.put("channel", channel);
// commandsOld.put("join", channel);
commandsOld.put("channelinfo", channelinfo);
commandsOld.put("chatinfo", chatinfo);
commandsOld.put("chatreload", chatreload);
commandsOld.put("chlist", chlist);
commandsOld.put("chwho", chwho);
commandsOld.put("clearchat", clearchat);
commandsOld.put("commandblock", commandblock);
commandsOld.put("commandspy", commandspy);
commandsOld.put("edit", edit);
commandsOld.put("filter", filter);
commandsOld.put("force", force);
commandsOld.put("forceall", forceall);
commandsOld.put("kickchannel", kickchannel);
commandsOld.put("kickchannelall", kickchannelall);
commandsOld.put("leave", leave);
commandsOld.put("listen", listen);
commandsOld.put("me", me);
commandsOld.put("venturechat", venturechat);
commandsOld.put("notifications", notifications);
commandsOld.put("party", party);
commandsOld.put("rangedspy", rangedSpy);
commandsOld.put("removemessage", removemessage);
commandsOld.put("setchannel", setchannel);
commandsOld.put("setchannelall", setchannelall);
commandsOld.put("spy", spy);
commandsOld.put("venturechatgui", ventureChatGui);
commandsOld.put("messagetoggle", messageToggle);
commandsOld.put("bungeetoggle", bungeeToggle);
for (String command : commandsOld.keySet()) {
registerCommand(command, this);
}
plugin.getServer().getScheduler().runTaskLater(plugin, () -> {
if (plugin.isEnabled()) {
commands.put("reply", reply);
commands.put("r", reply);
commandsOld.put("reply", reply);
commandsOld.put("r", reply);
registerCommand("reply", this);
registerCommand("r", this);
commands.put("mute", mute);
commands.put("muteall", muteall);
commands.put("unmute", unmute);
commands.put("unmuteall", unmuteall);
commandsOld.put("mute", mute);
commandsOld.put("muteall", muteall);
commandsOld.put("unmute", unmute);
commandsOld.put("unmuteall", unmuteall);
registerCommand("mute", this);
registerCommand("muteall", this);
registerCommand("unmute", this);
@ -225,15 +268,21 @@ public class CommandListener implements TabExecutor {
registerCommand("ignore", ignoreCommandExecutor);
}
}, 0);
registerCommand("channel", channel);
}
private void registerCommand(final String command, final CommandExecutor commandExecutor) {
try {
final PluginCommand pluginCommand = pluginCommandConstructor.newInstance(command, plugin);
pluginCommand.setExecutor(commandExecutor);
commandMap.getKnownCommands().put(command, pluginCommand);
knownCommands.put(command, pluginCommand);
} catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
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;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
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.localization.LocalizedMessage;
import venture.Aust1n46.chat.model.ChatChannel;
import venture.Aust1n46.chat.model.PlayerCommand;
import venture.Aust1n46.chat.model.VentureChatPlayer;
import venture.Aust1n46.chat.model.VentureCommand;
import venture.Aust1n46.chat.service.ConfigService;
import venture.Aust1n46.chat.service.VentureChatPlayerApiService;
@Singleton
public class Channel implements VentureCommand {
public class Channel extends PlayerCommand {
@Inject
private PluginMessageController pluginMessageController;
@Inject
@ -25,62 +24,57 @@ public class Channel implements VentureCommand {
@Inject
private ConfigService configService;
@Override
public void execute(CommandSender sender, String command, String[] args) {
if (!(sender instanceof Player)) {
Bukkit.getServer().getConsoleSender().sendMessage(LocalizedMessage.COMMAND_MUST_BE_RUN_BY_PLAYER.toString());
return;
}
VentureChatPlayer mcp = playerApiService.getOnlineMineverseChatPlayer((Player) sender);
if (args.length > 0) {
if (!configService.isChannel(args[0])) {
mcp.getPlayer().sendMessage(LocalizedMessage.INVALID_CHANNEL.toString()
.replace("{args}", args[0]));
return;
}
ChatChannel channel = configService.getChannel(args[0]);
ChannelJoinEvent channelJoinEvent = new ChannelJoinEvent(mcp.getPlayer(), channel, LocalizedMessage.SET_CHANNEL.toString()
.replace("{channel_color}", channel.getColor() + "")
.replace("{channel_name}", channel.getName()));
Bukkit.getServer().getPluginManager().callEvent(channelJoinEvent);
handleChannelJoinEvent(channelJoinEvent);
return;
}
mcp.getPlayer().sendMessage(LocalizedMessage.COMMAND_INVALID_ARGUMENTS.toString()
.replace("{command}", "/channel")
.replace("{args}", "[channel]"));
return;
}
private void handleChannelJoinEvent(final ChannelJoinEvent event) {
if (event.isCancelled())
return;
ChatChannel channel = event.getChannel();
VentureChatPlayer mcp = playerApiService.getOnlineMineverseChatPlayer(event.getPlayer());
if (channel.hasPermission()) {
if (!mcp.getPlayer().hasPermission(channel.getPermission())) {
mcp.removeListening(channel.getName());
mcp.getPlayer().sendMessage(LocalizedMessage.CHANNEL_NO_PERMISSION.toString());
return;
}
}
if (mcp.hasConversation()) {
for (VentureChatPlayer p : playerApiService.getOnlineMineverseChatPlayers()) {
if (p.isSpy()) {
p.getPlayer().sendMessage(LocalizedMessage.EXIT_PRIVATE_CONVERSATION_SPY.toString()
.replace("{player_sender}", mcp.getName())
.replace("{player_receiver}", playerApiService.getMineverseChatPlayer(mcp.getConversation()).getName()));
}
}
mcp.getPlayer().sendMessage(LocalizedMessage.EXIT_PRIVATE_CONVERSATION.toString()
.replace("{player_receiver}", playerApiService.getMineverseChatPlayer(mcp.getConversation()).getName()));
mcp.setConversation(null);
}
mcp.addListening(channel.getName());
mcp.setCurrentChannel(channel);
mcp.getPlayer().sendMessage(event.getMessage());
if (channel.getBungee()) {
pluginMessageController.synchronize(mcp, true);
}
}
@Inject
public Channel(String name) {
super(name);
}
@Override
public void execute(final Player player, final String commandLabel, final String[] args) {
final VentureChatPlayer mcp = playerApiService.getOnlineMineverseChatPlayer(player);
if (args.length > 0) {
if (!configService.isChannel(args[0])) {
mcp.getPlayer().sendMessage(LocalizedMessage.INVALID_CHANNEL.toString().replace("{args}", args[0]));
return;
}
ChatChannel channel = configService.getChannel(args[0]);
ChannelJoinEvent channelJoinEvent = new ChannelJoinEvent(mcp.getPlayer(), channel,
LocalizedMessage.SET_CHANNEL.toString().replace("{channel_color}", channel.getColor() + "").replace("{channel_name}", channel.getName()));
Bukkit.getServer().getPluginManager().callEvent(channelJoinEvent);
handleChannelJoinEvent(channelJoinEvent);
return;
}
mcp.getPlayer().sendMessage(LocalizedMessage.COMMAND_INVALID_ARGUMENTS.toString().replace("{command}", "/channel").replace("{args}", "[channel]"));
}
private void handleChannelJoinEvent(final ChannelJoinEvent event) {
if (event.isCancelled())
return;
ChatChannel channel = event.getChannel();
VentureChatPlayer mcp = playerApiService.getOnlineMineverseChatPlayer(event.getPlayer());
if (channel.hasPermission()) {
if (!mcp.getPlayer().hasPermission(channel.getPermission())) {
mcp.removeListening(channel.getName());
mcp.getPlayer().sendMessage(LocalizedMessage.CHANNEL_NO_PERMISSION.toString());
return;
}
}
if (mcp.hasConversation()) {
for (VentureChatPlayer p : playerApiService.getOnlineMineverseChatPlayers()) {
if (p.isSpy()) {
p.getPlayer().sendMessage(LocalizedMessage.EXIT_PRIVATE_CONVERSATION_SPY.toString().replace("{player_sender}", mcp.getName()).replace("{player_receiver}",
playerApiService.getMineverseChatPlayer(mcp.getConversation()).getName()));
}
}
mcp.getPlayer().sendMessage(
LocalizedMessage.EXIT_PRIVATE_CONVERSATION.toString().replace("{player_receiver}", playerApiService.getMineverseChatPlayer(mcp.getConversation()).getName()));
mcp.setConversation(null);
}
mcp.addListening(channel.getName());
mcp.setCurrentChannel(channel);
mcp.getPlayer().sendMessage(event.getMessage());
if (channel.getBungee()) {
pluginMessageController.synchronize(mcp, true);
}
}
}

View File

@ -17,11 +17,11 @@ import com.google.inject.Singleton;
import net.milkbowl.vault.permission.Permission;
import venture.Aust1n46.chat.VentureChatPlaceholders;
import venture.Aust1n46.chat.controllers.CommandController;
import venture.Aust1n46.chat.controllers.PluginMessageController;
import venture.Aust1n46.chat.controllers.VentureChatSpigotFlatFileController;
import venture.Aust1n46.chat.guice.VentureChatPluginModule;
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.PacketListener;
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 Injector injector = Guice.createInjector(pluginModule);
injector.injectMembers(this);
injector.injectMembers(new CommandListener());
injector.injectMembers(new CommandController());
injector.injectMembers(new UnmuteScheduler());
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