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

143 lines
5.9 KiB
Java
Raw Normal View History

2018-12-27 13:57:23 +01:00
package de.ellpeck.naturesaura.blocks.tiles;
import de.ellpeck.naturesaura.Helper;
import de.ellpeck.naturesaura.blocks.BlockGratedChute;
2021-12-08 00:31:29 +01:00
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
2021-12-04 15:40:09 +01:00
import net.minecraft.nbt.CompoundTag;
2021-12-08 00:31:29 +01:00
import net.minecraft.world.entity.item.ItemEntity;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.AABB;
2024-03-10 11:29:12 +01:00
import net.neoforged.neoforge.capabilities.Capabilities;
2018-12-27 13:57:23 +01:00
2021-12-04 15:40:09 +01:00
public class BlockEntityGratedChute extends BlockEntityImpl implements ITickableBlockEntity {
2018-12-27 13:57:23 +01:00
2020-02-07 15:22:30 +01:00
public boolean isBlacklist;
2024-03-10 15:54:58 +01:00
public final ItemStackHandlerNA items = new ItemStackHandlerNA(1, this, true) {
2018-12-27 13:57:23 +01:00
@Override
protected boolean canExtract(ItemStack stack, int slot, int amount) {
2021-12-04 15:40:09 +01:00
return BlockEntityGratedChute.this.redstonePower <= 0;
2018-12-27 13:57:23 +01:00
}
@Override
protected boolean canInsert(ItemStack stack, int slot) {
2021-12-04 15:40:09 +01:00
return BlockEntityGratedChute.this.isBlacklist != BlockEntityGratedChute.this.isItemInFrame(stack);
2018-12-27 13:57:23 +01:00
}
};
private int cooldown;
2021-12-08 00:31:29 +01:00
public BlockEntityGratedChute(BlockPos pos, BlockState state) {
2021-12-19 15:32:45 +01:00
super(ModBlockEntities.GRATED_CHUTE, pos, state);
2020-01-21 21:04:44 +01:00
}
2018-12-27 13:57:23 +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) {
2018-12-27 13:57:23 +01:00
if (this.cooldown <= 0) {
this.cooldown = 6;
if (this.redstonePower > 0)
return;
2021-12-15 16:30:22 +01:00
var curr = this.items.getStackInSlot(0);
2018-12-27 13:57:23 +01:00
push:
if (!curr.isEmpty()) {
2021-12-15 16:30:22 +01:00
var state = this.level.getBlockState(this.worldPosition);
var facing = state.getValue(BlockGratedChute.FACING);
var tile = this.level.getBlockEntity(this.worldPosition.relative(facing));
2020-01-21 21:04:44 +01:00
if (tile == null)
2018-12-27 13:57:23 +01:00
break push;
2024-03-10 11:29:12 +01:00
var handler = this.level.getCapability(Capabilities.ItemHandler.BLOCK, tile.getBlockPos(), tile.getBlockState(), tile, facing.getOpposite());
2018-12-27 13:57:23 +01:00
if (handler == null)
break push;
2021-12-15 16:30:22 +01:00
for (var i = 0; i < handler.getSlots(); i++) {
var theoreticalDrain = this.items.extractItem(0, 1, true);
2018-12-27 13:57:23 +01:00
if (!theoreticalDrain.isEmpty()) {
2021-12-15 16:30:22 +01:00
var left = handler.insertItem(i, theoreticalDrain, false);
2018-12-27 13:57:23 +01:00
if (left.isEmpty()) {
this.items.extractItem(0, 1, false);
break push;
}
}
}
}
pull:
if (curr.isEmpty() || curr.getCount() < curr.getMaxStackSize()) {
2021-12-15 16:30:22 +01:00
var items = this.level.getEntitiesOfClass(ItemEntity.class, new AABB(
2021-12-04 15:40:09 +01:00
this.worldPosition.getX(), this.worldPosition.getY() + 0.5, this.worldPosition.getZ(),
this.worldPosition.getX() + 1, this.worldPosition.getY() + 2, this.worldPosition.getZ() + 1));
2021-12-15 16:30:22 +01:00
for (var item : items) {
2020-01-21 21:04:44 +01:00
if (!item.isAlive())
2018-12-27 13:57:23 +01:00
continue;
2021-12-15 16:30:22 +01:00
var stack = item.getItem();
2018-12-27 13:57:23 +01:00
if (stack.isEmpty())
continue;
2021-12-15 16:30:22 +01:00
var left = this.items.insertItem(0, stack, false);
if (!ItemStack.matches(stack, left)) {
2021-12-08 00:31:29 +01:00
if (left.isEmpty()) {
item.kill();
} else {
2018-12-27 13:57:23 +01:00
item.setItem(left);
2021-12-08 00:31:29 +01:00
}
2018-12-27 13:57:23 +01:00
break pull;
}
}
2021-12-15 16:30:22 +01:00
var tileUp = this.level.getBlockEntity(this.worldPosition.above());
2020-01-21 21:04:44 +01:00
if (tileUp == null)
2018-12-27 13:57:23 +01:00
break pull;
2024-03-10 11:29:12 +01:00
var handlerUp = this.level.getCapability(Capabilities.ItemHandler.BLOCK, tileUp.getBlockPos(), tileUp.getBlockState(), tileUp, Direction.DOWN);
2018-12-27 13:57:23 +01:00
if (handlerUp == null)
break pull;
2021-12-15 16:30:22 +01:00
for (var i = 0; i < handlerUp.getSlots(); i++) {
var theoreticalDrain = handlerUp.extractItem(i, 1, true);
2018-12-27 13:57:23 +01:00
if (!theoreticalDrain.isEmpty()) {
2021-12-15 16:30:22 +01:00
var left = this.items.insertItem(0, theoreticalDrain, false);
2018-12-27 13:57:23 +01:00
if (left.isEmpty()) {
handlerUp.extractItem(i, 1, false);
break pull;
}
}
}
}
} else
this.cooldown--;
}
}
private boolean isItemInFrame(ItemStack stack) {
2021-12-15 16:30:22 +01:00
var frames = Helper.getAttachedItemFrames(this.level, this.worldPosition);
2018-12-27 13:57:23 +01:00
if (frames.isEmpty())
return false;
2021-12-15 16:30:22 +01:00
for (var frame : frames) {
var frameStack = frame.getItem();
2018-12-27 13:57:23 +01:00
if (Helper.areItemsEqual(stack, frameStack, true)) {
return true;
}
}
return false;
}
@Override
2021-12-04 15:40:09 +01:00
public void writeNBT(CompoundTag compound, SaveType type) {
2018-12-27 13:57:23 +01:00
super.writeNBT(compound, type);
if (type != SaveType.BLOCK) {
2020-01-21 21:04:44 +01:00
compound.putInt("cooldown", this.cooldown);
compound.put("items", this.items.serializeNBT());
compound.putBoolean("blacklist", this.isBlacklist);
}
2018-12-27 13:57:23 +01:00
}
@Override
2021-12-04 15:40:09 +01:00
public void readNBT(CompoundTag compound, SaveType type) {
2018-12-27 13:57:23 +01:00
super.readNBT(compound, type);
if (type != SaveType.BLOCK) {
2020-01-21 21:04:44 +01:00
this.cooldown = compound.getInt("cooldown");
this.items.deserializeNBT(compound.getCompound("items"));
this.isBlacklist = compound.getBoolean("blacklist");
}
2018-12-27 13:57:23 +01:00
}
2024-03-10 11:29:12 +01:00
2018-12-27 13:57:23 +01:00
}