NaturesAura/src/main/java/de/ellpeck/naturesaura/blocks/tiles/TileEntityGratedChute.java

153 lines
6.2 KiB
Java
Raw Normal View History

2018-12-27 13:57:23 +01:00
package de.ellpeck.naturesaura.blocks.tiles;
import de.ellpeck.naturesaura.Helper;
import de.ellpeck.naturesaura.blocks.BlockGratedChute;
2019-10-20 22:30:49 +02:00
import net.minecraft.block.BlockState;
import net.minecraft.entity.item.ItemEntity;
import net.minecraft.entity.item.ItemFrameEntity;
2018-12-27 13:57:23 +01:00
import net.minecraft.item.ItemStack;
2019-10-20 22:30:49 +02:00
import net.minecraft.nbt.CompoundNBT;
2020-01-21 21:04:44 +01:00
import net.minecraft.tileentity.ITickableTileEntity;
2018-12-27 13:57:23 +01:00
import net.minecraft.tileentity.TileEntity;
2020-01-21 21:04:44 +01:00
import net.minecraft.tileentity.TileEntityType;
2019-10-20 22:30:49 +02:00
import net.minecraft.util.Direction;
2018-12-27 13:57:23 +01:00
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.IItemHandler;
import net.minecraftforge.items.IItemHandlerModifiable;
import java.util.List;
2020-01-21 21:04:44 +01:00
public class TileEntityGratedChute extends TileEntityImpl implements ITickableTileEntity {
2018-12-27 13:57:23 +01:00
private final ItemStackHandlerNA items = new ItemStackHandlerNA(1, this, true) {
2018-12-27 13:57:23 +01:00
@Override
protected boolean canExtract(ItemStack stack, int slot, int amount) {
return TileEntityGratedChute.this.redstonePower <= 0;
}
@Override
protected boolean canInsert(ItemStack stack, int slot) {
return TileEntityGratedChute.this.isBlacklist != TileEntityGratedChute.this.isItemInFrame(stack);
2018-12-27 13:57:23 +01:00
}
};
public boolean isBlacklist;
2018-12-27 13:57:23 +01:00
private int cooldown;
2020-01-22 01:32:26 +01:00
public TileEntityGratedChute() {
super(ModTileEntities.GRATED_CHUTE);
2020-01-21 21:04:44 +01:00
}
2018-12-27 13:57:23 +01:00
@Override
2020-01-21 21:04:44 +01:00
public void tick() {
2018-12-27 13:57:23 +01:00
if (!this.world.isRemote) {
if (this.cooldown <= 0) {
this.cooldown = 6;
if (this.redstonePower > 0)
return;
ItemStack curr = this.items.getStackInSlot(0);
push:
if (!curr.isEmpty()) {
2019-10-20 22:30:49 +02:00
BlockState state = this.world.getBlockState(this.pos);
2020-01-21 21:04:44 +01:00
Direction facing = state.get(BlockGratedChute.FACING);
2018-12-27 13:57:23 +01:00
TileEntity tile = this.world.getTileEntity(this.pos.offset(facing));
2020-01-21 21:04:44 +01:00
if (tile == null)
2018-12-27 13:57:23 +01:00
break push;
IItemHandler handler = tile.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY,
2020-01-21 21:04:44 +01:00
facing.getOpposite()).orElse(null);
2018-12-27 13:57:23 +01:00
if (handler == null)
break push;
for (int i = 0; i < handler.getSlots(); i++) {
ItemStack theoreticalDrain = this.items.extractItem(0, 1, true);
if (!theoreticalDrain.isEmpty()) {
ItemStack left = handler.insertItem(i, theoreticalDrain, false);
if (left.isEmpty()) {
this.items.extractItem(0, 1, false);
break push;
}
}
}
}
pull:
if (curr.isEmpty() || curr.getCount() < curr.getMaxStackSize()) {
2019-10-20 22:30:49 +02:00
List<ItemEntity> items = this.world.getEntitiesWithinAABB(ItemEntity.class, new AxisAlignedBB(
2018-12-27 13:57:23 +01:00
this.pos.getX(), this.pos.getY() + 0.5, this.pos.getZ(),
this.pos.getX() + 1, this.pos.getY() + 2, this.pos.getZ() + 1));
2019-10-20 22:30:49 +02:00
for (ItemEntity item : items) {
2020-01-21 21:04:44 +01:00
if (!item.isAlive())
2018-12-27 13:57:23 +01:00
continue;
ItemStack stack = item.getItem();
if (stack.isEmpty())
continue;
ItemStack left = this.items.insertItem(0, stack, false);
if (!ItemStack.areItemStacksEqual(stack, left)) {
if (left.isEmpty())
2020-01-21 21:04:44 +01:00
item.remove();
2018-12-27 13:57:23 +01:00
else
item.setItem(left);
break pull;
}
}
TileEntity tileUp = this.world.getTileEntity(this.pos.up());
2020-01-21 21:04:44 +01:00
if (tileUp == null)
2018-12-27 13:57:23 +01:00
break pull;
2020-01-21 21:04:44 +01:00
IItemHandler handlerUp = tileUp.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, Direction.DOWN).orElse(null);
2018-12-27 13:57:23 +01:00
if (handlerUp == null)
break pull;
for (int i = 0; i < handlerUp.getSlots(); i++) {
ItemStack theoreticalDrain = handlerUp.extractItem(i, 1, true);
if (!theoreticalDrain.isEmpty()) {
ItemStack left = this.items.insertItem(0, theoreticalDrain, false);
if (left.isEmpty()) {
handlerUp.extractItem(i, 1, false);
break pull;
}
}
}
}
} else
this.cooldown--;
}
}
private boolean isItemInFrame(ItemStack stack) {
2019-10-20 22:30:49 +02:00
List<ItemFrameEntity> frames = Helper.getAttachedItemFrames(this.world, this.pos);
2018-12-27 13:57:23 +01:00
if (frames.isEmpty())
return false;
2019-10-20 22:30:49 +02:00
for (ItemFrameEntity frame : frames) {
2018-12-27 13:57:23 +01:00
ItemStack frameStack = frame.getDisplayedItem();
if (Helper.areItemsEqual(stack, frameStack, true)) {
return true;
}
}
return false;
}
@Override
2019-10-20 22:30:49 +02:00
public void writeNBT(CompoundNBT compound, SaveType type) {
2018-12-27 13:57:23 +01:00
super.writeNBT(compound, type);
if (type != SaveType.BLOCK) {
2020-01-21 21:04:44 +01:00
compound.putInt("cooldown", this.cooldown);
compound.put("items", this.items.serializeNBT());
compound.putBoolean("blacklist", this.isBlacklist);
}
2018-12-27 13:57:23 +01:00
}
@Override
2019-10-20 22:30:49 +02:00
public void readNBT(CompoundNBT compound, SaveType type) {
2018-12-27 13:57:23 +01:00
super.readNBT(compound, type);
if (type != SaveType.BLOCK) {
2020-01-21 21:04:44 +01:00
this.cooldown = compound.getInt("cooldown");
this.items.deserializeNBT(compound.getCompound("items"));
this.isBlacklist = compound.getBoolean("blacklist");
}
2018-12-27 13:57:23 +01:00
}
@Override
2019-10-20 22:30:49 +02:00
public IItemHandlerModifiable getItemHandler(Direction facing) {
2018-12-27 13:57:23 +01:00
return this.items;
}
}