Files
src
build.gradle
gradle.properties
settings.gradle
Code
5 287
src/main/java/com/kodari/clipbot/ClipBotMod.java
package com.kodari.clipbot;
import com.mojang.authlib.GameProfile;
import com.mojang.brigadier.arguments.IntegerArgumentType;
import com.mojang.brigadier.arguments.StringArgumentType;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
import net.fabricmc.fabric.api.entity.FakePlayer;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents;
import net.minecraft.command.argument.EntityAnchorArgumentType;
import net.minecraft.enchantment.Enchantment;
import net.minecraft.enchantment.Enchantments;
import net.minecraft.entity.EquipmentSlot;
import net.minecraft.entity.MovementType;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.entity.projectile.thrown.EnderPearlEntity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.network.packet.s2c.play.PlayerListS2CPacket;
import net.minecraft.network.packet.s2c.play.PlayerRemoveS2CPacket;
import net.minecraft.registry.entry.RegistryEntry;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.command.CommandManager;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.Hand;
import net.minecraft.util.Util;
import net.minecraft.util.math.Vec3d;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.ThreadLocalRandom;
public class ClipBotMod implements ModInitializer {
private static final Map<UUID, BotState> BOTS = new HashMap<>();
@Override
public void onInitialize() {
CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> dispatcher.register(
CommandManager.literal("spawnbot")
.then(CommandManager.argument("nickname", StringArgumentType.word())
.then(CommandManager.argument("totems", IntegerArgumentType.integer(0))
.executes(context -> {
String nickname = StringArgumentType.getString(context, "nickname");
int totems = IntegerArgumentType.getInteger(context, "totems");
ServerCommandSource source = context.getSource();
ServerPlayerEntity owner = source.getPlayerOrThrow();
ServerWorld world = source.getWorld();
MinecraftServer server = source.getServer();
Optional<GameProfile> profile = Optional.empty();
java.util.concurrent.CompletableFuture
.supplyAsync(
() -> server.getApiServices().profileResolver().getProfileByName(nickname),
Util.getDownloadWorkerExecutor()
)
.handle((resolved, error) -> error == null ? resolved : profile)
.thenAccept(resolved -> server.execute(() -> {
if (!owner.isAlive() || owner.getEntityWorld() != world) {
return;
}
GameProfile botProfile = resolved
.map(original -> new GameProfile(UUID.randomUUID(), nickname, original.properties()))
.orElseGet(() -> new GameProfile(UUID.randomUUID(), nickname));
spawnBot(server, world, owner, botProfile, totems);
}));
return 1;
}))
)
));
ServerTickEvents.END_WORLD_TICK.register(ClipBotMod::tickWorld);
}
private static void spawnBot(MinecraftServer server, ServerWorld world, ServerPlayerEntity owner, GameProfile profile, int totems) {
FakePlayer bot = FakePlayer.get(world, profile);
bot.setPosition(owner.getX() + 1.25, owner.getY(), owner.getZ());
bot.setYaw(owner.getYaw());
bot.setPitch(0.0F);
RegistryEntry<Enchantment> protection = world.getRegistryManager().getEntryOrThrow(Enchantments.PROTECTION);
RegistryEntry<Enchantment> blastProtection = world.getRegistryManager().getEntryOrThrow(Enchantments.BLAST_PROTECTION);
RegistryEntry<Enchantment> unbreaking = world.getRegistryManager().getEntryOrThrow(Enchantments.UNBREAKING);
equip(bot, EquipmentSlot.HEAD, Items.NETHERITE_HELMET, protection, unbreaking);
equip(bot, EquipmentSlot.CHEST, Items.NETHERITE_CHESTPLATE, protection, unbreaking);
equip(bot, EquipmentSlot.LEGS, Items.NETHERITE_LEGGINGS, blastProtection, unbreaking);
equip(bot, EquipmentSlot.FEET, Items.NETHERITE_BOOTS, protection, unbreaking);
if (totems > 0) {
bot.getInventory().setStack(PlayerInventory.OFF_HAND_SLOT, new ItemStack(Items.TOTEM_OF_UNDYING));
}
server.getPlayerManager().sendToAll(new PlayerListS2CPacket(PlayerListS2CPacket.Action.ADD_PLAYER, bot));
world.onPlayerConnected(bot);
BotState state = new BotState(server, world, owner, bot, totems);
BOTS.put(profile.id(), state);
}
private static void equip(FakePlayer bot, EquipmentSlot slot, Item item, RegistryEntry<Enchantment> primary, RegistryEntry<Enchantment> unbreaking) {
ItemStack stack = new ItemStack(item);
stack.addEnchantment(primary, 4);
stack.addEnchantment(unbreaking, 3);
bot.equipStack(slot, stack);
}
private static void tickWorld(ServerWorld world) {
Iterator<BotState> iterator = BOTS.values().iterator();
while (iterator.hasNext()) {
BotState state = iterator.next();
if (state.world != world) {
continue;
}
if (state.bot.isRemoved() || !state.bot.isAlive() || !state.owner.isAlive() || state.owner.getEntityWorld() != world) {
removeBot(state);
iterator.remove();
continue;
}
ItemStack offhand = state.bot.getInventory().getStack(PlayerInventory.OFF_HAND_SLOT);
if (offhand.isEmpty() && state.totemsRemaining > 0) {
state.totemsRemaining--;
state.approaching = false;
state.approachDelay = randomBetween(8, 21);
state.bot.setVelocity(0.0, 0.0, 0.0);
if (state.totemsRemaining > 0) {
state.bot.getInventory().setStack(PlayerInventory.OFF_HAND_SLOT, new ItemStack(Items.TOTEM_OF_UNDYING));
}
}
if (--state.swingCooldown <= 0) {
state.bot.swingHand(Hand.MAIN_HAND);
state.swingCooldown = randomBetween(25, 101);
}
if (--state.pearlCooldown <= 0) {
if (!state.approaching) {
state.bot.lookAt(EntityAnchorArgumentType.EntityAnchor.EYES, state.owner.getEyePos());
EnderPearlEntity pearl = new EnderPearlEntity(world, state.bot, new ItemStack(Items.ENDER_PEARL));
pearl.setVelocity(state.bot, state.bot.getPitch(), state.bot.getYaw(), 0.0F, 1.5F, 0.0F);
world.spawnEntity(pearl);
}
state.pearlCooldown = randomBetween(80, 301);
}
if (state.approachDelay > 0) {
state.approachDelay--;
} else if (!state.approaching) {
state.approaching = true;
state.bot.setYaw(state.bot.getYaw() + ThreadLocalRandom.current().nextFloat(90.0F, 271.0F));
}
if (state.approaching) {
double dx = state.owner.getX() - state.bot.getX();
double dz = state.owner.getZ() - state.bot.getZ();
double distance = Math.sqrt(dx * dx + dz * dz);
if (distance > 1.5) {
state.bot.move(MovementType.SELF, new Vec3d(dx / distance * 0.025, 0.0, dz / distance * 0.025));
}
}
}
}
private static void removeBot(BotState state) {
state.server.getPlayerManager().sendToAll(new PlayerRemoveS2CPacket(List.of(state.bot.getGameProfile().id())));
state.bot.discard();
}
private static int randomBetween(int minimumInclusive, int maximumExclusive) {
return ThreadLocalRandom.current().nextInt(minimumInclusive, maximumExclusive);
}
private static final class BotState {
private final MinecraftServer server;
private final ServerWorld world;
private final ServerPlayerEntity owner;
private final FakePlayer bot;
private int totemsRemaining;
private int pearlCooldown = randomBetween(80, 301);
private int swingCooldown = randomBetween(25, 101);
private int approachDelay = randomBetween(8, 21);
private boolean approaching;
private BotState(MinecraftServer server, ServerWorld world, ServerPlayerEntity owner, FakePlayer bot, int totemsRemaining) {
this.server = server;
this.world = world;
this.owner = owner;
this.bot = bot;
this.totemsRemaining = totemsRemaining;
}
}
}