set location and rotation command enhancement and tab complete

This commit is contained in:
D3v1s0m 2024-01-10 00:34:24 +05:30
parent da4e46497c
commit ecec49f2bb
No known key found for this signature in database
GPG Key ID: FA1F770C7B1D40C1
2 changed files with 42 additions and 5 deletions

@ -10,6 +10,7 @@ import lol.pyr.znpcsplus.util.NpcLocation;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor; import net.kyori.adventure.text.format.NamedTextColor;
import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
@ -24,9 +25,9 @@ public class SetLocationCommand implements CommandHandler {
public void run(CommandContext context) throws CommandExecutionException { public void run(CommandContext context) throws CommandExecutionException {
context.setUsage(context.getLabel() + " setlocation <id> <x> <y> <z>"); context.setUsage(context.getLabel() + " setlocation <id> <x> <y> <z>");
NpcImpl npc = context.parse(NpcEntryImpl.class).getNpc(); NpcImpl npc = context.parse(NpcEntryImpl.class).getNpc();
double x = context.parse(Double.class); double x = parseLocation(context.popString(), npc.getLocation().getX());
double y = context.parse(Double.class); double y = parseLocation(context.popString(), npc.getLocation().getY());
double z = context.parse(Double.class); double z = parseLocation(context.popString(), npc.getLocation().getZ());
npc.setLocation(new NpcLocation(x, y, z, npc.getLocation().getYaw(), npc.getLocation().getPitch())); npc.setLocation(new NpcLocation(x, y, z, npc.getLocation().getYaw(), npc.getLocation().getPitch()));
context.send(Component.text("NPC has been moved to " + x + ", " + y + ", " + z + ".", NamedTextColor.GREEN)); context.send(Component.text("NPC has been moved to " + x + ", " + y + ", " + z + ".", NamedTextColor.GREEN));
} }
@ -34,6 +35,22 @@ public class SetLocationCommand implements CommandHandler {
@Override @Override
public List<String> suggest(CommandContext context) throws CommandExecutionException { public List<String> suggest(CommandContext context) throws CommandExecutionException {
if (context.argSize() == 1) return context.suggestCollection(npcRegistry.getModifiableIds()); if (context.argSize() == 1) return context.suggestCollection(npcRegistry.getModifiableIds());
NpcImpl npc = context.suggestionParse(0, NpcEntryImpl.class).getNpc();
if (context.argSize() == 2) return Arrays.asList(String.valueOf(npc.getLocation().getX()), "~");
else if (context.argSize() == 3) return Arrays.asList(String.valueOf(npc.getLocation().getY()), "~");
else if (context.argSize() == 4) return Arrays.asList(String.valueOf(npc.getLocation().getZ()), "~");
return Collections.emptyList(); return Collections.emptyList();
} }
private static double parseLocation(String input, double current) throws CommandExecutionException {
if (input.equals("~")) return current;
if (input.startsWith("~")) {
try {
return current + Double.parseDouble(input.substring(1));
} catch (NumberFormatException e) {
throw new CommandExecutionException();
}
}
return Double.parseDouble(input);
}
} }

@ -10,6 +10,7 @@ import lol.pyr.znpcsplus.util.NpcLocation;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor; import net.kyori.adventure.text.format.NamedTextColor;
import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
@ -24,8 +25,12 @@ public class SetRotationCommand implements CommandHandler {
public void run(CommandContext context) throws CommandExecutionException { public void run(CommandContext context) throws CommandExecutionException {
context.setUsage(context.getLabel() + " setrotation <id> <yaw> <pitch>"); context.setUsage(context.getLabel() + " setrotation <id> <yaw> <pitch>");
NpcImpl npc = context.parse(NpcEntryImpl.class).getNpc(); NpcImpl npc = context.parse(NpcEntryImpl.class).getNpc();
float yaw = context.parse(Float.class); float yaw = parseRotation(context.popString(), npc.getLocation().getYaw());
float pitch = context.parse(Float.class); float pitch = parseRotation(context.popString(), npc.getLocation().getPitch());
if (pitch < -90 || pitch > 90) {
pitch = Math.min(Math.max(pitch, -90), 90);
context.send(Component.text("Warning: pitch is outside of the -90 to 90 range. It has been normalized to " + pitch + ".", NamedTextColor.YELLOW));
}
npc.setLocation(new NpcLocation(npc.getLocation().getX(), npc.getLocation().getY(), npc.getLocation().getZ(), yaw, pitch)); npc.setLocation(new NpcLocation(npc.getLocation().getX(), npc.getLocation().getY(), npc.getLocation().getZ(), yaw, pitch));
context.send(Component.text("NPC has been rotated to " + yaw + ", " + pitch + ".", NamedTextColor.GREEN)); context.send(Component.text("NPC has been rotated to " + yaw + ", " + pitch + ".", NamedTextColor.GREEN));
} }
@ -33,6 +38,21 @@ public class SetRotationCommand implements CommandHandler {
@Override @Override
public List<String> suggest(CommandContext context) throws CommandExecutionException { public List<String> suggest(CommandContext context) throws CommandExecutionException {
if (context.argSize() == 1) return context.suggestCollection(npcRegistry.getModifiableIds()); if (context.argSize() == 1) return context.suggestCollection(npcRegistry.getModifiableIds());
NpcImpl npc = context.suggestionParse(0, NpcEntryImpl.class).getNpc();
if (context.argSize() == 2) return Arrays.asList(String.valueOf(npc.getLocation().getYaw()), "~");
else if (context.argSize() == 3) return Arrays.asList(String.valueOf(npc.getLocation().getPitch()), "~");
return Collections.emptyList(); return Collections.emptyList();
} }
private static float parseRotation(String input, float current) throws CommandExecutionException {
if (input.equals("~")) return current;
if (input.startsWith("~")) {
try {
return current + Float.parseFloat(input.substring(1));
} catch (NumberFormatException e) {
throw new CommandExecutionException();
}
}
return Float.parseFloat(input);
}
} }