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

102 lines
3.5 KiB
Java
Raw Normal View History

2020-04-17 20:03:54 +02:00
package de.ellpeck.prettypipes.network;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.ListMultimap;
import de.ellpeck.prettypipes.misc.ItemEqualityType;
2020-05-09 12:57:25 +02:00
import de.ellpeck.prettypipes.pipe.PipeTileEntity;
2020-04-17 20:03:54 +02:00
import net.minecraft.item.ItemStack;
2020-05-09 12:57:25 +02:00
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.nbt.NBTUtil;
2020-04-17 20:03:54 +02:00
import net.minecraft.util.Direction;
import net.minecraft.util.math.BlockPos;
2020-05-09 12:57:25 +02:00
import net.minecraft.world.World;
import net.minecraftforge.common.util.INBTSerializable;
import net.minecraftforge.items.IItemHandler;
2020-04-17 20:03:54 +02:00
import org.apache.commons.lang3.tuple.Pair;
2020-05-07 18:30:40 +02:00
import java.util.*;
2020-05-07 21:10:29 +02:00
import java.util.stream.Collectors;
2020-04-17 20:03:54 +02:00
2020-05-09 12:57:25 +02:00
public class NetworkLocation implements INBTSerializable<CompoundNBT> {
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
}
2020-05-09 12:57:25 +02:00
public NetworkLocation(CompoundNBT nbt) {
this.deserializeNBT(nbt);
2020-04-17 20:03:54 +02:00
}
2020-05-09 12:57:25 +02:00
public List<Integer> getStackSlots(World world, ItemStack stack, ItemEqualityType... equalityTypes) {
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()
.filter(e -> ItemEqualityType.compareItems(e.getValue(), stack, equalityTypes))
2020-05-07 21:10:29 +02:00
.map(Map.Entry::getKey)
.collect(Collectors.toList());
2020-04-17 20:03:54 +02:00
}
2020-04-17 22:29:02 +02:00
2020-05-09 12:57:25 +02:00
public int getItemAmount(World world, ItemStack stack, ItemEqualityType... equalityTypes) {
if (this.isEmpty(world))
return 0;
return this.getItems(world).values().stream()
.filter(i -> ItemEqualityType.compareItems(stack, i, equalityTypes))
2020-04-20 03:37:23 +02:00
.mapToInt(ItemStack::getCount).sum();
}
2020-05-09 12:57:25 +02:00
public Map<Integer, ItemStack> getItems(World world) {
if (this.itemCache == null) {
IItemHandler handler = this.getItemHandler(world);
if (handler != null) {
for (int i = 0; i < handler.getSlots(); i++) {
ItemStack found = handler.extractItem(i, Integer.MAX_VALUE, true);
if (found.isEmpty())
continue;
if (this.itemCache == null)
this.itemCache = new HashMap<>();
this.itemCache.put(i, found);
}
}
}
return this.itemCache;
2020-05-07 18:30:40 +02:00
}
2020-05-09 12:57:25 +02:00
public IItemHandler getItemHandler(World world) {
if (this.handlerCache == null) {
PipeNetwork network = PipeNetwork.get(world);
PipeTileEntity 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;
}
public boolean isEmpty(World world) {
Map<Integer, ItemStack> items = this.getItems(world);
return items == null || items.isEmpty();
}
public BlockPos getPos() {
return this.pipePos.offset(this.direction);
}
@Override
public CompoundNBT serializeNBT() {
CompoundNBT nbt = new CompoundNBT();
nbt.put("pipe_pos", NBTUtil.writeBlockPos(this.pipePos));
nbt.putInt("direction", this.direction.getIndex());
return nbt;
2020-04-17 22:29:02 +02:00
}
@Override
2020-05-09 12:57:25 +02:00
public void deserializeNBT(CompoundNBT nbt) {
this.pipePos = NBTUtil.readBlockPos(nbt.getCompound("pipe_pos"));
this.direction = Direction.byIndex(nbt.getInt("direction"));
}
2020-04-17 20:03:54 +02:00
}