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

80 lines
3.2 KiB
Java
Raw Normal View History

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;
2021-12-04 15:40:09 +01:00
import net.minecraft.tileentity.HopperBlockEntity;
import net.minecraft.tileentity.ITickableBlockEntity;
import net.minecraft.tileentity.BlockEntity;
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;
2021-12-04 15:40:09 +01:00
public class BlockEntityHopperUpgrade extends BlockEntityImpl implements ITickableBlockEntity {
public BlockEntityHopperUpgrade() {
2020-01-22 01:32:26 +01:00
super(ModTileEntities.HOPPER_UPGRADE);
2020-01-21 21:04:44 +01:00
}
2021-12-04 15:40:09 +01:00
private static boolean isValidHopper(BlockEntity tile) {
if (tile instanceof HopperBlockEntity)
return tile.getLevel().getBlockState(tile.getPos()).get(HopperBlock.ENABLED);
if (tile instanceof BlockEntityGratedChute)
return ((BlockEntityGratedChute) tile).redstonePower <= 0;
2020-02-07 15:22:30 +01:00
return false;
}
2018-11-05 22:37:40 +01:00
@Override
2020-01-21 21:04:44 +01:00
public void tick() {
2021-12-04 15:40:09 +01:00
if (!this.level.isClientSide && this.level.getGameTime() % 10 == 0) {
if (IAuraChunk.getAuraInArea(this.level, this.worldPosition, 25) < 100000)
2018-11-05 22:37:40 +01:00
return;
2021-12-04 15:40:09 +01:00
BlockEntity tile = this.level.getBlockEntity(this.worldPosition.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;
2021-12-04 15:40:09 +01:00
List<ItemEntity> items = this.level.getEntitiesWithinAABB(ItemEntity.class,
new AxisAlignedBB(this.worldPosition).grow(7));
2018-11-05 22:37:40 +01:00
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
2021-12-04 15:40:09 +01:00
BlockPos spot = IAuraChunk.getHighestSpot(this.level, this.worldPosition, 25, this.worldPosition);
IAuraChunk.getAuraChunk(this.level, spot).drainAura(spot, 500);
2018-11-05 22:37:40 +01:00
2021-12-04 15:40:09 +01:00
PacketHandler.sendToAllAround(this.level, this.worldPosition, 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
}
}
}
}
}