Files
src
build.gradle
gradle.properties
settings.gradle
Code
7 196
src/main/java/com/kodari/mobstacker/MobStacker.java
package com.kodari.mobstacker;
import com.mojang.serialization.Codec;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.attachment.v1.AttachmentRegistry;
import net.fabricmc.fabric.api.attachment.v1.AttachmentType;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.Identifier;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EntitySpawnReason;
import net.minecraft.world.entity.EntityTypes;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.entity.Mob;
import net.minecraft.world.level.entity.EntityTypeTest;
public class MobStacker implements ModInitializer {
public static final long MAX_STACK = 10_000_000_000_000L;
private static final AttachmentType<Long> STACK_SIZE = AttachmentRegistry.createPersistent(
Identifier.fromNamespaceAndPath("mobstacker", "stack_size"), Codec.LONG);
private int ticks;
@Override
public void onInitialize() {
ServerTickEvents.END_SERVER_TICK.register(server -> {
if (++ticks < 100) return;
ticks = 0;
for (ServerLevel level : server.getAllLevels()) {
for (Mob mob : level.getEntities(EntityTypeTest.forClass(Mob.class), MobStacker::eligible)) {
if (!eligible(mob)) continue;
for (Mob other : level.getEntitiesOfClass(Mob.class, mob.getBoundingBox().inflate(8), MobStacker::eligible)) {
if (other == mob || other.getType() != mob.getType() || mob.distanceTo(other) > 8) continue;
long current = size(mob);
long amount = size(other);
if (current >= MAX_STACK) break;
long moved = Math.min(amount, MAX_STACK - current);
setSize(mob, current + moved);
if (moved == amount) other.discard();
else setSize(other, amount - moved);
}
}
}
});
}
private static boolean eligible(Mob mob) {
if (!mob.isAlive() || mob.isDeadOrDying() || mob.isBaby() || mob.isPersistenceRequired()
|| mob.isPassenger() || !mob.getPassengers().isEmpty()) return false;
if (mob.getType() != EntityTypes.ZOMBIE && mob.getType() != EntityTypes.SPIDER) return false;
if (mob.hasCustomName() && mob.getAttached(STACK_SIZE) == null) return false;
for (EquipmentSlot slot : EquipmentSlot.VALUES) {
if (!mob.getItemBySlot(slot).isEmpty()) return false;
}
return true;
}
private static long size(Mob mob) {
Long count = mob.getAttached(STACK_SIZE);
return count == null ? 1 : Math.max(1, Math.min(MAX_STACK, count));
}
private static void setSize(Mob mob, long count) {
if (count <= 1) {
mob.setAttached(STACK_SIZE, null);
mob.setCustomName(null);
mob.setCustomNameVisible(false);
return;
}
Component baseName = mob.getAttached(STACK_SIZE) == null ? mob.getName() : null;
mob.setAttached(STACK_SIZE, count);
if (baseName == null) {
mob.setCustomName(null);
baseName = mob.getName();
}
mob.setCustomName(baseName.copy().append(Component.literal(" ×" + count)));
mob.setCustomNameVisible(true);
}
public static void replaceAfterDeath(Mob mob) {
long count = size(mob);
if (count <= 1 || !(mob.level() instanceof ServerLevel level)) return;
Entity replacement = mob.getType().create(level, EntitySpawnReason.MOB_SUMMONED);
if (!(replacement instanceof Mob next)) return;
next.setPos(mob.getX(), mob.getY(), mob.getZ());
setSize(next, count - 1);
if (level.addFreshEntity(next)) {
mob.setAttached(STACK_SIZE, null);
}
}
}