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;
|
2022-09-03 12:06:41 +02:00
|
|
|
import net.minecraftforge.common.capabilities.ForgeCapabilities;
|
2018-12-27 13:57:23 +01:00
|
|
|
import net.minecraftforge.items.IItemHandlerModifiable;
|
|
|
|
|
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;
|
2019-01-30 16:03:41 +01:00
|
|
|
private 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;
|
2022-09-03 12:06:41 +02:00
|
|
|
var handler = tile.getCapability(ForgeCapabilities.ITEM_HANDLER, facing.getOpposite()).orElse(null);
|
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);
|
2021-12-19 15:40:53 +01:00
|
|
|
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;
|
2022-09-03 12:06:41 +02:00
|
|
|
var handlerUp = tileUp.getCapability(ForgeCapabilities.ITEM_HANDLER, Direction.DOWN).orElse(null);
|
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())
|
2019-02-25 17:15:39 +01:00
|
|
|
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);
|
2019-01-30 16:03:41 +01:00
|
|
|
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);
|
2019-01-30 16:03:41 +01:00
|
|
|
}
|
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);
|
2019-01-30 16:03:41 +01:00
|
|
|
if (type != SaveType.BLOCK) {
|
2020-01-21 21:04:44 +01:00
|
|
|
this.cooldown = compound.getInt("cooldown");
|
|
|
|
this.items.deserializeNBT(compound.getCompound("items"));
|
2019-02-25 17:15:39 +01:00
|
|
|
this.isBlacklist = compound.getBoolean("blacklist");
|
2019-01-30 16:03:41 +01:00
|
|
|
}
|
2018-12-27 13:57:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-10-19 21:26:32 +02:00
|
|
|
public IItemHandlerModifiable getItemHandler() {
|
2018-12-27 13:57:23 +01:00
|
|
|
return this.items;
|
|
|
|
}
|
|
|
|
}
|