Files
src
build.gradle
gradle.properties
settings.gradle
Code
8 353
src/main/java/dev/kodari/playerbots/PlayerBotsMod.java
package dev.kodari.playerbots;
import com.mojang.authlib.GameProfile;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.arguments.IntegerArgumentType;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ThreadLocalRandom;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents;
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
import net.minecraft.network.packet.s2c.play.PlayerListS2CPacket;
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.text.Text;
import net.minecraft.util.math.Vec3d;
public final class PlayerBotsMod implements ModInitializer {
public static final String MOD_ID = "playerbots";
private static final int DEFAULT_SKIN_VARIANTS = 18;
private static final String[] NAME_PREFIXES = {"Swift", "Quiet", "Mossy", "Bright", "River", "Lucky", "Frost", "Copper", "Cloud", "Pixel", "Sunny", "Brave"};
private static final String[] NAME_SUFFIXES = {"Fox", "Birch", "Otter", "Raven", "Maple", "Comet", "Finch", "Moth", "Cedar", "Panda", "Sparrow", "Wolf"};
private static final Set<String> GENERATED_NAMES = new HashSet<>();
private static final Set<Integer> GENERATED_SKIN_VARIANTS = new HashSet<>();
private static final List<BotPlayerEntity> BOTS = new ArrayList<>();
@Override
public void onInitialize() {
CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> registerCommands(dispatcher));
ServerPlayConnectionEvents.JOIN.register((handler, sender, server) -> {
for (BotPlayerEntity bot : BOTS) {
handler.sendPacket(new PlayerListS2CPacket(PlayerListS2CPacket.Action.ADD_PLAYER, bot));
}
});
ServerLifecycleEvents.SERVER_STOPPED.register(server -> {
BOTS.clear();
GENERATED_NAMES.clear();
GENERATED_SKIN_VARIANTS.clear();
});
}
private static void registerCommands(CommandDispatcher<ServerCommandSource> dispatcher) {
dispatcher.register(CommandManager.literal("bot")
.then(CommandManager.literal("spawn")
.executes(context -> spawnBots(context.getSource(), 1))
.then(CommandManager.argument("count", IntegerArgumentType.integer(1, 32))
.executes(context -> spawnBots(
context.getSource(),
IntegerArgumentType.getInteger(context, "count")
)))));
}
private static int spawnBots(ServerCommandSource source, int count) {
MinecraftServer server = source.getServer();
ServerWorld world = source.getWorld();
Vec3d spawnPosition = source.getPosition();
List<String> spawnedNames = new ArrayList<>();
for (int i = 0; i < count; i++) {
UUID profileId = generateUniqueSkinUuid();
if (profileId == null) {
break;
}
String username = generateUsername();
BotPlayerEntity bot = new BotPlayerEntity(server, world, new GameProfile(profileId, username));
double spawnAngle = ThreadLocalRandom.current().nextDouble(Math.PI * 2.0);
bot.setPosition(
spawnPosition.x + Math.cos(spawnAngle) * 1.5,
spawnPosition.y,
spawnPosition.z + Math.sin(spawnAngle) * 1.5
);
bot.setYaw((float) ThreadLocalRandom.current().nextDouble(360.0));
server.getPlayerManager().sendToAll(
new PlayerListS2CPacket(PlayerListS2CPacket.Action.ADD_PLAYER, bot)
);
world.onPlayerConnected(bot);
bot.onSpawn();
GENERATED_NAMES.add(username);
BOTS.add(bot);
spawnedNames.add(username);
}
if (spawnedNames.isEmpty()) {
source.sendFeedback(() -> Text.literal("All 18 different vanilla player skins are already in use."), false);
return 0;
}
String result = "Spawned " + spawnedNames.size() + " player bot(s): " + String.join(", ", spawnedNames);
if (spawnedNames.size() < count) {
result += ". Vanilla clients provide 18 distinct default player skins, so no more uniquely skinned bots can be added.";
}
String feedback = result;
source.sendFeedback(() -> Text.literal(feedback), false);
return spawnedNames.size();
}
private static UUID generateUniqueSkinUuid() {
if (GENERATED_SKIN_VARIANTS.size() >= DEFAULT_SKIN_VARIANTS) {
return null;
}
while (true) {
UUID uuid = UUID.randomUUID();
int skinVariant = Math.floorMod(uuid.hashCode(), DEFAULT_SKIN_VARIANTS);
if (GENERATED_SKIN_VARIANTS.add(skinVariant)) {
return uuid;
}
}
}
private static String generateUsername() {
ThreadLocalRandom random = ThreadLocalRandom.current();
String username;
do {
username = NAME_PREFIXES[random.nextInt(NAME_PREFIXES.length)]
+ NAME_SUFFIXES[random.nextInt(NAME_SUFFIXES.length)]
+ random.nextInt(100, 1000);
} while (GENERATED_NAMES.contains(username));
return username;
}
}