Advertisement
Exokem

Untitled

Mar 25th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.38 KB | None | 0 0
  1. //Crafting Container
  2. public class ContainerAssembly extends Container {
  3.  
  4.     public InventoryCrafting craftMatrix;
  5.     public InventoryCraftResult craftResult = new InventoryCraftResult();
  6.     public final World world;
  7.     public IRecipe lastRecipe;
  8.     final int x;
  9.     final int y;
  10.     final int z;
  11.     final BlockPos pos;
  12.     private final EntityPlayer player;
  13.  
  14.     public ContainerAssembly(EntityPlayer player, World world, int x, int y, int z) {
  15.         this(player, world, new BlockPos(x, y, z));
  16.     }
  17.     public ContainerAssembly(EntityPlayer player, World world, BlockPos pos) {
  18.         this.player = player;
  19.         this.inventorySlots.clear();
  20.         this.inventoryItemStacks.clear();
  21.         this.world = world;
  22.         this.x = pos.getX();
  23.         this.y = pos.getY();
  24.         this.z = pos.getZ();
  25.         this.pos = pos;
  26.         this.craftMatrix = new InventoryCrafting(this, 5, 3);
  27.  
  28.         this.addSlotToContainer(new SlotCraftingAssembly(this, player, this.craftMatrix, this.craftResult, 0, 137, 35));
  29.  
  30.         for(int i = 0; i < 3; ++i) {
  31.             for(int j = 0; j < 5; ++j) {
  32.                 this.addSlotToContainer(new Slot(this.craftMatrix, j + i * 5, 18 + j * 18, 17 + i * 18));
  33.             }
  34.         }
  35.         for (int k = 0; k < 3; ++k) {
  36.             for (int i1 = 0; i1 < 9; ++i1) {
  37.                 this.addSlotToContainer(new Slot(player.inventory, i1 + k * 9 + 9, 8 + i1 * 18, 84 + k * 18));
  38.             }
  39.         }
  40.         for (int l = 0; l < 9; ++l) {
  41.             this.addSlotToContainer(new Slot(player.inventory, l, 8 + l * 18, 142));
  42.         }
  43.     }
  44.  
  45.     @Override
  46.     public boolean canInteractWith(EntityPlayer playerIn) {
  47.         if (this.world.getBlockState(this.pos).getBlock() != ExkvaBlocks.assembly) {
  48.             return false;
  49.         } else {
  50.             return playerIn.getDistanceSq(x + 0.5D, y + 0.5D, z + 0.5D) <= 64.0D;
  51.         }
  52.     }
  53.  
  54.     @Override
  55.     public void onContainerClosed(EntityPlayer player) {
  56.         if (pos != BlockPos.ORIGIN) super.onContainerClosed(player);
  57.         else {
  58.             InventoryPlayer inv = player.inventory;
  59.             if (!inv.getItemStack().isEmpty()) {
  60.                 player.dropItem(inv.getItemStack(), false);
  61.                 inv.setItemStack(ItemStack.EMPTY);
  62.             }
  63.             if (!this.world.isRemote) this.clearContainer(player, this.world, this.craftMatrix);
  64.         }
  65.     }
  66.  
  67.     public void onCraftMatrixChanged(IInventory inventoryIn) {
  68.         this.slotChangedCraftingGrid(this.world, this.player, this.craftMatrix, this.craftResult);
  69.         NonNullList<ItemStack> pattern = this.craftMatrix.stackList;
  70.         System.out.println("detected change in craft matrix");
  71.         for(RecipeExkvaAssembly recipe : Exkva.ASSEMBLY_RECIPES) {
  72.             if(recipe.matches(pattern)) {
  73.                 System.out.println("attempting to send result");
  74.                 this.craftResult.setInventorySlotContents(0, recipe.getOutput());
  75.             }
  76.         }
  77.     }
  78. }
  79.  
  80. //Main recipe class
  81. public class RecipeExkvaAssembly {
  82.  
  83.     private final ItemStack output;
  84.     private final Object input;
  85.     private ItemStack[][] pattern = new ItemStack[3][5];
  86.  
  87.     public RecipeExkvaAssembly(Object input, ItemStack output, ItemStack[][] pattern) {
  88.         this.input = input;
  89.         this.output = output;
  90.         if(pattern.length == 3 && pattern[0].length == 5) {
  91.             for(int R = 0; R < 3; R++) {
  92.                 for(int C = 0; C < 5; C++) {
  93.                     this.pattern[R][C] = pattern[R][C];
  94.                 }
  95.             }
  96.         }
  97.     }
  98.  
  99.     public Object getInput() {
  100.         return input;
  101.     }
  102.  
  103.     public ItemStack getOutput() {
  104.         return output.copy();
  105.     }
  106.  
  107.     public boolean matches(ItemStack[][] pattern) {
  108.  
  109.         for(int R = 0; R < 3; R++) {
  110.             for(int C = 0; C < 5; C++) {
  111.                 if(pattern[R][C] != this.pattern[R][C]) {
  112.                     return false;
  113.                 }
  114.             }
  115.         }
  116.  
  117.         return true;
  118.     }
  119.  
  120.     public boolean matches(NonNullList<ItemStack> pattern) {
  121.  
  122.         int stackIndex = 0;
  123.         for(int R = 0; R < 3; R++) {
  124.             for(int C = 0; C < 5; C++) {
  125.                 if(pattern.get(stackIndex) != this.pattern[R][C]) {
  126.                     System.out.println("recipe does not match");
  127.                     return false;
  128.                 }
  129.             }
  130.         }
  131.         System.out.println("recipe match success");
  132.         return true;
  133.     }
  134. }
  135.  
  136. //Recipe Registry
  137. public class ExkvaAssemblyRecipes {
  138.  
  139.     private static final ItemStack X = ItemStack.EMPTY;
  140.  
  141.     public static void init() {
  142.         steelRod();
  143.     }
  144.  
  145.     public static void steelRod() {
  146.         ItemStack out = new ItemStack(ExkvaItems.steelRod, 1);
  147.         ItemStack in = new ItemStack(ExkvaItems.steelIngot,1);
  148.         ItemStack[][] pattern = {
  149.                 {X, X, in, X, X},
  150.                 {X, X, in, X, X},
  151.                 {X, X, in, X, X}
  152.         };
  153.         Exkva.registerAssemblyRecipe(in, out, pattern);
  154.     }
  155. }
  156.  
  157. //Setup in Mod class
  158. public static final List<RecipeExkvaAssembly> ASSEMBLY_RECIPES = new ArrayList<>();
  159.  
  160.     public static RecipeExkvaAssembly registerAssemblyRecipe(Object input, ItemStack output, ItemStack[][] pattern) {
  161.         RecipeExkvaAssembly recipe = new RecipeExkvaAssembly(input, output, pattern);
  162.         ASSEMBLY_RECIPES.add(recipe);
  163.         return recipe;
  164.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement