goldodemon/Minecraft-JavaSet / ExampleScreenHandler.java
goldodemon's picture
download
raw
3.01 kB
package com.example.examplemod;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.inventory.Inventory;
import net.minecraft.inventory.SimpleInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.registry.Registries;
import net.minecraft.registry.Registry;
import net.minecraft.screen.ScreenHandler;
import net.minecraft.screen.ScreenHandlerType;
import net.minecraft.screen.slot.Slot;
import net.minecraft.util.Identifier;
import net.minecraft.world.World;
/**
* A minimal 3-slot ScreenHandler (server-side GUI logic) you'd open from a
* block entity. Pair with a matching HandledScreen on the client for visuals.
* ScreenHandler API is unchanged across 1.21.7 - 1.21.11.
*/
public class ExampleScreenHandler extends ScreenHandler {
public static final ScreenHandlerType<ExampleScreenHandler> TYPE = Registry.register(
Registries.SCREEN_HANDLER,
Identifier.of(ExampleMod.MOD_ID, "example_screen_handler"),
new ScreenHandlerType<>(ExampleScreenHandler::new, net.minecraft.feature.FeatureFlags.VANILLA_FEATURES)
);
private final Inventory inventory;
// Client-side constructor (called when server tells client to open screen)
public ExampleScreenHandler(int syncId, PlayerInventory playerInventory) {
this(syncId, playerInventory, new SimpleInventory(3));
}
// Server-side constructor
public ExampleScreenHandler(int syncId, PlayerInventory playerInventory, Inventory inventory) {
super(TYPE, syncId);
this.inventory = inventory;
checkSize(inventory, 3);
inventory.onOpen(playerInventory.player);
int slotIndex = 0;
// Our 3 custom slots (e.g. input/fuel/output style layout)
for (int col = 0; col < 3; col++) {
this.addSlot(new Slot(inventory, slotIndex++, 44 + col * 18, 20));
}
// Standard player inventory + hotbar slots below
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 9; col++) {
this.addSlot(new Slot(playerInventory, col + row * 9 + 9, 8 + col * 18, 51 + row * 18));
}
}
for (int col = 0; col < 9; col++) {
this.addSlot(new Slot(playerInventory, col, 8 + col * 18, 109));
}
}
@Override
public boolean canUse(World world, PlayerEntity player) {
return inventory.canPlayerUse(player);
}
@Override
public ItemStack quickMove(PlayerEntity player, int slotIndex) {
// Simplified shift-click handling; real mods implement full slot transfer logic here.
Slot slot = this.slots.get(slotIndex);
if (!slot.hasStack()) return ItemStack.EMPTY;
ItemStack stack = slot.getStack().copy();
slot.setStack(ItemStack.EMPTY);
return stack;
}
@Override
public void onClosed(PlayerEntity player) {
super.onClosed(player);
inventory.onClose(player);
}
}

Xet Storage Details

Size:
3.01 kB
·
Xet hash:
e8e5b0201748e14708d957aceee01bd5b5969eb9ee71145ffec4df3942473611

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.