Skip to content

Commit

Permalink
Add relative dynamic argument types
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexProgrammerDE committed Oct 3, 2024
1 parent f768854 commit db586a2
Show file tree
Hide file tree
Showing 8 changed files with 392 additions and 60 deletions.
156 changes: 97 additions & 59 deletions server/src/main/java/com/soulfiremc/server/ServerCommandManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

import com.mojang.brigadier.Command;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.arguments.DoubleArgumentType;
import com.mojang.brigadier.arguments.IntegerArgumentType;
import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.context.CommandContext;
Expand All @@ -32,13 +31,14 @@
import com.soulfiremc.server.api.event.EventUtil;
import com.soulfiremc.server.api.event.bot.BotPreTickEvent;
import com.soulfiremc.server.api.event.lifecycle.CommandManagerInitEvent;
import com.soulfiremc.server.brigadier.BlockTagResolvable;
import com.soulfiremc.server.brigadier.TagBasedArgumentType;
import com.soulfiremc.server.brigadier.*;
import com.soulfiremc.server.data.BlockTags;
import com.soulfiremc.server.data.BlockType;
import com.soulfiremc.server.data.EntityType;
import com.soulfiremc.server.grpc.ServerRPCConstants;
import com.soulfiremc.server.pathfinding.SFVec3i;
import com.soulfiremc.server.pathfinding.controller.CollectBlockController;
import com.soulfiremc.server.pathfinding.controller.ExcavateAreaController;
import com.soulfiremc.server.pathfinding.controller.FollowEntityController;
import com.soulfiremc.server.pathfinding.execution.PathExecutor;
import com.soulfiremc.server.pathfinding.goals.GoalScorer;
Expand All @@ -59,7 +59,8 @@
import lombok.RequiredArgsConstructor;
import net.raphimc.vialoader.util.ProtocolVersionList;
import org.apache.commons.io.FileUtils;
import org.cloudburstmc.math.vector.Vector3d;
import org.cloudburstmc.math.GenericMath;
import org.cloudburstmc.math.vector.Vector2d;
import org.geysermc.mcprotocollib.protocol.data.game.entity.RotationOrigin;

import javax.annotation.PostConstruct;
Expand Down Expand Up @@ -168,43 +169,39 @@ public void postConstruct() {
});
}))))
.then(
argument("y", IntegerArgumentType.integer())
argument("y", new DynamicYArgumentType())
.executes(
help(
"Makes selected bots walk to the y coordinates",
c -> {
var y = IntegerArgumentType.getInteger(c, "y");
return executePathfinding(c, bot -> new YGoal(y));
var y = c.getArgument("y", DynamicYArgumentType.YLocationMapper.class);
return executePathfinding(c, bot -> new YGoal(GenericMath.floor(
y.getAbsoluteLocation(bot.dataManager().clientEntity().y())
)));
})))
.then(
argument("x", IntegerArgumentType.integer())
.then(
argument("z", IntegerArgumentType.integer())
.executes(
help(
"Makes selected bots walk to the xz coordinates",
c -> {
var x = IntegerArgumentType.getInteger(c, "x");
var z = IntegerArgumentType.getInteger(c, "z");

return executePathfinding(c, bot -> new XZGoal(x, z));
}))))
argument("xz", new DynamicXZArgumentType())
.executes(
help(
"Makes selected bots walk to the xz coordinates",
c -> {
var xz = c.getArgument("xz", DynamicXZArgumentType.XZLocationMapper.class);
return executePathfinding(c, bot -> new XZGoal(xz.getAbsoluteLocation(Vector2d.from(
bot.dataManager().clientEntity().x(),
bot.dataManager().clientEntity().z()
)).toInt()));
})))
.then(
argument("x", IntegerArgumentType.integer())
.then(
argument("y", IntegerArgumentType.integer())
.then(
argument("z", IntegerArgumentType.integer())
.executes(
help(
"Makes selected bots walk to the xyz coordinates",
c -> {
var x = IntegerArgumentType.getInteger(c, "x");
var y = IntegerArgumentType.getInteger(c, "y");
var z = IntegerArgumentType.getInteger(c, "z");

return executePathfinding(c, bot -> new PosGoal(x, y, z));
}))))));
argument("xyz", new DynamicXYZArgumentType())
.executes(
help(
"Makes selected bots walk to the xyz coordinates",
c -> {
var xyz = c.getArgument("xyz", DynamicXYZArgumentType.XYZLocationMapper.class);
return executePathfinding(c, bot -> new PosGoal(SFVec3i.fromDouble(
xyz.getAbsoluteLocation(bot.dataManager().clientEntity().pos())
)));
}))));
dispatcher.register(
literal("collect")
.then(argument("block", new TagBasedArgumentType<BlockType, BlockTagResolvable>(
Expand Down Expand Up @@ -286,6 +283,54 @@ public void postConstruct() {
return Command.SINGLE_SUCCESS;
});
})))));
dispatcher.register(
literal("excavate")
.then(literal("rectangle")
.then(argument("from", new DynamicXYZArgumentType())
.then(argument("to", new DynamicXYZArgumentType())
.executes(
help(
"Makes selected bots dig a rectangle from the from to the to coordinates",
c -> {
var from = c.getArgument("from", DynamicXYZArgumentType.XYZLocationMapper.class);
var to = c.getArgument("to", DynamicXYZArgumentType.XYZLocationMapper.class);

return forEveryBot(
c,
bot -> {
var dataManager = bot.dataManager();
bot.scheduler().schedule(() -> new ExcavateAreaController(
ExcavateAreaController.getRectangleFromTo(
SFVec3i.fromDouble(from.getAbsoluteLocation(dataManager.clientEntity().pos())),
SFVec3i.fromDouble(to.getAbsoluteLocation(dataManager.clientEntity().pos()))
)
).start(bot));

return Command.SINGLE_SUCCESS;
});
})))))
.then(literal("sphere")
.then(argument("position", new DynamicXYZArgumentType())
.then(argument("radius", IntegerArgumentType.integer(1))
.executes(
help(
"Makes selected bots dig a sphere with the given radius",
c -> {
var position = c.getArgument("position", DynamicXYZArgumentType.XYZLocationMapper.class);
var radius = IntegerArgumentType.getInteger(c, "radius");

return forEveryBot(
c,
bot -> {
var dataManager = bot.dataManager();

bot.scheduler().schedule(() -> new ExcavateAreaController(
ExcavateAreaController.getSphereRadius(SFVec3i.fromDouble(position.getAbsoluteLocation(dataManager.clientEntity().pos())), radius)
).start(bot));

return Command.SINGLE_SUCCESS;
});
}))))));
dispatcher.register(
literal("stop-path")
.executes(
Expand Down Expand Up @@ -324,31 +369,24 @@ public void postConstruct() {
// Movement controls
dispatcher.register(
literal("lookat")
.then(
argument("x", DoubleArgumentType.doubleArg())
.then(
argument("y", DoubleArgumentType.doubleArg())
.then(
argument("z", DoubleArgumentType.doubleArg())
.executes(
help(
"Makes selected bots look at the block at the xyz coordinates",
c -> {
var x = DoubleArgumentType.getDouble(c, "x");
var y = DoubleArgumentType.getDouble(c, "y");
var z = DoubleArgumentType.getDouble(c, "z");

return forEveryBot(
c,
bot -> {
bot.dataManager()
.clientEntity()
.lookAt(
RotationOrigin.EYES,
Vector3d.from(x, y, z));
return Command.SINGLE_SUCCESS;
});
}))))));
.then(argument("xyz", new DynamicXYZArgumentType())
.executes(
help(
"Makes selected bots look at the block at the xyz coordinates",
c -> {
var xyz = c.getArgument("xyz", DynamicXYZArgumentType.XYZLocationMapper.class);

return forEveryBot(
c,
bot -> {
bot.dataManager()
.clientEntity()
.lookAt(
RotationOrigin.EYES,
xyz.getAbsoluteLocation(bot.dataManager().clientEntity().pos()));
return Command.SINGLE_SUCCESS;
});
}))));
dispatcher.register(
literal("move")
.then(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* SoulFire
* Copyright (C) 2024 AlexProgrammerDE
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.soulfiremc.server.brigadier;

import com.mojang.brigadier.LiteralMessage;
import com.mojang.brigadier.StringReader;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;

public class ArgumentTypeHelper {
private ArgumentTypeHelper() {
}

public static void mustReadSpace(StringReader reader) throws CommandSyntaxException {
if (!reader.canRead() || reader.peek() != ' ') {
throw new SimpleCommandExceptionType(new LiteralMessage("Expected space")).createWithContext(reader);
}

reader.skip();
}

public static DoubleAxisData readAxis(StringReader reader) throws CommandSyntaxException {
if (reader.canRead() && reader.peek() == '~') {
reader.skip();
return new DoubleAxisData(true, reader.readDouble());
}

return new DoubleAxisData(false, reader.readDouble());
}

public record DoubleAxisData(boolean relative, double value) {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* SoulFire
* Copyright (C) 2024 AlexProgrammerDE
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.soulfiremc.server.brigadier;

import com.mojang.brigadier.StringReader;
import com.mojang.brigadier.arguments.ArgumentType;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import lombok.RequiredArgsConstructor;
import org.cloudburstmc.math.vector.Vector3d;

import java.util.Collection;
import java.util.List;

@RequiredArgsConstructor
public class DynamicXYZArgumentType implements ArgumentType<DynamicXYZArgumentType.XYZLocationMapper> {
@Override
public XYZLocationMapper parse(StringReader stringReader) throws CommandSyntaxException {
var x = ArgumentTypeHelper.readAxis(stringReader);
ArgumentTypeHelper.mustReadSpace(stringReader);
var y = ArgumentTypeHelper.readAxis(stringReader);
ArgumentTypeHelper.mustReadSpace(stringReader);
var z = ArgumentTypeHelper.readAxis(stringReader);

return baseLocation -> {
var xValue = x.relative() ? baseLocation.getX() + x.value() : x.value();
var yValue = y.relative() ? baseLocation.getY() + y.value() : y.value();
var zValue = z.relative() ? baseLocation.getZ() + z.value() : z.value();

return Vector3d.from(xValue, yValue, zValue);
};
}

@Override
public Collection<String> getExamples() {
return List.of("~ ~ ~", "~ ~1 ~", "1 2 3", "~ ~ ~-1", "~ ~ ~0");
}

public interface XYZLocationMapper {
Vector3d getAbsoluteLocation(Vector3d baseLocation);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* SoulFire
* Copyright (C) 2024 AlexProgrammerDE
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.soulfiremc.server.brigadier;

import com.mojang.brigadier.StringReader;
import com.mojang.brigadier.arguments.ArgumentType;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import lombok.RequiredArgsConstructor;
import org.cloudburstmc.math.vector.Vector2d;

import java.util.Collection;
import java.util.List;

@RequiredArgsConstructor
public class DynamicXZArgumentType implements ArgumentType<DynamicXZArgumentType.XZLocationMapper> {
@Override
public XZLocationMapper parse(StringReader stringReader) throws CommandSyntaxException {
var x = ArgumentTypeHelper.readAxis(stringReader);
ArgumentTypeHelper.mustReadSpace(stringReader);
var z = ArgumentTypeHelper.readAxis(stringReader);

return baseLocation -> {
var xValue = x.relative() ? baseLocation.getX() + x.value() : x.value();
var zValue = z.relative() ? baseLocation.getY() + z.value() : z.value();

return Vector2d.from(xValue, zValue);
};
}

@Override
public Collection<String> getExamples() {
return List.of("~ ~", "~ ~1", "1 2", "~ ~-1", "~ ~0");
}

public interface XZLocationMapper {
Vector2d getAbsoluteLocation(Vector2d baseLocation);
}
}
Loading

0 comments on commit db586a2

Please sign in to comment.