added pipe network energy storages

This commit is contained in:
Ellpeck 2020-05-07 14:39:26 +02:00
parent 539fdc2be4
commit 9a93443152
4 changed files with 47 additions and 2 deletions

View file

@ -141,7 +141,7 @@ public final class Registry {
);
}
private static <T extends AbstractPipeContainer<?>> ContainerType<T> createPipeContainer(String name) {
public static <T extends AbstractPipeContainer<?>> ContainerType<T> createPipeContainer(String name) {
return (ContainerType<T>) IForgeContainerType.create((windowId, inv, data) -> {
PipeTileEntity tile = Utility.getTileEntity(PipeTileEntity.class, inv.player.world, data.readBlockPos());
int moduleIndex = data.readInt();
@ -150,7 +150,7 @@ public final class Registry {
}).setRegistryName(name);
}
private static Item[] createTieredModule(String name, BiFunction<String, ModuleTier, ModuleItem> item) {
public static Item[] createTieredModule(String name, BiFunction<String, ModuleTier, ModuleItem> item) {
List<Item> items = new ArrayList<>();
for (ModuleTier tier : ModuleTier.values())
items.add(item.apply(name, tier).setRegistryName(tier.name().toLowerCase(Locale.ROOT) + "_" + name));

View file

@ -6,6 +6,7 @@ import com.google.common.collect.Streams;
import de.ellpeck.prettypipes.PrettyPipes;
import de.ellpeck.prettypipes.Registry;
import de.ellpeck.prettypipes.Utility;
import de.ellpeck.prettypipes.pipe.IPipeConnectable;
import de.ellpeck.prettypipes.pipe.PipeBlock;
import de.ellpeck.prettypipes.pipe.PipeTileEntity;
import de.ellpeck.prettypipes.packets.PacketHandler;
@ -15,6 +16,7 @@ import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.nbt.ListNBT;
import net.minecraft.nbt.NBTUtil;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.Direction;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
@ -22,6 +24,9 @@ import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.ICapabilitySerializable;
import net.minecraftforge.common.util.Constants;
import net.minecraftforge.common.util.LazyOptional;
import net.minecraftforge.energy.CapabilityEnergy;
import net.minecraftforge.energy.IEnergyStorage;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.IItemHandler;
import org.apache.commons.lang3.tuple.Pair;
import org.jgrapht.GraphPath;
@ -48,6 +53,7 @@ public class PipeNetwork implements ICapabilitySerializable<CompoundNBT>, GraphL
private final DijkstraShortestPath<BlockPos, NetworkEdge> dijkstra;
private final Map<BlockPos, List<BlockPos>> nodeToConnectedNodes = new HashMap<>();
private final Map<BlockPos, PipeTileEntity> tileCache = new HashMap<>();
private final Map<BlockPos, Pair<TileEntity, Direction>> energyStorageCache = new HashMap<>();
private final ListMultimap<BlockPos, PipeItem> pipeItems = ArrayListMultimap.create();
private final World world;
@ -178,6 +184,30 @@ public class PipeNetwork implements ICapabilitySerializable<CompoundNBT>, GraphL
return tile;
}
public IEnergyStorage getEnergyStorage(BlockPos node) {
Pair<TileEntity, Direction> value = this.energyStorageCache.get(node);
isNull:
if (value == null || value.getLeft().isRemoved()) {
for (BlockPos other : this.getOrderedNetworkNodes(node)) {
PipeTileEntity pipe = this.getPipe(other);
for (Direction dir : Direction.values()) {
IPipeConnectable connectable = pipe.getConnectable(dir);
if (connectable == null)
continue;
BlockPos pos = other.offset(dir);
if (!connectable.provideEnergyStorage(this.world, pos, other, dir.getOpposite()))
continue;
value = Pair.of(this.world.getTileEntity(pos), dir.getOpposite());
this.energyStorageCache.put(node, value);
break isNull;
}
}
}
if (value == null)
return null;
return value.getLeft().getCapability(CapabilityEnergy.ENERGY, value.getRight()).orElse(null);
}
public List<NetworkLocation> getOrderedNetworkItems(BlockPos node) {
if (!this.isNode(node))
return Collections.emptyList();

View file

@ -9,4 +9,7 @@ public interface IPipeConnectable {
ConnectionType getConnectionType(World world, BlockPos pos, BlockState state, BlockPos pipePos, Direction direction);
default boolean provideEnergyStorage(World world, BlockPos pos, BlockPos pipePos, Direction direction) {
return false;
}
}

View file

@ -6,6 +6,7 @@ import de.ellpeck.prettypipes.items.IModule;
import de.ellpeck.prettypipes.network.PipeItem;
import de.ellpeck.prettypipes.network.PipeNetwork;
import de.ellpeck.prettypipes.pipe.modules.containers.MainPipeContainer;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.ChestBlock;
import net.minecraft.entity.player.PlayerEntity;
@ -200,6 +201,17 @@ public class PipeTileEntity extends TileEntity implements INamedContainerProvide
return tile.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, dir.getOpposite()).orElse(null);
}
public IPipeConnectable getConnectable(Direction dir) {
if (!this.isConnected(dir))
return null;
BlockPos offset = this.pos.offset(dir);
BlockState state = this.world.getBlockState(offset);
Block block = state.getBlock();
if (block instanceof IPipeConnectable)
return (IPipeConnectable) block;
return null;
}
public boolean isConnectedInventory(Direction dir) {
return this.getItemHandler(dir) != null;
}