Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions src/autoclef/java/adris/altoclef/AltoClefCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import adris.altoclef.commands.AttackPlayerOrMobCommand;
import adris.altoclef.commands.BodyLanguageCommand;
import adris.altoclef.commands.UseItemOnEntityCommand;
import adris.altoclef.commands.DepositCommand;
import adris.altoclef.commands.EquipCommand;
import adris.altoclef.commands.FarmCommand;
Expand All @@ -20,6 +21,7 @@
import adris.altoclef.commands.ResetMemoryCommand;
import adris.altoclef.commands.SetAIBridgeEnabledCommand;
import adris.altoclef.commands.StopCommand;
import adris.altoclef.commands.UseItemOnBlockCommand;
import adris.altoclef.commands.random.ScanCommand;
import adris.altoclef.commands.BuildStructureCommand;
import adris.altoclef.commandsystem.CommandException;
Expand All @@ -30,8 +32,9 @@ public static void init(AltoClefController controller) throws CommandException {
.registerNewCommand(
new GetCommand(),
new EquipCommand(),
new BuildStructureCommand(),
new BodyLanguageCommand(),
new BuildStructureCommand(),
new UseItemOnEntityCommand(),
new DepositCommand(),
new GotoCommand(),
new IdleCommand(),
Expand All @@ -45,10 +48,13 @@ public static void init(AltoClefController controller) throws CommandException {
new GamerCommand(),
new FollowCommand(),
new GiveCommand(),
new ScanCommand(),
// new ScanCommand(), // remove scan cmd for now
new AttackPlayerOrMobCommand(),
new SetAIBridgeEnabledCommand(),
new FarmCommand(),
new FishCommand());
new FishCommand(),
new UseItemOnBlockCommand()

);
}
}
16 changes: 16 additions & 0 deletions src/autoclef/java/adris/altoclef/AltoClefController.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@
import java.util.Optional;
import java.util.UUID;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.mojang.authlib.GameProfile;

import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.level.ServerLevel;
Expand Down Expand Up @@ -80,6 +85,8 @@ public class AltoClefController {
private Task storedTask;
public boolean isStopping = false;
private Player owner;
public FakePlayerManager copiedServerPlayer;
private static final Logger LOGGER = LogManager.getLogger();

public AltoClefController(IBaritone baritone, Character character, String player2GameId) {
this.baritone = baritone;
Expand Down Expand Up @@ -132,6 +139,9 @@ public AltoClefController(IBaritone baritone, Character character, String player
ConversationManager.getOrCreateEventQueueData(this);
this.aiPersistantData = new AIPersistantData(this, character);
this.player2apiService = new Player2APIService(this, player2GameId);
this.copiedServerPlayer = new FakePlayerManager(getWorld(),
this.getPlayer().getUUID(),
character.name());
}

public void serverTick() {
Expand All @@ -144,6 +154,7 @@ public void serverTick() {
this.inputControls.onTickPost();
this.baritone.serverTick();
this.player2apiService.trySendHeartbeat();
copiedServerPlayer.update(this);
}

static {
Expand Down Expand Up @@ -397,4 +408,9 @@ public Optional<ServerPlayer> getClosestPlayer() {
return Float.compare(adist, bdist);
}).findFirst();
}

public void logSignificantError(String err) {
LOGGER.info("Logging significant err={}", err);
ConversationManager.getOrCreateEventQueueData(this).addAltoclefLogMessage(err);
}
}
157 changes: 157 additions & 0 deletions src/autoclef/java/adris/altoclef/FakePlayerManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
package adris.altoclef;

import java.util.UUID;

import com.mojang.authlib.GameProfile;

import net.fabricmc.fabric.api.entity.FakePlayer;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;

public class FakePlayerManager {
FakePlayer playerCopy;

public FakePlayerManager(ServerLevel world, UUID uuid, String name) {
this.playerCopy = FakePlayer.get(world, new GameProfile(uuid, name));
// this.playerCopy = new ServerPlayer(world.getServer(), world,
// new GameProfile(UUID.fromString("TEMP"), name));
}

public void update(AltoClefController controller) {
LivingEntity original = controller.getPlayer();

// ((EntityIMixin) entity).setLevel(copied.level());
// ->
playerCopy.setServerLevel(controller.getWorld().getLevel());

playerCopy.deathTime = original.deathTime;
playerCopy.walkDist = original.walkDist;
playerCopy.walkDistO = original.walkDist;
playerCopy.moveDist = original.moveDist;

playerCopy.zza = original.zza;
playerCopy.xxa = original.xxa;
playerCopy.setOnGround(original.onGround());
playerCopy.fallDistance = original.fallDistance;

// entity.setJumping(((EntityLivingIMixin) copied).jumping());

// List<SynchedEntityData.DataItem<?>> copiedData = ((ISynchedEntityData)
// copied.getEntityData()).getAll();
// List<SynchedEntityData.DataItem<?>> data = ((ISynchedEntityData)
// entity.getEntityData()).getAll();
// for (SynchedEntityData.DataItem<?> entry : copiedData) {
// if (data.stream().anyMatch(e -> e.getAccessor() == entry.getAccessor())) {
// if (entry.getValue() instanceof SynchedEntityData.DataValue) {
// entity.getEntityData().set((EntityDataAccessor<Object>) entry.getAccessor(),
// ((SynchedEntityData.DataValue) entry.getValue()).value());
// }
// }
// }

playerCopy.xo = original.xo;
playerCopy.yo = original.yo;
playerCopy.zo = original.zo;

playerCopy.setPos(original.getX(), original.getY(), original.getZ());
// entity.setEntityBoundingBox(copied.getEntityBoundingBox());

playerCopy.xOld = original.xOld;
playerCopy.yOld = original.yOld;
playerCopy.zOld = original.zOld;

playerCopy.setDeltaMovement(original.getDeltaMovement());

playerCopy.setXRot(original.getXRot());
playerCopy.setYRot(original.getYRot());
playerCopy.xRotO = original.xRotO;
playerCopy.yRotO = original.yRotO;
playerCopy.yHeadRot = original.yHeadRot;
playerCopy.yHeadRotO = original.yHeadRotO;
playerCopy.yBodyRot = original.yBodyRot;
playerCopy.yBodyRotO = original.yBodyRotO;

// ((EntityLivingIMixin)
// entity).useItemRemaining(copied.getUseItemRemainingTicks());

// ((WalkAnimationStateMixin)
// entity.walkAnimation).setPosition(copied.walkAnimation.position());
// ((EntityLivingIMixin) entity).animStep(((EntityLivingIMixin)
// copied).animStep());
// ((EntityLivingIMixin) entity).animStepO(((EntityLivingIMixin)
// copied).animStepO());
// ((EntityLivingIMixin) entity).swimAmount(((EntityLivingIMixin)
// copied).swimAmount());
// ((EntityLivingIMixin) entity).swimAmountO(((EntityLivingIMixin)
// copied).swimAmountO());
playerCopy.swinging = original.swinging;
playerCopy.swingTime = original.swingTime;

playerCopy.walkAnimation.setSpeed(original.walkAnimation.speed());

// ((WalkAnimationStateMixin) entity.walkAnimation)
// .setSpeedOld(((WalkAnimationStateMixin) copied.walkAnimation).getSpeedOld());
playerCopy.attackAnim = original.attackAnim;
playerCopy.oAttackAnim = original.oAttackAnim;

playerCopy.tickCount = original.tickCount;

playerCopy.setHealth(Math.min(original.getHealth(), playerCopy.getMaxHealth()));
playerCopy.hurtTime = original.hurtTime;
playerCopy.deathTime = original.deathTime;

// entity.getPersistentData().merge(copied.getPersistentData());

// if(entity.getVehicle() != copied.getVehicle())
// entity.vehicle = copied.vehicle;

if (playerCopy instanceof Player && original instanceof Player) {
Player ePlayer = (Player) playerCopy;
Player cPlayer = (Player) original;

ePlayer.bob = cPlayer.bob;
ePlayer.oBob = cPlayer.oBob;

ePlayer.xCloakO = cPlayer.xCloakO;
ePlayer.yCloakO = cPlayer.yCloakO;
ePlayer.zCloakO = cPlayer.zCloakO;
ePlayer.xCloak = cPlayer.xCloak;
ePlayer.yCloak = cPlayer.yCloak;
ePlayer.zCloak = cPlayer.zCloak;
}
for (EquipmentSlot slot : EquipmentSlot.values()) {
playerCopy.setItemSlot(slot, original.getItemBySlot(slot));
}

// if (entity instanceof EnderDragon) {
// entity.setXRot(entity.getXRot() + 180);
// }

// ((EntityIMixin) entity).removal(((EntityIMixin) copied).removal());
playerCopy.deathTime = original.deathTime;

playerCopy.tickCount = original.tickCount;

// if (entity instanceof EnderDragon) {
// entity.setYRot(entity.getYRot() + 180);
// }
// if (entity instanceof Chicken) {
// ((Chicken) entity).flap = copied.onGround() ? 0 : 1;
// }

}

public FakePlayer getPlayerCopy() {
return playerCopy;
}

public void updateOriginalItem(AltoClefController mod) {
for (EquipmentSlot slot : EquipmentSlot.values()) {
mod.getPlayer().setItemSlot(slot, playerCopy.getItemBySlot(slot));
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package adris.altoclef.commands;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import adris.altoclef.AltoClefController;
import adris.altoclef.commandsystem.Arg;
import adris.altoclef.commandsystem.ArgParser;
import adris.altoclef.commandsystem.Command;
import adris.altoclef.commandsystem.CommandException;
import adris.altoclef.tasks.misc.UseItemOnBlockTask;
import adris.altoclef.util.helpers.FuzzySearchHelper;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.resources.ResourceLocation;

public class UseItemOnBlockCommand extends Command {
public UseItemOnBlockCommand() throws CommandException {
super("useItemOnBlock",
"Uses an item on a block. Use 'hand' as item if you want empty use (activating lever, etc.). Examples: `useItemOnBlock flintandsteel tnt`, `useItemOnBlock wooden_axe oak_log` `useITemOnEntity hand lever` ",
new Arg<>(String.class, "itemName"),
new Arg<>(String.class, "blockName"));
}

@Override
protected void call(AltoClefController mod, ArgParser parser) throws CommandException {
String itemName = parser.get(String.class);
String blockName = parser.get(String.class);
List<String> allBlockNames = new ArrayList<>();
Block block = null;
for (Block b : BuiltInRegistries.BLOCK) {
ResourceLocation id = BuiltInRegistries.BLOCK.getKey(b); // minecraft:stone, ...
allBlockNames.add(id.toString());
if (id.toString().contains(blockName)) {
block = b;
}
}
if (block == null) {
String closest = FuzzySearchHelper.getClosestMatchMinecraftItems(blockName, allBlockNames);
String errmsg = ("Block named: \"" + blockName + "\" not a valid block. Perhaps the user meant \"" + closest
+ "\"?"
+ (blockName.contains("log") ? " Can try 'log' as well" : ""));
mod.logSignificantError(errmsg);
this.finish();
} else {
mod.runUserTask(new UseItemOnBlockTask(itemName, block), () -> {
this.finish();
});
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package adris.altoclef.commands;

import adris.altoclef.AltoClefController;
import adris.altoclef.commandsystem.Arg;
import adris.altoclef.commandsystem.ArgParser;
import adris.altoclef.commandsystem.Command;
import adris.altoclef.commandsystem.CommandException;
import adris.altoclef.tasks.entity.UseItemOnEntityTask;

public class UseItemOnEntityCommand extends Command {
public UseItemOnEntityCommand() throws CommandException {
super("useItemOnEntity",
"Uses an item on an entity. Use 'hand' as item if you want empty use (get in vehicle, etc.). Examples: `useItemOnEntity shears sheep`, `useItemOnEntity saddle pig` `useITemOnEntity hand minecart` ",
new Arg<>(String.class, "itemName"),
new Arg<>(String.class, "entityName"));
}

@Override
protected void call(AltoClefController mod, ArgParser parser) throws CommandException {
String itemName = parser.get(String.class);
String entityName = parser.get(String.class);
mod.runUserTask(new UseItemOnEntityTask(itemName, entityName), () -> {
this.finish();
});
}

}
Loading