diff --git a/src/main/java/ru/dbotthepony/mc/otm/AbstractGrid.java b/src/main/java/ru/dbotthepony/mc/otm/AbstractGrid.java new file mode 100644 index 000000000..b1b2b6f1d --- /dev/null +++ b/src/main/java/ru/dbotthepony/mc/otm/AbstractGrid.java @@ -0,0 +1,152 @@ +package ru.dbotthepony.mc.otm; + +import net.minecraft.MethodsReturnNonnullByDefault; +import net.minecraft.core.BlockPos; +import net.minecraft.core.Direction; +import net.minecraft.core.SectionPos; +import net.minecraft.world.level.Level; +import net.minecraft.world.level.block.entity.BlockEntity; +import net.minecraft.world.level.chunk.LevelChunk; +import net.minecraftforge.common.capabilities.Capability; +import net.minecraftforge.common.util.LazyOptional; +import ru.dbotthepony.mc.otm.capability.IStorageGridCell; +import ru.dbotthepony.mc.otm.capability.MatteryCapability; +import ru.dbotthepony.mc.otm.storage.StorageGrid; + +import javax.annotation.ParametersAreNonnullByDefault; +import java.security.Provider; +import java.util.HashSet; +import java.util.List; +import java.util.function.Consumer; +import java.util.function.Function; +import java.util.function.Supplier; + +@MethodsReturnNonnullByDefault +@ParametersAreNonnullByDefault +abstract public class AbstractGrid { + protected final HashSet cells = new HashSet<>(); + public final Capability capability; + + protected AbstractGrid(Capability capability) { + this.capability = capability; + } + + public int size() { + return cells.size(); + } + + abstract protected void onAdded(T cell); + abstract protected void onRemoved(T cell); + + public boolean add(LazyOptional cell) { + if (!cell.isPresent()) + return false; + + var resolved = cell.resolve(); + + if (resolved.isPresent()) { + final var value = resolved.get(); + + if (add(value)) { + cell.addListener((lazy) -> remove(value)); + return true; + } + } + + return false; + } + + public boolean add(T cell) { + if (cells.add(cell)) { + onAdded(cell); + return true; + } + + return false; + } + + public boolean remove(T cell) { + if (cells.remove(cell)) { + onRemoved(cell); + return true; + } + + return false; + } + + public void merge(AbstractGrid other) { + if (other == this) + return; + + if (size() < other.size()) { + var copy = List.copyOf(cells); + + for (var cell : copy) { + remove(cell); + other.add(cell); + } + } else { + var copy = List.copyOf(other.cells); + + for (var cell : copy) { + other.remove(cell); + add(cell); + } + } + } + + public interface OnNeighbourFunction { + void apply(Level level, BlockPos pos, Direction dir, T other, BlockEntity ent); + } + + public static boolean createOrConnectGrid( + T self, + Capability capability, + Level level, + BlockPos pos, + boolean force, + Supplier> factory, + Function> getter, + OnNeighbourFunction neighbour + ) { + if (getter.apply(self) != null && !force) + return true; + + boolean full_discovery = true; + + for (Direction direction : Direction.values()) { + BlockPos offset = pos.offset(direction.getNormal()); + + // level.getBlockEntity can drink big cup of deadlocks + LevelChunk get_chunk = level.getChunkSource().getChunkNow(SectionPos.blockToSectionCoord(offset.getX()), SectionPos.blockToSectionCoord(offset.getZ())); + + if (get_chunk == null) { + full_discovery = false; + continue; + } + + BlockEntity get_entity = get_chunk.getBlockEntity(offset); + + if (get_entity != null && get_entity.getCapability(capability, direction.getOpposite()).isPresent()) { + final var cell = get_entity.getCapability(capability, direction.getOpposite()).resolve().get(); + final var grid = getter.apply(cell); + + if (grid != null && grid != getter.apply(self)) { + if (getter.apply(self) == null) { + grid.add(self); + } else { + grid.merge(getter.apply(self)); + } + } + + neighbour.apply(level, pos, direction, cell, get_entity); + } + } + + if (getter.apply(self) == null) { + factory.get().add(self); + } + + return full_discovery; + } +} diff --git a/src/main/java/ru/dbotthepony/mc/otm/block/BlockMatterCable.java b/src/main/java/ru/dbotthepony/mc/otm/block/BlockMatterCable.java index 400cb7621..04ead0386 100644 --- a/src/main/java/ru/dbotthepony/mc/otm/block/BlockMatterCable.java +++ b/src/main/java/ru/dbotthepony/mc/otm/block/BlockMatterCable.java @@ -201,6 +201,7 @@ public class BlockMatterCable extends Block implements EntityBlock { // blocks @Override + @SuppressWarnings("deprecation") public void neighborChanged(BlockState self, Level level, BlockPos position_self, Block block_notifier, BlockPos position_notifier, boolean unknown_flag) { Direction normal = Direction.fromNormal(position_notifier.subtract(position_self)); boolean updated = false; diff --git a/src/main/java/ru/dbotthepony/mc/otm/block/entity/BlockEntityMatterBottler.java b/src/main/java/ru/dbotthepony/mc/otm/block/entity/BlockEntityMatterBottler.java index 8c1ad0345..1e08ceeff 100644 --- a/src/main/java/ru/dbotthepony/mc/otm/block/entity/BlockEntityMatterBottler.java +++ b/src/main/java/ru/dbotthepony/mc/otm/block/entity/BlockEntityMatterBottler.java @@ -17,7 +17,6 @@ import net.minecraft.world.level.block.state.BlockState; import net.minecraftforge.common.capabilities.Capability; import net.minecraftforge.common.util.LazyOptional; import net.minecraftforge.items.CapabilityItemHandler; -import net.minecraftforge.items.IItemHandler; import ru.dbotthepony.mc.otm.Registry; import ru.dbotthepony.mc.otm.block.entity.worker.WorkerState; import ru.dbotthepony.mc.otm.capability.*; @@ -131,7 +130,7 @@ public class BlockEntityMatterBottler extends BlockEntityMatteryPowered implemen super.setLevel(p_155231_); if (grid == null) - scheduleDiscoverNeighbours(getBlockPos(), p_155231_); + scheduleDiscoverNeighbours(p_155231_, getBlockPos()); } private boolean valid = true; diff --git a/src/main/java/ru/dbotthepony/mc/otm/block/entity/BlockEntityMatterCable.java b/src/main/java/ru/dbotthepony/mc/otm/block/entity/BlockEntityMatterCable.java index b62fd575e..8b20ddd51 100644 --- a/src/main/java/ru/dbotthepony/mc/otm/block/entity/BlockEntityMatterCable.java +++ b/src/main/java/ru/dbotthepony/mc/otm/block/entity/BlockEntityMatterCable.java @@ -72,8 +72,7 @@ public class BlockEntityMatterCable extends BlockEntity implements IMatterGridCe } @Override - public void onNeighbourMatterCell(BlockPos pos, Level level, Direction direction, IMatterGridCell cell) { - // OverdriveThatMatters.LOGGER.debug("Try to connect cable entity {} {} to {} {} ({})", this, pos, pos.offset(direction.getNormal()), direction, cell); + public void onNeighbourMatterCell(Level level, BlockPos pos, Direction direction, IMatterGridCell cell, BlockEntity entity) { BlockState new_state = getBlockState().setValue(BlockMatterCable.MAPPING_CONNECTION_PROP[direction.ordinal()], true); if (new_state != getBlockState()) @@ -85,7 +84,7 @@ public class BlockEntityMatterCable extends BlockEntity implements IMatterGridCe super.setRemoved(); if (grid != null) - grid.untrack(this); + grid.remove(this); } @Nullable diff --git a/src/main/java/ru/dbotthepony/mc/otm/block/entity/BlockEntityMatterCapacitorBank.java b/src/main/java/ru/dbotthepony/mc/otm/block/entity/BlockEntityMatterCapacitorBank.java index 66be9ef05..294c379c3 100644 --- a/src/main/java/ru/dbotthepony/mc/otm/block/entity/BlockEntityMatterCapacitorBank.java +++ b/src/main/java/ru/dbotthepony/mc/otm/block/entity/BlockEntityMatterCapacitorBank.java @@ -243,7 +243,7 @@ public class BlockEntityMatterCapacitorBank extends BlockEntityMattery implement super.setRemoved(); if (grid != null) - grid.untrack(this); + grid.remove(this); } @Override diff --git a/src/main/java/ru/dbotthepony/mc/otm/block/entity/BlockEntityMatterDecomposer.java b/src/main/java/ru/dbotthepony/mc/otm/block/entity/BlockEntityMatterDecomposer.java index a2309b045..00df095d7 100644 --- a/src/main/java/ru/dbotthepony/mc/otm/block/entity/BlockEntityMatterDecomposer.java +++ b/src/main/java/ru/dbotthepony/mc/otm/block/entity/BlockEntityMatterDecomposer.java @@ -176,10 +176,10 @@ public class BlockEntityMatterDecomposer extends BlockEntityMatteryWorker implem @Override public void setRemoved() { - if (level != null && !level.isClientSide && grid != null) - grid.untrack(this); - super.setRemoved(); + + if (level != null && !level.isClientSide && grid != null) + grid.remove(this); } @Override diff --git a/src/main/java/ru/dbotthepony/mc/otm/block/entity/BlockEntityMatterPanel.java b/src/main/java/ru/dbotthepony/mc/otm/block/entity/BlockEntityMatterPanel.java index 06c8d7265..b59f94ff3 100644 --- a/src/main/java/ru/dbotthepony/mc/otm/block/entity/BlockEntityMatterPanel.java +++ b/src/main/java/ru/dbotthepony/mc/otm/block/entity/BlockEntityMatterPanel.java @@ -91,15 +91,15 @@ public class BlockEntityMatterPanel extends BlockEntityMattery implements IMatte super.setLevel(p_155231_); if (grid == null) - scheduleDiscoverNeighbours(getBlockPos(), p_155231_); + scheduleDiscoverNeighbours(p_155231_, getBlockPos()); } @Override public void setRemoved() { - if (level != null && !level.isClientSide && grid != null) - grid.untrack(this); - super.setRemoved(); + + if (level != null && !level.isClientSide && grid != null) + grid.remove(this); } @Nullable diff --git a/src/main/java/ru/dbotthepony/mc/otm/block/entity/BlockEntityMatterReplicator.java b/src/main/java/ru/dbotthepony/mc/otm/block/entity/BlockEntityMatterReplicator.java index 5f37cb05f..6e915135c 100644 --- a/src/main/java/ru/dbotthepony/mc/otm/block/entity/BlockEntityMatterReplicator.java +++ b/src/main/java/ru/dbotthepony/mc/otm/block/entity/BlockEntityMatterReplicator.java @@ -103,10 +103,10 @@ public class BlockEntityMatterReplicator extends BlockEntityMatteryWorker implem @Override public void setRemoved() { - if (level != null && !level.isClientSide && grid != null) - grid.untrack(this); - super.setRemoved(); + + if (level != null && !level.isClientSide && grid != null) + grid.remove(this); } private static final double TICKS_PER_MTU = 20_000d; @@ -119,7 +119,7 @@ public class BlockEntityMatterReplicator extends BlockEntityMatteryWorker implem super.setLevel(p_155231_); if (grid == null) - scheduleDiscoverNeighbours(getBlockPos(), p_155231_); + scheduleDiscoverNeighbours(p_155231_, getBlockPos()); } @Nullable diff --git a/src/main/java/ru/dbotthepony/mc/otm/block/entity/BlockEntityMatterScanner.java b/src/main/java/ru/dbotthepony/mc/otm/block/entity/BlockEntityMatterScanner.java index cb4a6d2fd..83ff7604c 100644 --- a/src/main/java/ru/dbotthepony/mc/otm/block/entity/BlockEntityMatterScanner.java +++ b/src/main/java/ru/dbotthepony/mc/otm/block/entity/BlockEntityMatterScanner.java @@ -11,11 +11,11 @@ import net.minecraft.world.entity.player.Player; import net.minecraft.world.inventory.AbstractContainerMenu; import net.minecraft.world.item.ItemStack; import net.minecraft.world.level.Level; +import net.minecraft.world.level.block.entity.BlockEntity; import net.minecraft.world.level.block.state.BlockState; import net.minecraftforge.common.capabilities.Capability; import net.minecraftforge.common.util.LazyOptional; import net.minecraftforge.items.CapabilityItemHandler; -import net.minecraftforge.items.IItemHandler; import ru.dbotthepony.mc.otm.block.entity.worker.BlockEntityMatteryWorker; import ru.dbotthepony.mc.otm.block.entity.worker.MachineJob; import ru.dbotthepony.mc.otm.block.entity.worker.MachineJobStatus; @@ -103,14 +103,14 @@ public class BlockEntityMatterScanner extends BlockEntityMatteryWorker implement @Override public void setRemoved() { - if (level != null && !level.isClientSide && grid != null) - grid.untrack(this); - super.setRemoved(); + + if (level != null && !level.isClientSide && grid != null) + grid.remove(this); } @Override - public void onNeighbourMatterCell(BlockPos pos, Level level, Direction direction, IMatterGridCell cell) { + public void onNeighbourMatterCell(Level level, BlockPos pos, Direction direction, IMatterGridCell cell, BlockEntity entity) { is_idling = false; } @@ -239,7 +239,7 @@ public class BlockEntityMatterScanner extends BlockEntityMatteryWorker implement super.setLevel(p_155231_); if (grid == null) - scheduleDiscoverNeighbours(getBlockPos(), p_155231_); + scheduleDiscoverNeighbours(p_155231_, getBlockPos()); } @Nullable diff --git a/src/main/java/ru/dbotthepony/mc/otm/block/entity/BlockEntityPatternStorage.java b/src/main/java/ru/dbotthepony/mc/otm/block/entity/BlockEntityPatternStorage.java index 0dcc085bd..df2310741 100644 --- a/src/main/java/ru/dbotthepony/mc/otm/block/entity/BlockEntityPatternStorage.java +++ b/src/main/java/ru/dbotthepony/mc/otm/block/entity/BlockEntityPatternStorage.java @@ -210,10 +210,10 @@ public class BlockEntityPatternStorage extends BlockEntityMattery implements IMa @Override public void setRemoved() { - if (level != null && !level.isClientSide && grid != null) - grid.untrack(this); - super.setRemoved(); + + if (level != null && !level.isClientSide && grid != null) + grid.remove(this); } @Override diff --git a/src/main/java/ru/dbotthepony/mc/otm/capability/IStorageGridCell.java b/src/main/java/ru/dbotthepony/mc/otm/capability/IStorageGridCell.java index 10ace03e0..12aa19b0a 100644 --- a/src/main/java/ru/dbotthepony/mc/otm/capability/IStorageGridCell.java +++ b/src/main/java/ru/dbotthepony/mc/otm/capability/IStorageGridCell.java @@ -16,6 +16,8 @@ import javax.annotation.Nullable; import javax.annotation.ParametersAreNonnullByDefault; import java.util.List; +import static ru.dbotthepony.mc.otm.AbstractGrid.createOrConnectGrid; + @MethodsReturnNonnullByDefault @ParametersAreNonnullByDefault public interface IStorageGridCell { @@ -76,50 +78,20 @@ public interface IStorageGridCell { return connectOrCreateStorageGrid(level, pos, false); } - default void onNeighbourStorageCell(Level level, BlockPos pos, Direction direction, IStorageGridCell cell) { + default void onNeighbourStorageCell(Level level, BlockPos pos, Direction direction, IStorageGridCell cell, BlockEntity entity) { } default boolean connectOrCreateStorageGrid(Level level, BlockPos pos, boolean force) { - if (getStorageGrid() != null && !force) - return true; - - boolean full_discovery = true; - - for (Direction direction : Direction.values()) { - BlockPos offset = pos.offset(direction.getNormal()); - - // level.getBlockEntity can drink big cup of deadlocks - LevelChunk get_chunk = level.getChunkSource().getChunkNow(SectionPos.blockToSectionCoord(offset.getX()), SectionPos.blockToSectionCoord(offset.getZ())); - - if (get_chunk == null) { - full_discovery = false; - continue; - } - - BlockEntity get_entity = get_chunk.getBlockEntity(offset); - - if (get_entity != null && get_entity.getCapability(MatteryCapability.STORAGE_CELL, direction.getOpposite()).isPresent()) { - var cell = get_entity.getCapability(MatteryCapability.STORAGE_CELL, direction.getOpposite()).resolve().get(); - var grid = cell.getStorageGrid(); - - if (grid != null && grid != getStorageGrid()) { - if (getStorageGrid() == null) { - grid.add(this); - } else { - grid.merge(getStorageGrid()); - } - } - - if (cell.isValidStorageCell()) - onNeighbourStorageCell(level, pos, direction, cell); - } - } - - if (getStorageGrid() == null) { - new StorageGrid().add(this); - } - - return full_discovery; + return createOrConnectGrid( + this, + MatteryCapability.STORAGE_CELL, + level, + pos, + force, + StorageGrid::new, + IStorageGridCell::getStorageGrid, + this::onNeighbourStorageCell + ); } } diff --git a/src/main/java/ru/dbotthepony/mc/otm/capability/matter/IMatterGridCell.java b/src/main/java/ru/dbotthepony/mc/otm/capability/matter/IMatterGridCell.java index 0dcf9d957..e9aaf2951 100644 --- a/src/main/java/ru/dbotthepony/mc/otm/capability/matter/IMatterGridCell.java +++ b/src/main/java/ru/dbotthepony/mc/otm/capability/matter/IMatterGridCell.java @@ -3,16 +3,16 @@ package ru.dbotthepony.mc.otm.capability.matter; import net.minecraft.MethodsReturnNonnullByDefault; import net.minecraft.core.BlockPos; import net.minecraft.core.Direction; -import net.minecraft.core.SectionPos; import net.minecraft.world.level.Level; import net.minecraft.world.level.block.entity.BlockEntity; -import net.minecraft.world.level.chunk.LevelChunk; import ru.dbotthepony.mc.otm.capability.MatteryCapability; import ru.dbotthepony.mc.otm.matter.MatterGrid; import javax.annotation.Nullable; import javax.annotation.ParametersAreNonnullByDefault; +import static ru.dbotthepony.mc.otm.AbstractGrid.createOrConnectGrid; + @MethodsReturnNonnullByDefault @ParametersAreNonnullByDefault public interface IMatterGridCell extends IMatterGridListener { @@ -36,65 +36,30 @@ public interface IMatterGridCell extends IMatterGridListener { boolean isValidMatterCell(); - void setMatterGrid(MatterGrid grid); + void setMatterGrid(@Nullable MatterGrid grid); - default void scheduleDiscoverNeighbours(BlockPos pos, Level level) { + default void scheduleDiscoverNeighbours(Level level, BlockPos pos) { MatterGrid.scheduleDiscoverNeighbours(this, pos, level); } - default boolean connectOrCreateMatterGrid(BlockPos pos, Level level) { + default boolean connectOrCreateMatterGrid(Level level, BlockPos pos) { return connectOrCreateMatterGrid(pos, level, false); } - default void onNeighbourMatterCell(BlockPos pos, Level level, Direction direction, IMatterGridCell cell) { + default void onNeighbourMatterCell(Level level, BlockPos pos, Direction direction, IMatterGridCell cell, BlockEntity entity) { } default boolean connectOrCreateMatterGrid(BlockPos pos, Level level, boolean force) { - if (getMatterGrid() != null && !force) - return true; - - boolean full_discovery = true; - - for (Direction direction : Direction.values()) { - BlockPos offset = pos.offset(direction.getNormal()); - - // level.getBlockEntity can suck big cuks of deadlocks - LevelChunk get_chunk = level.getChunkSource().getChunkNow(SectionPos.blockToSectionCoord(offset.getX()), SectionPos.blockToSectionCoord(offset.getZ())); - - if (get_chunk == null) { - full_discovery = false; - continue; - } - - BlockEntity get_entity = get_chunk.getBlockEntity(offset); - - if (get_entity != null) { - final var cap = get_entity.getCapability(MatteryCapability.MATTER_CELL).resolve(); - - if (cap.isPresent()) { - final var cell = cap.get(); - - MatterGrid grid = cell.getMatterGrid(); - - if (grid != null && grid != getMatterGrid()) { - if (getMatterGrid() == null) { - grid.track(this); - } else { - grid.mergeWith(getMatterGrid()); - } - } - - if (cell.isValidMatterCell()) - onNeighbourMatterCell(pos, level, direction, cell); - } - } - } - - if (getMatterGrid() == null) { - new MatterGrid().track(this); - } - - return full_discovery; + return createOrConnectGrid( + this, + MatteryCapability.MATTER_CELL, + level, + pos, + force, + MatterGrid::new, + IMatterGridCell::getMatterGrid, + this::onNeighbourMatterCell + ); } } diff --git a/src/main/java/ru/dbotthepony/mc/otm/matter/MatterGrid.java b/src/main/java/ru/dbotthepony/mc/otm/matter/MatterGrid.java index 6e151373f..d047d8e59 100644 --- a/src/main/java/ru/dbotthepony/mc/otm/matter/MatterGrid.java +++ b/src/main/java/ru/dbotthepony/mc/otm/matter/MatterGrid.java @@ -6,7 +6,9 @@ import net.minecraft.core.BlockPos; import net.minecraft.server.level.ServerLevel; import net.minecraft.world.item.Item; import net.minecraft.world.level.Level; +import ru.dbotthepony.mc.otm.AbstractGrid; import ru.dbotthepony.mc.otm.OverdriveThatMatters; +import ru.dbotthepony.mc.otm.capability.MatteryCapability; import ru.dbotthepony.mc.otm.capability.matter.*; import javax.annotation.Nullable; @@ -17,10 +19,7 @@ import java.util.function.Predicate; @MethodsReturnNonnullByDefault @ParametersAreNonnullByDefault -public class MatterGrid implements IMatterGridListener { - public static final Set NETWORKS = new HashSet<>(); - - private final Set cells = new HashSet<>(); +public class MatterGrid extends AbstractGrid implements IMatterGridListener { private final Set listeners = new HashSet<>(); public static void scheduleDiscoverNeighbours(IMatterGridCell cell, BlockPos pos, Level level) { @@ -29,7 +28,7 @@ public class MatterGrid implements IMatterGridListener { } public MatterGrid() { - NETWORKS.add(this); + super(MatteryCapability.MATTER_CELL); } public BigDecimal getCapacity() { @@ -336,14 +335,11 @@ public class MatterGrid implements IMatterGridListener { return false; } - public void track(IMatterGridCell entity) { - if (cells.contains(entity)) - return; + @Override + protected void onAdded(IMatterGridCell cell) { + cell.setMatterGrid(this); - cells.add(entity); - entity.setMatterGrid(this); - - var tasks = entity.getTaskProvider(); + var tasks = cell.getTaskProvider(); if (tasks != null) { for (var task : tasks.getAllTasks()) { @@ -351,7 +347,7 @@ public class MatterGrid implements IMatterGridListener { } } - var patterns = entity.getPatternStorage(); + var patterns = cell.getPatternStorage(); if (patterns != null) { for (var pattern : patterns.getStoredPatterns()) { @@ -360,14 +356,11 @@ public class MatterGrid implements IMatterGridListener { } } - public void untrack(IMatterGridCell entity) { - if (!cells.contains(entity)) - return; + @Override + protected void onRemoved(IMatterGridCell cell) { + cell.setMatterGrid(null); - cells.remove(entity); - entity.setMatterGrid(null); - - var tasks = entity.getTaskProvider(); + var tasks = cell.getTaskProvider(); if (tasks != null) { for (var task : tasks.getAllTasks()) { @@ -375,16 +368,13 @@ public class MatterGrid implements IMatterGridListener { } } - var patterns = entity.getPatternStorage(); + var patterns = cell.getPatternStorage(); if (patterns != null) { for (var pattern : patterns.getStoredPatterns()) { onPatternRemoved(pattern); } } - - if (cells.size() == 0) - NETWORKS.remove(this); } public void attach(IMatterGridListener listener) { @@ -395,34 +385,6 @@ public class MatterGrid implements IMatterGridListener { listeners.remove(listener); } - public int networkSize() { - return cells.size(); - } - - public void mergeWith(MatterGrid other) { - if (other == this) - return; - - MatterGrid a; - MatterGrid b; - - if (networkSize() > other.networkSize()) { - a = this; - b = other; - } else { - a = other; - b = this; - } - - b.validate(); - - for (IMatterGridCell entity : b.cells) { - a.track(entity); - } - - NETWORKS.remove(b); - } - public void validate() { ArrayList invalid = new ArrayList<>(); @@ -434,13 +396,9 @@ public class MatterGrid implements IMatterGridListener { if (invalid.size() != 0) { for (IMatterGridCell entity : invalid) { - untrack(entity); + remove(entity); } } - - if (cells.size() == 0) { - NETWORKS.remove(this); - } } @Override diff --git a/src/main/java/ru/dbotthepony/mc/otm/storage/StorageGrid.java b/src/main/java/ru/dbotthepony/mc/otm/storage/StorageGrid.java index bf890e3aa..ff9889082 100644 --- a/src/main/java/ru/dbotthepony/mc/otm/storage/StorageGrid.java +++ b/src/main/java/ru/dbotthepony/mc/otm/storage/StorageGrid.java @@ -4,20 +4,22 @@ import net.minecraft.MethodsReturnNonnullByDefault; import net.minecraft.core.BlockPos; import net.minecraft.world.level.Level; import net.minecraftforge.common.util.LazyOptional; +import ru.dbotthepony.mc.otm.AbstractGrid; import ru.dbotthepony.mc.otm.OverdriveThatMatters; import ru.dbotthepony.mc.otm.capability.IStorageGridCell; +import ru.dbotthepony.mc.otm.capability.MatteryCapability; import javax.annotation.ParametersAreNonnullByDefault; import java.util.*; @MethodsReturnNonnullByDefault @ParametersAreNonnullByDefault -public class StorageGrid { - private final HashSet cells = new HashSet<>(); - +public class StorageGrid extends AbstractGrid { private final HashMap, VirtualComponent> virtual_components = new HashMap<>(); - public StorageGrid() {} + public StorageGrid() { + super(MatteryCapability.STORAGE_CELL); + } public T insertObject(Class type, T object, boolean simulate) { return getVirtualComponent(type).insertStack(object, simulate); @@ -33,62 +35,30 @@ public class StorageGrid { return (VirtualComponent) virtual_components.computeIfAbsent(type, (k) -> new VirtualComponent<>(type)); } - public int size() { - return cells.size(); - } - public void add(IStorageIdentity identity) { getVirtualComponent(identity.storageIdentity()).add(identity); } - public boolean add(LazyOptional cell) { - if (!cell.isPresent()) - return false; - - var resolved = cell.resolve(); - - if (resolved.isPresent()) { - final var value = resolved.get(); - - if (add(value)) { - cell.addListener((lazy) -> remove(value)); - return true; - } - } - - return false; - } - - public boolean add(IStorageGridCell cell) { - if (cells.add(cell)) { - cell.setStorageGrid(this); - - for (var identity : cell.getComponents()) { - add(identity); - } - - return true; - } - - return false; - } - public void remove(IStorageIdentity identity) { getVirtualComponent(identity.storageIdentity()).remove(identity); } - public boolean remove(IStorageGridCell cell) { - if (cells.remove(cell)) { - cell.setStorageGrid(null); + @Override + protected void onAdded(IStorageGridCell cell) { + cell.setStorageGrid(this); - for (var identity : cell.getComponents()) { - remove(identity); - } - - return true; + for (var identity : cell.getComponents()) { + add(identity); } + } - return false; + @Override + protected void onRemoved(IStorageGridCell cell) { + cell.setStorageGrid(null); + + for (var identity : cell.getComponents()) { + remove(identity); + } } public void detach(IStorageGridCell cell) { @@ -107,31 +77,6 @@ public class StorageGrid { } } - public StorageGrid merge(StorageGrid other) { - if (other == this) - return this; - - if (size() < other.size()) { - var copy = List.copyOf(cells); - - for (var cell : copy) { - remove(cell); - other.add(cell); - } - - return other; - } else { - var copy = List.copyOf(other.cells); - - for (var cell : copy) { - other.remove(cell); - add(cell); - } - - return this; - } - } - public static void scheduleDiscoverNeighbours(IStorageGridCell cell, Level level, BlockPos pos) { OverdriveThatMatters.tickUntil(level, () -> !cell.isValidStorageCell() || cell.connectOrCreateStorageGrid(level, pos, true)); }