Skip to content

Commit

Permalink
Add support for positions as parameters in commands
Browse files Browse the repository at this point in the history
  • Loading branch information
Redned235 committed Dec 14, 2024
1 parent 29bce0b commit 552a1bf
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@
String permissionNode() default "";

String description() default "";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.battleplugins.arena.command;

import io.papermc.paper.math.Position;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextComponent;
import net.kyori.adventure.text.event.ClickEvent;
Expand Down Expand Up @@ -455,6 +456,30 @@ private Object verifyArgument(CommandSender sender, String arg, Class<?> paramet
return null;
}
}
case "position" -> {
String[] coords = arg.split(",");
if (coords.length != 3) {
return null;
}

double x = Double.parseDouble(coords[0]);
double y = Double.parseDouble(coords[1]);
double z = Double.parseDouble(coords[2]);

return Position.fine(x, y, z);
}
case "blockposition" -> {
String[] coords = arg.split(",");
if (coords.length != 3) {
return null;
}

int x = Integer.parseInt(coords[0]);
int y = Integer.parseInt(coords[1]);
int z = Integer.parseInt(coords[2]);

return Position.block(x, y, z);
}
case "material" -> {
try {
return ItemStackParser.deserializeSingular(arg);
Expand Down Expand Up @@ -519,6 +544,10 @@ protected boolean invalidArgument(CommandSender sender, Class<?> parameter, Stri
Messages.ARENA_DOES_NOT_EXIST.send(sender, input);
return true;
}
case "position", "blockposition" -> {
Messages.INVALID_POSITION.send(sender, input);
return true;
}
}

return false;
Expand Down Expand Up @@ -623,6 +652,7 @@ private String getUsageString(Class<?> parameter, @Nullable Argument argument) {
case "player", "offlineplayer" -> "<player> ";
case "world" -> "<world> ";
case "arena" -> "<arena> ";
case "position", "blockposition" -> "<x,y,z> ";
default -> {
for (SubCommandExecutor subCommandExecutor : this.subCommandExecutors) {
String usage = subCommandExecutor.getUsageString(parameter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public final class Messages {
public static final Message AN_ERROR_OCCURRED = error("command-an-error-occurred", "An error occurred while executing this command: {}!");
public static final Message PLAYER_NOT_ONLINE = error("command-player-not-found", "The player <secondary>{}</secondary> could not be found!");
public static final Message INVALID_TYPE = error("command-invalid-type", "The specified type <secondary>{}</secondary> was not a valid {}!");
public static final Message INVALID_POSITION = error("command-invalid-position", "The specified position (<secondary>{}</secondary>) is not valid! Expected format: <secondary><x>,<y>,<z></secondary>.");
public static final Message CLICK_TO_PREPARE = info("command-click-to-prepare", "Click to prepare: <secondary>{}</secondary>");

// Arena messages
Expand Down

0 comments on commit 552a1bf

Please sign in to comment.