diff --git a/src/main/java/net/minestom/server/command/builder/arguments/relative/ArgumentRelativeBlockPosition.java b/src/main/java/net/minestom/server/command/builder/arguments/relative/ArgumentRelativeBlockPosition.java index eeda34d76..dc21b063a 100644 --- a/src/main/java/net/minestom/server/command/builder/arguments/relative/ArgumentRelativeBlockPosition.java +++ b/src/main/java/net/minestom/server/command/builder/arguments/relative/ArgumentRelativeBlockPosition.java @@ -2,10 +2,11 @@ package net.minestom.server.command.builder.arguments.relative; import net.minestom.server.command.builder.NodeMaker; import net.minestom.server.command.builder.exception.ArgumentSyntaxException; +import net.minestom.server.coordinate.Vec; import net.minestom.server.network.packet.server.play.DeclareCommandsPacket; import net.minestom.server.utils.BlockPosition; -import net.minestom.server.utils.location.RelativeBlockPosition; import net.minestom.server.utils.StringUtils; +import net.minestom.server.utils.location.RelativeVec; import org.jetbrains.annotations.NotNull; /** @@ -13,7 +14,7 @@ import org.jetbrains.annotations.NotNull; *

* Example: 5 ~ -3 */ -public class ArgumentRelativeBlockPosition extends ArgumentRelative { +public class ArgumentRelativeBlockPosition extends ArgumentRelative { public ArgumentRelativeBlockPosition(@NotNull String id) { super(id, 3); @@ -21,19 +22,17 @@ public class ArgumentRelativeBlockPosition extends ArgumentRelative { @Override public RelativeVec parse(@NotNull String input) throws ArgumentSyntaxException { final String[] split = input.split(StringUtils.SPACE); - // Check if the value has enough element to be correct if (split.length != getNumberCount()) { throw new ArgumentSyntaxException("Invalid number of values", input, INVALID_NUMBER_COUNT_ERROR); } - Vector vector = new Vector(); + double x = 0, z = 0; boolean relativeX = false; boolean relativeZ = false; @@ -47,18 +47,18 @@ public class ArgumentRelativeVec2 extends ArgumentRelative { final String potentialNumber = element.substring(1); final float number = Float.parseFloat(potentialNumber); if (i == 0) { - vector.setX(number); + x = number; } else if (i == 1) { - vector.setZ(number); + z = number; } } } else { final float number = Float.parseFloat(element); if (i == 0) { - vector.setX(number); + x = number; } else if (i == 1) { - vector.setZ(number); + z = number; } } } catch (NumberFormatException e) { @@ -66,7 +66,7 @@ public class ArgumentRelativeVec2 extends ArgumentRelative { } } - return new RelativeVec(vector, relativeX, false, relativeZ); + return new RelativeVec(new Vec(x, z), relativeX, false, relativeZ); } @Override diff --git a/src/main/java/net/minestom/server/command/builder/arguments/relative/ArgumentRelativeVec3.java b/src/main/java/net/minestom/server/command/builder/arguments/relative/ArgumentRelativeVec3.java index 6e6f8a4fe..f5abd0206 100644 --- a/src/main/java/net/minestom/server/command/builder/arguments/relative/ArgumentRelativeVec3.java +++ b/src/main/java/net/minestom/server/command/builder/arguments/relative/ArgumentRelativeVec3.java @@ -2,10 +2,11 @@ package net.minestom.server.command.builder.arguments.relative; import net.minestom.server.command.builder.NodeMaker; import net.minestom.server.command.builder.exception.ArgumentSyntaxException; +import net.minestom.server.coordinate.Vec; import net.minestom.server.network.packet.server.play.DeclareCommandsPacket; +import net.minestom.server.utils.StringUtils; import net.minestom.server.utils.Vector; import net.minestom.server.utils.location.RelativeVec; -import net.minestom.server.utils.StringUtils; import org.jetbrains.annotations.NotNull; /** @@ -23,17 +24,15 @@ public class ArgumentRelativeVec3 extends ArgumentRelative { @Override public RelativeVec parse(@NotNull String input) throws ArgumentSyntaxException { final String[] split = input.split(StringUtils.SPACE); - // Check if the value has enough element to be correct if (split.length != getNumberCount()) { throw new ArgumentSyntaxException("Invalid number of values", input, INVALID_NUMBER_COUNT_ERROR); } - Vector vector = new Vector(); + double x = 0, y = 0, z = 0; boolean relativeX = false; boolean relativeY = false; boolean relativeZ = false; - for (int i = 0; i < split.length; i++) { final String element = split[i]; try { @@ -50,22 +49,22 @@ public class ArgumentRelativeVec3 extends ArgumentRelative { final String potentialNumber = element.substring(1); final float number = Float.parseFloat(potentialNumber); if (i == 0) { - vector.setX(number); + x = number; } else if (i == 1) { - vector.setY(number); + y = number; } else if (i == 2) { - vector.setZ(number); + z = number; } } } else { final float number = Float.parseFloat(element); if (i == 0) { - vector.setX(number); + x = number; } else if (i == 1) { - vector.setY(number); + y = number; } else if (i == 2) { - vector.setZ(number); + z = number; } } } catch (NumberFormatException e) { @@ -73,7 +72,7 @@ public class ArgumentRelativeVec3 extends ArgumentRelative { } } - return new RelativeVec(vector, relativeX, relativeY, relativeZ); + return new RelativeVec(new Vec(x, y, z), relativeX, relativeY, relativeZ); } @Override diff --git a/src/main/java/net/minestom/server/instance/block/BlockSetter.java b/src/main/java/net/minestom/server/instance/block/BlockSetter.java index dddc179bd..c61758ea9 100644 --- a/src/main/java/net/minestom/server/instance/block/BlockSetter.java +++ b/src/main/java/net/minestom/server/instance/block/BlockSetter.java @@ -1,8 +1,8 @@ package net.minestom.server.instance.block; +import net.minestom.server.coordinate.Point; import net.minestom.server.instance.Instance; import net.minestom.server.instance.batch.Batch; -import net.minestom.server.utils.BlockPosition; import org.jetbrains.annotations.NotNull; /** @@ -13,7 +13,7 @@ import org.jetbrains.annotations.NotNull; public interface BlockSetter { void setBlock(int x, int y, int z, @NotNull Block block); - default void setBlock(@NotNull BlockPosition blockPosition, @NotNull Block block) { - setBlock(blockPosition.getX(), blockPosition.getY(), blockPosition.getZ(), block); + default void setBlock(@NotNull Point blockPosition, @NotNull Block block) { + setBlock(blockPosition.blockX(), blockPosition.blockY(), blockPosition.blockZ(), block); } } diff --git a/src/main/java/net/minestom/server/utils/location/RelativeBlockPosition.java b/src/main/java/net/minestom/server/utils/location/RelativeBlockPosition.java deleted file mode 100644 index 2a3ee592c..000000000 --- a/src/main/java/net/minestom/server/utils/location/RelativeBlockPosition.java +++ /dev/null @@ -1,44 +0,0 @@ -package net.minestom.server.utils.location; - -import net.minestom.server.utils.BlockPosition; -import net.minestom.server.utils.Position; -import org.jetbrains.annotations.Nullable; - -/** - * Represents a relative {@link BlockPosition}. - * - * @see RelativeLocation - */ -public class RelativeBlockPosition extends RelativeLocation { - - public RelativeBlockPosition(BlockPosition location, boolean relativeX, boolean relativeY, boolean relativeZ) { - super(location, relativeX, relativeY, relativeZ); - } - - @Override - public BlockPosition from(@Nullable Position position) { - if (!relativeX && !relativeY && !relativeZ) { - return location.clone(); - } - final Position entityPosition = position != null ? position : new Position(); - - final int x = location.getX() + (relativeX ? (int) entityPosition.getX() : 0); - final int y = location.getY() + (relativeY ? (int) entityPosition.getY() : 0); - final int z = location.getZ() + (relativeZ ? (int) entityPosition.getZ() : 0); - - return new BlockPosition(x, y, z); - } - - @Override - public BlockPosition fromView(@Nullable Position position) { - if (!relativeX && !relativeY && !relativeZ) { - return location.clone(); - } - final Position entityPosition = position != null ? position : new Position(); - - final int x = location.getX() + (relativeX ? (int) entityPosition.getYaw() : 0); - final int z = location.getZ() + (relativeZ ? (int) entityPosition.getPitch() : 0); - - return new BlockPosition(x, 0, z); - } -} diff --git a/src/main/java/net/minestom/server/utils/location/RelativeLocation.java b/src/main/java/net/minestom/server/utils/location/RelativeLocation.java deleted file mode 100644 index 6ac6f705a..000000000 --- a/src/main/java/net/minestom/server/utils/location/RelativeLocation.java +++ /dev/null @@ -1,82 +0,0 @@ -package net.minestom.server.utils.location; - -import net.minestom.server.entity.Entity; -import net.minestom.server.utils.Position; -import net.minestom.server.coordinate.Point; -import net.minestom.server.coordinate.Pos; -import org.jetbrains.annotations.ApiStatus; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; - -/** - * Represents a location which can have fields relative to an {@link Entity} position. - * - * @param the location type - */ -public abstract class RelativeLocation { - - protected T location; - protected boolean relativeX, relativeY, relativeZ; - - public RelativeLocation(@NotNull T location, boolean relativeX, boolean relativeY, boolean relativeZ) { - this.location = location; - this.relativeX = relativeX; - this.relativeY = relativeY; - this.relativeZ = relativeZ; - } - - /** - * Gets the location based on the relative fields and {@code position}. - * - * @param position the relative position - * @return the location - */ - public abstract T from(@Nullable Position position); - - @ApiStatus.Experimental - public abstract T fromView(@Nullable Position position); - - /** - * Gets the location based on the relative fields and {@code entity}. - * - * @param entity the entity to get the relative position from - * @return the location - */ - public T from(@Nullable Entity entity) { - final var entityPosition = entity != null ? entity.getPosition() : Pos.ZERO; - return null;//from(entityPosition); FIXME - } - - @ApiStatus.Experimental - public T fromView(@Nullable Entity entity) { - final var entityPosition = entity != null ? entity.getPosition() : Pos.ZERO; - return null;//fromView(entityPosition); FIXME - } - - /** - * Gets if the 'x' field is relative. - * - * @return true if the 'x' field is relative - */ - public boolean isRelativeX() { - return relativeX; - } - - /** - * Gets if the 'y' field is relative. - * - * @return true if the 'y' field is relative - */ - public boolean isRelativeY() { - return relativeY; - } - - /** - * Gets if the 'z' field is relative. - * - * @return true if the 'z' field is relative - */ - public boolean isRelativeZ() { - return relativeZ; - } -} diff --git a/src/main/java/net/minestom/server/utils/location/RelativeVec.java b/src/main/java/net/minestom/server/utils/location/RelativeVec.java index df1c02ed9..25e4f1332 100644 --- a/src/main/java/net/minestom/server/utils/location/RelativeVec.java +++ b/src/main/java/net/minestom/server/utils/location/RelativeVec.java @@ -1,44 +1,108 @@ package net.minestom.server.utils.location; -import net.minestom.server.utils.Position; -import net.minestom.server.utils.Vector; +import net.minestom.server.command.CommandSender; +import net.minestom.server.coordinate.Point; +import net.minestom.server.coordinate.Pos; +import net.minestom.server.coordinate.Vec; +import net.minestom.server.entity.Entity; +import net.minestom.server.entity.Player; +import org.jetbrains.annotations.ApiStatus; +import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import java.util.Objects; + /** - * Represents a relative {@link Vector}. + * Represents a location which can have fields relative to an {@link Entity} position. * - * @see RelativeLocation + * @param the location type */ -public class RelativeVec extends RelativeLocation { +public final class RelativeVec { - public RelativeVec(Vector location, boolean relativeX, boolean relativeY, boolean relativeZ) { - super(location, relativeX, relativeY, relativeZ); + private final Vec vec; + private boolean relativeX, relativeY, relativeZ; + + public RelativeVec(@NotNull Vec vec, boolean relativeX, boolean relativeY, boolean relativeZ) { + this.vec = vec; + this.relativeX = relativeX; + this.relativeY = relativeY; + this.relativeZ = relativeZ; } - @Override - public Vector from(@Nullable Position position) { + /** + * Gets the location based on the relative fields and {@code position}. + * + * @param point the relative position + * @return the location + */ + public @NotNull Vec from(@Nullable Point point) { if (!relativeX && !relativeY && !relativeZ) { - return location.clone(); + return vec; } - final Position entityPosition = position != null ? position : new Position(); - - final double x = location.getX() + (relativeX ? entityPosition.getX() : 0); - final double y = location.getY() + (relativeY ? entityPosition.getY() : 0); - final double z = location.getZ() + (relativeZ ? entityPosition.getZ() : 0); - - return new Vector(x, y, z); + final var absolute = Objects.requireNonNullElse(point, Vec.ZERO); + final double x = vec.x() + (relativeX ? absolute.x() : 0); + final double y = vec.y() + (relativeY ? absolute.y() : 0); + final double z = vec.z() + (relativeZ ? absolute.z() : 0); + return new Vec(x, y, z); } - @Override - public Vector fromView(@Nullable Position position) { + @ApiStatus.Experimental + public Vec fromView(@Nullable Pos point) { if (!relativeX && !relativeY && !relativeZ) { - return location.clone(); + return vec; } - final Position entityPosition = position != null ? position : new Position(); + final var absolute = Objects.requireNonNullElse(point, Pos.ZERO); + final double x = vec.x() + (relativeX ? absolute.yaw() : 0); + final double z = vec.z() + (relativeZ ? absolute.pitch() : 0); + return new Vec(x, 0, z); + } - final double x = location.getX() + (relativeX ? entityPosition.getYaw() : 0); - final double z = location.getZ() + (relativeZ ? entityPosition.getPitch() : 0); + /** + * Gets the location based on the relative fields and {@code entity}. + * + * @param entity the entity to get the relative position from + * @return the location + */ + public @NotNull Vec from(@Nullable Entity entity) { + final var entityPosition = entity != null ? entity.getPosition() : Pos.ZERO; + return from(entityPosition); + } - return new Vector(x, 0, z); + public @NotNull Vec fromSender(@Nullable CommandSender sender) { + final var entityPosition = sender instanceof Player ? ((Player) sender).getPosition() : Pos.ZERO; + return from(entityPosition); + } + + @ApiStatus.Experimental + public @NotNull Vec fromView(@Nullable Entity entity) { + final var entityPosition = entity != null ? entity.getPosition() : Pos.ZERO; + return fromView(entityPosition); + } + + /** + * Gets if the 'x' field is relative. + * + * @return true if the 'x' field is relative + */ + public boolean isRelativeX() { + return relativeX; + } + + /** + * Gets if the 'y' field is relative. + * + * @return true if the 'y' field is relative + */ + public boolean isRelativeY() { + return relativeY; + } + + /** + * Gets if the 'z' field is relative. + * + * @return true if the 'z' field is relative + */ + public boolean isRelativeZ() { + return relativeZ; } } diff --git a/src/test/java/demo/commands/SetBlockCommand.java b/src/test/java/demo/commands/SetBlockCommand.java index ab9578418..7a41da93f 100644 --- a/src/test/java/demo/commands/SetBlockCommand.java +++ b/src/test/java/demo/commands/SetBlockCommand.java @@ -16,7 +16,7 @@ public class SetBlockCommand extends Command { RelativeVec relativeVec = context.get("position"); Block block = context.get("block"); final Player player = sender.asPlayer(); - player.getInstance().setBlock(relativeVec.from(player).toPosition().toBlockPosition(), block); + player.getInstance().setBlock(relativeVec.from(player), block); }, RelativeVec3("position"), BlockState("block")); } } diff --git a/src/test/java/demo/commands/SummonCommand.java b/src/test/java/demo/commands/SummonCommand.java index e47a49c0f..f7e68d09a 100644 --- a/src/test/java/demo/commands/SummonCommand.java +++ b/src/test/java/demo/commands/SummonCommand.java @@ -36,7 +36,8 @@ public class SummonCommand extends Command { private void execute(@NotNull CommandSender commandSender, @NotNull CommandContext commandContext) { final Entity entity = commandContext.get(entityClass).instantiate(commandContext.get(this.entity)); //noinspection ConstantConditions - One couldn't possibly execute a command without being in an instance - entity.setInstance(commandSender.asPlayer().getInstance(), commandContext.get(pos).from(commandSender.asPlayer()).toPosition()); + // FIXME + //entity.setInstance(commandSender.asPlayer().getInstance(), commandContext.get(pos).fromSender(commandSender)); } @SuppressWarnings("unused") diff --git a/src/test/java/demo/commands/TeleportCommand.java b/src/test/java/demo/commands/TeleportCommand.java index c23b0996d..f70313a36 100644 --- a/src/test/java/demo/commands/TeleportCommand.java +++ b/src/test/java/demo/commands/TeleportCommand.java @@ -6,9 +6,9 @@ import net.minestom.server.command.CommandSender; import net.minestom.server.command.builder.Command; import net.minestom.server.command.builder.CommandContext; import net.minestom.server.command.builder.arguments.ArgumentType; -import net.minestom.server.entity.Player; -import net.minestom.server.utils.Position; import net.minestom.server.coordinate.Pos; +import net.minestom.server.coordinate.Vec; +import net.minestom.server.entity.Player; import net.minestom.server.utils.location.RelativeVec; public class TeleportCommand extends Command { @@ -32,17 +32,15 @@ public class TeleportCommand extends Command { Player player = (Player) sender; player.teleport(pl.getPosition()); } - sender.sendMessage(Component.text("Teleported to player "+playerName)); + sender.sendMessage(Component.text("Teleported to player " + playerName)); } private void onPositionTeleport(CommandSender sender, CommandContext context) { final Player player = sender.asPlayer(); final RelativeVec relativeVec = context.get("pos"); - final Position position = relativeVec.from(player).toPosition(); - + final Vec position = relativeVec.from(player); player.teleport(new Pos(position)); player.sendMessage(Component.text("You have been teleported to " + position)); } - }