Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Container
- public class ContainerAssembly extends Container {
- private InventoryCrafting craftMatrix = new InventoryCrafting(this, GRID_WIDTH, GRID_HEIGHT);
- private InventoryCraftResult craftResult = new InventoryCraftResult();
- public final World world;
- private final int x;
- private final int y;
- private final int z;
- private final BlockPos pos;
- private final EntityPlayer player;
- private static final int GRID_WIDTH = 5;
- private static final int GRID_HEIGHT = 3;
- private RecipeExkvaAssembly lastRecipe;
- 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 SlotCrafting(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) {
- // grid slots: [1,15]
- // player inv slots: [16, 42]
- // player hotbar slots: [43, 51]
- ItemStack itemstack = ItemStack.EMPTY;
- Slot slot = this.inventorySlots.get(index);
- if (slot != null && slot.getHasStack()) {
- ItemStack itemstack1 = slot.getStack();
- itemstack = itemstack1.copy();
- if (index == 0) {
- itemstack1.getItem().onCreated(itemstack1, this.world, playerIn);
- if (!this.mergeItemStack(itemstack1, 16, 52, true)) {
- return ItemStack.EMPTY;
- }
- slot.onSlotChange(itemstack1, itemstack);
- } else if (index >= 16 && index < 43) {
- if (!this.mergeItemStack(itemstack1, 43, 52, false)) {
- return ItemStack.EMPTY;
- }
- } else if (index >= 43 && index < 52) {
- if (!this.mergeItemStack(itemstack1, 16, 43, false)) {
- return ItemStack.EMPTY;
- }
- } else if (!this.mergeItemStack(itemstack1, 16, 43, false)) {
- return ItemStack.EMPTY;
- }
- if (itemstack1.isEmpty()) {
- slot.putStack(ItemStack.EMPTY);
- } else {
- slot.onSlotChanged();
- }
- if (itemstack1.getCount() == itemstack.getCount()) {
- return ItemStack.EMPTY;
- }
- ItemStack itemstack2 = slot.onTake(playerIn, itemstack1);
- if (index == 0) {
- playerIn.dropItem(itemstack2, false);
- }
- }
- 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 inventory provided
- */
- 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;
- if(lastRecipe != null && lastRecipe.matches(pattern)) {
- this.inventorySlots.get(0).putStack(lastRecipe.getOutput().copy());
- return;
- }
- /* 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());
- lastRecipe = recipe;
- return;
- }
- if(!recipe.matches(pattern)) {
- this.inventorySlots.get(0).putStack(ItemStack.EMPTY);
- }
- }
- }
- private int getCraftAmount() {
- int amount = 64;
- for(int k = 1; k < 16; k++) {
- if(inventorySlots.get(k).getStack() != ItemStack.EMPTY) {
- int slotAmt = inventorySlots.get(k).getStack().getCount();
- if(slotAmt < amount) {
- amount = slotAmt;
- }
- }
- }
- return amount;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement