2018-11-05 22:37:40 +01:00
|
|
|
package de.ellpeck.naturesaura.blocks.tiles;
|
|
|
|
|
2018-11-11 13:26:19 +01:00
|
|
|
import de.ellpeck.naturesaura.api.aura.chunk.IAuraChunk;
|
2018-11-05 22:37:40 +01:00
|
|
|
import de.ellpeck.naturesaura.packet.PacketHandler;
|
|
|
|
import de.ellpeck.naturesaura.packet.PacketParticles;
|
|
|
|
import net.minecraft.block.BlockHopper;
|
|
|
|
import net.minecraft.entity.item.EntityItem;
|
|
|
|
import net.minecraft.item.ItemStack;
|
|
|
|
import net.minecraft.tileentity.TileEntity;
|
|
|
|
import net.minecraft.tileentity.TileEntityHopper;
|
|
|
|
import net.minecraft.util.EnumFacing;
|
|
|
|
import net.minecraft.util.ITickable;
|
|
|
|
import net.minecraft.util.math.AxisAlignedBB;
|
|
|
|
import net.minecraft.util.math.BlockPos;
|
|
|
|
import net.minecraftforge.items.CapabilityItemHandler;
|
|
|
|
import net.minecraftforge.items.IItemHandler;
|
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
public class TileEntityHopperUpgrade extends TileEntityImpl implements ITickable {
|
|
|
|
@Override
|
|
|
|
public void update() {
|
|
|
|
if (!this.world.isRemote && this.world.getTotalWorldTime() % 10 == 0) {
|
2018-11-11 13:26:19 +01:00
|
|
|
if (IAuraChunk.getAuraInArea(this.world, this.pos, 25) < 1000)
|
2018-11-05 22:37:40 +01:00
|
|
|
return;
|
|
|
|
TileEntity tile = this.world.getTileEntity(this.pos.down());
|
2018-12-27 13:57:23 +01:00
|
|
|
if (!isValidHopper(tile))
|
2018-11-05 22:37:40 +01:00
|
|
|
return;
|
2018-12-27 13:57:23 +01:00
|
|
|
if (!tile.hasCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, EnumFacing.UP))
|
2018-11-05 22:37:40 +01:00
|
|
|
return;
|
2018-12-27 13:57:23 +01:00
|
|
|
IItemHandler handler = tile.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, EnumFacing.UP);
|
2018-11-05 22:37:40 +01:00
|
|
|
if (handler == null)
|
|
|
|
return;
|
|
|
|
|
|
|
|
List<EntityItem> items = this.world.getEntitiesWithinAABB(EntityItem.class,
|
|
|
|
new AxisAlignedBB(this.pos).grow(7));
|
|
|
|
if (items.isEmpty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (EntityItem item : items) {
|
|
|
|
if (item.isDead || item.cannotPickup())
|
|
|
|
continue;
|
|
|
|
ItemStack stack = item.getItem();
|
|
|
|
if (stack.isEmpty())
|
|
|
|
continue;
|
|
|
|
ItemStack copy = stack.copy();
|
|
|
|
|
|
|
|
for (int i = 0; i < handler.getSlots(); i++) {
|
|
|
|
copy = handler.insertItem(i, copy, false);
|
|
|
|
if (copy.isEmpty()) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ItemStack.areItemStacksEqual(stack, copy)) {
|
|
|
|
item.setItem(copy);
|
|
|
|
if (copy.isEmpty())
|
|
|
|
item.setDead();
|
|
|
|
|
2018-11-11 13:26:19 +01:00
|
|
|
BlockPos spot = IAuraChunk.getHighestSpot(this.world, this.pos, 25, this.pos);
|
2019-01-29 11:46:38 +01:00
|
|
|
IAuraChunk.getAuraChunk(this.world, spot).drainAura(spot, 500);
|
2018-11-05 22:37:40 +01:00
|
|
|
|
|
|
|
PacketHandler.sendToAllAround(this.world, this.pos, 32,
|
|
|
|
new PacketParticles((float) item.posX, (float) item.posY, (float) item.posZ, 10));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-12-27 13:57:23 +01:00
|
|
|
|
|
|
|
private static boolean isValidHopper(TileEntity tile) {
|
|
|
|
if (tile instanceof TileEntityHopper)
|
|
|
|
return BlockHopper.isEnabled(tile.getBlockMetadata());
|
|
|
|
if (tile instanceof TileEntityGratedChute)
|
|
|
|
return ((TileEntityGratedChute) tile).redstonePower <= 0;
|
|
|
|
return false;
|
|
|
|
}
|
2018-11-05 22:37:40 +01:00
|
|
|
}
|