Skip to content

Commit

Permalink
Add /playerjoingroup [help, reload, status] commands (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
G-Lauz committed Jun 11, 2023
1 parent eb5af49 commit 525a7c6
Show file tree
Hide file tree
Showing 9 changed files with 219 additions and 2 deletions.
6 changes: 6 additions & 0 deletions .dev/compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ services:
args:
- PROXY_PORT=25577
image: g-lauz/bungeecord
stdin_open: true
tty: true
depends_on:
- server1
- server2
Expand All @@ -24,6 +26,8 @@ services:
- SERVER_MEMORY=1
- SERVER_PORT=25565
image: g-lauz/spigot:1.19.4
stdin_open: true
tty: true
volumes:
- ./spigot/server1/plugins:/app/server/plugins
server2:
Expand All @@ -35,5 +39,7 @@ services:
- SERVER_MEMORY=1
- SERVER_PORT=25565
image: g-lauz/spigot:1.19.4
stdin_open: true
tty: true
volumes:
- ./spigot/server2/plugins:/app/server/plugins
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public void run() {
private void consumeMessage() {
while(!Thread.currentThread().isInterrupted()) {
synchronized (messages) {
if (messages.isEmpty()) {
while (messages.isEmpty()) {
this.waitForMessage();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package fr.freebuild.playerjoingroup.spigot;

import fr.freebuild.playerjoingroup.spigot.commands.CommandHandler;
import fr.freebuild.playerjoingroup.spigot.commands.ReloadCommand;
import fr.freebuild.playerjoingroup.spigot.commands.StatusCommand;
import fr.freebuild.playerjoingroup.spigot.firework.FireworkBuilder;
import fr.freebuild.playerjoingroup.spigot.listener.PlayerJoinListener;

Expand Down Expand Up @@ -39,6 +42,10 @@ public void onEnable() {
getServer().getPluginManager().registerEvents(new PlayerJoinListener(this), this);
getServer().getPluginManager().registerEvents(new SocketConnectedListener(this),this);

CommandHandler commandHandler = new CommandHandler(this, "playerjoingroup");
commandHandler.register(new ReloadCommand(this));
commandHandler.register(new StatusCommand(this));

this.messagesManager.initialize();

this.fireworkBuilder = new FireworkBuilder();
Expand Down
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;
}
}
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;
}
}
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());
}
}
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.");
}
}
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") + ".");
}
}
24 changes: 23 additions & 1 deletion spigot/src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,26 @@ main: fr.freebuild.playerjoingroup.spigot.PlayerJoinGroup
version: ${project.version}
author: G-Lauz
api-version: 1.13
description: Plugin that display custom message on player join
description: Plugin that display custom message on player join
commands:
playerjoingroup:
description: PlayerJoinGroup admin command
usage: PlayerJoinGroup command. Type '/playerjoingroup help' for more informations.
permission: playerjoingroup.use
aliases: [pjg]
permissions:
playerjoingroup.*:
description: Give access to all PlayerJoinGroup functionalities
children:
playerjoingroup.use: true
playerjoingroup.reload: true
playerjoingroup.status: true
playerjoingroup.use:
description: Players with this permission can use the plugin
default: op
playerjoingroup.reload:
description: Players with this permission can reload the plugin
default: op
playerjoingroup.status:
description: Players with this permission can see the status of the plugin
default: op

0 comments on commit 525a7c6

Please sign in to comment.