Fixed number arguments

This commit is contained in:
Felix Cravic 2020-08-09 10:59:12 +02:00
parent 6fb1c86e08
commit 63ed21380c
12 changed files with 92 additions and 7 deletions

View File

@ -34,6 +34,7 @@ public class Main {
commandManager.register(new GamemodeCommand());
commandManager.register(new DimensionCommand());
commandManager.register(new ShutdownCommand());
commandManager.register(new TeleportCommand());
/*RecipeManager recipeManager = MinecraftServer.getRecipeManager();
ShapelessRecipe shapelessRecipe = new ShapelessRecipe("test", "groupname") {

View File

@ -255,7 +255,7 @@ public class PlayerInit {
});
player.addEventCallback(PlayerSpawnEvent.class, event -> {
player.setGameMode(GameMode.SURVIVAL);
player.setGameMode(GameMode.CREATIVE);
player.teleport(new Position(0, 41f, 0));
//player.setHeldItemSlot((byte) 5);

View File

@ -0,0 +1,32 @@
package fr.themode.demo.commands;
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.entity.Player;
import net.minestom.server.utils.Position;
public class TeleportCommand extends Command {
public TeleportCommand() {
super("tp");
setDefaultExecutor((source, args) -> source.sendMessage("Usage: /tp x y z"));
Argument x = ArgumentType.Float("x");
Argument y = ArgumentType.Float("y");
Argument z = ArgumentType.Float("z");
addCallback((source, value, error) -> {
System.out.println("error: " + error);
}, x);
addSyntax((source, args) -> {
final float posX = args.getFloat("x");
final float posY = args.getFloat("y");
final float posZ = args.getFloat("z");
((Player) source).teleport(new Position(posX, posY, posZ));
source.sendMessage("TELEPORTING");
}, x, y, z);
}
}

View File

@ -37,8 +37,12 @@ public class ArgumentDouble extends ArgumentNumber<Double> {
@Override
public int getConditionResult(Double value) {
// Check range
if (value < this.min || value > this.max)
if (hasMin && value < min) {
return RANGE_ERROR;
}
if (hasMax && value > max) {
return RANGE_ERROR;
}
return SUCCESS;
}

View File

@ -37,8 +37,12 @@ public class ArgumentFloat extends ArgumentNumber<Float> {
@Override
public int getConditionResult(Float value) {
// Check range
if (value < this.min || value > this.max)
if (hasMin && value < min) {
return RANGE_ERROR;
}
if (hasMax && value > max) {
return RANGE_ERROR;
}
return SUCCESS;
}

View File

@ -26,8 +26,12 @@ public class ArgumentInteger extends ArgumentNumber<Integer> {
@Override
public int getConditionResult(Integer value) {
// Check range
if (value < this.min || value > this.max)
if (hasMin && value < min) {
return RANGE_ERROR;
}
if (hasMax && value > max) {
return RANGE_ERROR;
}
return SUCCESS;
}

View File

@ -26,8 +26,12 @@ public class ArgumentLong extends ArgumentNumber<Long> {
@Override
public int getConditionResult(Long value) {
// Check range
if (value < this.min || value > this.max)
if (hasMin && value < min) {
return RANGE_ERROR;
}
if (hasMax && value > max) {
return RANGE_ERROR;
}
return SUCCESS;
}

View File

@ -26,6 +26,7 @@ public abstract class ArgumentNumber<T extends Number> extends Argument<T> {
public ArgumentNumber<T> max(T value) {
this.max = value;
this.hasMax = true;
return this;
}
@ -37,18 +38,38 @@ public abstract class ArgumentNumber<T extends Number> extends Argument<T> {
return this;
}
/**
* Get if the argument has a minimum
*
* @return true if the argument has a minimum
*/
public boolean hasMin() {
return hasMin;
}
/**
* Get the minimum value for this argument
*
* @return the minimum of this argument
*/
public T getMin() {
return min;
}
/**
* Get if the argument has a maximum
*
* @return true if the argument has a maximum
*/
public boolean hasMax() {
return hasMax;
}
/**
* Get the maximum value for this argument
*
* @return the maximum of this argument
*/
public T getMax() {
return max;
}

View File

@ -203,7 +203,7 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer {
Check.notNull(position, "Teleport position cannot be null");
Check.stateCondition(instance == null, "You need to use Entity#setInstance before teleporting an entity!");
Runnable runnable = () -> {
final Runnable runnable = () -> {
refreshPosition(position.getX(), position.getY(), position.getZ());
refreshView(position.getYaw(), position.getPitch());
sendSynchronization();

View File

@ -99,6 +99,7 @@ public class PerGroupChunkProvider extends ThreadProvider {
// Set of already-updated instances this tick
final Set<Instance> updatedInstance = new HashSet<>();
getLock().lock();
instanceInstanceMap.entrySet().forEach(entry -> {
final Instance instance = entry.getKey();
final Map<Set<ChunkCoordinate>, Instance> instanceMap = entry.getValue();
@ -129,6 +130,7 @@ public class PerGroupChunkProvider extends ThreadProvider {
}
});
getLock().unlock();
}
/**

View File

@ -34,6 +34,7 @@ public class PerInstanceThreadProvider extends ThreadProvider {
@Override
public void update(long time) {
getLock().lock();
for (Map.Entry<Instance, Set<ChunkCoordinate>> entry : instanceChunkMap.entrySet()) {
final Instance instance = entry.getKey();
final Set<ChunkCoordinate> chunkCoordinates = entry.getValue();
@ -52,7 +53,7 @@ public class PerInstanceThreadProvider extends ThreadProvider {
}
});
getLock().unlock();
}
}

View File

@ -9,6 +9,7 @@ import net.minestom.server.utils.thread.MinestomThread;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.locks.ReentrantLock;
import java.util.function.Function;
/**
@ -26,6 +27,8 @@ public abstract class ThreadProvider {
*/
private int threadCount;
private ReentrantLock lock = new ReentrantLock();
{
// Default thread count in the pool
setThreadCount(5);
@ -75,6 +78,15 @@ public abstract class ThreadProvider {
refreshPool();
}
/**
* Get the lock of this thread provider
*
* @return the thread provider lock
*/
public ReentrantLock getLock() {
return lock;
}
private void refreshPool() {
this.pool = new MinestomThread(threadCount, MinecraftServer.THREAD_NAME_TICK);
}