Files
src
build.gradle
gradle.properties
manifest.kod
settings.gradle
Code
14 817
src/client/java/com/badhunter005/client/InventoryEditorClient.java
package com.badhunter005.client;
import io.wispforest.owo.ui.base.BaseOwoScreen;
import io.wispforest.owo.ui.component.ButtonComponent;
import io.wispforest.owo.ui.component.ItemComponent;
import io.wispforest.owo.ui.component.LabelComponent;
import io.wispforest.owo.ui.component.UIComponents;
import io.wispforest.owo.ui.container.FlowLayout;
import io.wispforest.owo.ui.container.ScrollContainer;
import io.wispforest.owo.ui.container.UIContainers;
import io.wispforest.owo.ui.core.Color;
import io.wispforest.owo.ui.core.HorizontalAlignment;
import io.wispforest.owo.ui.core.Insets;
import io.wispforest.owo.ui.core.OwoUIAdapter;
import io.wispforest.owo.ui.core.Sizing;
import io.wispforest.owo.ui.core.Surface;
import io.wispforest.owo.ui.core.VerticalAlignment;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
import net.minecraft.client.Minecraft;
import net.minecraft.network.chat.Component;
import net.minecraft.world.item.ItemStack;
import org.jetbrains.annotations.NotNull;
import com.badhunter005.InventoryEditPayload;
import com.badhunter005.InventoryEditorPayload;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
public final class InventoryEditorClient implements ClientModInitializer {
@Override
public void onInitializeClient() {
ClientAdminCommands.register();
ClientPlayNetworking.registerGlobalReceiver(InventoryEditorPayload.TYPE, (payload, context) -> {
Minecraft client = context.client();
if (client.gui.screen() instanceof InventoryEditorScreen screen && screen.targetId.equals(payload.targetId())) {
screen.update(payload);
} else {
client.gui.setScreen(new InventoryEditorScreen(payload));
}
});
}
private static final class InventoryEditorScreen extends BaseOwoScreen<FlowLayout> {
private final UUID targetId;
private String targetName;
private List<ItemStack> contents;
private final List<SlotRow> rows = new ArrayList<>();
private FlowLayout slotList;
private LabelComponent managedPlayer;
private InventoryEditorScreen(InventoryEditorPayload payload) {
super(Component.literal("BADHUNTER005 Inventory Editor"));
this.targetId = payload.targetId();
this.targetName = payload.targetName();
this.contents = payload.items();
}
@Override
protected @NotNull OwoUIAdapter<FlowLayout> createAdapter() {
return OwoUIAdapter.create(this, UIContainers::verticalFlow);
}
@Override
protected void build(FlowLayout root) {
root.surface(Surface.VANILLA_TRANSLUCENT);
root.alignment(HorizontalAlignment.CENTER, VerticalAlignment.CENTER);
int panelWidth = Math.max(280, Math.min(560, this.width - 16));
int panelHeight = Math.max(150, this.height - 16);
int leftWidth = 142;
int rightWidth = panelWidth - 30 - leftWidth;
FlowLayout panel = UIContainers.horizontalFlow(Sizing.fixed(panelWidth), Sizing.fixed(panelHeight));
panel.padding(Insets.of(10));
panel.gap(8);
panel.surface(Surface.DARK_PANEL);
panel.child(this.adminPanel(leftWidth));
panel.child(this.inventoryPanel(rightWidth));
root.child(panel);
}
private FlowLayout adminPanel(int width) {
FlowLayout admin = UIContainers.verticalFlow(Sizing.fixed(width), Sizing.fill(100));
admin.padding(Insets.of(8));
admin.gap(6);
admin.surface(Surface.PANEL_INSET);
LabelComponent title = UIComponents.label(Component.literal("ADMIN PANEL"));
title.color(Color.ofRgb(0xFF6B35));
title.shadow(true);
admin.child(title);
String adminName = Minecraft.getInstance().player == null
? "Console"
: Minecraft.getInstance().player.getName().getString();
LabelComponent commandUser = UIComponents.label(Component.literal("Command user: " + adminName));
commandUser.maxWidth(width - 16);
admin.child(commandUser);
this.managedPlayer = UIComponents.label(Component.literal("Managing: " + this.targetName));
this.managedPlayer.maxWidth(width - 16);
this.managedPlayer.color(Color.ofRgb(0x75D6A3));
admin.child(this.managedPlayer);
admin.child(UIComponents.label(Component.literal("Inventory tools")));
admin.child(UIComponents.label(Component.literal("Hold an item, then add it to the target inventory."))
.maxWidth(width - 16));
admin.child(UIComponents.button(Component.literal("Give held item"), button -> this.sendEdit(-1, InventoryEditPayload.GIVE_HELD_ITEM))
.horizontalSizing(Sizing.fill(100)));
admin.child(UIComponents.button(Component.literal("Refresh inventory"), button -> this.sendEdit(-1, InventoryEditPayload.REFRESH))
.horizontalSizing(Sizing.fill(100)));
admin.child(UIComponents.button(Component.literal("Close"), button -> this.onClose())
.horizontalSizing(Sizing.fill(100)));
return admin;
}
private FlowLayout inventoryPanel(int width) {
FlowLayout inventory = UIContainers.verticalFlow(Sizing.fixed(width), Sizing.fill(100));
inventory.gap(6);
LabelComponent title = UIComponents.label(Component.literal("PLAYER INVENTORY"));
title.color(Color.ofRgb(0xFFB347));
inventory.child(title);
this.slotList = UIContainers.verticalFlow(Sizing.fill(100), Sizing.content());
this.slotList.gap(2);
this.slotList.margins(Insets.right(5));
ScrollContainer<FlowLayout> scroll = UIContainers.verticalScroll(Sizing.fill(100), Sizing.expand(), this.slotList);
scroll.scrollbar(ScrollContainer.Scrollbar.vanillaFlat());
scroll.scrollbarThiccness(3);
inventory.child(scroll);
this.rebuildRows();
return inventory;
}
private void rebuildRows() {
this.slotList.clearChildren();
this.rows.clear();
for (int slot = 0; slot < this.contents.size(); slot++) {
ItemComponent icon = UIComponents.item(ItemStack.EMPTY);
icon.setTooltipFromStack(true);
icon.sizing(Sizing.fixed(18), Sizing.fixed(18));
LabelComponent details = UIComponents.label(Component.literal(""));
details.horizontalSizing(Sizing.expand());
details.maxWidth(190);
int slotIndex = slot;
ButtonComponent remove = UIComponents.button(Component.literal("Remove"), button -> this.sendEdit(slotIndex, InventoryEditPayload.REMOVE_SLOT));
remove.sizing(Sizing.fixed(48), Sizing.fixed(18));
FlowLayout row = UIContainers.horizontalFlow(Sizing.fill(100), Sizing.fixed(20));
row.gap(4);
row.verticalAlignment(VerticalAlignment.CENTER);
row.padding(Insets.horizontal(3));
row.surface(Surface.PANEL_INSET);
row.child(icon);
row.child(details);
row.child(remove);
this.slotList.child(row);
this.rows.add(new SlotRow(icon, details, remove));
}
this.updateRows();
}
private void update(InventoryEditorPayload payload) {
this.targetName = payload.targetName();
this.contents = payload.items();
if (this.managedPlayer != null && !this.managedPlayer.text().getString().equals("Managing: " + this.targetName)) {
this.managedPlayer.text(Component.literal("Managing: " + this.targetName));
}
if (this.rows.size() != this.contents.size()) {
this.rebuildRows();
} else {
this.updateRows();
}
}
private void updateRows() {
for (int slot = 0; slot < this.rows.size(); slot++) {
ItemStack stack = this.contents.get(slot);
SlotRow row = this.rows.get(slot);
row.icon.stack(stack.copy());
String description = stack.isEmpty()
? "Slot " + slot + " — empty"
: "Slot " + slot + " — " + stack.getItem().toString() + " × " + stack.getCount();
if (!row.details.text().getString().equals(description)) {
row.details.text(Component.literal(description));
}
row.remove.active(!stack.isEmpty());
}
}
private void sendEdit(int slot, int action) {
ClientPlayNetworking.send(new InventoryEditPayload(this.targetId, slot, action));
}
}