Scary M10

Make minecraft 1.21.11 plugin in which hopper collects everything in its chunk

Kodari AI
KodariSep 30 at 10:34 am 1
ChunkHopperCollector
A Minecraft 1.21.11 plugin that empowers hoppers to collect all items within their chunk. When a hopper is placed, it registers itself with the chunk it resides in. The plugin periodically scans each registered chunk for loose items on the ground and transports them into the hopper's inventory. Multiple hoppers in the same chunk share the collection load, distributing items evenly. Hoppers automatically unregister when removed or destroyed. Configuration options allow adjusting the collection radius, scan interval, and maximum items collected per scan. The plugin uses efficient chunk-based tracking to minimize performance impact, making it ideal for farms and automated sorting systems.

I’ll create a Paper 1.21.11 plugin that periodically transfers dropped items into hopper inventories in the same loaded chunk, plus the Gradle and plugin metadata.

Created the Paper 1.21.11 ChunkHopper plugin. Every 10 ticks, it scans loaded chunks and transfers dropped items into hopper inventories in the same chunk, trying multiple hoppers if one can only take part of a stack.

Kodari can make mistakes. Review important code before using.
Files
src
build.gradle
settings.gradle
Code
4 100
src/main/java/com/kodari/chunkhopper/ChunkHopperPlugin.java
package com.kodari.chunkhopper;

import org.bukkit.Bukkit;
import org.bukkit.Chunk;
import org.bukkit.World;
import org.bukkit.block.BlockState;
import org.bukkit.block.Hopper;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Item;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.java.JavaPlugin;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public final class ChunkHopperPlugin extends JavaPlugin {
    private static final long SCAN_PERIOD_TICKS = 10L;

    @Override
    public void onEnable() {
        Bukkit.getScheduler().runTaskTimer(this, this::collectLoadedChunks, SCAN_PERIOD_TICKS, SCAN_PERIOD_TICKS);
    }

    private void collectLoadedChunks() {
        for (World world : Bukkit.getWorlds()) {
            for (Chunk chunk : world.getLoadedChunks()) {
                collectChunkItems(chunk);
            }
        }
    }

    private void collectChunkItems(Chunk chunk) {
        List<Item> droppedItems = new ArrayList<>();
        List<Inventory> hopperInventories = new ArrayList<>();

        for (Entity entity : chunk.getEntities()) {
            if (entity instanceof Item item) {
                droppedItems.add(item);
            }
        }

        for (BlockState state : chunk.getTileEntities()) {
            if (state instanceof Hopper hopper) {
                hopperInventories.add(hopper.getInventory());
            }
        }

        if (droppedItems.isEmpty() || hopperInventories.isEmpty()) {
            return;
        }

        for (Item droppedItem : droppedItems) {
            ItemStack remaining = droppedItem.getItemStack();

            for (Inventory hopperInventory : hopperInventories) {
                Map<Integer, ItemStack> leftovers = hopperInventory.addItem(remaining.clone());
                if (leftovers.isEmpty()) {
                    droppedItem.remove();
                    break;
                }

                remaining = leftovers.values().iterator().next();
                droppedItem.setItemStack(remaining);
            }
        }
    }
}