Skip to content

Instantly share code, notes, and snippets.

@zaralX
Created April 29, 2025 01:46
Show Gist options
  • Save zaralX/2834cc8206cb81afdc98ab3318758f73 to your computer and use it in GitHub Desktop.
Save zaralX/2834cc8206cb81afdc98ab3318758f73 to your computer and use it in GitHub Desktop.
dump all minecraft items Fabric 1.21.5
package ru.zaralx.mineAssets.client;
import com.mojang.blaze3d.systems.RenderSystem;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.rendering.v1.HudRenderCallback;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gl.Framebuffer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import org.lwjgl.opengl.GL11;
import ru.zaralx.mineAssets.client.utils.Color;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;
public class MineAssetsClient implements ClientModInitializer {
private static final MinecraftClient client = MinecraftClient.getInstance();
private static List<Item> allItems = new ArrayList<>();
private static int ticks = 0;
private static final int cooldown = 20;
private static final int wait = 100;
private static boolean isCapturing = false;
private static int captureDelay = 0;
private static String currentItemName = null;
@Override
public void onInitializeClient() {
HudRenderCallback.EVENT.register((drawContext, tickDelta) -> {
if (client.player == null || client.world == null) return;
if (allItems.isEmpty()) {
allItems = getAllItems();
System.out.println("Items: " + allItems.size());
File screenshotsDir = new File("screenshots");
int fileCount = screenshotsDir.list() != null ? Objects.requireNonNull(screenshotsDir.list()).length : 0;
System.out.println("Screenshots has: " + fileCount + " files");
}
if (!isCapturing) {
ticks++;
}
if (ticks < wait) {
return;
}
int currentIndex = (ticks - wait) / cooldown;
if (currentIndex >= allItems.size()) {
return;
}
Item currentItem = allItems.get(currentIndex);
// Draw the item with increased size
drawContext.getMatrices().push();
drawContext.getMatrices().scale(8.0f, 8.0f, 8.0f);
drawContext.drawItem(new ItemStack(currentItem), 0, 0);
drawContext.getMatrices().pop();
// Handle capture timing
if (!isCapturing && (ticks - wait) % cooldown == 0) {
currentItemName = currentItem.getRegistryEntry().getKey().get().getValue().getPath();
isCapturing = true;
captureDelay = 0;
}
if (isCapturing) {
captureDelay++;
if (captureDelay >= 5) { // Wait for 5 ticks to ensure proper rendering
capture(currentItemName);
isCapturing = false;
currentItemName = null;
}
}
});
}
public static List<Item> getAllItems() {
List<Item> items = new ArrayList<>();
try {
Class<?> itemsClass = Class.forName("net.minecraft.item.Items");
Field[] fields = itemsClass.getFields();
for (Field field : fields) {
if (Item.class.isAssignableFrom(field.getType())) {
items.add((Item) field.get(null));
}
}
} catch (Exception e) {
e.printStackTrace();
}
return items;
}
public static void capture(String name) {
try {
File screenshotsDir = new File("screenshots");
if (!screenshotsDir.exists()) {
screenshotsDir.mkdir();
}
BufferedImage image = new BufferedImage(256, 256, BufferedImage.TYPE_INT_ARGB);
ByteBuffer buffer = ByteBuffer.allocateDirect(256 * 256 * 4);
int x = 0;
int y = client.getWindow().getScaledHeight() - 16;
GL11.glReadPixels(x, y, 256, 256, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, buffer);
int[] pixels = new int[256 * 256];
buffer.rewind();
for (int i = 0; i < pixels.length; i++) {
int r = buffer.get() & 0xFF;
int g = buffer.get() & 0xFF;
int b = buffer.get() & 0xFF;
int a = buffer.get() & 0xFF;
pixels[i] = (a << 24) | (r << 16) | (g << 8) | b;
}
for (int yy = 0; yy < 256; yy++) {
for (int xx = 0; xx < 256; xx++) {
image.setRGB(xx, 255 - yy, pixels[yy * 256 + xx]);
}
}
File outputFile = new File(screenshotsDir, name + ".png");
ImageIO.write(image, "PNG", outputFile);
System.out.println("Screenshot saved as: " + outputFile.getAbsolutePath());
} catch (IOException e) {
e.printStackTrace();
}
}
}
@zaralX
Copy link
Author

zaralX commented Apr 29, 2025

  • dump item categories
package ru.zaralx.mineAssets.client;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.mojang.blaze3d.systems.RenderSystem;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.rendering.v1.HudRenderCallback;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gl.Framebuffer;
import net.minecraft.item.*;
import net.minecraft.registry.Registries;
import net.minecraft.registry.RegistryKey;
import net.minecraft.util.Identifier;
import org.lwjgl.opengl.GL11;
import ru.zaralx.mineAssets.client.utils.Color;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.Field;
import java.nio.ByteBuffer;
import java.util.*;
import java.util.concurrent.CompletableFuture;

public class MineAssetsClient implements ClientModInitializer {

    private static final MinecraftClient client = MinecraftClient.getInstance();
    private static List<Item> allItems = new ArrayList<>();
    private static int ticks = 0;
    private static final int cooldown = 20;
    private static final int wait = 100;
    private static boolean isCapturing = false;
    private static int captureDelay = 0;
    private static String currentItemName = null;
    private static boolean dumpingCategories = true; // true to disable
    private static Map<String, Integer> itemCountMap = new HashMap<>();

    @Override
    public void onInitializeClient() {
        HudRenderCallback.EVENT.register((drawContext, tickDelta) -> {
            if (client.player == null || client.world == null) return;

            if (allItems.isEmpty()) {
                allItems = getAllItems();
                System.out.println("Items: " + allItems.size());
                File screenshotsDir = new File("screenshots");
                int fileCount = screenshotsDir.list() != null ? Objects.requireNonNull(screenshotsDir.list()).length : 0;
                System.out.println("Screenshots has: " + fileCount + " files");
            }

            if (!isCapturing) {
                ticks++;
            }

            if (ticks < wait) {
                return;
            }

            if (!dumpingCategories) {
                System.out.println("Dumping categories");
                dumpingCategories = true;
                Map<String, List<String>> categoryItems = new HashMap<>();
                itemCountMap.clear(); // Clear the map before reusing it

                // Initialize all categories
                for (ItemGroup itemGroup : Registries.ITEM_GROUP) {
                    RegistryKey<ItemGroup> key = Registries.ITEM_GROUP.getKey(itemGroup).orElse(null);
                    if (key != null) {
                        String categoryId = key.getValue().getPath();
                        if (categoryId.equals("search") || categoryId.equals("hotbar") || categoryId.equals("inventory")) {
                            continue;
                        }
                        categoryItems.put(categoryId, new ArrayList<>());

                        Collection<ItemStack> items = itemGroup.getDisplayStacks();
                        for (ItemStack stack : items) {
                            Item item = stack.getItem();
                            String baseName = item.getRegistryEntry().getKey().get().getValue().getPath();

                            // Get or increment the count for this item name
                            int count = itemCountMap.getOrDefault(baseName, 0) + 1;
                            itemCountMap.put(baseName, count);

                            if (count > 1) {
                                continue;
//                                if (baseName.equals("enchanted_book")
//                                        || baseName.equals("painting")
//                                        || baseName.equals("goat_horn")
//                                        || baseName.equals("suspicious_stew")
//                                        || baseName.equals("ominous_bottle")
//                                        || baseName.equals("firework_rocket")
//                                ) {
//                                    continue;
//                                }
                            }

                            System.out.println(baseName + ": " + count);

                            // Add the item with unique ID
                            String uniqueName = count > 1 ? baseName + count : baseName;
                            categoryItems.get(categoryId).add(uniqueName);
                        }
                        itemCountMap.clear();
                    }
                }

                // Write to JSON file
                try (FileWriter writer = new FileWriter("item_categories.json")) {
                    Gson gson = new GsonBuilder().setPrettyPrinting().create();
                    gson.toJson(categoryItems, writer);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                itemCountMap.clear();
            }

            int currentIndex = (ticks - wait) / cooldown;
            if (currentIndex >= allItems.size()) {
                return;
            }

            Item currentItem = allItems.get(currentIndex);

            // Draw the item with increased size
            drawContext.getMatrices().push();
            drawContext.getMatrices().scale(8.0f, 8.0f, 8.0f);
            drawContext.drawItem(new ItemStack(currentItem), 0, 0);
            drawContext.getMatrices().pop();

            // Handle capture timing
            if (!isCapturing && (ticks - wait) % cooldown == 0) {
                String baseName = currentItem.getRegistryEntry().getKey().get().getValue().getPath();
                int count = itemCountMap.getOrDefault(baseName, 0) + 1;
                itemCountMap.put(baseName, count);
                currentItemName = count > 1 ? baseName + count : baseName;
                if (count > 1) {
                    if (baseName.equals("enchanted_book")
                            || baseName.equals("painting")
                            || baseName.equals("goat_horn")
                            || baseName.equals("suspicious_stew")
                            || baseName.equals("ominous_bottle")
                            || baseName.equals("firework_rocket")
                            || baseName.equals("white_banner")
                    ) {
                        currentItemName = baseName;
                    }
                }
                isCapturing = true;
                captureDelay = 0;
            }

            if (isCapturing) {
                captureDelay++;
                if (captureDelay >= 5) { // Wait for 5 ticks to ensure proper rendering
                    capture(currentItemName);
                    isCapturing = false;
                    currentItemName = null;
                }
            }
        });
    }

    public static List<Item> getAllItems() {
        List<Item> items = new ArrayList<>();
        try {
            Class<?> itemsClass = Class.forName("net.minecraft.item.Items");
            Field[] fields = itemsClass.getFields();
            for (Field field : fields) {
                if (Item.class.isAssignableFrom(field.getType())) {
                    items.add((Item) field.get(null));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return items;
    }

    public static void capture(String name) {
        try {
            File screenshotsDir = new File("screenshots");
            if (!screenshotsDir.exists()) {
                screenshotsDir.mkdir();
            }

            BufferedImage image = new BufferedImage(256, 256, BufferedImage.TYPE_INT_ARGB);
            ByteBuffer buffer = ByteBuffer.allocateDirect(256 * 256 * 4);

            int x = 0;
            int y = client.getWindow().getScaledHeight() - 16;

            GL11.glReadPixels(x, y, 256, 256, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, buffer);

            int[] pixels = new int[256 * 256];
            buffer.rewind();
            for (int i = 0; i < pixels.length; i++) {
                int r = buffer.get() & 0xFF;
                int g = buffer.get() & 0xFF;
                int b = buffer.get() & 0xFF;
                int a = buffer.get() & 0xFF;
                pixels[i] = (a << 24) | (r << 16) | (g << 8) | b;
            }

            for (int yy = 0; yy < 256; yy++) {
                for (int xx = 0; xx < 256; xx++) {
                    image.setRGB(xx, 255 - yy, pixels[yy * 256 + xx]);
                }
            }

            File outputFile = new File(screenshotsDir, name + ".png");
            ImageIO.write(image, "PNG", outputFile);

            System.out.println("Screenshot saved as: " + outputFile.getAbsolutePath());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment