2016-07-25 49 views
2

我已經使用Minecraft Forge創建了一個新的Tile實體。
這是一個簡單的胸部,有81個插槽(9行,9列)。
基本上,我需要創建一個x和y座標。
i的每個間隔,將SLOT_X_SPACING添加到x座標,但在9個間隔後重置爲0。
i的每9個間隔,我需要添加18到SLOT_Y_SPACING
如何將這些For循環重新排列爲一個循環?
(我覺得,因爲就在for循環不會是足夠的信息我的整個容器類是必要的)
如何系統地將插槽添加到Minecraft GUI容器

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); 
    } 
} 

這裏是清單的圖片,讓你可以想像怎麼回事: DrawerGUI.png

回答

2

對於水平位置,則可以使用模運算符%)9得到劃分的剩餘部分,這將做你想做的。 i % 9將始終返回從0到8的值。對於那個您將使用TILE_INVENTORY_XPOS + SLOT_X_SPACING * (i % 9)

對於垂直位置,您可以簡單地使用除法得到行 - i/9將是當前行。所以爲了獲得這個位置,你需要使用8 + 18 * (i/9)

把兩者的那些一起並在循環而不是使用i產生slotNumber此:

final int TILE_INVENTORY_XPOS = 12; 
final int TILE_INVENTORY_YPOS = 8; 

for (int slotNumber = 0; slotNumber < TE_INVENTORY_SLOT_COUNT; slotNumber++) { 
    addSlotToContainer(new Slot(tileEntityInventoryBasic, slotNumber, 
       TILE_INVENTORY_XPOS + SLOT_X_SPACING * (i % 9), 
       TILE_INVENTORY_YPOS + SLOT_Y_SPACING * (i/9))); 
}