Advertisement
TheOnlyTails

Container

Sep 29th, 2020
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.55 KB | None | 0 0
  1. package com.theonlytails.ruby.container;
  2.  
  3. import com.theonlytails.ruby.init.BlocksRegistry;
  4. import com.theonlytails.ruby.init.ContainersRegistry;
  5. import com.theonlytails.ruby.tileentity.RubyBarrelTileEntity;
  6. import net.minecraft.entity.player.PlayerEntity;
  7. import net.minecraft.entity.player.PlayerInventory;
  8. import net.minecraft.inventory.container.Container;
  9. import net.minecraft.inventory.container.Slot;
  10. import net.minecraft.item.ItemStack;
  11. import net.minecraft.network.PacketBuffer;
  12. import net.minecraft.tileentity.TileEntity;
  13. import net.minecraft.util.IWorldPosCallable;
  14. import org.jetbrains.annotations.NotNull;
  15.  
  16. import java.util.Objects;
  17.  
  18. public class RubyBarrelContainer extends Container {
  19.     public final RubyBarrelTileEntity tileEntity;
  20.     private final IWorldPosCallable canInteractWithCallable;
  21.  
  22.     public RubyBarrelContainer(final int windowId,
  23.                                @NotNull final PlayerInventory playerInventory,
  24.                                final RubyBarrelTileEntity tileEntity) {
  25.         super(ContainersRegistry.RUBY_BARREL.get(), windowId);
  26.  
  27.         this.tileEntity = tileEntity;
  28.  
  29.         assert tileEntity.getWorld() != null;
  30.  
  31.         this.canInteractWithCallable = IWorldPosCallable.of(tileEntity.getWorld(), tileEntity.getPos());
  32.  
  33.         int startX = 8;
  34.         int startY = 18;
  35.         int slotSizePlus2 = 18;
  36.  
  37.         // Barrel inventory slot assignment
  38.         for (int row = 0; row < 5; ++row) {
  39.             for (int column = 0; column < 9; ++column) {
  40.                 this.addSlot(new Slot(tileEntity,
  41.                         (row * 9) + column,
  42.                         startX + (column * slotSizePlus2),
  43.                         startY + (row * slotSizePlus2))
  44.                 );
  45.             }
  46.         }
  47.  
  48.         // Player inventory slot assignment
  49.         int startPlayerInvY = startY * 5 + 32;
  50.  
  51.         for (int row = 0; row < 3; ++row) {
  52.             for (int column = 0; column < 9; ++column) {
  53.                 this.addSlot(new Slot(playerInventory,
  54.                         9 + (row * 9) + column,
  55.                         startX + (column * slotSizePlus2),
  56.                         startPlayerInvY + (row * slotSizePlus2))
  57.                 );
  58.             }
  59.         }
  60.  
  61.         // Hotbar inventory slot assignment
  62.         int hotbarY = startPlayerInvY + (startPlayerInvY / 2) - 3;
  63.  
  64.         for (int column = 0; column < 9; column++) {
  65.             this.addSlot(new Slot(playerInventory,
  66.                     column,
  67.                     startX + (column * slotSizePlus2),
  68.                     hotbarY)
  69.             );
  70.         }
  71.     }
  72.  
  73.     private static RubyBarrelTileEntity getTileEntity(final PlayerInventory playerInventory,
  74.                                                       final PacketBuffer data) {
  75.         Objects.requireNonNull(playerInventory, "Player inventory cannot be null!");
  76.         Objects.requireNonNull(data, "data cannot be null!");
  77.  
  78.         final TileEntity tileEntityAtPos = playerInventory.player.world.getTileEntity(data.readBlockPos());
  79.  
  80.         if (tileEntityAtPos instanceof RubyBarrelTileEntity) {
  81.             return (RubyBarrelTileEntity) tileEntityAtPos;
  82.         }
  83.  
  84.         throw new IllegalStateException("Tile entity is not correct! " + tileEntityAtPos);
  85.     }
  86.  
  87.     public RubyBarrelContainer(final int windowId, final PlayerInventory playerInventory, final PacketBuffer data) {
  88.         this(windowId, playerInventory, getTileEntity(playerInventory, data));
  89.     }
  90.  
  91.     @Override
  92.     public boolean canInteractWith(@NotNull PlayerEntity playerIn) {
  93.         return isWithinUsableDistance(canInteractWithCallable, playerIn, BlocksRegistry.RUBY_BARREL.get());
  94.     }
  95.  
  96.     @Override
  97.     public @NotNull ItemStack transferStackInSlot(@NotNull PlayerEntity playerIn, int index) {
  98.         ItemStack itemStack = ItemStack.EMPTY;
  99.         Slot slot = this.inventorySlots.get(index);
  100.  
  101.         if (slot != null && slot.getHasStack()) {
  102.             ItemStack itemStack1 = slot.getStack();
  103.             itemStack = itemStack1.copy();
  104.  
  105.             if (index < 45) {
  106.                 if (!this.mergeItemStack(itemStack1,
  107.                         45, this.inventorySlots.size(), true)) {
  108.                     return ItemStack.EMPTY;
  109.                 }
  110.             } else if (this.mergeItemStack(itemStack1, 0, 45, false)) {
  111.                 return ItemStack.EMPTY;
  112.             }
  113.  
  114.             if (itemStack1.isEmpty()) {
  115.                 slot.putStack(ItemStack.EMPTY);
  116.             } else {
  117.                 slot.onSlotChanged();
  118.             }
  119.  
  120.         }
  121.  
  122.         return itemStack;
  123.     }
  124. }
  125.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement