สกลวรรธน์ กลางเภา

ทำมอด MobStacker ในมายคราฟ เวอชั่น26.2 สูงสุด10000000000000 fabric ให้หน่อย

Kodari AI
KodariNov 8 at 11:32 am 3
MobStackerUltra
A Fabric mod for Minecraft 26.2 that stacks identical mobs into single entities to reduce server lag. When multiple mobs of the same type occupy the same block, they merge into one entity displaying a count label. The stack count can reach up to 10,000,000,000,000 (10 trillion). Features include configurable stacking radius, blacklist/whitelist for mob types, automatic stacking on spawn, manual stacking via interaction, and a command to view or adjust stack sizes. The mod displays the stack count as a name tag or particle effect, and when damaged, the stack splits proportionally. Includes server-side synchronization for multiplayer, performance-optimized algorithms for high entity counts, and full compatibility with vanilla spawning mechanics. Supports both survival and creative modes with configurable drop behavior when stacked mobs die.
  • สร้างมอด Fabric 26.2 ที่รวมมอนสเตอร์ใกล้กัน สูงสุด 10,000,000,000,000 ตัวต่อกอง

  • เก็บจำนวนข้ามการบันทึกโลก และแยกมอนสเตอร์ออกมาทีละตัวเมื่อถูกกำจัด

  • จำกัดชนิดที่รวมได้เพื่อไม่ให้ข้อมูลเฉพาะตัวของมอนสเตอร์สูญหาย

สร้างมอด MobStacker สำหรับ Fabric 26.2 แล้ว รองรับสูงสุด 10,000,000,000,000 ตัวต่อกอง และบันทึกจำนวนไว้ข้ามการเข้าโลก

ขณะนี้รวมเฉพาะซอมบี้และแมงมุมที่ไม่มีชื่อหรืออุปกรณ์ โดยเมื่อฆ่าจะได้รับดรอปทีละตัวตามปกติ ยังไม่รองรับมอนสเตอร์ทุกชนิด เพื่อหลีกเลี่ยงการสูญเสียข้อมูลเฉพาะตัวของมอนสเตอร์

Kodari can make mistakes. Review important code before using.
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);
        }
    }
}