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

108 lines
3.9 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;
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
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
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
}
2021-03-03 01:56:19 +01:00
public List<Integer> getStackSlots(World 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-03-03 01:56:19 +01:00
public int getItemAmount(World 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
}
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++) {
2020-11-28 02:15:29 +01:00
ItemStack 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
}
2020-11-28 02:15:29 +01:00
public boolean canExtract(World world, int slot) {
IItemHandler handler = this.getItemHandler(world);
return handler != null && !handler.extractItem(slot, 1, true).isEmpty();
}
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
}