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

77 lines
3.1 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;
2021-12-08 00:31:29 +01:00
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.world.entity.item.ItemEntity;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.block.HopperBlock;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.entity.HopperBlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.AABB;
2018-11-05 22:37:40 +01:00
import net.minecraftforge.items.CapabilityItemHandler;
2021-12-04 15:40:09 +01:00
public class BlockEntityHopperUpgrade extends BlockEntityImpl implements ITickableBlockEntity {
2021-12-08 00:31:29 +01:00
public BlockEntityHopperUpgrade(BlockPos pos, BlockState state) {
2021-12-19 15:32:45 +01:00
super(ModBlockEntities.HOPPER_UPGRADE, pos, state);
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)
2021-12-08 00:31:29 +01:00
return tile.getLevel().getBlockState(tile.getBlockPos()).getValue(HopperBlock.ENABLED);
2021-12-04 15:40:09 +01:00
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-15 16:30:22 +01:00
var tile = this.level.getBlockEntity(this.worldPosition.below());
2022-06-27 15:24:04 +02:00
if (!BlockEntityHopperUpgrade.isValidHopper(tile))
2018-11-05 22:37:40 +01:00
return;
2021-12-15 16:30:22 +01:00
var 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-15 16:30:22 +01:00
var items = this.level.getEntitiesOfClass(ItemEntity.class, new AABB(this.worldPosition).inflate(7));
2018-11-05 22:37:40 +01:00
if (items.isEmpty())
return;
2021-12-15 16:30:22 +01:00
for (var item : items) {
2021-12-08 00:31:29 +01:00
if (!item.isAlive() || item.hasPickUpDelay())
2018-11-05 22:37:40 +01:00
continue;
2021-12-15 16:30:22 +01:00
var stack = item.getItem();
2018-11-05 22:37:40 +01:00
if (stack.isEmpty())
continue;
2021-12-15 16:30:22 +01:00
var copy = stack.copy();
2018-11-05 22:37:40 +01:00
2021-12-15 16:30:22 +01:00
for (var i = 0; i < handler.getSlots(); i++) {
2018-11-05 22:37:40 +01:00
copy = handler.insertItem(i, copy, false);
if (copy.isEmpty()) {
break;
}
}
if (!ItemStack.matches(stack, copy)) {
2018-11-05 22:37:40 +01:00
item.setItem(copy);
if (copy.isEmpty())
2021-12-08 00:31:29 +01:00
item.kill();
2018-11-05 22:37:40 +01:00
2021-12-15 16:30:22 +01:00
var spot = IAuraChunk.getHighestSpot(this.level, this.worldPosition, 25, this.worldPosition);
2021-12-04 15:40:09 +01:00
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,
2021-12-08 00:31:29 +01:00
new PacketParticles((float) item.getX(), (float) item.getY(), (float) item.getZ(), PacketParticles.Type.HOPPER_UPGRADE));
2018-11-05 22:37:40 +01:00
}
}
}
}
}