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

107 lines
3.5 KiB
Java
Raw Normal View History

2020-02-21 18:15:37 +01:00
package de.ellpeck.naturesaura.blocks.tiles;
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.item.ItemStack;
import net.minecraft.world.level.block.state.BlockState;
2024-02-03 14:56:07 +01:00
import net.neoforged.neoforge.common.capabilities.Capabilities;
import net.neoforged.neoforge.items.IItemHandler;
2020-02-21 18:15:37 +01:00
import java.util.ArrayList;
import java.util.List;
2021-12-04 15:40:09 +01:00
public class BlockEntityItemDistributor extends BlockEntityImpl implements ITickableBlockEntity {
2020-02-21 18:15:37 +01:00
private int cooldown;
private Direction currentSide = Direction.NORTH;
public boolean isRandomMode;
2021-12-08 00:31:29 +01:00
public BlockEntityItemDistributor(BlockPos pos, BlockState state) {
2021-12-19 15:32:45 +01:00
super(ModBlockEntities.ITEM_DISTRIBUTOR, pos, state);
2020-02-21 18:15:37 +01:00
}
@Override
public void tick() {
2021-12-04 15:40:09 +01:00
if (this.level.isClientSide)
2020-02-21 18:15:37 +01:00
return;
if (this.cooldown > 0) {
this.cooldown--;
return;
}
this.cooldown = 1;
2021-12-15 16:30:22 +01:00
var above = this.getHandler(Direction.UP);
2020-02-21 18:15:37 +01:00
if (above == null)
return;
2021-12-15 16:30:22 +01:00
var dest = this.getNextSide();
2020-02-21 18:15:37 +01:00
if (dest == null)
return;
2021-12-15 16:30:22 +01:00
for (var i = 0; i < above.getSlots(); i++) {
var stack = above.extractItem(i, 1, true);
2020-02-21 18:15:37 +01:00
if (stack.isEmpty())
continue;
2021-12-15 16:30:22 +01:00
for (var j = 0; j < dest.getSlots(); j++) {
var remain = dest.insertItem(j, stack, false);
if (!ItemStack.matches(remain, stack)) {
2020-02-21 18:15:37 +01:00
above.extractItem(i, 1, false);
this.cooldown = 3;
return;
}
}
}
}
private IItemHandler getHandler(Direction direction) {
2021-12-15 16:30:22 +01:00
var offset = this.worldPosition.relative(direction);
var tile = this.level.getBlockEntity(offset);
2020-02-21 18:15:37 +01:00
if (tile == null)
return null;
2024-02-03 14:56:07 +01:00
return tile.getCapability(Capabilities.ITEM_HANDLER, direction.getOpposite()).orElse(null);
2020-02-21 18:15:37 +01:00
}
private IItemHandler getNextSide() {
if (this.isRandomMode) {
List<IItemHandler> handlers = new ArrayList<>();
2021-12-15 16:30:22 +01:00
for (var i = 0; i < 4; i++) {
var handler = this.getHandler(Direction.values()[i]);
2020-02-21 18:15:37 +01:00
if (handler != null)
handlers.add(handler);
}
if (handlers.isEmpty())
return null;
2021-12-08 00:31:29 +01:00
return handlers.get(this.level.random.nextInt(handlers.size()));
2020-02-21 18:15:37 +01:00
} else {
2021-12-15 16:30:22 +01:00
for (var i = 0; i < 4; i++) {
2021-12-08 00:31:29 +01:00
this.currentSide = this.currentSide.getClockWise();
2021-12-15 16:30:22 +01:00
var handler = this.getHandler(this.currentSide);
2020-02-21 18:15:37 +01:00
if (handler != null)
return handler;
}
return null;
}
}
@Override
2021-12-04 15:40:09 +01:00
public void writeNBT(CompoundTag compound, SaveType type) {
2020-02-21 18:15:37 +01:00
super.writeNBT(compound, type);
if (type == SaveType.TILE) {
compound.putInt("cooldown", this.cooldown);
compound.putInt("side", this.currentSide.ordinal());
}
if (type != SaveType.BLOCK)
compound.putBoolean("random", this.isRandomMode);
}
@Override
2021-12-04 15:40:09 +01:00
public void readNBT(CompoundTag compound, SaveType type) {
2020-02-21 18:15:37 +01:00
super.readNBT(compound, type);
if (type == SaveType.TILE) {
this.cooldown = compound.getInt("cooldown");
this.currentSide = Direction.values()[compound.getInt("side")];
}
if (type != SaveType.BLOCK)
this.isRandomMode = compound.getBoolean("random");
}
}