Fix RelativeVec

This commit is contained in:
TheMode 2021-07-08 18:15:57 +02:00
parent fff78cdf94
commit c2a84c25b5
10 changed files with 128 additions and 193 deletions

View File

@ -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.NodeMaker;
import net.minestom.server.command.builder.exception.ArgumentSyntaxException; 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.network.packet.server.play.DeclareCommandsPacket;
import net.minestom.server.utils.BlockPosition; import net.minestom.server.utils.BlockPosition;
import net.minestom.server.utils.location.RelativeBlockPosition;
import net.minestom.server.utils.StringUtils; import net.minestom.server.utils.StringUtils;
import net.minestom.server.utils.location.RelativeVec;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
/** /**
@ -13,7 +14,7 @@ import org.jetbrains.annotations.NotNull;
* <p> * <p>
* Example: 5 ~ -3 * Example: 5 ~ -3
*/ */
public class ArgumentRelativeBlockPosition extends ArgumentRelative<RelativeBlockPosition> { public class ArgumentRelativeBlockPosition extends ArgumentRelative<RelativeVec> {
public ArgumentRelativeBlockPosition(@NotNull String id) { public ArgumentRelativeBlockPosition(@NotNull String id) {
super(id, 3); super(id, 3);
@ -21,19 +22,17 @@ public class ArgumentRelativeBlockPosition extends ArgumentRelative<RelativeBloc
@NotNull @NotNull
@Override @Override
public RelativeBlockPosition parse(@NotNull String input) throws ArgumentSyntaxException { public RelativeVec parse(@NotNull String input) throws ArgumentSyntaxException {
final String[] split = input.split(StringUtils.SPACE); final String[] split = input.split(StringUtils.SPACE);
// Check if the value has enough element to be correct // Check if the value has enough element to be correct
if (split.length != getNumberCount()) { if (split.length != getNumberCount()) {
throw new ArgumentSyntaxException("Invalid number of values", input, INVALID_NUMBER_COUNT_ERROR); throw new ArgumentSyntaxException("Invalid number of values", input, INVALID_NUMBER_COUNT_ERROR);
} }
BlockPosition blockPosition = new BlockPosition(0, 0, 0); int x = 0, y = 0, z = 0;
boolean relativeX = false; boolean relativeX = false;
boolean relativeY = false; boolean relativeY = false;
boolean relativeZ = false; boolean relativeZ = false;
for (int i = 0; i < split.length; i++) { for (int i = 0; i < split.length; i++) {
final String element = split[i]; final String element = split[i];
if (element.startsWith(RELATIVE_CHAR)) { if (element.startsWith(RELATIVE_CHAR)) {
@ -51,11 +50,11 @@ public class ArgumentRelativeBlockPosition extends ArgumentRelative<RelativeBloc
final String potentialNumber = element.substring(1); final String potentialNumber = element.substring(1);
final int number = Integer.parseInt(potentialNumber); final int number = Integer.parseInt(potentialNumber);
if (i == 0) { if (i == 0) {
blockPosition.setX(number); x = number;
} else if (i == 1) { } else if (i == 1) {
blockPosition.setY(number); y = number;
} else if (i == 2) { } else if (i == 2) {
blockPosition.setZ(number); z = number;
} }
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
throw new ArgumentSyntaxException("Invalid number", input, INVALID_NUMBER_ERROR); throw new ArgumentSyntaxException("Invalid number", input, INVALID_NUMBER_ERROR);
@ -66,11 +65,11 @@ public class ArgumentRelativeBlockPosition extends ArgumentRelative<RelativeBloc
try { try {
final int number = Integer.parseInt(element); final int number = Integer.parseInt(element);
if (i == 0) { if (i == 0) {
blockPosition.setX(number); x = number;
} else if (i == 1) { } else if (i == 1) {
blockPosition.setY(number); y = number;
} else if (i == 2) { } else if (i == 2) {
blockPosition.setZ(number); z = number;
} }
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
throw new ArgumentSyntaxException("Invalid number", input, INVALID_NUMBER_ERROR); throw new ArgumentSyntaxException("Invalid number", input, INVALID_NUMBER_ERROR);
@ -78,7 +77,7 @@ public class ArgumentRelativeBlockPosition extends ArgumentRelative<RelativeBloc
} }
} }
return new RelativeBlockPosition(blockPosition, relativeX, relativeY, relativeZ); return new RelativeVec(new Vec(x, y, z), relativeX, relativeY, relativeZ);
} }
@Override @Override

View File

@ -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.NodeMaker;
import net.minestom.server.command.builder.exception.ArgumentSyntaxException; 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.network.packet.server.play.DeclareCommandsPacket;
import net.minestom.server.utils.StringUtils;
import net.minestom.server.utils.Vector; import net.minestom.server.utils.Vector;
import net.minestom.server.utils.location.RelativeVec; import net.minestom.server.utils.location.RelativeVec;
import net.minestom.server.utils.StringUtils;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
/** /**
@ -23,13 +24,12 @@ public class ArgumentRelativeVec2 extends ArgumentRelative<RelativeVec> {
@Override @Override
public RelativeVec parse(@NotNull String input) throws ArgumentSyntaxException { public RelativeVec parse(@NotNull String input) throws ArgumentSyntaxException {
final String[] split = input.split(StringUtils.SPACE); final String[] split = input.split(StringUtils.SPACE);
// Check if the value has enough element to be correct // Check if the value has enough element to be correct
if (split.length != getNumberCount()) { if (split.length != getNumberCount()) {
throw new ArgumentSyntaxException("Invalid number of values", input, INVALID_NUMBER_COUNT_ERROR); 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 relativeX = false;
boolean relativeZ = false; boolean relativeZ = false;
@ -47,18 +47,18 @@ public class ArgumentRelativeVec2 extends ArgumentRelative<RelativeVec> {
final String potentialNumber = element.substring(1); final String potentialNumber = element.substring(1);
final float number = Float.parseFloat(potentialNumber); final float number = Float.parseFloat(potentialNumber);
if (i == 0) { if (i == 0) {
vector.setX(number); x = number;
} else if (i == 1) { } else if (i == 1) {
vector.setZ(number); z = number;
} }
} }
} else { } else {
final float number = Float.parseFloat(element); final float number = Float.parseFloat(element);
if (i == 0) { if (i == 0) {
vector.setX(number); x = number;
} else if (i == 1) { } else if (i == 1) {
vector.setZ(number); z = number;
} }
} }
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
@ -66,7 +66,7 @@ public class ArgumentRelativeVec2 extends ArgumentRelative<RelativeVec> {
} }
} }
return new RelativeVec(vector, relativeX, false, relativeZ); return new RelativeVec(new Vec(x, z), relativeX, false, relativeZ);
} }
@Override @Override

View File

@ -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.NodeMaker;
import net.minestom.server.command.builder.exception.ArgumentSyntaxException; 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.network.packet.server.play.DeclareCommandsPacket;
import net.minestom.server.utils.StringUtils;
import net.minestom.server.utils.Vector; import net.minestom.server.utils.Vector;
import net.minestom.server.utils.location.RelativeVec; import net.minestom.server.utils.location.RelativeVec;
import net.minestom.server.utils.StringUtils;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
/** /**
@ -23,17 +24,15 @@ public class ArgumentRelativeVec3 extends ArgumentRelative<RelativeVec> {
@Override @Override
public RelativeVec parse(@NotNull String input) throws ArgumentSyntaxException { public RelativeVec parse(@NotNull String input) throws ArgumentSyntaxException {
final String[] split = input.split(StringUtils.SPACE); final String[] split = input.split(StringUtils.SPACE);
// Check if the value has enough element to be correct // Check if the value has enough element to be correct
if (split.length != getNumberCount()) { if (split.length != getNumberCount()) {
throw new ArgumentSyntaxException("Invalid number of values", input, INVALID_NUMBER_COUNT_ERROR); 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 relativeX = false;
boolean relativeY = false; boolean relativeY = false;
boolean relativeZ = false; boolean relativeZ = false;
for (int i = 0; i < split.length; i++) { for (int i = 0; i < split.length; i++) {
final String element = split[i]; final String element = split[i];
try { try {
@ -50,22 +49,22 @@ public class ArgumentRelativeVec3 extends ArgumentRelative<RelativeVec> {
final String potentialNumber = element.substring(1); final String potentialNumber = element.substring(1);
final float number = Float.parseFloat(potentialNumber); final float number = Float.parseFloat(potentialNumber);
if (i == 0) { if (i == 0) {
vector.setX(number); x = number;
} else if (i == 1) { } else if (i == 1) {
vector.setY(number); y = number;
} else if (i == 2) { } else if (i == 2) {
vector.setZ(number); z = number;
} }
} }
} else { } else {
final float number = Float.parseFloat(element); final float number = Float.parseFloat(element);
if (i == 0) { if (i == 0) {
vector.setX(number); x = number;
} else if (i == 1) { } else if (i == 1) {
vector.setY(number); y = number;
} else if (i == 2) { } else if (i == 2) {
vector.setZ(number); z = number;
} }
} }
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
@ -73,7 +72,7 @@ public class ArgumentRelativeVec3 extends ArgumentRelative<RelativeVec> {
} }
} }
return new RelativeVec(vector, relativeX, relativeY, relativeZ); return new RelativeVec(new Vec(x, y, z), relativeX, relativeY, relativeZ);
} }
@Override @Override

View File

@ -1,8 +1,8 @@
package net.minestom.server.instance.block; package net.minestom.server.instance.block;
import net.minestom.server.coordinate.Point;
import net.minestom.server.instance.Instance; import net.minestom.server.instance.Instance;
import net.minestom.server.instance.batch.Batch; import net.minestom.server.instance.batch.Batch;
import net.minestom.server.utils.BlockPosition;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
/** /**
@ -13,7 +13,7 @@ import org.jetbrains.annotations.NotNull;
public interface BlockSetter { public interface BlockSetter {
void setBlock(int x, int y, int z, @NotNull Block block); void setBlock(int x, int y, int z, @NotNull Block block);
default void setBlock(@NotNull BlockPosition blockPosition, @NotNull Block block) { default void setBlock(@NotNull Point blockPosition, @NotNull Block block) {
setBlock(blockPosition.getX(), blockPosition.getY(), blockPosition.getZ(), block); setBlock(blockPosition.blockX(), blockPosition.blockY(), blockPosition.blockZ(), block);
} }
} }

View File

@ -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<BlockPosition> {
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);
}
}

View File

@ -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 <T> the location type
*/
public abstract class RelativeLocation<T extends Point> {
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;
}
}

View File

@ -1,44 +1,108 @@
package net.minestom.server.utils.location; package net.minestom.server.utils.location;
import net.minestom.server.utils.Position; import net.minestom.server.command.CommandSender;
import net.minestom.server.utils.Vector; 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 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 <T> the location type
*/ */
public class RelativeVec extends RelativeLocation<Vector> { public final class RelativeVec {
public RelativeVec(Vector location, boolean relativeX, boolean relativeY, boolean relativeZ) { private final Vec vec;
super(location, relativeX, relativeY, relativeZ); 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) { if (!relativeX && !relativeY && !relativeZ) {
return location.clone(); return vec;
} }
final Position entityPosition = position != null ? position : new Position(); final var absolute = Objects.requireNonNullElse(point, Vec.ZERO);
final double x = vec.x() + (relativeX ? absolute.x() : 0);
final double x = location.getX() + (relativeX ? entityPosition.getX() : 0); final double y = vec.y() + (relativeY ? absolute.y() : 0);
final double y = location.getY() + (relativeY ? entityPosition.getY() : 0); final double z = vec.z() + (relativeZ ? absolute.z() : 0);
final double z = location.getZ() + (relativeZ ? entityPosition.getZ() : 0); return new Vec(x, y, z);
return new Vector(x, y, z);
} }
@Override @ApiStatus.Experimental
public Vector fromView(@Nullable Position position) { public Vec fromView(@Nullable Pos point) {
if (!relativeX && !relativeY && !relativeZ) { 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;
} }
} }

View File

@ -16,7 +16,7 @@ public class SetBlockCommand extends Command {
RelativeVec relativeVec = context.get("position"); RelativeVec relativeVec = context.get("position");
Block block = context.get("block"); Block block = context.get("block");
final Player player = sender.asPlayer(); 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")); }, RelativeVec3("position"), BlockState("block"));
} }
} }

View File

@ -36,7 +36,8 @@ public class SummonCommand extends Command {
private void execute(@NotNull CommandSender commandSender, @NotNull CommandContext commandContext) { private void execute(@NotNull CommandSender commandSender, @NotNull CommandContext commandContext) {
final Entity entity = commandContext.get(entityClass).instantiate(commandContext.get(this.entity)); 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 //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") @SuppressWarnings("unused")

View File

@ -6,9 +6,9 @@ import net.minestom.server.command.CommandSender;
import net.minestom.server.command.builder.Command; import net.minestom.server.command.builder.Command;
import net.minestom.server.command.builder.CommandContext; import net.minestom.server.command.builder.CommandContext;
import net.minestom.server.command.builder.arguments.ArgumentType; 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.Pos;
import net.minestom.server.coordinate.Vec;
import net.minestom.server.entity.Player;
import net.minestom.server.utils.location.RelativeVec; import net.minestom.server.utils.location.RelativeVec;
public class TeleportCommand extends Command { public class TeleportCommand extends Command {
@ -32,17 +32,15 @@ public class TeleportCommand extends Command {
Player player = (Player) sender; Player player = (Player) sender;
player.teleport(pl.getPosition()); 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) { private void onPositionTeleport(CommandSender sender, CommandContext context) {
final Player player = sender.asPlayer(); final Player player = sender.asPlayer();
final RelativeVec relativeVec = context.get("pos"); 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.teleport(new Pos(position));
player.sendMessage(Component.text("You have been teleported to " + position)); player.sendMessage(Component.text("You have been teleported to " + position));
} }
} }