diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/commands/SetLocationCommand.java b/plugin/src/main/java/lol/pyr/znpcsplus/commands/SetLocationCommand.java index 91cde48..948d017 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/commands/SetLocationCommand.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/commands/SetLocationCommand.java @@ -10,6 +10,7 @@ import lol.pyr.znpcsplus.util.NpcLocation; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.format.NamedTextColor; +import java.util.Arrays; import java.util.Collections; import java.util.List; @@ -24,9 +25,9 @@ public class SetLocationCommand implements CommandHandler { public void run(CommandContext context) throws CommandExecutionException { context.setUsage(context.getLabel() + " setlocation "); NpcImpl npc = context.parse(NpcEntryImpl.class).getNpc(); - double x = context.parse(Double.class); - double y = context.parse(Double.class); - double z = context.parse(Double.class); + double x = parseLocation(context.popString(), npc.getLocation().getX()); + double y = parseLocation(context.popString(), npc.getLocation().getY()); + double z = parseLocation(context.popString(), npc.getLocation().getZ()); 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)); } @@ -34,6 +35,22 @@ public class SetLocationCommand implements CommandHandler { @Override public List suggest(CommandContext context) throws CommandExecutionException { 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(); } + + 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); + } } diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/commands/SetRotationCommand.java b/plugin/src/main/java/lol/pyr/znpcsplus/commands/SetRotationCommand.java index 99cb7ff..dda74dc 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/commands/SetRotationCommand.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/commands/SetRotationCommand.java @@ -10,6 +10,7 @@ import lol.pyr.znpcsplus.util.NpcLocation; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.format.NamedTextColor; +import java.util.Arrays; import java.util.Collections; import java.util.List; @@ -24,8 +25,12 @@ public class SetRotationCommand implements CommandHandler { public void run(CommandContext context) throws CommandExecutionException { context.setUsage(context.getLabel() + " setrotation "); NpcImpl npc = context.parse(NpcEntryImpl.class).getNpc(); - float yaw = context.parse(Float.class); - float pitch = context.parse(Float.class); + float yaw = parseRotation(context.popString(), npc.getLocation().getYaw()); + 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)); context.send(Component.text("NPC has been rotated to " + yaw + ", " + pitch + ".", NamedTextColor.GREEN)); } @@ -33,6 +38,21 @@ public class SetRotationCommand implements CommandHandler { @Override public List suggest(CommandContext context) throws CommandExecutionException { 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(); } + + 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); + } }