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;
|
2020-01-22 23:21:52 +01:00
|
|
|
import de.ellpeck.naturesaura.packet.PacketHandler;
|
|
|
|
import de.ellpeck.naturesaura.packet.PacketParticles;
|
2019-10-20 22:30:49 +02:00
|
|
|
import net.minecraft.block.HopperBlock;
|
|
|
|
import net.minecraft.entity.item.ItemEntity;
|
2018-11-05 22:37:40 +01:00
|
|
|
import net.minecraft.item.ItemStack;
|
2019-10-20 22:30:49 +02:00
|
|
|
import net.minecraft.tileentity.HopperTileEntity;
|
2020-01-21 21:04:44 +01:00
|
|
|
import net.minecraft.tileentity.ITickableTileEntity;
|
|
|
|
import net.minecraft.tileentity.TileEntity;
|
2019-10-20 22:30:49 +02:00
|
|
|
import net.minecraft.util.Direction;
|
2018-11-05 22:37:40 +01:00
|
|
|
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;
|
|
|
|
|
2020-01-21 21:04:44 +01:00
|
|
|
public class TileEntityHopperUpgrade extends TileEntityImpl implements ITickableTileEntity {
|
2020-01-22 01:32:26 +01:00
|
|
|
public TileEntityHopperUpgrade() {
|
|
|
|
super(ModTileEntities.HOPPER_UPGRADE);
|
2020-01-21 21:04:44 +01:00
|
|
|
}
|
|
|
|
|
2020-02-07 15:22:30 +01:00
|
|
|
private static boolean isValidHopper(TileEntity tile) {
|
|
|
|
if (tile instanceof HopperTileEntity)
|
|
|
|
return tile.getWorld().getBlockState(tile.getPos()).get(HopperBlock.ENABLED);
|
|
|
|
if (tile instanceof TileEntityGratedChute)
|
|
|
|
return ((TileEntityGratedChute) tile).redstonePower <= 0;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-11-05 22:37:40 +01:00
|
|
|
@Override
|
2020-01-21 21:04:44 +01:00
|
|
|
public void tick() {
|
|
|
|
if (!this.world.isRemote && this.world.getGameTime() % 10 == 0) {
|
2019-02-03 22:05:56 +01:00
|
|
|
if (IAuraChunk.getAuraInArea(this.world, this.pos, 25) < 100000)
|
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;
|
2020-01-21 21:04:44 +01:00
|
|
|
IItemHandler handler = tile.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, Direction.UP).orElse(null);
|
2018-11-05 22:37:40 +01:00
|
|
|
if (handler == null)
|
|
|
|
return;
|
|
|
|
|
2019-10-20 22:30:49 +02:00
|
|
|
List<ItemEntity> items = this.world.getEntitiesWithinAABB(ItemEntity.class,
|
2018-11-05 22:37:40 +01:00
|
|
|
new AxisAlignedBB(this.pos).grow(7));
|
|
|
|
if (items.isEmpty())
|
|
|
|
return;
|
|
|
|
|
2019-10-20 22:30:49 +02:00
|
|
|
for (ItemEntity item : items) {
|
2020-01-21 21:04:44 +01:00
|
|
|
if (!item.isAlive() || item.cannotPickup())
|
2018-11-05 22:37:40 +01:00
|
|
|
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())
|
2020-01-21 21:04:44 +01:00
|
|
|
item.remove();
|
2018-11-05 22:37:40 +01:00
|
|
|
|
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
|
|
|
|
2020-01-22 23:21:52 +01:00
|
|
|
PacketHandler.sendToAllAround(this.world, this.pos, 32,
|
2020-01-28 18:08:56 +01:00
|
|
|
new PacketParticles((float) item.getPosX(), (float) item.getPosY(), (float) item.getPosZ(), PacketParticles.Type.HOPPER_UPGRADE));
|
2018-11-05 22:37:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|