Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // recipe class
- public class RecipeExkvaAssembly {
- /* the ItemStack representing the item output of the recipe and the quantity produced */
- private final ItemStack output;
- /* the pattern representing the correct arrangement of ItemStacks in the assembly grid that will produce the output item */
- private ItemStack[][] pattern = new ItemStack[3][5];
- public RecipeExkvaAssembly(ItemStack output, ItemStack[][] pattern) {
- this.output = output;
- if(pattern.length == 3 && pattern[0].length == 5) {
- for(int R = 0; R < 3; R++) {
- for(int C = 0; C < 5; C++) {
- this.pattern[R][C] = pattern[R][C];
- }
- }
- }
- }
- public ItemStack getOutput() {
- return output.copy();
- }
- /**
- * Compares the provided list of ItemStacks to the recipe pattern.
- *
- * @param matrix the provided grid pattern.
- * @return boolean representing whether or not the provided grid pattern matches the recipe pattern.
- */
- public boolean matches(NonNullList<ItemStack> matrix) {
- int stackIndex = 0;
- for (int R = 0; R < 3; R++) {
- for (int C = 0; C < 5; C++) {
- if (matrix.get(stackIndex).getItem() != pattern[R][C].getItem()) {
- return false;
- }
- stackIndex++;
- }
- }
- return true;
- }
- }
- // container
- public class ContainerAssembly extends Container {
- public InventoryCrafting craftMatrix = new InventoryCrafting(this, GRID_WIDTH, GRID_HEIGHT);
- public InventoryCraftResult craftResult = new InventoryCraftResult();
- public final World world;
- final int x;
- final int y;
- final int z;
- final BlockPos pos;
- private final EntityPlayer player;
- public static final int GRID_WIDTH = 5;
- public static final int GRID_HEIGHT = 3;
- public ContainerAssembly(InventoryPlayer inv, World world, BlockPos pos) {
- this.player = inv.player;
- this.inventorySlots.clear();
- this.inventoryItemStacks.clear();
- this.world = world;
- this.x = pos.getX();
- this.y = pos.getY();
- this.z = pos.getZ();
- this.pos = pos;
- this.addSlotToContainer(new SlotAssemblyCrafting(player, craftMatrix, craftResult, 0, 137, 35));
- /* adds grid slots */
- for(int h = 0; h < GRID_HEIGHT; h++) {
- for(int w = 0; w < GRID_WIDTH; w++) {
- this.addSlotToContainer(new Slot(craftMatrix, w + h * GRID_WIDTH, 18 + w * 18, 17 + h * 18));
- }
- }
- /* adds player inventory slots */
- for(int r = 0; r < 3; r++) {
- for(int c = 0; c < 9; c++) {
- this.addSlotToContainer(new Slot(inv, c + r * 9 + 9, 8 + c * 18, 84 + r * 18));
- }
- }
- /* adds hotbar slots */
- for(int b = 0; b < 9; b++) {
- this.addSlotToContainer(new Slot(inv, b, 8 + b * 18, 142));
- }
- }
- @Override
- public boolean canInteractWith(EntityPlayer playerIn) {
- if (this.world.getBlockState(this.pos).getBlock() != ExkvaBlocks.assembly) {
- return false;
- } else {
- return playerIn.getDistanceSq(x + 0.5D, y + 0.5D, z + 0.5D) <= 64.0D;
- }
- }
- @Override
- public void onContainerClosed(EntityPlayer player) {
- if (pos != BlockPos.ORIGIN) super.onContainerClosed(player);
- else {
- InventoryPlayer inv = player.inventory;
- if (!inv.getItemStack().isEmpty()) {
- player.dropItem(inv.getItemStack(), false);
- inv.setItemStack(ItemStack.EMPTY);
- }
- if (!this.world.isRemote) this.clearContainer(player, this.world, this.craftMatrix);
- }
- }
- @Override
- public ItemStack transferStackInSlot(EntityPlayer playerIn, int index) {
- ItemStack itemstack = ItemStack.EMPTY;
- Slot slot = this.inventorySlots.get(index);
- if (slot != null && slot.getHasStack()) {
- ItemStack itemstack1 = slot.getStack();
- itemstack = itemstack1.copy();
- int minPlayerSlot = inventorySlots.size() - playerIn.inventory.mainInventory.size();
- if (index < minPlayerSlot) {
- if (!this.mergeItemStack(itemstack1, minPlayerSlot, this.inventorySlots.size(), true)) {
- return ItemStack.EMPTY;
- }
- } else if (!this.mergeItemStack(itemstack1, 0, minPlayerSlot, false)) {
- return ItemStack.EMPTY;
- }
- if (itemstack1.isEmpty()) {
- slot.putStack(ItemStack.EMPTY);
- } else {
- slot.onSlotChanged();
- }
- }
- return itemstack;
- }
- /**
- * Checks the assembly crafting grid against all registered Exkva recipes to determine potential output items.
- * When a match is found, the result slot of the assembly is set to contain the result ItemStack of the matched recipe.
- *
- * @param inventoryIn
- */
- public void onCraftMatrixChanged(IInventory inventoryIn) {
- super.detectAndSendChanges();
- /* Stores the list of items in the assembly grid to be used by the recipe checker */
- NonNullList<ItemStack> pattern = this.craftMatrix.stackList;
- /* Loops through Exkva recipe registry, comparing the assembly grid to each until a match is found */
- for(RecipeExkvaAssembly recipe : Exkva.ASSEMBLY_RECIPES) {
- if(recipe.matches(pattern)) {
- this.inventorySlots.get(0).putStack(recipe.getOutput().copy());
- return;
- }
- if(!recipe.matches(pattern)) {
- this.inventorySlots.get(0).putStack(ItemStack.EMPTY);
- }
- }
- }
- }
- // initialization of recipes
- // public static void example() {
- // ItemStack out = X;
- // ItemStack[][] pattern = {
- // {},
- // {},
- // {}
- // };
- // Exkva.registerAssemblyRecipe(out, pattern);
- // }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement