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

60 lines
2.7 KiB
Java
Raw Normal View History

2019-02-22 19:06:47 +01:00
package de.ellpeck.naturesaura.blocks.tiles;
import de.ellpeck.naturesaura.entities.EntityEffectInhibitor;
import de.ellpeck.naturesaura.items.ModItems;
2021-12-08 00:31:29 +01:00
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.AABB;
2024-02-03 14:56:07 +01:00
import net.neoforged.neoforge.common.capabilities.Capabilities;
2019-02-22 19:06:47 +01:00
2021-12-04 15:40:09 +01:00
public class BlockEntityPowderPlacer extends BlockEntityImpl {
2019-02-22 19:06:47 +01:00
2021-12-08 00:31:29 +01:00
public BlockEntityPowderPlacer(BlockPos pos, BlockState state) {
2021-12-19 15:32:45 +01:00
super(ModBlockEntities.POWDER_PLACER, pos, state);
2020-01-21 23:02:39 +01:00
}
2019-02-22 19:06:47 +01:00
@Override
2019-03-19 17:21:06 +01:00
public void onRedstonePowerChange(int newPower) {
if (this.redstonePower <= 0 && newPower > 0) {
2021-12-15 16:30:22 +01:00
var powders = this.level.getEntitiesOfClass(EntityEffectInhibitor.class, new AABB(this.worldPosition, this.worldPosition.offset(1, 2, 1)), Entity::isAlive);
for (var facing : Direction.values()) {
2020-01-21 23:02:39 +01:00
if (!facing.getAxis().isHorizontal())
continue;
2021-12-15 16:30:22 +01:00
var tile = this.level.getBlockEntity(this.worldPosition.relative(facing));
2020-01-21 23:02:39 +01:00
if (tile == null)
2019-03-19 17:21:06 +01:00
continue;
2024-02-03 14:56:07 +01:00
var handler = tile.getCapability(Capabilities.ITEM_HANDLER, facing.getOpposite()).orElse(null);
2019-03-19 17:21:06 +01:00
if (handler == null)
continue;
2019-02-22 19:06:47 +01:00
2019-03-19 17:21:06 +01:00
if (!powders.isEmpty()) {
2021-12-15 16:30:22 +01:00
for (var powder : powders) {
var drop = powder.getDrop();
for (var i = 0; i < handler.getSlots(); i++) {
var remain = handler.insertItem(i, drop, false);
2019-03-19 17:21:06 +01:00
if (remain.isEmpty()) {
2021-12-08 00:31:29 +01:00
powder.kill();
2019-03-19 17:21:06 +01:00
break;
} else if (remain.getCount() != drop.getCount()) {
powder.setAmount(remain.getCount());
}
2019-02-22 19:06:47 +01:00
}
}
2019-03-19 17:21:06 +01:00
} else {
2021-12-15 16:30:22 +01:00
for (var i = 0; i < handler.getSlots(); i++) {
var stack = handler.extractItem(i, Integer.MAX_VALUE, true);
2019-03-19 17:21:06 +01:00
if (stack.isEmpty() || stack.getItem() != ModItems.EFFECT_POWDER)
continue;
2021-12-04 15:40:09 +01:00
EntityEffectInhibitor.place(this.level, stack, this.worldPosition.getX() + 0.5, this.worldPosition.getY() + 1, this.worldPosition.getZ() + 0.5);
2019-03-19 17:21:06 +01:00
handler.extractItem(i, Integer.MAX_VALUE, false);
break;
}
2019-02-22 19:06:47 +01:00
}
}
}
2019-03-19 17:21:06 +01:00
super.onRedstonePowerChange(newPower);
2019-02-22 19:06:47 +01:00
}
}