Files
src
build.gradle
gradle.properties
settings.gradle
Code
5 171
src/client/java/pl/kodari/opblockcatalog/BlockCatalogClient.java
package pl.kodari.opblockcatalog;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.screen.v1.ScreenEvents;
import net.fabricmc.fabric.api.client.screen.v1.Screens;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.components.Button;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.client.gui.screens.inventory.InventoryScreen;
import net.minecraft.network.chat.Component;
public class BlockCatalogClient implements ClientModInitializer {
@Override
public void onInitializeClient() {
ScreenEvents.AFTER_INIT.register((client, screen, scaledWidth, scaledHeight) -> {
if (!(screen instanceof InventoryScreen)) {
return;
}
int x = Math.max(2, Math.min(scaledWidth / 2 + 90, scaledWidth - 30));
int y = scaledHeight / 2 - 10;
Button catalogButton = Button.builder(Component.literal("♦"), button ->
client.gui.setScreen(new BlockCatalogScreen(client, screen, 0)))
.bounds(x, y, 26, 20)
.build();
Screens.getWidgets(screen).add(catalogButton);
});
}
private static final class BlockCatalogScreen extends Screen {
private static final java.util.List<net.minecraft.world.item.BlockItem> BLOCKS =
net.minecraft.core.registries.BuiltInRegistries.ITEM.stream()
.filter(net.minecraft.world.item.BlockItem.class::isInstance)
.map(net.minecraft.world.item.BlockItem.class::cast)
.toList();
private static final int COLUMNS = 3;
private static final int ROWS = 6;
private static final int BUTTON_WIDTH = 98;
private static final int BUTTON_HEIGHT = 20;
private static final int GAP = 4;
private final Minecraft client;
private final Screen parent;
private final int page;
private BlockCatalogScreen(Minecraft client, Screen parent, int page) {
super(Component.literal("Katalog bloków"));
this.client = client;
this.parent = parent;
this.page = page;
}
@Override
protected void init() {
int pageSize = COLUMNS * ROWS;
int pageCount = Math.max(1, (BLOCKS.size() + pageSize - 1) / pageSize);
int firstItem = page * pageSize;
int gridWidth = COLUMNS * BUTTON_WIDTH + (COLUMNS - 1) * GAP;
int gridHeight = ROWS * BUTTON_HEIGHT + (ROWS - 1) * GAP;
int left = Math.max(4, (this.width - gridWidth) / 2);
int top = Math.max(52, (this.height - gridHeight - 46) / 2);
for (int slot = 0; slot < pageSize && firstItem + slot < BLOCKS.size(); slot++) {
net.minecraft.world.item.BlockItem blockItem = BLOCKS.get(firstItem + slot);
net.minecraft.resources.Identifier id =
net.minecraft.core.registries.BuiltInRegistries.ITEM.getKey(blockItem);
int column = slot % COLUMNS;
int row = slot / COLUMNS;
String idString = id.toString();
int separator = idString.indexOf(':');
String label = separator >= 0 ? idString.substring(separator + 1) : idString;
this.addRenderableWidget(Button.builder(Component.literal(label), button -> {
if (this.client.player != null) {
this.client.player.connection.sendCommand("give @s " + idString + " 1");
}
}).bounds(left + column * (BUTTON_WIDTH + GAP), top + row * (BUTTON_HEIGHT + GAP),
BUTTON_WIDTH, BUTTON_HEIGHT).build());
}
int footerWidth = 72 + GAP + 90 + GAP + 72;
int footerLeft = (this.width - footerWidth) / 2;
int footerY = top + gridHeight + 8;
this.addRenderableWidget(Button.builder(Component.literal("<"), button -> {
if (this.page > 0) {
this.client.gui.setScreen(new BlockCatalogScreen(this.client, this.parent, this.page - 1));
}
}).bounds(footerLeft, footerY, 72, BUTTON_HEIGHT).build());
this.addRenderableWidget(Button.builder(Component.literal((this.page + 1) + "/" + pageCount), button -> {
}).bounds(footerLeft + 76, footerY, 90, BUTTON_HEIGHT).build());
this.addRenderableWidget(Button.builder(Component.literal(">"), button -> {
if (this.page + 1 < pageCount) {
this.client.gui.setScreen(new BlockCatalogScreen(this.client, this.parent, this.page + 1));
}
}).bounds(footerLeft + 170, footerY, 72, BUTTON_HEIGHT).build());
}
@Override
public void onClose() {
this.client.gui.setScreen(this.parent);
}
}
}