Improve some grid logic

This commit is contained in:
DBotThePony 2021-09-18 13:51:04 +07:00
parent f8e0443b84
commit d9baced685
Signed by: DBot
GPG Key ID: DCC23B5715498507
2 changed files with 25 additions and 8 deletions

View File

@ -14,12 +14,15 @@ import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.level.block.state.BlockState;
import net.minecraftforge.common.capabilities.Capability; import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.util.LazyOptional; import net.minecraftforge.common.util.LazyOptional;
import ru.dbotthepony.mc.otm.OverdriveThatMatters;
import ru.dbotthepony.mc.otm.Registry; import ru.dbotthepony.mc.otm.Registry;
import ru.dbotthepony.mc.otm.capability.AbstractStorageGridCell; import ru.dbotthepony.mc.otm.capability.AbstractStorageGridCell;
import ru.dbotthepony.mc.otm.capability.MatteryCapability; import ru.dbotthepony.mc.otm.capability.MatteryCapability;
import ru.dbotthepony.mc.otm.capability.MatteryMachineEnergyStorage; import ru.dbotthepony.mc.otm.capability.MatteryMachineEnergyStorage;
import ru.dbotthepony.mc.otm.container.MatteryContainer; import ru.dbotthepony.mc.otm.container.MatteryContainer;
import ru.dbotthepony.mc.otm.menu.DriveRackMenu; import ru.dbotthepony.mc.otm.menu.DriveRackMenu;
import ru.dbotthepony.mc.otm.storage.ItemStackWrapper;
import ru.dbotthepony.mc.otm.storage.VirtualComponent;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.Nullable; import javax.annotation.Nullable;
@ -39,16 +42,13 @@ public class BlockEntityDriveRack extends BlockEntityMatteryPowered {
public void setChanged(int slot, ItemStack new_state, ItemStack old_state) { public void setChanged(int slot, ItemStack new_state, ItemStack old_state) {
super.setChanged(slot, new_state, old_state); super.setChanged(slot, new_state, old_state);
/*old_state.getCapability(MatteryCapability.DRIVE).ifPresent((drive) -> { old_state.getCapability(MatteryCapability.DRIVE).ifPresent((drive) -> {
drive.removeListenerAuto(cell.computeIfAbsent(ItemStackObject.class, StorageItemView::new)); cell.computeIfAbsent(drive.storageIdentity(), VirtualComponent::new).remove(drive);
}); });
new_state.getCapability(MatteryCapability.DRIVE).ifPresent((drive) -> { new_state.getCapability(MatteryCapability.DRIVE).ifPresent((drive) -> {
drive.addListenerAuto(cell.computeIfAbsent(ItemStackObject.class, StorageItemView::new)); cell.computeIfAbsent(drive.storageIdentity(), VirtualComponent::new).add(drive);
});*/ });
old_state.getCapability(MatteryCapability.DRIVE).ifPresent(cell::removeStorageComponent);
new_state.getCapability(MatteryCapability.DRIVE).ifPresent(cell::addStorageComponent);
} }
}; };

View File

@ -12,6 +12,7 @@ import javax.annotation.ParametersAreNonnullByDefault;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.function.Function;
import java.util.function.Supplier; import java.util.function.Supplier;
@MethodsReturnNonnullByDefault @MethodsReturnNonnullByDefault
@ -26,6 +27,7 @@ public class AbstractStorageGridCell implements IStorageGridCell {
return Collections.unmodifiableList(components); return Collections.unmodifiableList(components);
} }
@SuppressWarnings("unchecked")
public <T extends IStorageStack, U extends IStorageIdentity<T>> U computeIfAbsent(Class<T> identity, Supplier<U> provider) { public <T extends IStorageStack, U extends IStorageIdentity<T>> U computeIfAbsent(Class<T> identity, Supplier<U> provider) {
for (var component : components) { for (var component : components) {
if (component.storageIdentity() == identity) { if (component.storageIdentity() == identity) {
@ -38,6 +40,19 @@ public class AbstractStorageGridCell implements IStorageGridCell {
return factory; return factory;
} }
@SuppressWarnings("unchecked")
public <T extends IStorageStack, U extends IStorageIdentity<T>> U computeIfAbsent(Class<T> identity, Function<Class<T>, U> provider) {
for (var component : components) {
if (component.storageIdentity() == identity) {
return (U) component;
}
}
var factory = provider.apply(identity);
addStorageComponent(factory);
return factory;
}
public void addStorageComponent(IStorageIdentity<?> component) { public void addStorageComponent(IStorageIdentity<?> component) {
for (var component1 : components) { for (var component1 : components) {
if (component == component1 || component1.storageIdentity() == component.storageIdentity()) { if (component == component1 || component1.storageIdentity() == component.storageIdentity()) {
@ -67,6 +82,7 @@ public class AbstractStorageGridCell implements IStorageGridCell {
valid = false; valid = false;
resolver.invalidate(); resolver.invalidate();
// detachStorage();
} }
public void revive() { public void revive() {
@ -75,6 +91,7 @@ public class AbstractStorageGridCell implements IStorageGridCell {
valid = true; valid = true;
resolver = LazyOptional.of(() -> this); resolver = LazyOptional.of(() -> this);
// attachStorage();
} }
public LazyOptional<IStorageGridCell> get() { public LazyOptional<IStorageGridCell> get() {
@ -90,7 +107,7 @@ public class AbstractStorageGridCell implements IStorageGridCell {
} }
@Override @Override
public void setStorageGrid(StorageGrid grid) { public void setStorageGrid(@Nullable StorageGrid grid) {
storage_grid = grid; storage_grid = grid;
} }