-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from das-kaesebrot/feature/ingame-commands
Merge in-game commands into main
- Loading branch information
Showing
8 changed files
with
252 additions
and
49 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
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
172 changes: 172 additions & 0 deletions
172
src/main/java/eu/kaesebrot/dev/commands/CronAnnouncerCommand.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,172 @@ | ||
package eu.kaesebrot.dev.commands; | ||
|
||
import eu.kaesebrot.dev.CronAnnouncerPlugin; | ||
import eu.kaesebrot.dev.utils.ScheduleConfigParser; | ||
import org.bukkit.command.Command; | ||
import org.bukkit.command.CommandExecutor; | ||
import org.bukkit.command.CommandSender; | ||
import org.bukkit.entity.Player; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
public class CronAnnouncerCommand implements CommandExecutor { | ||
private final String KEY_ROOT = "schedules"; // keep this key in sync with ScheduleConfigParser! | ||
private final String SUBCOMMAND_LIST = "list"; | ||
private final String SUBCOMMAND_ADD = "add"; | ||
private final String SUBCOMMAND_REMOVE = "rm"; | ||
private final String SUBCOMMAND_RELOAD = "reload"; | ||
|
||
private final String PERMISSION_SEPARATOR = "."; | ||
private final String PERMISSION_ROOT = "eu.kaesebrot.dev.cronannouncer"; | ||
private final String PERMISSION_LIST = PERMISSION_ROOT + PERMISSION_SEPARATOR + "list"; | ||
private final String PERMISSION_ADD = PERMISSION_ROOT + PERMISSION_SEPARATOR + "add"; | ||
private final String PERMISSION_REMOVE = PERMISSION_ROOT + PERMISSION_SEPARATOR + "remove"; | ||
private final String PERMISSION_RELOAD = PERMISSION_ROOT + PERMISSION_SEPARATOR + "reload"; | ||
private final CronAnnouncerPlugin plugin; | ||
|
||
public CronAnnouncerCommand(CronAnnouncerPlugin plugin) { | ||
this.plugin = plugin; | ||
} | ||
|
||
@Override | ||
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { | ||
// don't handle if the args are empty or if the command doesn't come from a player | ||
if (!(sender instanceof Player player) || args.length == 0) | ||
return false; | ||
|
||
List<String> argsList = Arrays.asList(args); | ||
|
||
switch (argsList.get(0)) { | ||
case SUBCOMMAND_LIST -> { | ||
if (args.length == 1 && player.hasPermission(PERMISSION_LIST)) { | ||
player.sendMessage(listRegisteredMessages()); | ||
return true; | ||
} | ||
} | ||
case SUBCOMMAND_ADD -> { | ||
// command: /cronannouncer add name "schedule" "message" type | ||
argsList = correctlyParseArgsWithQuotes(argsList); | ||
|
||
if (argsList.size() == 5 && player.hasPermission(PERMISSION_ADD)) { | ||
String key = argsList.get(1).replaceAll("^\"|\"$", ""); | ||
String schedule = argsList.get(2); | ||
String message = argsList.get(3); | ||
String type = argsList.get(4); | ||
|
||
if (plugin.getCronAnnouncerConfig().getScheduledMessageMap().containsKey(key)) { | ||
player.sendMessage(String.format("Key '%s' already exists, please pick another name", key)); | ||
return false; | ||
} | ||
|
||
try { | ||
var newMessage = new ScheduleConfigParser(plugin).parseFromStrings(schedule, message, type); | ||
|
||
plugin.getLogger().info(String.format("Added new message via command: %s", newMessage.toString())); | ||
|
||
plugin.getConfig().set(String.format("%s.%s", KEY_ROOT, key), newMessage.asStringMap()); | ||
plugin.saveConfig(); | ||
plugin.init(); | ||
|
||
player.sendMessage(String.format("Successfully added new message:\n[%s]\n%s\n", key, newMessage.toStringInCommand())); | ||
|
||
return true; | ||
|
||
} catch (Exception e) { | ||
player.sendMessage("Error while parsing!\nSyntax: /cronannouncer add <unique-name> \"<cron-expression>\" \"<message-text>\" <type>"); | ||
return false; | ||
} | ||
} | ||
} | ||
case SUBCOMMAND_REMOVE -> { | ||
if (args.length == 2 && player.hasPermission(PERMISSION_REMOVE)) { | ||
var messageKey = argsList.get(1).toLowerCase(); | ||
if (removeScheduledMessage(messageKey)) { | ||
player.sendMessage(String.format("Successfully removed scheduled message '%s'", messageKey)); | ||
return true; | ||
} else { | ||
player.sendMessage(String.format("Failed removing scheduled message - no message found by key '%s'", messageKey)); | ||
return false; | ||
} | ||
} | ||
} | ||
case SUBCOMMAND_RELOAD -> { | ||
if (args.length == 1 && player.hasPermission(PERMISSION_RELOAD)) { | ||
plugin.init(); | ||
player.sendMessage("Reload successful"); | ||
return true; | ||
} | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
private String listRegisteredMessages() | ||
{ | ||
var messages = plugin.getCronAnnouncerConfig().getScheduledMessageMap(); | ||
|
||
if (messages.isEmpty()) | ||
return "No scheduled messages registered (yet!)"; | ||
|
||
StringBuilder returnText = new StringBuilder(); | ||
returnText.append("Registered messages:\n"); | ||
|
||
for (var message : messages.entrySet()) { | ||
returnText.append(String.format("[%s]\n%s\n", message.getKey(), message.getValue().toStringInCommand())); | ||
} | ||
|
||
return returnText.toString(); | ||
} | ||
|
||
private boolean removeScheduledMessage(String messageKey) | ||
{ | ||
var messages = plugin.getCronAnnouncerConfig().getScheduledMessageMap(); | ||
|
||
if (messages.containsKey(messageKey)) { | ||
plugin.getConfig().set(String.format("%s.%s", KEY_ROOT, messageKey), null); | ||
plugin.saveConfig(); | ||
plugin.init(); | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
private List<String> correctlyParseArgsWithQuotes(List<String> args) { | ||
|
||
List<String> parsedArgs = new ArrayList<>(); | ||
String argsString = String.join(" ", args).trim() + " "; // add a space to the end to cover the last element | ||
|
||
boolean quoteFound = false; | ||
int lastQuoteIndex = 0; | ||
int lastSpaceIndex = -1; | ||
|
||
for (int index = 0; index < argsString.length(); index++) { | ||
if (argsString.charAt(index) == '\"') { | ||
if (quoteFound) { | ||
parsedArgs.add(argsString.substring(lastQuoteIndex + 1, index)); | ||
} else { | ||
lastQuoteIndex = index; | ||
} | ||
|
||
quoteFound = !quoteFound; | ||
} | ||
|
||
if (!quoteFound | ||
&& argsString.charAt(index) == ' ' | ||
&& argsString.charAt(index - 1) != '\"') { | ||
parsedArgs.add(argsString.substring(lastSpaceIndex + 1, index)); | ||
} | ||
|
||
if (argsString.charAt(index) == ' ') { | ||
lastSpaceIndex = index; | ||
} | ||
} | ||
|
||
plugin.getLogger().info(parsedArgs.toString()); | ||
|
||
return parsedArgs; | ||
} | ||
} |
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