Skip to content

Commit

Permalink
Add metadata cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexProgrammerDE committed Nov 20, 2024
1 parent 484a5ec commit 7124c73
Show file tree
Hide file tree
Showing 6 changed files with 196 additions and 111 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ private Stream<String> generateFields(Class<?> clazz) {
dataName = dataName.substring(fieldPrefix.length());
}

var fieldSuffix = "_ID";
if (dataName.endsWith(fieldSuffix)) {
dataName = dataName.substring(0, dataName.length() - fieldSuffix.length());
}

field.setAccessible(true);
var dataValue = (EntityDataAccessor<?>) field.get(null);
fields.add("public static final NamedEntityData %s = register(\"%s\", %d, \"%s\");".formatted(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
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;
Expand All @@ -53,7 +52,6 @@
import com.soulfiremc.server.spark.SFSparkCommandSender;
import com.soulfiremc.server.spark.SFSparkPlugin;
import com.soulfiremc.server.user.ServerCommandSource;
import com.soulfiremc.server.util.SFHelpers;
import com.soulfiremc.server.util.SFPathConstants;
import com.soulfiremc.server.util.UUIDHelper;
import com.soulfiremc.server.viaversion.SFVersionConstants;
Expand Down Expand Up @@ -277,37 +275,14 @@ public void postConstruct() {
return forEveryBot(
c,
bot -> {
var dataManager = bot.dataManager();

var parsedUniqueId = UUIDHelper.tryParseUniqueId(entityName);
var entityId = -1;
for (var entity : dataManager.entityTrackerState().getEntities()) {
if (entity.entityType() != EntityType.PLAYER) {
continue;
}

var connectedUsers = dataManager.playerListState();
var entry = connectedUsers.entries().get(entity.uuid());
if (entry != null
&& ((parsedUniqueId.isPresent() && entry.getProfileId().equals(parsedUniqueId.get()))
|| (entry.getProfile() != null && entry.getProfile().getName().equalsIgnoreCase(entityName)))
) {
entityId = entity.entityId();
break;
}
}

var entityId = ArgumentTypeHelper.parseEntityId(bot, entityName);
if (entityId == -1) {
var parsedEntityId = SFHelpers.parseInt(entityName);
if (parsedEntityId.isEmpty()) {
c.getSource().sendWarn("Invalid entity specified!");
return Command.SINGLE_SUCCESS;
}
c.getSource().sendWarn("Invalid entity specified!");
return Command.SINGLE_SUCCESS;
}

var finalEntityId = entityId;
bot.scheduler().schedule(() -> new FollowEntityController(
finalEntityId,
entityId,
maxRadius
).start(bot));

Expand Down Expand Up @@ -681,6 +656,35 @@ public void postConstruct() {

return Command.SINGLE_SUCCESS;
}))));
dispatcher.register(
literal("metadata")
.then(argument("entity", StringArgumentType.string())
.executes(
help(
"Makes selected bots follow an entity by id",
c -> {
var entityName = StringArgumentType.getString(c, "entity");

return forEveryBot(
c,
bot -> {
var entityId = ArgumentTypeHelper.parseEntityId(bot, entityName);
if (entityId == -1) {
c.getSource().sendWarn("Invalid entity specified!");
return Command.SINGLE_SUCCESS;
}

var entity = bot.dataManager().entityTrackerState().getEntity(entityId);
if (entity == null) {
c.getSource().sendWarn("Entity not found!");
return Command.SINGLE_SUCCESS;
}

c.getSource().sendInfo("Metadata for entity {}: {}", entityId, entity.metadataState().toNamedMap());

return Command.SINGLE_SUCCESS;
});
}))));
dispatcher.register(
literal("export-map")
.executes(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
import com.mojang.brigadier.StringReader;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
import com.soulfiremc.server.data.EntityType;
import com.soulfiremc.server.protocol.BotConnection;
import com.soulfiremc.server.util.SFHelpers;
import com.soulfiremc.server.util.UUIDHelper;

public class ArgumentTypeHelper {
private ArgumentTypeHelper() {
Expand Down Expand Up @@ -50,4 +54,37 @@ public static DoubleAxisData readAxis(StringReader reader) throws CommandSyntaxE

public record DoubleAxisData(boolean relative, double value) {
}

public static int parseEntityId(BotConnection bot, String input) {
var dataManager = bot.dataManager();

var parsedUniqueId = UUIDHelper.tryParseUniqueId(input);
var entityId = -1;
for (var entity : dataManager.entityTrackerState().getEntities()) {
if (entity.entityType() != EntityType.PLAYER) {
continue;
}

var connectedUsers = dataManager.playerListState();
var entry = connectedUsers.entries().get(entity.uuid());
if (entry != null
&& ((parsedUniqueId.isPresent() && entry.getProfileId().equals(parsedUniqueId.get()))
|| (entry.getProfile() != null && entry.getProfile().getName().equalsIgnoreCase(input)))
) {
entityId = entity.entityId();
break;
}
}

if (entityId == -1) {
var parsedEntityId = SFHelpers.parseInt(input);
if (parsedEntityId.isEmpty()) {
return -1;
}

entityId = parsedEntityId.getAsInt();
}

return entityId;
}
}
Loading

0 comments on commit 7124c73

Please sign in to comment.