Skip to content

Commit

Permalink
Implement experimental mimic command
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexProgrammerDE committed Dec 16, 2024
1 parent 7ce9085 commit 57ab9a7
Show file tree
Hide file tree
Showing 9 changed files with 163 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -221,12 +221,12 @@ public void postConstruct() {
c,
bot -> {
var entityId = ArgumentTypeHelper.parseEntityId(bot, entityName);
if (entityId == -1) {
if (entityId.isEmpty()) {
c.getSource().sendWarn("Invalid entity specified!");
return Command.SINGLE_SUCCESS;
}

var entity = bot.dataManager().entityTrackerState().getEntity(entityId);
var entity = bot.dataManager().entityTrackerState().getEntity(entityId.getAsInt());
if (entity == null) {
c.getSource().sendWarn("Entity not found!");
return Command.SINGLE_SUCCESS;
Expand Down Expand Up @@ -345,13 +345,13 @@ public void postConstruct() {
c,
bot -> {
var entityId = ArgumentTypeHelper.parseEntityId(bot, entityName);
if (entityId == -1) {
if (entityId.isEmpty()) {
c.getSource().sendWarn("Invalid entity specified!");
return Command.SINGLE_SUCCESS;
}

bot.scheduler().schedule(() -> new FollowEntityController(
entityId,
entityId.getAsInt(),
maxRadius
).start(bot));

Expand Down Expand Up @@ -406,6 +406,58 @@ public void postConstruct() {
return Command.SINGLE_SUCCESS;
});
}))))));
dispatcher.register(
literal("mimic")
.then(argument("entity", StringArgumentType.string())
.executes(
help(
"Makes selected bots mimic the movement of other entities",
c -> {
var entityName = StringArgumentType.getString(c, "entity");

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

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

var offset = entity.pos().sub(bot.dataManager().localPlayer().pos());
bot.botControl().registerControllingTask(new ControllingTask() {
@Override
public void tick() {
bot.controlState().resetAll();

var localPlayer = bot.dataManager().localPlayer();
localPlayer.setYRot(entity.yRot());
localPlayer.setXRot(entity.xRot());

localPlayer.setPos(entity.pos().sub(offset));
localPlayer.setDeltaMovement(entity.deltaMovement());
}

@Override
public void stop() {
bot.controlState().resetAll();
}

@Override
public boolean isDone() {
return false;
}
});

return Command.SINGLE_SUCCESS;
});
}))));
dispatcher.register(
literal("stop-task")
.executes(
Expand Down Expand Up @@ -734,12 +786,12 @@ public void postConstruct() {
c,
bot -> {
var entityId = ArgumentTypeHelper.parseEntityId(bot, entityName);
if (entityId == -1) {
if (entityId.isEmpty()) {
c.getSource().sendWarn("Invalid entity specified!");
return Command.SINGLE_SUCCESS;
}

var entity = bot.dataManager().entityTrackerState().getEntity(entityId);
var entity = bot.dataManager().entityTrackerState().getEntity(entityId.getAsInt());
if (entity == null) {
c.getSource().sendWarn("Entity not found!");
return Command.SINGLE_SUCCESS;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@
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;

import java.util.OptionalInt;

public class ArgumentTypeHelper {
private ArgumentTypeHelper() {
}
Expand Down Expand Up @@ -55,36 +55,27 @@ public static DoubleAxisData readAxis(StringReader reader) throws CommandSyntaxE
public record DoubleAxisData(boolean relative, double value) {
}

public static int parseEntityId(BotConnection bot, String input) {
public static OptionalInt 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;
if (parsedUniqueId.isPresent() && entity.uuid().equals(parsedUniqueId.get())) {
return OptionalInt.of(entity.entityId());
}

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;
var entityProfile = bot.getEntityProfile(entity.uuid());
if (entityProfile.isEmpty()) {
continue;
}
}

if (entityId == -1) {
var parsedEntityId = SFHelpers.parseInt(input);
if (parsedEntityId.isEmpty()) {
return -1;
var profile = entityProfile.get();
var gameProfile = profile.getProfile();
if (gameProfile != null && gameProfile.getName().equals(input)) {
return OptionalInt.of(entity.entityId());
}

entityId = parsedEntityId.getAsInt();
}

return entityId;
return OptionalInt.empty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import org.slf4j.MDC;

import java.util.List;
import java.util.Optional;
import java.util.Queue;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
Expand Down Expand Up @@ -253,12 +254,7 @@ public void sendPacket(Packet packet) {
session.send(packet);
}

public PlayerListEntry getEntityProfile(UUID uuid) {
var profile = dataManager.playerListState().entries().get(uuid);
if (profile == null) {
throw new IllegalStateException("Profile not found for " + uuid);
}

return profile;
public Optional<PlayerListEntry> getEntityProfile(UUID uuid) {
return Optional.ofNullable(dataManager.playerListState().entries().get(uuid));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,7 @@
import com.soulfiremc.server.protocol.bot.model.ChunkKey;
import com.soulfiremc.server.protocol.bot.model.ServerPlayData;
import com.soulfiremc.server.protocol.bot.state.*;
import com.soulfiremc.server.protocol.bot.state.entity.EntityFactory;
import com.soulfiremc.server.protocol.bot.state.entity.ExperienceOrbEntity;
import com.soulfiremc.server.protocol.bot.state.entity.LivingEntity;
import com.soulfiremc.server.protocol.bot.state.entity.LocalPlayer;
import com.soulfiremc.server.protocol.bot.state.entity.*;
import com.soulfiremc.server.protocol.bot.state.registry.Biome;
import com.soulfiremc.server.protocol.bot.state.registry.DimensionType;
import com.soulfiremc.server.protocol.bot.state.registry.SFChatType;
Expand Down Expand Up @@ -70,6 +67,7 @@
import org.geysermc.mcprotocollib.protocol.data.game.chunk.palette.PaletteType;
import org.geysermc.mcprotocollib.protocol.data.game.entity.attribute.AttributeModifier;
import org.geysermc.mcprotocollib.protocol.data.game.entity.player.GameMode;
import org.geysermc.mcprotocollib.protocol.data.game.entity.player.PositionElement;
import org.geysermc.mcprotocollib.protocol.data.game.level.notify.LimitedCraftingValue;
import org.geysermc.mcprotocollib.protocol.data.game.level.notify.RainStrengthValue;
import org.geysermc.mcprotocollib.protocol.data.game.level.notify.RespawnScreenValue;
Expand Down Expand Up @@ -152,6 +150,24 @@ public SessionDataManager(BotConnection connection) {
this.connection = connection;
}

private static void setValuesFromPositionPacket(EntityMovement newMovement, List<PositionElement> set, Entity entity, boolean canLerp) {
var lv = EntityMovement.ofEntityUsingLerpTarget(entity);
var absolutePos = EntityMovement.calculateAbsolute(lv, newMovement, set);
var teleport = lv.pos().distanceSquared(absolutePos.pos()) > 4096.0;
if (canLerp && !teleport) {
entity.lerpTo(absolutePos.pos().getX(), absolutePos.pos().getY(), absolutePos.pos().getZ(), absolutePos.yRot(), absolutePos.xRot(), 3);
entity.setDeltaMovement(absolutePos.deltaMovement());
} else {
entity.setPos(absolutePos.pos());
entity.setDeltaMovement(absolutePos.deltaMovement());
entity.setYRot(absolutePos.yRot());
entity.setXRot(absolutePos.xRot());
var oldPos = new EntityMovement(entity.oldPosition(), Vector3d.ZERO, entity.yRotO, entity.xRotO);
var movedOldPos = EntityMovement.calculateAbsolute(oldPos, newMovement, set);
entity.setOldPosAndRot(movedOldPos.pos(), movedOldPos.yRot(), movedOldPos.xRot());
}
}

private static String toPlainText(Component component) {
return SoulFireServer.PLAIN_MESSAGE_SERIALIZER.serialize(component);
}
Expand Down Expand Up @@ -364,12 +380,12 @@ public void onTickingStep(ClientboundTickingStepPacket packet) {

@EventHandler
public void onPosition(ClientboundPlayerPositionPacket packet) {
localPlayer.setFrom(new EntityMovement(
setValuesFromPositionPacket(new EntityMovement(
packet.getPosition(),
packet.getDeltaMovement(),
packet.getYRot(),
packet.getXRot()
), packet.getRelatives());
), packet.getRelatives(), localPlayer, false);

var position = localPlayer.blockPos();
if (!joinedWorld) {
Expand Down Expand Up @@ -1092,12 +1108,16 @@ public void onEntityTeleport(ClientboundTeleportEntityPacket packet) {
return;
}

state.setFrom(new EntityMovement(
var horizontalAbsolute = packet.getRelatives().contains(PositionElement.X)
|| packet.getRelatives().contains(PositionElement.Y)
|| packet.getRelatives().contains(PositionElement.Z);
var canLerp = !state.isControlledByLocalInstance() || horizontalAbsolute;
setValuesFromPositionPacket(new EntityMovement(
packet.getPosition(),
packet.getDeltaMovement(),
packet.getYRot(),
packet.getXRot()
), packet.getRelatives());
), packet.getRelatives(), state, canLerp);
state.setOnGround(packet.isOnGround());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public boolean isCreative() {

private PlayerListEntry getPlayerListEntry() {
if (playerListEntry == null) {
playerListEntry = connection.getEntityProfile(uuid);
playerListEntry = connection.getEntityProfile(uuid).orElseThrow();
}

return playerListEntry;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import com.soulfiremc.server.protocol.bot.state.EntityEffectState;
import com.soulfiremc.server.protocol.bot.state.EntityMetadataState;
import com.soulfiremc.server.protocol.bot.state.Level;
import com.soulfiremc.server.util.EntityMovement;
import com.soulfiremc.server.util.MathHelper;
import com.soulfiremc.server.util.SectionUtils;
import com.soulfiremc.server.util.VectorHelper;
Expand Down Expand Up @@ -56,7 +55,6 @@
import org.geysermc.mcprotocollib.protocol.data.game.entity.metadata.type.ByteEntityMetadata;
import org.geysermc.mcprotocollib.protocol.data.game.entity.metadata.type.ObjectEntityMetadata;
import org.geysermc.mcprotocollib.protocol.data.game.entity.object.ObjectData;
import org.geysermc.mcprotocollib.protocol.data.game.entity.player.PositionElement;
import org.geysermc.mcprotocollib.protocol.packet.ingame.clientbound.entity.spawn.ClientboundAddEntityPacket;
import org.jetbrains.annotations.Nullable;

Expand Down Expand Up @@ -167,21 +165,6 @@ public void fromAddEntityPacket(ClientboundAddEntityPacket packet) {
uuid(packet.getUuid());
}

public EntityMovement toMovement() {
return new EntityMovement(pos, deltaMovement, yRot, xRot);
}

public void setFrom(EntityMovement packetMovement, List<PositionElement> relatives) {
var entityMovement = EntityMovement.toAbsolute(toMovement(), packetMovement, relatives);
setPos(entityMovement.pos());
setDeltaMovement(entityMovement.deltaMovement());
setRot(entityMovement.yRot(), entityMovement.xRot());

var oldMovement = new EntityMovement(oldPosition(), Vector3d.ZERO, yRotO, xRotO);
var absoluteOldMovement = EntityMovement.toAbsolute(oldMovement, packetMovement, relatives);
setOldPosAndRot(absoluteOldMovement.pos(), absoluteOldMovement.yRot(), absoluteOldMovement.xRot());
}

public void syncPacketPositionCodec(double x, double y, double z) {
this.packetPositionCodec.base(Vector3d.from(x, y, z));
}
Expand All @@ -198,6 +181,10 @@ public void moveTo(double x, double y, double z, float yRot, float xRot) {
this.reapplyPosition();
}

public Vector3d getKnownMovement() {
return this.getDeltaMovement();
}

public double x() {
return pos.getX();
}
Expand All @@ -210,6 +197,26 @@ public double z() {
return pos.getZ();
}

public double lerpTargetX() {
return this.x();
}

public double lerpTargetY() {
return this.y();
}

public double lerpTargetZ() {
return this.z();
}

public float lerpTargetXRot() {
return this.xRot();
}

public float lerpTargetYRot() {
return this.yRot();
}

public EntityDimensions getDimensions(Pose pose) {
return entityType.dimensions();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
public class EntityFactory {
public static Entity createEntity(BotConnection connection, EntityType entityType, Level level, UUID uuid) {
if (entityType.playerEntity()) {
return new RemotePlayer(connection, level, connection.getEntityProfile(uuid).getProfile());
return new RemotePlayer(connection, level, connection.getEntityProfile(uuid).orElseThrow().getProfile());
} else if (entityType.livingEntity()) {
// TODO: Implement entity inventories
return new LivingEntity(entityType, level) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,31 @@ public void lerpTo(double x, double y, double z, float yRot, float xRot, int ste
this.lerpSteps = steps;
}

@Override
public double lerpTargetX() {
return this.lerpSteps > 0 ? this.lerpX : this.x();
}

@Override
public double lerpTargetY() {
return this.lerpSteps > 0 ? this.lerpY : this.y();
}

@Override
public double lerpTargetZ() {
return this.lerpSteps > 0 ? this.lerpZ : this.z();
}

@Override
public float lerpTargetXRot() {
return this.lerpSteps > 0 ? (float) this.lerpXRot : this.xRot();
}

@Override
public float lerpTargetYRot() {
return this.lerpSteps > 0 ? (float) this.lerpYRot : this.yRot();
}

public void aiStep() {
if (this.noJumpDelay > 0) {
this.noJumpDelay--;
Expand Down
Loading

0 comments on commit 57ab9a7

Please sign in to comment.