Advertisement
Exokem

Untitled

Apr 18th, 2019
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.86 KB | None | 0 0
  1. // Container
  2. public class ContainerAssembly extends Container {
  3.  
  4.     private InventoryCrafting craftMatrix = new InventoryCrafting(this, GRID_WIDTH, GRID_HEIGHT);
  5.     private InventoryCraftResult craftResult = new InventoryCraftResult();
  6.     public final World world;
  7.     private final int x;
  8.     private final int y;
  9.     private final int z;
  10.     private final BlockPos pos;
  11.     private final EntityPlayer player;
  12.  
  13.     private static final int GRID_WIDTH = 5;
  14.     private static final int GRID_HEIGHT = 3;
  15.  
  16.     private RecipeExkvaAssembly lastRecipe;
  17.  
  18.     public ContainerAssembly(InventoryPlayer inv, World world, BlockPos pos) {
  19.         this.player = inv.player;
  20.         this.inventorySlots.clear();
  21.         this.inventoryItemStacks.clear();
  22.         this.world = world;
  23.         this.x = pos.getX();
  24.         this.y = pos.getY();
  25.         this.z = pos.getZ();
  26.         this.pos = pos;
  27.  
  28.         this.addSlotToContainer(new SlotCrafting(player, craftMatrix, craftResult, 0, 137, 35));
  29.  
  30.         /* adds grid slots */
  31.         for(int h = 0; h < GRID_HEIGHT; h++) {
  32.             for(int w = 0; w < GRID_WIDTH; w++) {
  33.                 this.addSlotToContainer(new Slot(craftMatrix, w + h * GRID_WIDTH, 18 + w * 18, 17 + h * 18));
  34.             }
  35.         }
  36.  
  37.         /* adds player inventory slots */
  38.         for(int r = 0; r < 3; r++) {
  39.             for(int c = 0; c < 9; c++) {
  40.                 this.addSlotToContainer(new Slot(inv, c + r * 9 + 9, 8 + c * 18, 84 + r * 18));
  41.             }
  42.         }
  43.  
  44.         /* adds hotbar slots */
  45.         for(int b = 0; b < 9; b++) {
  46.             this.addSlotToContainer(new Slot(inv, b, 8 + b * 18, 142));
  47.         }
  48.     }
  49.  
  50.     @Override
  51.     public boolean canInteractWith(EntityPlayer playerIn) {
  52.         if (this.world.getBlockState(this.pos).getBlock() != ExkvaBlocks.assembly) {
  53.             return false;
  54.         } else {
  55.             return playerIn.getDistanceSq(x + 0.5D, y + 0.5D, z + 0.5D) <= 64.0D;
  56.         }
  57.     }
  58.  
  59.     @Override
  60.     public void onContainerClosed(EntityPlayer player) {
  61.         if (pos != BlockPos.ORIGIN) super.onContainerClosed(player);
  62.         else {
  63.             InventoryPlayer inv = player.inventory;
  64.             if (!inv.getItemStack().isEmpty()) {
  65.                 player.dropItem(inv.getItemStack(), false);
  66.                 inv.setItemStack(ItemStack.EMPTY);
  67.             }
  68.             if (!this.world.isRemote) this.clearContainer(player, this.world, this.craftMatrix);
  69.         }
  70.     }
  71.  
  72.     @Override
  73.     public ItemStack transferStackInSlot(EntityPlayer playerIn, int index) {
  74.  
  75.         // grid slots: [1,15]
  76.         // player inv slots: [16, 42]
  77.         // player hotbar slots: [43, 51]
  78.  
  79.         ItemStack itemstack = ItemStack.EMPTY;
  80.         Slot slot = this.inventorySlots.get(index);
  81.         if (slot != null && slot.getHasStack()) {
  82.             ItemStack itemstack1 = slot.getStack();
  83.             itemstack = itemstack1.copy();
  84.             if (index == 0) {
  85.                 itemstack1.getItem().onCreated(itemstack1, this.world, playerIn);
  86.                 if (!this.mergeItemStack(itemstack1, 16, 52, true)) {
  87.                     return ItemStack.EMPTY;
  88.                 }
  89.  
  90.                 slot.onSlotChange(itemstack1, itemstack);
  91.             } else if (index >= 16 && index < 43) {
  92.                 if (!this.mergeItemStack(itemstack1, 43, 52, false)) {
  93.                     return ItemStack.EMPTY;
  94.                 }
  95.             } else if (index >= 43 && index < 52) {
  96.                 if (!this.mergeItemStack(itemstack1, 16, 43, false)) {
  97.                     return ItemStack.EMPTY;
  98.                 }
  99.             } else if (!this.mergeItemStack(itemstack1, 16, 43, false)) {
  100.                 return ItemStack.EMPTY;
  101.             }
  102.  
  103.             if (itemstack1.isEmpty()) {
  104.                 slot.putStack(ItemStack.EMPTY);
  105.             } else {
  106.                 slot.onSlotChanged();
  107.             }
  108.  
  109.             if (itemstack1.getCount() == itemstack.getCount()) {
  110.                 return ItemStack.EMPTY;
  111.             }
  112.  
  113.             ItemStack itemstack2 = slot.onTake(playerIn, itemstack1);
  114.             if (index == 0) {
  115.                 playerIn.dropItem(itemstack2, false);
  116.             }
  117.         }
  118.  
  119.         return itemstack;
  120.     }
  121.  
  122.     /**
  123.      * Checks the assembly crafting grid against all registered Exkva recipes to determine potential output items.
  124.      * When a match is found, the result slot of the assembly is set to contain the result ItemStack of the matched recipe.
  125.      *
  126.      * @param inventoryIn inventory provided
  127.      */
  128.     public void onCraftMatrixChanged(IInventory inventoryIn) {
  129.  
  130.         super.detectAndSendChanges();
  131.  
  132.         /* Stores the list of items in the assembly grid to be used by the recipe checker */
  133.         NonNullList<ItemStack> pattern = this.craftMatrix.stackList;
  134.  
  135.         if(lastRecipe != null && lastRecipe.matches(pattern)) {
  136.             this.inventorySlots.get(0).putStack(lastRecipe.getOutput().copy());
  137.             return;
  138.         }
  139.  
  140.         /* Loops through Exkva recipe registry, comparing the assembly grid to each until a match is found */
  141.         for(RecipeExkvaAssembly recipe : Exkva.ASSEMBLY_RECIPES) {
  142.             if(recipe.matches(pattern)) {
  143.                 this.inventorySlots.get(0).putStack(recipe.getOutput());
  144.                 lastRecipe = recipe;
  145.                 return;
  146.             }
  147.             if(!recipe.matches(pattern)) {
  148.                 this.inventorySlots.get(0).putStack(ItemStack.EMPTY);
  149.             }
  150.         }
  151.     }
  152.  
  153.     private int getCraftAmount() {
  154.  
  155.         int amount = 64;
  156.  
  157.         for(int k = 1; k < 16; k++) {
  158.             if(inventorySlots.get(k).getStack() != ItemStack.EMPTY) {
  159.                 int slotAmt = inventorySlots.get(k).getStack().getCount();
  160.                 if(slotAmt < amount) {
  161.                     amount = slotAmt;
  162.                 }
  163.             }
  164.         }
  165.  
  166.         return amount;
  167.     }
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement