Skip to content

Commit

Permalink
ADD Guard tp/flags subcommands
Browse files Browse the repository at this point in the history
  • Loading branch information
sanguine6660 committed Dec 18, 2023
1 parent 4566faf commit c4beaa3
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,7 @@
*/
public class MessageUtils {

private static LegacyComponentSerializer componentSerializer = LegacyComponentSerializer.builder()
.character('&').hexCharacter('#').build();
private static LegacyComponentSerializer componentSerializer = LegacyComponentSerializer.builder().character('&').hexCharacter('#').build();

/**
* Gets the instance of LegacyComponentSerializer used for message formatting.
Expand Down Expand Up @@ -129,8 +128,7 @@ public static void sendPrefixedMessages(CommandSender sender, String... lines) {
* @param lines The array of message lines to wrap and send.
*/
public static void sendWrappedMessage(CommandSender sender, String... lines) {
sendMessage(sender, "\n" + GuardConstants.CHAT_COLOR +
String.join("\n" + GuardConstants.CHAT_COLOR, lines));
sendMessage(sender, "\n" + GuardConstants.HEADER + "\n" + GuardConstants.CHAT_COLOR + String.join("\n" + GuardConstants.CHAT_COLOR, lines) + "\n&r" + GuardConstants.FOOTER);
}

/**
Expand All @@ -145,8 +143,7 @@ public static void sendErrorMessage(CommandSender sender, Throwable throwable) {
throwable.printStackTrace(pw);
String stackTrace = sw.toString();

String errorMessage = GuardConstants.CHAT_PREFIX +
"&cAn unhandled error occurred: " + throwable.getMessage() + "\n\n" + stackTrace;
String errorMessage = GuardConstants.CHAT_PREFIX + "&cAn unhandled error occurred: " + throwable.getMessage() + "\n\n" + stackTrace;

sendMessage(sender, errorMessage);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
import com.izanagicraft.guard.utils.MessageUtils;
import com.izanagicraft.guard.utils.StringUtils;
import org.bukkit.command.Command;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.YamlConfiguration;

import java.util.ArrayList;
Expand All @@ -75,9 +76,9 @@ public class WorldGuardCommand extends GuardCommand {
public WorldGuardCommand(IzanagiWorldGuardPlugin plugin) {
super("worldguard");
this.plugin = plugin;
setAliases(List.of("wg", "guard"));
setAliases(List.of("g", "wg", "guard"));
setDescription("Guard your worlds and set region specific flags.");
setUsage("&e/worldguard <reload/flag> [flag: <flagname> <value>]");
setUsage("&e/worldguard <reload/flag/flags/tp> [flag: <flagname> <value> | tp: <regionName>]");
setPermission(GuardPermission.COMMAND_WORLDGUARD.getName());
}

Expand Down Expand Up @@ -120,7 +121,7 @@ public void run(CommandSource source, String commandLabel, Command command, Stri
return;
}

if (argCount == 3) {
if (argCount >= 3) {
GuardFlag choice = GuardFlag.getByName(args[1]);
if (choice == null) {
break;
Expand Down Expand Up @@ -152,6 +153,56 @@ public void run(CommandSource source, String commandLabel, Command command, Stri
));
return;
}
break;
case "flags":
if (!source.isPlayer()) {
MessageUtils.sendPrefixedMessage(source, "&cYou can only run this command as a player.");
return;
}

YamlConfiguration config = plugin.getWorldConfigs().get(source.getPlayer().getWorld().getName());

if (config == null) {
MessageUtils.sendPrefixedMessage(source, "&cYour current world is not under protection. &aYou can remove it in the WorldGuard config.yml in section 'ignored' to protect it properly.");
return;
}

List<String> lines = new ArrayList<>();

// TODO: add paginated functionality
// TODO: send wrapped interactive component to make command suggestions on changing value of a flag

ConfigurationSection flagSection = config.getConfigurationSection("flags");

flagSection.getKeys(false).forEach(flag -> {
GuardFlag guardFlag = GuardFlag.getByName(flag);

if (guardFlag != null) {
String flagValue = flagSection.getString(flag);
boolean hasValueEnabled = (!flagValue.equalsIgnoreCase("empty") &&
!flagValue.equalsIgnoreCase("deny") &&
!flagValue.equalsIgnoreCase("false") && !flagValue.isEmpty());

lines.add("&8» &7" + flag + ": " + (hasValueEnabled ? "&a" : "&c") + flagValue + "&r");
}
});

MessageUtils.sendWrappedMessage(source, lines.toArray(new String[lines.size()]));
return;

case "tp":
if (!source.isPlayer()) {
MessageUtils.sendPrefixedMessage(source, "&cYou can only run this command as a player.");
return;
}

if (argCount >= 2 && args[1].equalsIgnoreCase("__global__")) {
// TODO: add region based tp command
source.getPlayer().teleport(source.getPlayer().getWorld().getSpawnLocation().add(0.5, 1, 0.5));
MessageUtils.sendPrefixedMessage(source, "&aYou have been teleported to &e" + args[1] + " &a.");
return;
}

break;
default:
break;
Expand All @@ -171,8 +222,18 @@ public List<String> tab(CommandSource source, String commandLabel, Command cmd,
if (argCount == 1) {
available.add("reload");
available.add("flag");
available.add("flags");
available.add("tp");

StringUtils.copyPartialMatches(args[0], available, completions);
return completions;
}

if (argCount == 2 && args[0].equalsIgnoreCase("tp")) {
// TODO: add list of available regions
available.add("__global__");
StringUtils.copyPartialMatches(args[1], available, completions);
return completions;
}

if (argCount == 2 && args[0].equalsIgnoreCase("flag")) {
Expand All @@ -182,6 +243,7 @@ public List<String> tab(CommandSource source, String commandLabel, Command cmd,
}

StringUtils.copyPartialMatches(args[1], available, completions);
return completions;
}

if (argCount == 3 && args[0].equalsIgnoreCase("flag")) {
Expand All @@ -192,6 +254,7 @@ public List<String> tab(CommandSource source, String commandLabel, Command cmd,
available.addAll(choice.getValidValues());
// System.out.println(String.join(", ", available));
StringUtils.copyPartialMatches(args[2], available, completions);
return completions;
}


Expand Down

0 comments on commit c4beaa3

Please sign in to comment.