From 9570db0c070ead9963744cad3078187db127030a Mon Sep 17 00:00:00 2001 From: Daniel Legt Date: Sun, 14 Jan 2024 17:29:33 +0200 Subject: [PATCH] Feature/commands (#3) * Added initial command framework and updated the plugin.yml * plugin.yml + DDCommand + CommandManager + InfoCommand * Implemented most of hte logic of the commands + Toggle command * Toggle command functionality in DamageHandler event + Help command * Updated plugin.yml + Reload command + Reload command functionality * Fixed slight issue with info command * Fixed info command * Version Bump * Beautified the info command * Fixed the command registration * Polished, fixed and implemented all commands * Updated README.md --- README.md | 12 ++++- pom.xml | 2 +- .../Commands/CommandManager.java | 36 +++++++++++++ .../damagedisplay/Commands/DDCommand.java | 14 +++++ .../damagedisplay/Commands/HelpCommand.java | 33 ++++++++++++ .../damagedisplay/Commands/InfoCommand.java | 51 +++++++++++++++++++ .../damagedisplay/Commands/ReloadCommand.java | 31 +++++++++++ .../damagedisplay/Commands/ToggleCommand.java | 48 +++++++++++++++++ .../danlegt/damagedisplay/DamageDisplay.java | 4 +- .../Event/EventListeners/OnDamageHandler.java | 11 ++++ src/main/resources/plugin.yml | 15 +++++- 11 files changed, 251 insertions(+), 6 deletions(-) create mode 100644 src/main/java/com/danlegt/damagedisplay/Commands/CommandManager.java create mode 100644 src/main/java/com/danlegt/damagedisplay/Commands/DDCommand.java create mode 100644 src/main/java/com/danlegt/damagedisplay/Commands/HelpCommand.java create mode 100644 src/main/java/com/danlegt/damagedisplay/Commands/InfoCommand.java create mode 100644 src/main/java/com/danlegt/damagedisplay/Commands/ReloadCommand.java create mode 100644 src/main/java/com/danlegt/damagedisplay/Commands/ToggleCommand.java diff --git a/README.md b/README.md index 41c220d..eb02cea 100644 --- a/README.md +++ b/README.md @@ -25,14 +25,22 @@ be displayed to anyone looking as a TextDisplay floating near the position that - Description: Enables the ActionBar messages for the player - Default: True -### damagedisplay.command.info **( !! WIP !! )** +### damagedisplay.command.info - Description: Enables the ActionBar messages for the player - Default: True -### damagedisplay.command.toggle **( !! WIP !! )** +### damagedisplay.command.toggle - Description: Enables the ActionBar messages for the player - Default: True +### damagedisplay.command.reload +- Description: Grants permission to /dd reload which will reload the configuration file, so you won't have to reload it +- Default: False + +### damagedisplay.command.help +- Description: Grants permission to /dd help +- Default: False + # Support & Contribution In regard to possible bugs that might show up, please open an issue on the [Github Repository](https://github.com/JustKato/DamageDisplay). Contributions are more than welcome! Fork the repository, make your changes and submit a pull request. diff --git a/pom.xml b/pom.xml index 9b0649b..208d13e 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ com.danlegt DamageDisplay - 0.2.0 + 0.3.5 jar DamageDisplay diff --git a/src/main/java/com/danlegt/damagedisplay/Commands/CommandManager.java b/src/main/java/com/danlegt/damagedisplay/Commands/CommandManager.java new file mode 100644 index 0000000..71a2d16 --- /dev/null +++ b/src/main/java/com/danlegt/damagedisplay/Commands/CommandManager.java @@ -0,0 +1,36 @@ +package com.danlegt.damagedisplay.Commands; + +import com.danlegt.damagedisplay.DamageDisplay; +import org.bukkit.ChatColor; +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; + +import java.util.List; + +public class CommandManager implements CommandExecutor { + + public static List registeredCommands = List.of( + new InfoCommand(), + new ToggleCommand(), + new HelpCommand(), + new ReloadCommand() + ); + + @Override + public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + if (args.length == 0) { + sender.sendMessage(ChatColor.RED + "Usage: /dd "); + return true; + } + + for ( var cmd: registeredCommands ) { + if ( cmd.getCommandLabel().equalsIgnoreCase(args[0]) ) { + return cmd.handleCommand(sender, command, label, args); + } + } + + return false; + } + +} diff --git a/src/main/java/com/danlegt/damagedisplay/Commands/DDCommand.java b/src/main/java/com/danlegt/damagedisplay/Commands/DDCommand.java new file mode 100644 index 0000000..bcac36e --- /dev/null +++ b/src/main/java/com/danlegt/damagedisplay/Commands/DDCommand.java @@ -0,0 +1,14 @@ +package com.danlegt.damagedisplay.Commands; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandSender; + +public interface DDCommand { + + public String getCommandLabel(); + + public boolean handleCommand(CommandSender sender, Command command, String label, String[] args); + + public String getDescription(); + +} diff --git a/src/main/java/com/danlegt/damagedisplay/Commands/HelpCommand.java b/src/main/java/com/danlegt/damagedisplay/Commands/HelpCommand.java new file mode 100644 index 0000000..d63c589 --- /dev/null +++ b/src/main/java/com/danlegt/damagedisplay/Commands/HelpCommand.java @@ -0,0 +1,33 @@ +package com.danlegt.damagedisplay.Commands; + +import org.bukkit.ChatColor; +import org.bukkit.command.Command; +import org.bukkit.command.CommandSender; + +public class HelpCommand implements DDCommand { + @Override + public String getCommandLabel() { + return "help"; + } + + @Override + public boolean handleCommand(CommandSender sender, Command command, String label, String[] args) { + if ( !sender.hasPermission("damagedisplay.command.help") ) { + sender.sendMessage(ChatColor.RED + "You do not have permission to use this command."); + return true; + } + + sender.sendMessage(ChatColor.GRAY + "===================="); + CommandManager.registeredCommands.forEach( cmd -> { + sender.sendMessage(ChatColor.GOLD + "/dd " + cmd.getCommandLabel() + ChatColor.GRAY + " - " + ChatColor.RESET + cmd.getDescription()); + }); + sender.sendMessage(ChatColor.GRAY + "===================="); + + return true; + } + + @Override + public String getDescription() { + return "Show the help screen with all the available commands"; + } +} diff --git a/src/main/java/com/danlegt/damagedisplay/Commands/InfoCommand.java b/src/main/java/com/danlegt/damagedisplay/Commands/InfoCommand.java new file mode 100644 index 0000000..21b68cb --- /dev/null +++ b/src/main/java/com/danlegt/damagedisplay/Commands/InfoCommand.java @@ -0,0 +1,51 @@ +package com.danlegt.damagedisplay.Commands; + +import com.danlegt.damagedisplay.DamageDisplay; +import org.bukkit.ChatColor; +import org.bukkit.command.Command; +import org.bukkit.command.CommandSender; +import org.bukkit.configuration.file.FileConfiguration; + +import java.util.List; +import java.util.Objects; + +public class InfoCommand implements DDCommand{ + + @Override + public String getCommandLabel() { + return "info"; + } + + @Override + public boolean handleCommand(CommandSender sender, Command command, String label, String[] args) { + if ( !sender.hasPermission("damagedisplay.command.info") ) { + sender.sendMessage(ChatColor.RED + "You do not have permission to use this command."); + return true; + } + + FileConfiguration config = DamageDisplay.me.getConfig(); + sender.sendMessage(ChatColor.GREEN + "DamageDisplay (" + ChatColor.AQUA + DamageDisplay.me.getDescription().getVersion() + ChatColor.GREEN + ")"); + for (String key : config.getKeys(true)) { + var val = config.get(key); + if ( val == null ) { + val = ""; + } + + if ( val instanceof Boolean v) { + val = v ? "True" : "False"; + } + + if ( val instanceof String || val instanceof Integer || val instanceof Float || val instanceof Double ) { + sender.sendMessage(ChatColor.GOLD + key + ChatColor.GRAY + ": " + ChatColor.WHITE + config.get(key)); + } + } + + return true; + } + + @Override + public String getDescription() { + return "Retrieve information about the plugin"; + } + +} diff --git a/src/main/java/com/danlegt/damagedisplay/Commands/ReloadCommand.java b/src/main/java/com/danlegt/damagedisplay/Commands/ReloadCommand.java new file mode 100644 index 0000000..32677e6 --- /dev/null +++ b/src/main/java/com/danlegt/damagedisplay/Commands/ReloadCommand.java @@ -0,0 +1,31 @@ +package com.danlegt.damagedisplay.Commands; + +import com.danlegt.damagedisplay.DamageDisplay; +import org.bukkit.ChatColor; +import org.bukkit.command.Command; +import org.bukkit.command.CommandSender; + +public class ReloadCommand implements DDCommand { + @Override + public String getCommandLabel() { + return "reload"; + } + + @Override + public boolean handleCommand(CommandSender sender, Command command, String label, String[] args) { + if ( !sender.hasPermission("damagedisplay.command.reload") ) { + sender.sendMessage(ChatColor.RED + "You do not have permission to use this command."); + return true; + } + + sender.sendMessage(ChatColor.GREEN + "Configuration reloaded."); + DamageDisplay.me.reloadConfig(); + + return true; + } + + @Override + public String getDescription() { + return "Reload the configuration from the config file into memory."; + } +} diff --git a/src/main/java/com/danlegt/damagedisplay/Commands/ToggleCommand.java b/src/main/java/com/danlegt/damagedisplay/Commands/ToggleCommand.java new file mode 100644 index 0000000..1540b58 --- /dev/null +++ b/src/main/java/com/danlegt/damagedisplay/Commands/ToggleCommand.java @@ -0,0 +1,48 @@ +package com.danlegt.damagedisplay.Commands; + +import com.danlegt.damagedisplay.DamageDisplay; +import org.bukkit.ChatColor; +import org.bukkit.command.Command; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +import java.util.ArrayList; +import java.util.List; + +public class ToggleCommand implements DDCommand { + + public static List disabledPlayers = new ArrayList<>(); + + @Override + public String getCommandLabel() { + return "toggle"; + } + + @Override + public boolean handleCommand(CommandSender sender, Command command, String label, String[] args) { + if ( !sender.hasPermission("damagedisplay.command.toggle") ) { + sender.sendMessage(ChatColor.RED + "You do not have permission to use this command."); + return true; + } + + if ( !(sender instanceof Player player) ) { + sender.sendMessage(ChatColor.RED + "Only players can use this command."); + return true; + } + + if ( ToggleCommand.disabledPlayers.contains(player) ) { + sender.sendMessage("Action bar messages toggled " + ChatColor.GREEN + "ON"); + ToggleCommand.disabledPlayers.remove(player); + } else { + sender.sendMessage("Action bar messages toggled " + ChatColor.RED + "OFF"); + ToggleCommand.disabledPlayers.add(player); + } + + return true; + } + + @Override + public String getDescription() { + return "Toggle the actionbar message you receive whenever dealing/taking damage"; + } +} diff --git a/src/main/java/com/danlegt/damagedisplay/DamageDisplay.java b/src/main/java/com/danlegt/damagedisplay/DamageDisplay.java index ccf60b9..c545965 100644 --- a/src/main/java/com/danlegt/damagedisplay/DamageDisplay.java +++ b/src/main/java/com/danlegt/damagedisplay/DamageDisplay.java @@ -1,5 +1,6 @@ package com.danlegt.damagedisplay; +import com.danlegt.damagedisplay.Commands.CommandManager; import com.danlegt.damagedisplay.Event.EventManager; import org.bstats.bukkit.Metrics; import org.bukkit.plugin.java.JavaPlugin; @@ -21,7 +22,8 @@ public void onEnable() { RegisterMetrics(); // Save the default settings if they are not saved - DamageDisplay.me.saveDefaultConfig(); + this.saveDefaultConfig(); + this.getCommand("dd").setExecutor(new CommandManager()); } @Override diff --git a/src/main/java/com/danlegt/damagedisplay/Event/EventListeners/OnDamageHandler.java b/src/main/java/com/danlegt/damagedisplay/Event/EventListeners/OnDamageHandler.java index 3320ee9..ae2912e 100644 --- a/src/main/java/com/danlegt/damagedisplay/Event/EventListeners/OnDamageHandler.java +++ b/src/main/java/com/danlegt/damagedisplay/Event/EventListeners/OnDamageHandler.java @@ -1,5 +1,6 @@ package com.danlegt.damagedisplay.Event.EventListeners; +import com.danlegt.damagedisplay.Commands.ToggleCommand; import com.danlegt.damagedisplay.DamageDisplay; import com.danlegt.damagedisplay.Services.ActionBar; import org.bukkit.Bukkit; @@ -30,6 +31,10 @@ public void onPlayerDamage(EntityDamageByEntityEvent e) { if ( e.getDamager().getType().equals(EntityType.PLAYER) ) { // Yoink a ref to the player Player p = (Player) e.getDamager(); + + // Toggle Setting check + if (ToggleCommand.disabledPlayers.contains(p)) return; + // Permission check if ( !p.hasPermission("damagedisplay.enabled") ) return; @@ -46,6 +51,9 @@ public void onPlayerDamage(EntityDamageByEntityEvent e) { if ( !p.hasPermission("damagedisplay.enabled") ) return; + // Toggle Setting check + if (ToggleCommand.disabledPlayers.contains(p)) return; + // Send out a Damage indicator to the player ActionBar.sendToPlayer(p, "★ Damage Dealt: " + ChatColor.YELLOW + Math.round(e.getDamage() * 100.0) / 100.0); } @@ -66,6 +74,9 @@ public void onEnvironmentDamage(EntityDamageEvent e) { if ( !p.hasPermission("damagedisplay.enabled") ) return; + // Toggle Setting check + if (ToggleCommand.disabledPlayers.contains(p)) return; + // Send out the Damage indicator to the player ActionBar.sendToPlayer(p, "☠ Damage Taken: " + ChatColor.RED + receivedDamage + ChatColor.GRAY + " | " + ActionBar.parseStringToPretty(e.getCause().toString()) ); } diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index f55c5db..bf741e2 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -6,13 +6,24 @@ prefix: DamageDisplay authors: [Daniel Legt ] description: "A simple and efficient plugin for displaying damage events" website: https://danlegt.com +commands: + dd: + description: "Main command for DamageDisplay" + usage: "/ " + aliases: [damagedisplay] permissions: damagedisplay.enabled: default: true description: "Grants permission to view ActionBar messages to a user" damagedisplay.command.info: default: false - description: "Grants permission to /damagedisplay info to show information about the plugin" + description: "Grants permission to /dd info to show information about the plugin" damagedisplay.command.toggle: + default: true + description: "Grants permission to /dd toggle which enables or disables the ActionBar messages" + damagedisplay.command.reload: + default: false + description: "Grants permission to /dd reload which will reload the configuration file, so you won't have to reload it" + damagedisplay.command.help: default: false - description: "Grants permission to /damagedisplay toggle which enables or disables the ActionBar messages" \ No newline at end of file + description: "Grants permission to /dd help" \ No newline at end of file