Skip to content

Commit

Permalink
Feature/commands (#3)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
JustKato authored Jan 14, 2024
1 parent 6252416 commit 9570db0
Show file tree
Hide file tree
Showing 11 changed files with 251 additions and 6 deletions.
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.danlegt</groupId>
<artifactId>DamageDisplay</artifactId>
<version>0.2.0</version>
<version>0.3.5</version>
<packaging>jar</packaging>

<name>DamageDisplay</name>
Expand Down
Original file line number Diff line number Diff line change
@@ -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<DDCommand> 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 <info|help|toggle|reload>");
return true;
}

for ( var cmd: registeredCommands ) {
if ( cmd.getCommandLabel().equalsIgnoreCase(args[0]) ) {
return cmd.handleCommand(sender, command, label, args);
}
}

return false;
}

}
14 changes: 14 additions & 0 deletions src/main/java/com/danlegt/damagedisplay/Commands/DDCommand.java
Original file line number Diff line number Diff line change
@@ -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();

}
33 changes: 33 additions & 0 deletions src/main/java/com/danlegt/damagedisplay/Commands/HelpCommand.java
Original file line number Diff line number Diff line change
@@ -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";
}
}
51 changes: 51 additions & 0 deletions src/main/java/com/danlegt/damagedisplay/Commands/InfoCommand.java
Original file line number Diff line number Diff line change
@@ -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";
}

}
Original file line number Diff line number Diff line change
@@ -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.";
}
}
Original file line number Diff line number Diff line change
@@ -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<Player> 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";
}
}
4 changes: 3 additions & 1 deletion src/main/java/com/danlegt/damagedisplay/DamageDisplay.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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);
}
Expand All @@ -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()) );
}
Expand Down
15 changes: 13 additions & 2 deletions src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,24 @@ prefix: DamageDisplay
authors: [Daniel Legt <[email protected]>]
description: "A simple and efficient plugin for displaying damage events"
website: https://danlegt.com
commands:
dd:
description: "Main command for DamageDisplay"
usage: "/<command> <info|help|toggle|reload>"
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"
description: "Grants permission to /dd help"

0 comments on commit 9570db0

Please sign in to comment.