PrettyPipes/src/main/java/de/ellpeck/prettypipes/network/NetworkLocation.java

108 lines
3.8 KiB
Java
Raw Normal View History

2020-04-17 20:03:54 +02:00
package de.ellpeck.prettypipes.network;
2021-03-03 01:56:19 +01:00
import de.ellpeck.prettypipes.misc.ItemEquality;
2021-12-02 14:44:26 +01:00
import de.ellpeck.prettypipes.pipe.PipeBlockEntity;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
2021-12-02 16:55:04 +01:00
import net.minecraft.nbt.NbtUtils;
2021-12-02 12:31:04 +01:00
import net.minecraft.world.item.ItemStack;
import net.minecraft.nbt.CompoundTag;
2021-12-02 14:44:26 +01:00
import net.minecraft.world.level.Level;
2020-05-09 12:57:25 +02:00
import net.minecraftforge.common.util.INBTSerializable;
import net.minecraftforge.items.IItemHandler;
2020-04-17 20:03:54 +02:00
2020-11-28 02:15:29 +01:00
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
2020-05-07 21:10:29 +02:00
import java.util.stream.Collectors;
2020-04-17 20:03:54 +02:00
2021-12-02 12:31:04 +01:00
public class NetworkLocation implements INBTSerializable<CompoundTag> {
2020-04-17 20:03:54 +02:00
2020-05-09 12:57:25 +02:00
public BlockPos pipePos;
public Direction direction;
private Map<Integer, ItemStack> itemCache;
private IItemHandler handlerCache;
2020-04-17 20:03:54 +02:00
2020-05-09 12:57:25 +02:00
public NetworkLocation(BlockPos pipePos, Direction direction) {
2020-04-17 20:03:54 +02:00
this.pipePos = pipePos;
this.direction = direction;
2020-04-17 22:29:02 +02:00
}
2021-12-02 12:31:04 +01:00
public NetworkLocation(CompoundTag nbt) {
2020-05-09 12:57:25 +02:00
this.deserializeNBT(nbt);
2020-04-17 20:03:54 +02:00
}
2021-12-02 14:44:26 +01:00
public List<Integer> getStackSlots(Level world, ItemStack stack, ItemEquality... equalityTypes) {
2020-05-09 12:57:25 +02:00
if (this.isEmpty(world))
2020-05-07 21:10:29 +02:00
return Collections.emptyList();
2020-05-09 12:57:25 +02:00
return this.getItems(world).entrySet().stream()
2021-03-03 01:56:19 +01:00
.filter(kv -> ItemEquality.compareItems(kv.getValue(), stack, equalityTypes) && this.canExtract(world, kv.getKey()))
2020-11-28 02:15:29 +01:00
.map(Map.Entry::getKey).collect(Collectors.toList());
2020-04-17 20:03:54 +02:00
}
2020-04-17 22:29:02 +02:00
2021-12-02 14:44:26 +01:00
public int getItemAmount(Level world, ItemStack stack, ItemEquality... equalityTypes) {
2020-05-09 12:57:25 +02:00
if (this.isEmpty(world))
return 0;
2020-11-28 02:15:29 +01:00
return this.getItems(world).entrySet().stream()
2021-03-03 01:56:19 +01:00
.filter(kv -> ItemEquality.compareItems(stack, kv.getValue(), equalityTypes) && this.canExtract(world, kv.getKey()))
2020-11-28 02:15:29 +01:00
.mapToInt(kv -> kv.getValue().getCount()).sum();
2020-04-20 03:37:23 +02:00
}
2021-12-02 14:44:26 +01:00
public Map<Integer, ItemStack> getItems(Level world) {
2020-05-09 12:57:25 +02:00
if (this.itemCache == null) {
2021-12-02 17:46:56 +01:00
var handler = this.getItemHandler(world);
2020-05-09 12:57:25 +02:00
if (handler != null) {
2021-12-02 17:46:56 +01:00
for (var i = 0; i < handler.getSlots(); i++) {
var stack = handler.getStackInSlot(i);
// check if the slot is accessible to us
2020-11-28 02:15:29 +01:00
if (stack.isEmpty())
2020-05-09 12:57:25 +02:00
continue;
if (this.itemCache == null)
this.itemCache = new HashMap<>();
// use getStackInSlot since there might be more than 64 items in there
2020-11-28 02:15:29 +01:00
this.itemCache.put(i, stack);
2020-05-09 12:57:25 +02:00
}
}
}
return this.itemCache;
2020-05-07 18:30:40 +02:00
}
2021-12-02 14:44:26 +01:00
public boolean canExtract(Level world, int slot) {
2021-12-02 17:46:56 +01:00
var handler = this.getItemHandler(world);
2020-11-28 02:15:29 +01:00
return handler != null && !handler.extractItem(slot, 1, true).isEmpty();
}
2021-12-02 14:44:26 +01:00
public IItemHandler getItemHandler(Level world) {
2020-05-09 12:57:25 +02:00
if (this.handlerCache == null) {
2021-12-02 17:46:56 +01:00
var network = PipeNetwork.get(world);
var pipe = network.getPipe(this.pipePos);
2020-10-15 01:19:22 +02:00
this.handlerCache = pipe.getItemHandler(this.direction);
2020-05-09 12:57:25 +02:00
}
return this.handlerCache;
}
2021-12-02 14:44:26 +01:00
public boolean isEmpty(Level world) {
2021-12-02 17:46:56 +01:00
var items = this.getItems(world);
2020-05-09 12:57:25 +02:00
return items == null || items.isEmpty();
}
public BlockPos getPos() {
2021-12-02 14:44:26 +01:00
return this.pipePos.relative(this.direction);
2020-05-09 12:57:25 +02:00
}
@Override
2021-12-02 12:31:04 +01:00
public CompoundTag serializeNBT() {
2021-12-02 17:46:56 +01:00
var nbt = new CompoundTag();
2021-12-02 16:55:04 +01:00
nbt.put("pipe_pos", NbtUtils.writeBlockPos(this.pipePos));
nbt.putInt("direction", this.direction.ordinal());
2020-05-09 12:57:25 +02:00
return nbt;
2020-04-17 22:29:02 +02:00
}
@Override
2021-12-02 12:31:04 +01:00
public void deserializeNBT(CompoundTag nbt) {
2021-12-02 16:55:04 +01:00
this.pipePos = NbtUtils.readBlockPos(nbt.getCompound("pipe_pos"));
this.direction = Direction.values()[(nbt.getInt("direction"))];
}
2020-04-17 20:03:54 +02:00
}