package com.aranha.chickscangrown.blocks;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.inventory.container.Container;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.IWorldPosCallable;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.IItemHandler;
import net.minecraftforge.items.SlotItemHandler;
import net.minecraftforge.items.wrapper.InvWrapper;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import static com.aranha.chickscangrown.blocks.ModBlocks.INCUBATOR_CONTAINER;
/*
Problems:
Shift + right click crash minecraft
*/
public class IncubatorContainer extends Container {
private TileEntity tileEntity;
private PlayerEntity playerEntity;
private IItemHandler playerInventory;
public IncubatorContainer(int windowId, World world, BlockPos pos, PlayerInventory playerInventory, PlayerEntity player){
super(INCUBATOR_CONTAINER, windowId);
tileEntity = world.getTileEntity(pos);
this.playerEntity = player;
this.playerInventory = new InvWrapper(playerInventory);
//Slot for egg
tileEntity.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY).ifPresent(h -> {
addSlot(new SlotItemHandler(h, 0, 81, 31));
});
//Slot for first light bulb
tileEntity.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY).ifPresent(h -> {
addSlot(new SlotItemHandler(h, 0, 27, 13));
});
//Slot for second light bulb
tileEntity.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY).ifPresent(h -> {
addSlot(new SlotItemHandler(h, 0, 135, 13));
});
layoutPlayerInventorySlots(9, 69);
}
@Override
public boolean canInteractWith(PlayerEntity playerIn) {
return isWithinUsableDistance(IWorldPosCallable.of(tileEntity.getWorld(), tileEntity.getPos()), playerEntity, ModBlocks.INCUBATOR);
}
private int addSlotRange(IItemHandler handler, int index, int x, int y, int amount, int dx) {
for (int i = 0 ; i < amount ; i++) {
addSlot(new SlotItemHandler(handler, index, x, y));
x += dx;
index++;
}
return index;
}
private int addSlotBox(IItemHandler handler, int index, int x, int y, int horAmount, int dx, int verAmount, int dy) {
for (int j = 0 ; j < verAmount ; j++) {
index = addSlotRange(handler, index, x, y, horAmount, dx);
y += dy;
}
return index;
}
private void layoutPlayerInventorySlots(int leftCol, int topRow) {
// Player inventory
addSlotBox(playerInventory, 9, leftCol, topRow, 9, 18, 3, 18);
// Hotbar
topRow += 58;
addSlotRange(playerInventory, 0, leftCol, topRow, 9, 18);
}
}
Anything more?