-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add /playerjoingroup [help, reload, status] commands (#2)
- Loading branch information
Showing
9 changed files
with
219 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
spigot/src/main/java/fr/freebuild/playerjoingroup/spigot/commands/Command.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package fr.freebuild.playerjoingroup.spigot.commands; | ||
|
||
import org.bukkit.command.CommandSender; | ||
|
||
public abstract class Command { | ||
private final String name; | ||
private final String permission; | ||
private final String description; | ||
|
||
protected Command(String name) { | ||
this(name, null, null); | ||
} | ||
|
||
protected Command(String name, String permission) { | ||
this(name,null, permission); | ||
} | ||
|
||
protected Command(String name, String description, String permission) { | ||
this.name = name; | ||
this.permission = permission; | ||
this.description = description; | ||
} | ||
|
||
public abstract void execute(CommandSender sender, String[] args); | ||
|
||
public boolean hasPermission(CommandSender sender) { | ||
return permission == null || sender.hasPermission(permission); | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public String getPermission() { | ||
return permission; | ||
} | ||
|
||
public String getDescription() { | ||
return description; | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
spigot/src/main/java/fr/freebuild/playerjoingroup/spigot/commands/CommandHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package fr.freebuild.playerjoingroup.spigot.commands; | ||
|
||
import fr.freebuild.playerjoingroup.spigot.PlayerJoinGroup; | ||
import org.bukkit.command.CommandExecutor; | ||
import org.bukkit.command.CommandSender; | ||
import org.bukkit.command.TabCompleter; | ||
|
||
import java.util.*; | ||
|
||
public class CommandHandler implements CommandExecutor, TabCompleter { | ||
private final Map<String, Command> commands; | ||
private final String commandName; | ||
|
||
public CommandHandler(PlayerJoinGroup plugin, String commandName) { | ||
this.commands = new HashMap<>(); | ||
this.commandName = commandName.toLowerCase(); | ||
|
||
plugin.getCommand(commandName).setExecutor(this); | ||
plugin.getCommand(commandName).setTabCompleter(this); | ||
|
||
this.register(new HelpCommand(this, plugin.getName())); | ||
} | ||
|
||
@Override | ||
public boolean onCommand(CommandSender sender, org.bukkit.command.Command command, String s, String[] args) { | ||
if (!command.getName().equals(this.commandName) || args.length < 1) | ||
return false; | ||
|
||
Command cmd = commands.get(args[0].toLowerCase()); | ||
if (cmd == null) | ||
return false; | ||
|
||
if (!cmd.hasPermission(sender)) { | ||
sender.sendMessage("§cYou don't have the permission to execute this command."); | ||
return true; | ||
} | ||
|
||
List<String> argList = Arrays.asList(args).subList(1, args.length); | ||
cmd.execute(sender, argList.toArray(new String[0])); | ||
return true; | ||
} | ||
|
||
@Override | ||
public List<String> onTabComplete(CommandSender sender, org.bukkit.command.Command command, String s, String[] args) { | ||
if (!command.getName().equals(this.commandName) || args.length < 1) | ||
return null; | ||
|
||
List<String> results = new LinkedList<>(); | ||
boolean ignoreArg = args.length == 0; | ||
for (Command cmd: commands.values()) | ||
if (cmd.hasPermission(sender) && (ignoreArg || cmd.getName().startsWith(args[0]))) | ||
results.add(cmd.getName()); | ||
return results; | ||
} | ||
|
||
public void register(Command command) { | ||
this.commands.put(command.getName().toLowerCase(), command); | ||
} | ||
|
||
public Map<String, Command> getCommands() { | ||
return commands; | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
spigot/src/main/java/fr/freebuild/playerjoingroup/spigot/commands/HelpCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package fr.freebuild.playerjoingroup.spigot.commands; | ||
|
||
import org.bukkit.ChatColor; | ||
import org.bukkit.command.CommandSender; | ||
|
||
public class HelpCommand extends Command { | ||
private final CommandHandler commandHandler; | ||
private final String pluginName; | ||
|
||
protected HelpCommand(CommandHandler commandHandler, String pluginName) { | ||
super("help", "Show this help message", "playerjoingroup.use"); | ||
this.commandHandler = commandHandler; | ||
this.pluginName = pluginName; | ||
} | ||
|
||
@Override | ||
public void execute(CommandSender sender, String[] args) { | ||
StringBuilder builder = new StringBuilder() | ||
.append(ChatColor.GOLD) | ||
.append(this.pluginName) | ||
.append(ChatColor.RESET) | ||
.append("\nCommands: \n"); | ||
|
||
for (Command command : this.commandHandler.getCommands().values()) { | ||
if (command.hasPermission(sender)) { | ||
builder.append("- ").append(ChatColor.BLUE).append(command.getName()).append(ChatColor.RESET); | ||
if (command.getDescription() != null) { | ||
builder.append(": ").append(command.getDescription()); | ||
} | ||
builder.append("\n"); | ||
} | ||
} | ||
sender.sendMessage(builder.deleteCharAt(builder.length() - 1).toString()); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
spigot/src/main/java/fr/freebuild/playerjoingroup/spigot/commands/ReloadCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package fr.freebuild.playerjoingroup.spigot.commands; | ||
|
||
import fr.freebuild.playerjoingroup.spigot.PlayerJoinGroup; | ||
import org.bukkit.command.CommandSender; | ||
|
||
public class ReloadCommand extends Command { | ||
private final PlayerJoinGroup plugin; | ||
|
||
public ReloadCommand(PlayerJoinGroup plugin) { | ||
super("reload", "Reload the plugin", "playerjoingroup.reload"); | ||
this.plugin = plugin; | ||
} | ||
|
||
@Override | ||
public void execute(CommandSender sender, String[] args) { | ||
this.plugin.reloadConfig(); | ||
if (!this.plugin.isMessageManagerEnabled()) { | ||
this.plugin.enableMessageManager(true); | ||
this.plugin.getMessageManager().initialize(); | ||
} | ||
this.plugin.getFireworkBuilder().load(); | ||
sender.sendMessage("§aPlayerJoinGroup has been reloaded."); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
spigot/src/main/java/fr/freebuild/playerjoingroup/spigot/commands/StatusCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package fr.freebuild.playerjoingroup.spigot.commands; | ||
|
||
import fr.freebuild.playerjoingroup.spigot.PlayerJoinGroup; | ||
import org.bukkit.command.CommandSender; | ||
|
||
public class StatusCommand extends Command { | ||
private final PlayerJoinGroup plugin; | ||
|
||
public StatusCommand(PlayerJoinGroup plugin) { | ||
super("status", "Check if the group feature is enable.", "playerjoingroup.status"); | ||
this.plugin = plugin; | ||
} | ||
|
||
@Override | ||
public void execute(CommandSender sender, String[] args) { | ||
boolean isMessageManagerEnabled = this.plugin.isMessageManagerEnabled(); | ||
sender.sendMessage("§fPlayerJoinGroup group feature is " + (isMessageManagerEnabled ? "§aenabled" : "§cdisabled") + "."); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters