Advertisement
Exokem

Untitled

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