mirror of
https://github.com/Minestom/Minestom.git
synced 2025-02-06 15:31:43 +01:00
Added ArgumentRelativeBlockPosition, ArgumentRelativeVec3 and ArgumentRelativeVec2
This commit is contained in:
parent
bbf9f92b52
commit
ad357d70fc
@ -40,6 +40,7 @@ public final class UpdateManager {
|
||||
private final ConcurrentLinkedQueue<DoubleConsumer> tickEndCallbacks = new ConcurrentLinkedQueue<>();
|
||||
|
||||
{
|
||||
// DEFAULT THREAD PROVIDER
|
||||
//threadProvider = new PerInstanceThreadProvider();
|
||||
threadProvider = new PerGroupChunkProvider();
|
||||
}
|
||||
|
@ -15,6 +15,9 @@ import net.minestom.server.command.builder.arguments.number.ArgumentDouble;
|
||||
import net.minestom.server.command.builder.arguments.number.ArgumentFloat;
|
||||
import net.minestom.server.command.builder.arguments.number.ArgumentInteger;
|
||||
import net.minestom.server.command.builder.arguments.number.ArgumentNumber;
|
||||
import net.minestom.server.command.builder.arguments.relative.ArgumentRelativeBlockPosition;
|
||||
import net.minestom.server.command.builder.arguments.relative.ArgumentRelativeVec2;
|
||||
import net.minestom.server.command.builder.arguments.relative.ArgumentRelativeVec3;
|
||||
import net.minestom.server.command.builder.condition.CommandCondition;
|
||||
import net.minestom.server.entity.Player;
|
||||
import net.minestom.server.event.player.PlayerCommandEvent;
|
||||
@ -442,7 +445,7 @@ public final class CommandManager {
|
||||
|
||||
// You can uncomment this to test any brigadier parser on the client
|
||||
/*DeclareCommandsPacket.Node testNode = simpleArgumentNode(nodes, argument, executable, false);
|
||||
testNode.parser = "minecraft:item_predicate";
|
||||
testNode.parser = "minecraft:vec3";
|
||||
|
||||
if (true) {
|
||||
return nodes;
|
||||
@ -595,6 +598,15 @@ public final class CommandManager {
|
||||
} else if (argument instanceof ArgumentNbtTag) {
|
||||
DeclareCommandsPacket.Node argumentNode = simpleArgumentNode(nodes, argument, executable, false);
|
||||
argumentNode.parser = "minecraft:nbt_tag";
|
||||
} else if (argument instanceof ArgumentRelativeBlockPosition) {
|
||||
DeclareCommandsPacket.Node argumentNode = simpleArgumentNode(nodes, argument, executable, false);
|
||||
argumentNode.parser = "minecraft:block_pos";
|
||||
} else if (argument instanceof ArgumentRelativeVec3) {
|
||||
DeclareCommandsPacket.Node argumentNode = simpleArgumentNode(nodes, argument, executable, false);
|
||||
argumentNode.parser = "minecraft:vec3";
|
||||
} else if (argument instanceof ArgumentRelativeVec2) {
|
||||
DeclareCommandsPacket.Node argumentNode = simpleArgumentNode(nodes, argument, executable, false);
|
||||
argumentNode.parser = "minecraft:vec2";
|
||||
}
|
||||
|
||||
return nodes;
|
||||
|
@ -7,6 +7,8 @@ import net.minestom.server.item.Enchantment;
|
||||
import net.minestom.server.item.ItemStack;
|
||||
import net.minestom.server.particle.Particle;
|
||||
import net.minestom.server.potion.PotionEffect;
|
||||
import net.minestom.server.utils.location.RelativeBlockPosition;
|
||||
import net.minestom.server.utils.location.RelativeVec;
|
||||
import net.minestom.server.utils.math.FloatRange;
|
||||
import net.minestom.server.utils.math.IntRange;
|
||||
import net.minestom.server.utils.time.UpdateOption;
|
||||
@ -126,6 +128,16 @@ public final class Arguments {
|
||||
return (NBT) getObject(id);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public RelativeBlockPosition getRelativeBlockPosition(@NotNull String id) {
|
||||
return (RelativeBlockPosition) getObject(id);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public RelativeVec getRelativeVector(@NotNull String id) {
|
||||
return (RelativeVec) getObject(id);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public Object getObject(@NotNull String id) {
|
||||
return args.computeIfAbsent(id, s -> {
|
||||
|
@ -9,6 +9,9 @@ import net.minestom.server.command.builder.arguments.number.ArgumentDouble;
|
||||
import net.minestom.server.command.builder.arguments.number.ArgumentFloat;
|
||||
import net.minestom.server.command.builder.arguments.number.ArgumentInteger;
|
||||
import net.minestom.server.command.builder.arguments.number.ArgumentLong;
|
||||
import net.minestom.server.command.builder.arguments.relative.ArgumentRelativeBlockPosition;
|
||||
import net.minestom.server.command.builder.arguments.relative.ArgumentRelativeVec2;
|
||||
import net.minestom.server.command.builder.arguments.relative.ArgumentRelativeVec3;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
/**
|
||||
@ -109,4 +112,16 @@ public class ArgumentType {
|
||||
return new ArgumentNbtTag(id);
|
||||
}
|
||||
|
||||
public static ArgumentRelativeBlockPosition RelativeBlockPosition(@NotNull String id) {
|
||||
return new ArgumentRelativeBlockPosition(id);
|
||||
}
|
||||
|
||||
public static ArgumentRelativeVec3 RelativeVec3(@NotNull String id) {
|
||||
return new ArgumentRelativeVec3(id);
|
||||
}
|
||||
|
||||
public static ArgumentRelativeVec2 RelativeVec2(@NotNull String id) {
|
||||
return new ArgumentRelativeVec2(id);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,23 @@
|
||||
package net.minestom.server.command.builder.arguments.relative;
|
||||
|
||||
import net.minestom.server.command.builder.arguments.Argument;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public abstract class ArgumentRelative<T> extends Argument<T> {
|
||||
|
||||
public static final String RELATIVE_CHAR = "~";
|
||||
|
||||
public static final int INVALID_NUMBER_COUNT_ERROR = 1;
|
||||
public static final int INVALID_NUMBER_ERROR = 2;
|
||||
|
||||
private final int numberCount;
|
||||
|
||||
public ArgumentRelative(@NotNull String id, int numberCount) {
|
||||
super(id, true);
|
||||
this.numberCount = numberCount;
|
||||
}
|
||||
|
||||
public int getNumberCount() {
|
||||
return numberCount;
|
||||
}
|
||||
}
|
@ -0,0 +1,76 @@
|
||||
package net.minestom.server.command.builder.arguments.relative;
|
||||
|
||||
import net.minestom.server.utils.BlockPosition;
|
||||
import net.minestom.server.utils.location.RelativeBlockPosition;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class ArgumentRelativeBlockPosition extends ArgumentRelative<RelativeBlockPosition> {
|
||||
|
||||
public ArgumentRelativeBlockPosition(@NotNull String id) {
|
||||
super(id, 3);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getCorrectionResult(@NotNull String value) {
|
||||
final String[] split = value.split(" ");
|
||||
|
||||
// Check if the value has enough element to be correct
|
||||
if (split.length != getNumberCount()) {
|
||||
return INVALID_NUMBER_COUNT_ERROR;
|
||||
}
|
||||
|
||||
// Check if each element is correct
|
||||
for (String element : split) {
|
||||
if (!element.equals(RELATIVE_CHAR)) {
|
||||
try {
|
||||
// Will throw the exception if not an integer
|
||||
Integer.parseInt(element);
|
||||
} catch (NumberFormatException e) {
|
||||
return INVALID_NUMBER_ERROR;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public RelativeBlockPosition parse(@NotNull String value) {
|
||||
final String[] split = value.split(" ");
|
||||
|
||||
BlockPosition blockPosition = new BlockPosition(0, 0, 0);
|
||||
boolean relativeX = false;
|
||||
boolean relativeY = false;
|
||||
boolean relativeZ = false;
|
||||
|
||||
for (int i = 0; i < split.length; i++) {
|
||||
final String element = split[i];
|
||||
if (element.equals(RELATIVE_CHAR)) {
|
||||
if (i == 0) {
|
||||
relativeX = true;
|
||||
} else if (i == 1) {
|
||||
relativeY = true;
|
||||
} else if (i == 2) {
|
||||
relativeZ = true;
|
||||
}
|
||||
} else {
|
||||
final int number = Integer.parseInt(element);
|
||||
if (i == 0) {
|
||||
blockPosition.setX(number);
|
||||
} else if (i == 1) {
|
||||
blockPosition.setY(number);
|
||||
} else if (i == 2) {
|
||||
blockPosition.setZ(number);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return new RelativeBlockPosition(blockPosition, relativeX, relativeY, relativeZ);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getConditionResult(@NotNull RelativeBlockPosition value) {
|
||||
return SUCCESS;
|
||||
}
|
||||
}
|
@ -0,0 +1,71 @@
|
||||
package net.minestom.server.command.builder.arguments.relative;
|
||||
|
||||
import net.minestom.server.utils.Vector;
|
||||
import net.minestom.server.utils.location.RelativeVec;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class ArgumentRelativeVec2 extends ArgumentRelative<RelativeVec> {
|
||||
|
||||
public ArgumentRelativeVec2(@NotNull String id) {
|
||||
super(id, 2);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getCorrectionResult(@NotNull String value) {
|
||||
final String[] split = value.split(" ");
|
||||
|
||||
// Check if the value has enough element to be correct
|
||||
if (split.length != getNumberCount()) {
|
||||
return INVALID_NUMBER_COUNT_ERROR;
|
||||
}
|
||||
|
||||
// Check if each element is correct
|
||||
for (String element : split) {
|
||||
if (!element.equals(RELATIVE_CHAR)) {
|
||||
try {
|
||||
// Will throw the exception if not a float
|
||||
Float.parseFloat(element);
|
||||
} catch (NumberFormatException e) {
|
||||
return INVALID_NUMBER_ERROR;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public RelativeVec parse(@NotNull String value) {
|
||||
final String[] split = value.split(" ");
|
||||
|
||||
Vector vector = new Vector();
|
||||
boolean relativeX = false;
|
||||
boolean relativeZ = false;
|
||||
|
||||
for (int i = 0; i < split.length; i++) {
|
||||
final String element = split[i];
|
||||
if (element.equals(RELATIVE_CHAR)) {
|
||||
if (i == 0) {
|
||||
relativeX = true;
|
||||
} else if (i == 1) {
|
||||
relativeZ = true;
|
||||
}
|
||||
} else {
|
||||
final float number = Float.parseFloat(element);
|
||||
if (i == 0) {
|
||||
vector.setX(number);
|
||||
} else if (i == 1) {
|
||||
vector.setZ(number);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return new RelativeVec(vector, relativeX, false, relativeZ);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getConditionResult(@NotNull RelativeVec value) {
|
||||
return SUCCESS;
|
||||
}
|
||||
}
|
@ -0,0 +1,76 @@
|
||||
package net.minestom.server.command.builder.arguments.relative;
|
||||
|
||||
import net.minestom.server.utils.Vector;
|
||||
import net.minestom.server.utils.location.RelativeVec;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class ArgumentRelativeVec3 extends ArgumentRelative<RelativeVec> {
|
||||
|
||||
public ArgumentRelativeVec3(@NotNull String id) {
|
||||
super(id, 3);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getCorrectionResult(@NotNull String value) {
|
||||
final String[] split = value.split(" ");
|
||||
|
||||
// Check if the value has enough element to be correct
|
||||
if (split.length != getNumberCount()) {
|
||||
return INVALID_NUMBER_COUNT_ERROR;
|
||||
}
|
||||
|
||||
// Check if each element is correct
|
||||
for (String element : split) {
|
||||
if (!element.equals(RELATIVE_CHAR)) {
|
||||
try {
|
||||
// Will throw the exception if not a float
|
||||
Float.parseFloat(element);
|
||||
} catch (NumberFormatException e) {
|
||||
return INVALID_NUMBER_ERROR;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public RelativeVec parse(@NotNull String value) {
|
||||
final String[] split = value.split(" ");
|
||||
|
||||
Vector vector = new Vector();
|
||||
boolean relativeX = false;
|
||||
boolean relativeY = false;
|
||||
boolean relativeZ = false;
|
||||
|
||||
for (int i = 0; i < split.length; i++) {
|
||||
final String element = split[i];
|
||||
if (element.equals(RELATIVE_CHAR)) {
|
||||
if (i == 0) {
|
||||
relativeX = true;
|
||||
} else if (i == 1) {
|
||||
relativeY = true;
|
||||
} else if (i == 2) {
|
||||
relativeZ = true;
|
||||
}
|
||||
} else {
|
||||
final float number = Float.parseFloat(element);
|
||||
if (i == 0) {
|
||||
vector.setX(number);
|
||||
} else if (i == 1) {
|
||||
vector.setY(number);
|
||||
} else if (i == 2) {
|
||||
vector.setZ(number);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return new RelativeVec(vector, relativeX, relativeY, relativeZ);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getConditionResult(@NotNull RelativeVec value) {
|
||||
return SUCCESS;
|
||||
}
|
||||
}
|
@ -98,6 +98,13 @@ public final class PacketProcessor {
|
||||
connectionPlayerConnectionMap.remove(channel);
|
||||
}
|
||||
|
||||
/**
|
||||
* Calls {@link Readable#read(BinaryReader)} and catch all the exceptions to be printed using the packet processor logger.
|
||||
*
|
||||
* @param connection the connection who sent the packet
|
||||
* @param readable the readable interface
|
||||
* @param reader the buffer containing the packet
|
||||
*/
|
||||
private void safeRead(@NotNull PlayerConnection connection, @NotNull Readable readable, @NotNull BinaryReader reader) {
|
||||
try {
|
||||
readable.read(reader);
|
||||
|
@ -0,0 +1,26 @@
|
||||
package net.minestom.server.utils.location;
|
||||
|
||||
import net.minestom.server.entity.Entity;
|
||||
import net.minestom.server.utils.BlockPosition;
|
||||
import net.minestom.server.utils.Position;
|
||||
|
||||
public class RelativeBlockPosition extends RelativeLocation<BlockPosition> {
|
||||
|
||||
public RelativeBlockPosition(BlockPosition location, boolean relativeX, boolean relativeY, boolean relativeZ) {
|
||||
super(location, relativeX, relativeY, relativeZ);
|
||||
}
|
||||
|
||||
@Override
|
||||
public BlockPosition fromRelativePosition(Entity entity) {
|
||||
if (!relativeX && !relativeY && !relativeZ) {
|
||||
return location.copy();
|
||||
}
|
||||
final Position entityPosition = entity.getPosition();
|
||||
|
||||
final int x = relativeX ? (int) entityPosition.getX() : location.getX();
|
||||
final int y = relativeY ? (int) entityPosition.getY() : location.getY();
|
||||
final int z = relativeZ ? (int) entityPosition.getZ() : location.getZ();
|
||||
|
||||
return new BlockPosition(x, y, z);
|
||||
}
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
package net.minestom.server.utils.location;
|
||||
|
||||
import net.minestom.server.entity.Entity;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public abstract class RelativeLocation<T> {
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
public abstract T fromRelativePosition(@Nullable Entity entity);
|
||||
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
package net.minestom.server.utils.location;
|
||||
|
||||
import net.minestom.server.entity.Entity;
|
||||
import net.minestom.server.utils.Position;
|
||||
import net.minestom.server.utils.Vector;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public class RelativeVec extends RelativeLocation<Vector> {
|
||||
|
||||
public RelativeVec(Vector location, boolean relativeX, boolean relativeY, boolean relativeZ) {
|
||||
super(location, relativeX, relativeY, relativeZ);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Vector fromRelativePosition(@Nullable Entity entity) {
|
||||
if (!relativeX && !relativeY && !relativeZ) {
|
||||
return location.copy();
|
||||
}
|
||||
final Position entityPosition = entity.getPosition();
|
||||
|
||||
final float x = relativeX ? (int) entityPosition.getX() : location.getX();
|
||||
final float y = relativeY ? (int) entityPosition.getY() : location.getY();
|
||||
final float z = relativeZ ? (int) entityPosition.getZ() : location.getZ();
|
||||
|
||||
return new Vector(x, y, z);
|
||||
}
|
||||
}
|
@ -7,7 +7,6 @@ import demo.commands.GamemodeCommand;
|
||||
import demo.commands.TestCommand;
|
||||
import net.minestom.server.MinecraftServer;
|
||||
import net.minestom.server.command.CommandManager;
|
||||
import net.minestom.server.extras.bungee.BungeeCordProxy;
|
||||
import net.minestom.server.instance.block.BlockManager;
|
||||
import net.minestom.server.instance.block.rule.vanilla.RedstonePlacementRule;
|
||||
import net.minestom.server.storage.StorageManager;
|
||||
@ -52,7 +51,7 @@ public class Main {
|
||||
PlayerInit.init();
|
||||
|
||||
//VelocityProxy.enable("rBeJJ79W4MVU");
|
||||
BungeeCordProxy.enable();
|
||||
//BungeeCordProxy.enable();
|
||||
|
||||
// MojangAuth.init();
|
||||
|
||||
|
@ -5,7 +5,7 @@ import net.minestom.server.command.builder.Arguments;
|
||||
import net.minestom.server.command.builder.Command;
|
||||
import net.minestom.server.command.builder.arguments.Argument;
|
||||
import net.minestom.server.command.builder.arguments.ArgumentType;
|
||||
import net.minestom.server.item.ItemStack;
|
||||
import net.minestom.server.utils.location.RelativeVec;
|
||||
|
||||
public class TestCommand extends Command {
|
||||
|
||||
@ -18,7 +18,7 @@ public class TestCommand extends Command {
|
||||
//addSyntax(this::execute, dynamicWord);
|
||||
}
|
||||
|
||||
Argument test = ArgumentType.ItemStack("item");
|
||||
Argument test = ArgumentType.RelativeVec2("pos");
|
||||
|
||||
test.setCallback((source, value, error) -> {
|
||||
System.out.println("ERROR " + error);
|
||||
@ -30,8 +30,8 @@ public class TestCommand extends Command {
|
||||
});
|
||||
|
||||
addSyntax((source, args) -> {
|
||||
ItemStack itemStack = args.getItemStack("item");
|
||||
System.out.println("HEY IT WORKS "+itemStack.getMaterial());
|
||||
RelativeVec location = args.getRelativeVector("pos");
|
||||
System.out.println("IT WORKS " + location.fromRelativePosition(source.asPlayer()));
|
||||
}, test);
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user