Ich habe mit Minecraft Forge eine neue Tile Entity erstellt.
Es ist eine einfache Truhe mit 81 Steckplätzen (9 Reihen, 9 Spalten).
Grundsätzlich muss ich eine X- und eine Y-Koordinate erstellen.
Jedes Intervall von i, fügen Sie SLOT_X_SPACING
der X-Koordinate hinzu, aber nach 9 Intervallen auf 0 zurückgesetzt.
Alle 9 Intervalle von I, ich muss hinzufügen 18 bis SLOT_Y_SPACING
.
Wie kann ich diese For
Schleifen in nur einer Schleife neu anordnen?
(Ich fühlte, dass meine ganze Container-Klasse notwendig war, weil nur die for-Schleife nicht genug Informationen wären)
Wie man Slots systematisch einem Minecraft GUI Container hinzufügt
public class ModDrawerContainer extends Container {
private ModTileEntityDrawer tileEntityInventoryBasic;
private final int HOTBAR_SLOT_COUNT = 9;
private final int PLAYER_INVENTORY_ROW_COUNT = 3;
private final int PLAYER_INVENTORY_COLUMN_COUNT = 9;
private final int PLAYER_INVENTORY_SLOT_COUNT = PLAYER_INVENTORY_COLUMN_COUNT * PLAYER_INVENTORY_ROW_COUNT;
private final int VANILLA_SLOT_COUNT = HOTBAR_SLOT_COUNT + PLAYER_INVENTORY_SLOT_COUNT;
private final int VANILLA_FIRST_SLOT_INDEX = 0;
private final int TE_INVENTORY_FIRST_SLOT_INDEX = VANILLA_FIRST_SLOT_INDEX + VANILLA_SLOT_COUNT;
private final int TE_INVENTORY_SLOT_COUNT = 81;
public ModDrawerContainer(InventoryPlayer invPlayer, ModTileEntityDrawer tileEntityInventoryBasic) {
this.tileEntityInventoryBasic = tileEntityInventoryBasic;
final int SLOT_X_SPACING = 18;
final int SLOT_Y_SPACING = 18;
final int HOTBAR_XPOS = 12;
final int HOTBAR_YPOS = 232;
for (int x = 0; x < HOTBAR_SLOT_COUNT; x++) {
int slotNumber = x;
addSlotToContainer(new Slot(invPlayer, slotNumber, HOTBAR_XPOS + SLOT_X_SPACING * x, HOTBAR_YPOS));
}
final int PLAYER_INVENTORY_XPOS = 12;
final int PLAYER_INVENTORY_YPOS = 174;
for (int y = 0; y < PLAYER_INVENTORY_ROW_COUNT; y++) {
for (int x = 0; x < PLAYER_INVENTORY_COLUMN_COUNT; x++) {
int slotNumber = HOTBAR_SLOT_COUNT + y * PLAYER_INVENTORY_COLUMN_COUNT + x;
int xpos = PLAYER_INVENTORY_XPOS + x * SLOT_X_SPACING;
int ypos = PLAYER_INVENTORY_YPOS + y * SLOT_Y_SPACING;
addSlotToContainer(new Slot(invPlayer, slotNumber, xpos, ypos));
}
}
if (TE_INVENTORY_SLOT_COUNT != tileEntityInventoryBasic.getSizeInventory()) {
System.err.println("Mismatched slot count in ContainerBasic(" + TE_INVENTORY_SLOT_COUNT
+ ") and TileInventory (" + tileEntityInventoryBasic.getSizeInventory()+")");
}
final int TILE_INVENTORY_XPOS = 12;
int TILE_INVENTORY_YPOS = 8;
for (int i = 0; i < 9; i++) {
int slotNumber = i;
addSlotToContainer(new Slot(tileEntityInventoryBasic, slotNumber, TILE_INVENTORY_XPOS + SLOT_X_SPACING * i, TILE_INVENTORY_YPOS));
}
TILE_INVENTORY_YPOS = 8 + 18 *1;
for (int i = 9; i < 18; i++) {
int slotNumber = i;
addSlotToContainer(new Slot(tileEntityInventoryBasic, slotNumber, TILE_INVENTORY_XPOS + SLOT_X_SPACING * (i-9), TILE_INVENTORY_YPOS));
}
TILE_INVENTORY_YPOS = 8 + 18 *2;
for (int i = 18; i < 27; i++) {
int slotNumber = i;
addSlotToContainer(new Slot(tileEntityInventoryBasic, slotNumber, TILE_INVENTORY_XPOS + SLOT_X_SPACING * (i-18), TILE_INVENTORY_YPOS));
}
TILE_INVENTORY_YPOS = 8 + 18 *3;
for (int i = 27; i < 36; i++) {
int slotNumber = i;
addSlotToContainer(new Slot(tileEntityInventoryBasic, slotNumber, TILE_INVENTORY_XPOS + SLOT_X_SPACING * (i-27), TILE_INVENTORY_YPOS));
}
TILE_INVENTORY_YPOS = 8 + 18 *4;
for (int i = 36; i < 45; i++) {
int slotNumber = i;
addSlotToContainer(new Slot(tileEntityInventoryBasic, slotNumber, TILE_INVENTORY_XPOS + SLOT_X_SPACING * (i-36), TILE_INVENTORY_YPOS));
}
TILE_INVENTORY_YPOS = 8 + 18 *5;
for (int i = 45; i < 54; i++) {
int slotNumber = i;
addSlotToContainer(new Slot(tileEntityInventoryBasic, slotNumber, TILE_INVENTORY_XPOS + SLOT_X_SPACING * (i-45), TILE_INVENTORY_YPOS));
}
TILE_INVENTORY_YPOS = 8 + 18 *6;
for (int i = 54; i < 63; i++) {
int slotNumber = i;
addSlotToContainer(new Slot(tileEntityInventoryBasic, slotNumber, TILE_INVENTORY_XPOS + SLOT_X_SPACING * (i-54), TILE_INVENTORY_YPOS));
}
TILE_INVENTORY_YPOS = 8 + 18 *7;
for (int i =63; i < 72; i++) {
int slotNumber = i;
addSlotToContainer(new Slot(tileEntityInventoryBasic, slotNumber, TILE_INVENTORY_XPOS + SLOT_X_SPACING * (i-63), TILE_INVENTORY_YPOS));
}
TILE_INVENTORY_YPOS = 8 + 18 *8;
for (int i = 73; i < 81; i++) {
int slotNumber = i;
addSlotToContainer(new Slot(tileEntityInventoryBasic, slotNumber, TILE_INVENTORY_XPOS + SLOT_X_SPACING * (i-72), TILE_INVENTORY_YPOS));
}
}
@Override
public boolean canInteractWith(EntityPlayer player)
{
return tileEntityInventoryBasic.isUseableByPlayer(player);
}
@Override
public ItemStack transferStackInSlot(EntityPlayer player, int sourceSlotIndex)
{
Slot sourceSlot = (Slot)inventorySlots.get(sourceSlotIndex);
if (sourceSlot == null || !sourceSlot.getHasStack()) return null;
ItemStack sourceStack = sourceSlot.getStack();
ItemStack copyOfSourceStack = sourceStack.copy();
if (sourceSlotIndex >= VANILLA_FIRST_SLOT_INDEX && sourceSlotIndex < VANILLA_FIRST_SLOT_INDEX + VANILLA_SLOT_COUNT) {
if (!mergeItemStack(sourceStack, TE_INVENTORY_FIRST_SLOT_INDEX, TE_INVENTORY_FIRST_SLOT_INDEX + TE_INVENTORY_SLOT_COUNT, false)){
return null;
}
} else if (sourceSlotIndex >= TE_INVENTORY_FIRST_SLOT_INDEX && sourceSlotIndex < TE_INVENTORY_FIRST_SLOT_INDEX + TE_INVENTORY_SLOT_COUNT) {
if (!mergeItemStack(sourceStack, VANILLA_FIRST_SLOT_INDEX, VANILLA_FIRST_SLOT_INDEX + VANILLA_SLOT_COUNT, false)) {
return null;
}
} else {
System.err.print("Invalid slotIndex:" + sourceSlotIndex);
return null;
}
if (sourceStack.stackSize == 0) {
sourceSlot.putStack(null);
} else {
sourceSlot.onSlotChanged();
}
sourceSlot.onPickupFromSlot(player, sourceStack);
return copyOfSourceStack;
}
@Override
public void onContainerClosed(EntityPlayer playerIn)
{
super.onContainerClosed(playerIn);
this.tileEntityInventoryBasic.closeInventory(playerIn);
}
}
Hier ein Bild des Inventars so ist, dass Sie visualisieren können, was los ist: