diff --git a/src/main/java/de/ellpeck/prettypipes/network/PipeItem.java b/src/main/java/de/ellpeck/prettypipes/network/PipeItem.java index dee9db0..e379887 100644 --- a/src/main/java/de/ellpeck/prettypipes/network/PipeItem.java +++ b/src/main/java/de/ellpeck/prettypipes/network/PipeItem.java @@ -4,6 +4,7 @@ import com.mojang.blaze3d.matrix.MatrixStack; import de.ellpeck.prettypipes.PrettyPipes; import de.ellpeck.prettypipes.Utility; import de.ellpeck.prettypipes.pipe.IPipeConnectable; +import de.ellpeck.prettypipes.pipe.IPipeItem; import de.ellpeck.prettypipes.pipe.PipeTileEntity; import joptsimple.internal.Strings; import net.minecraft.block.BlockState; @@ -43,15 +44,10 @@ import java.util.*; import java.util.function.BiFunction; import java.util.function.Consumer; -public class PipeItem implements INBTSerializable { +public class PipeItem implements IPipeItem { - public static final Map> TYPES = new HashMap<>(); public static final ResourceLocation TYPE = new ResourceLocation(PrettyPipes.ID, "pipe_item"); - static { - TYPES.put(TYPE, PipeItem::new); - } - public ItemStack stack; public float speed; public float x; @@ -80,12 +76,18 @@ public class PipeItem implements INBTSerializable { this(TYPE, stack, speed); } - private PipeItem(ResourceLocation type, CompoundNBT nbt) { + public PipeItem(ResourceLocation type, CompoundNBT nbt) { this.type = type; this.path = new ArrayList<>(); this.deserializeNBT(nbt); } + @Override + public ItemStack getContent() { + return this.stack; + } + + @Override public void setDestination(BlockPos startInventory, BlockPos destInventory, GraphPath path) { this.startInventory = startInventory; this.destInventory = destInventory; @@ -101,6 +103,7 @@ public class PipeItem implements INBTSerializable { } } + @Override public void updateInPipe(PipeTileEntity currPipe) { // this prevents pipes being updated after one another // causing an item that just switched to tick twice @@ -203,6 +206,7 @@ public class PipeItem implements INBTSerializable { } } + @Override public void drop(World world, ItemStack stack) { ItemEntity item = new ItemEntity(world, this.x, this.y, this.z, stack.copy()); item.world.addEntity(item); @@ -233,14 +237,17 @@ public class PipeItem implements INBTSerializable { return this.path.get(0); } + @Override public BlockPos getDestPipe() { return this.path.get(this.path.size() - 1); } + @Override public BlockPos getCurrentPipe() { return this.path.get(this.currentTile); } + @Override public BlockPos getDestInventory() { return this.destInventory; } @@ -284,10 +291,12 @@ public class PipeItem implements INBTSerializable { this.path.add(NBTUtil.readBlockPos(list.getCompound(i))); } - public int getItemsOnTheWay(BlockPos goalInv){ + @Override + public int getItemsOnTheWay(BlockPos goalInv) { return this.stack.getCount(); } + @Override @OnlyIn(Dist.CLIENT) public void render(PipeTileEntity tile, MatrixStack matrixStack, Random random, float partialTicks, int light, int overlay, IRenderTypeBuffer buffer) { matrixStack.translate( @@ -367,14 +376,4 @@ public class PipeItem implements INBTSerializable { } return ret; } - - public static PipeItem load(CompoundNBT nbt) { - // TODO legacy compat, remove eventually - if (!nbt.contains("type")) - nbt.putString("type", TYPE.toString()); - - ResourceLocation type = new ResourceLocation(nbt.getString("type")); - BiFunction func = TYPES.get(type); - return func != null ? func.apply(type, nbt) : null; - } } diff --git a/src/main/java/de/ellpeck/prettypipes/network/PipeNetwork.java b/src/main/java/de/ellpeck/prettypipes/network/PipeNetwork.java index 17b5655..6e354ca 100644 --- a/src/main/java/de/ellpeck/prettypipes/network/PipeNetwork.java +++ b/src/main/java/de/ellpeck/prettypipes/network/PipeNetwork.java @@ -7,6 +7,7 @@ import de.ellpeck.prettypipes.PrettyPipes; import de.ellpeck.prettypipes.Registry; import de.ellpeck.prettypipes.Utility; import de.ellpeck.prettypipes.misc.ItemEqualityType; +import de.ellpeck.prettypipes.pipe.IPipeItem; import de.ellpeck.prettypipes.pipe.PipeBlock; import de.ellpeck.prettypipes.pipe.PipeTileEntity; import de.ellpeck.prettypipes.packets.PacketHandler; @@ -17,16 +18,13 @@ import net.minecraft.nbt.CompoundNBT; import net.minecraft.nbt.ListNBT; import net.minecraft.nbt.NBTUtil; import net.minecraft.util.Direction; -import net.minecraft.util.Util; import net.minecraft.util.math.BlockPos; import net.minecraft.world.World; import net.minecraftforge.common.capabilities.Capability; import net.minecraftforge.common.capabilities.ICapabilitySerializable; -import net.minecraftforge.common.util.Constants; import net.minecraftforge.common.util.Constants.NBT; import net.minecraftforge.common.util.LazyOptional; import net.minecraftforge.items.IItemHandler; -import net.minecraftforge.items.ItemHandlerHelper; import org.apache.commons.lang3.tuple.Pair; import org.jgrapht.GraphPath; import org.jgrapht.ListenableGraph; @@ -45,7 +43,6 @@ import java.util.*; import java.util.function.BiFunction; import java.util.function.Consumer; import java.util.function.Function; -import java.util.function.Predicate; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -55,7 +52,7 @@ public class PipeNetwork implements ICapabilitySerializable, GraphL private final DijkstraShortestPath dijkstra; private final Map> nodeToConnectedNodes = new HashMap<>(); private final Map tileCache = new HashMap<>(); - private final ListMultimap pipeItems = ArrayListMultimap.create(); + private final ListMultimap pipeItems = ArrayListMultimap.create(); private final ListMultimap networkLocks = ArrayListMultimap.create(); private final World world; @@ -100,7 +97,7 @@ public class PipeNetwork implements ICapabilitySerializable, GraphL ListNBT edges = nbt.getList("edges", NBT.TAG_COMPOUND); for (int i = 0; i < edges.size(); i++) this.addEdge(new NetworkEdge(edges.getCompound(i))); - for (PipeItem item : Utility.deserializeAll(nbt.getList("items", NBT.TAG_COMPOUND), PipeItem::load)) + for (IPipeItem item : Utility.deserializeAll(nbt.getList("items", NBT.TAG_COMPOUND), IPipeItem::load)) this.pipeItems.put(item.getCurrentPipe(), item); for (NetworkLock lock : Utility.deserializeAll(nbt.getList("locks", NBT.TAG_COMPOUND), NetworkLock::new)) this.createNetworkLock(lock); @@ -137,7 +134,7 @@ public class PipeNetwork implements ICapabilitySerializable, GraphL return this.routeItem(startPipePos, startInventory, stack, PipeItem::new, preventOversending); } - public ItemStack routeItem(BlockPos startPipePos, BlockPos startInventory, ItemStack stack, BiFunction itemSupplier, boolean preventOversending) { + public ItemStack routeItem(BlockPos startPipePos, BlockPos startInventory, ItemStack stack, BiFunction itemSupplier, boolean preventOversending) { if (!this.isNode(startPipePos)) return stack; if (!this.world.isBlockLoaded(startPipePos)) @@ -153,7 +150,7 @@ public class PipeNetwork implements ICapabilitySerializable, GraphL Pair dest = pipe.getAvailableDestination(stack, false, preventOversending); if (dest == null || dest.getLeft().equals(startInventory)) continue; - Function sup = speed -> itemSupplier.apply(dest.getRight(), speed); + Function sup = speed -> itemSupplier.apply(dest.getRight(), speed); if (this.routeItemToLocation(startPipePos, startInventory, pipe.getPos(), dest.getLeft(), dest.getRight(), sup)) { ItemStack remain = stack.copy(); remain.shrink(dest.getRight().getCount()); @@ -165,7 +162,7 @@ public class PipeNetwork implements ICapabilitySerializable, GraphL return stack; } - public boolean routeItemToLocation(BlockPos startPipePos, BlockPos startInventory, BlockPos destPipePos, BlockPos destInventory, ItemStack stack, Function itemSupplier) { + public boolean routeItemToLocation(BlockPos startPipePos, BlockPos startInventory, BlockPos destPipePos, BlockPos destInventory, ItemStack stack, Function itemSupplier) { if (!this.isNode(startPipePos) || !this.isNode(destPipePos)) return false; if (!this.world.isBlockLoaded(startPipePos) || !this.world.isBlockLoaded(destPipePos)) @@ -178,7 +175,7 @@ public class PipeNetwork implements ICapabilitySerializable, GraphL this.endProfile(); if (path == null) return false; - PipeItem item = itemSupplier.apply(startPipe.getItemSpeed(stack)); + IPipeItem item = itemSupplier.apply(startPipe.getItemSpeed(stack)); item.setDestination(startInventory, destInventory, path); startPipe.addNewItem(item); PacketHandler.sendToAllLoaded(this.world, startPipePos, new PacketItemEnterPipe(startPipePos, item)); @@ -215,7 +212,7 @@ public class PipeNetwork implements ICapabilitySerializable, GraphL return this.requestExistingItem(location, destPipe, destInventory, ignoredLock, PipeItem::new, stack, equalityTypes); } - public ItemStack requestExistingItem(NetworkLocation location, BlockPos destPipe, BlockPos destInventory, NetworkLock ignoredLock, BiFunction itemSupplier, ItemStack stack, ItemEqualityType... equalityTypes) { + public ItemStack requestExistingItem(NetworkLocation location, BlockPos destPipe, BlockPos destInventory, NetworkLock ignoredLock, BiFunction itemSupplier, ItemStack stack, ItemEqualityType... equalityTypes) { if (location.getPos().equals(destInventory)) return stack; // make sure we don't pull any locked items @@ -440,20 +437,20 @@ public class PipeNetwork implements ICapabilitySerializable, GraphL this.endProfile(); } - public List getItemsInPipe(BlockPos pos) { + public List getItemsInPipe(BlockPos pos) { return this.pipeItems.get(pos); } - public Stream getPipeItemsOnTheWay(BlockPos goalInv) { + public Stream getPipeItemsOnTheWay(BlockPos goalInv) { this.startProfile("get_pipe_items_on_the_way"); - Stream ret = this.pipeItems.values().stream().filter(i -> i.getDestInventory().equals(goalInv)); + Stream ret = this.pipeItems.values().stream().filter(i -> i.getDestInventory().equals(goalInv)); this.endProfile(); return ret; } public int getItemsOnTheWay(BlockPos goalInv, ItemStack type, ItemEqualityType... equalityTypes) { return this.getPipeItemsOnTheWay(goalInv) - .filter(i -> type == null || ItemEqualityType.compareItems(i.stack, type, equalityTypes)) + .filter(i -> type == null || ItemEqualityType.compareItems(i.getContent(), type, equalityTypes)) .mapToInt(i -> i.getItemsOnTheWay(goalInv)).sum(); } diff --git a/src/main/java/de/ellpeck/prettypipes/packets/PacketItemEnterPipe.java b/src/main/java/de/ellpeck/prettypipes/packets/PacketItemEnterPipe.java index 4316544..dfee4b6 100644 --- a/src/main/java/de/ellpeck/prettypipes/packets/PacketItemEnterPipe.java +++ b/src/main/java/de/ellpeck/prettypipes/packets/PacketItemEnterPipe.java @@ -1,6 +1,7 @@ package de.ellpeck.prettypipes.packets; import de.ellpeck.prettypipes.Utility; +import de.ellpeck.prettypipes.pipe.IPipeItem; import de.ellpeck.prettypipes.pipe.PipeTileEntity; import de.ellpeck.prettypipes.network.PipeItem; import net.minecraft.client.Minecraft; @@ -16,7 +17,7 @@ public class PacketItemEnterPipe { private BlockPos tilePos; private CompoundNBT item; - public PacketItemEnterPipe(BlockPos tilePos, PipeItem item) { + public PacketItemEnterPipe(BlockPos tilePos, IPipeItem item) { this.tilePos = tilePos; this.item = item.serializeNBT(); } @@ -45,7 +46,7 @@ public class PacketItemEnterPipe { Minecraft mc = Minecraft.getInstance(); if (mc.world == null) return; - PipeItem item = PipeItem.load(message.item); + IPipeItem item = IPipeItem.load(message.item); PipeTileEntity pipe = Utility.getTileEntity(PipeTileEntity.class, mc.world, message.tilePos); if (pipe != null) pipe.getItems().add(item); diff --git a/src/main/java/de/ellpeck/prettypipes/pipe/IPipeItem.java b/src/main/java/de/ellpeck/prettypipes/pipe/IPipeItem.java new file mode 100644 index 0000000..6d01d3c --- /dev/null +++ b/src/main/java/de/ellpeck/prettypipes/pipe/IPipeItem.java @@ -0,0 +1,58 @@ +package de.ellpeck.prettypipes.pipe; + +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.Maps; +import com.mojang.blaze3d.matrix.MatrixStack; +import de.ellpeck.prettypipes.network.NetworkEdge; +import de.ellpeck.prettypipes.network.PipeItem; +import net.minecraft.client.renderer.IRenderTypeBuffer; +import net.minecraft.item.ItemStack; +import net.minecraft.nbt.CompoundNBT; +import net.minecraft.util.ResourceLocation; +import net.minecraft.util.math.BlockPos; +import net.minecraft.world.World; +import net.minecraftforge.api.distmarker.Dist; +import net.minecraftforge.api.distmarker.OnlyIn; +import net.minecraftforge.common.util.INBTSerializable; +import org.jgrapht.GraphPath; + +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; +import java.util.Random; +import java.util.function.BiFunction; + +public interface IPipeItem extends INBTSerializable { + + Map> TYPES = new HashMap<>( + Collections.singletonMap(PipeItem.TYPE, PipeItem::new)); + + ItemStack getContent(); + + void setDestination(BlockPos startInventory, BlockPos destInventory, GraphPath path); + + void updateInPipe(PipeTileEntity currPipe); + + void drop(World world, ItemStack stack); + + BlockPos getDestPipe(); + + BlockPos getCurrentPipe(); + + BlockPos getDestInventory(); + + int getItemsOnTheWay(BlockPos goalInv); + + @OnlyIn(Dist.CLIENT) + void render(PipeTileEntity tile, MatrixStack matrixStack, Random random, float partialTicks, int light, int overlay, IRenderTypeBuffer buffer); + + static IPipeItem load(CompoundNBT nbt) { + // TODO legacy compat, remove eventually + if (!nbt.contains("type")) + nbt.putString("type", PipeItem.TYPE.toString()); + + ResourceLocation type = new ResourceLocation(nbt.getString("type")); + BiFunction func = TYPES.get(type); + return func != null ? func.apply(type, nbt) : null; + } +} diff --git a/src/main/java/de/ellpeck/prettypipes/pipe/PipeBlock.java b/src/main/java/de/ellpeck/prettypipes/pipe/PipeBlock.java index 4347b67..8d2a1a6 100644 --- a/src/main/java/de/ellpeck/prettypipes/pipe/PipeBlock.java +++ b/src/main/java/de/ellpeck/prettypipes/pipe/PipeBlock.java @@ -261,8 +261,8 @@ public class PipeBlock extends ContainerBlock { PipeTileEntity tile = Utility.getTileEntity(PipeTileEntity.class, worldIn, pos); if (tile != null) { Utility.dropInventory(tile, tile.modules); - for (PipeItem item : tile.getItems()) - item.drop(worldIn, item.stack); + for (IPipeItem item : tile.getItems()) + item.drop(worldIn, item.getContent()); } PipeNetwork network = PipeNetwork.get(worldIn); network.removeNode(pos); diff --git a/src/main/java/de/ellpeck/prettypipes/pipe/PipeRenderer.java b/src/main/java/de/ellpeck/prettypipes/pipe/PipeRenderer.java index 80848af..b132e2b 100644 --- a/src/main/java/de/ellpeck/prettypipes/pipe/PipeRenderer.java +++ b/src/main/java/de/ellpeck/prettypipes/pipe/PipeRenderer.java @@ -34,7 +34,7 @@ public class PipeRenderer extends TileEntityRenderer { matrixStack.push(); BlockPos tilePos = tile.getPos(); matrixStack.translate(-tilePos.getX(), -tilePos.getY(), -tilePos.getZ()); - for (PipeItem item : tile.getItems()) { + for (IPipeItem item : tile.getItems()) { matrixStack.push(); item.render(tile, matrixStack, this.random, partialTicks, light, overlay, buffer); matrixStack.pop(); diff --git a/src/main/java/de/ellpeck/prettypipes/pipe/PipeTileEntity.java b/src/main/java/de/ellpeck/prettypipes/pipe/PipeTileEntity.java index 5069abe..d8a0fcc 100644 --- a/src/main/java/de/ellpeck/prettypipes/pipe/PipeTileEntity.java +++ b/src/main/java/de/ellpeck/prettypipes/pipe/PipeTileEntity.java @@ -4,16 +4,12 @@ import de.ellpeck.prettypipes.PrettyPipes; import de.ellpeck.prettypipes.Registry; import de.ellpeck.prettypipes.Utility; import de.ellpeck.prettypipes.items.IModule; -import de.ellpeck.prettypipes.misc.ItemEqualityType; -import de.ellpeck.prettypipes.network.NetworkLocation; import de.ellpeck.prettypipes.network.NetworkLock; import de.ellpeck.prettypipes.network.PipeItem; import de.ellpeck.prettypipes.network.PipeNetwork; import de.ellpeck.prettypipes.pipe.containers.MainPipeContainer; import de.ellpeck.prettypipes.pressurizer.PressurizerTileEntity; import net.minecraft.block.BlockState; -import net.minecraft.block.Blocks; -import net.minecraft.block.ChestBlock; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.entity.player.PlayerInventory; import net.minecraft.inventory.container.Container; @@ -26,7 +22,6 @@ import net.minecraft.nbt.NBTUtil; import net.minecraft.network.NetworkManager; import net.minecraft.network.play.server.SUpdateTileEntityPacket; import net.minecraft.profiler.IProfiler; -import net.minecraft.tileentity.ChestTileEntity; import net.minecraft.tileentity.ITickableTileEntity; import net.minecraft.tileentity.TileEntity; import net.minecraft.tileentity.TileEntityType; @@ -35,7 +30,6 @@ import net.minecraft.util.math.AxisAlignedBB; import net.minecraft.util.math.BlockPos; import net.minecraft.util.text.ITextComponent; import net.minecraft.util.text.TranslationTextComponent; -import net.minecraft.world.World; import net.minecraftforge.api.distmarker.Dist; import net.minecraftforge.api.distmarker.OnlyIn; import net.minecraftforge.common.capabilities.Capability; @@ -45,9 +39,7 @@ import net.minecraftforge.items.CapabilityItemHandler; import net.minecraftforge.items.IItemHandler; import net.minecraftforge.items.ItemHandlerHelper; import net.minecraftforge.items.ItemStackHandler; -import net.minecraftforge.items.wrapper.InvWrapper; import org.apache.commons.lang3.tuple.Pair; -import org.apache.commons.lang3.tuple.Triple; import javax.annotation.Nonnull; import javax.annotation.Nullable; @@ -78,7 +70,7 @@ public class PipeTileEntity extends TileEntity implements INamedContainerProvide public PressurizerTileEntity pressurizer; public BlockState cover; public int moduleDropCheck; - protected List items; + protected List items; private int lastItemAmount; private int priority; @@ -137,9 +129,9 @@ public class PipeTileEntity extends TileEntity implements INamedContainerProvide @Override public void handleUpdateTag(BlockState state, CompoundNBT nbt) { this.read(state, nbt); - List items = this.getItems(); + List items = this.getItems(); items.clear(); - items.addAll(Utility.deserializeAll(nbt.getList("items", NBT.TAG_COMPOUND), PipeItem::load)); + items.addAll(Utility.deserializeAll(nbt.getList("items", NBT.TAG_COMPOUND), IPipeItem::load)); } @Override @@ -182,7 +174,7 @@ public class PipeTileEntity extends TileEntity implements INamedContainerProvide } profiler.startSection("ticking_items"); - List items = this.getItems(); + List items = this.getItems(); for (int i = items.size() - 1; i >= 0; i--) items.get(i).updateInPipe(this); if (items.size() != this.lastItemAmount) { @@ -192,18 +184,18 @@ public class PipeTileEntity extends TileEntity implements INamedContainerProvide profiler.endSection(); } - public List getItems() { + public List getItems() { if (this.items == null) this.items = PipeNetwork.get(this.world).getItemsInPipe(this.pos); return this.items; } - public void addNewItem(PipeItem item) { + public void addNewItem(IPipeItem item) { // an item might be re-routed from a previous location, but it should still count as a new item then if (!this.getItems().contains(item)) this.getItems().add(item); if (this.pressurizer != null) - this.pressurizer.pressurizeItem(item.stack, false); + this.pressurizer.pressurizeItem(item.getContent(), false); } public boolean isConnected(Direction dir) {