Skip to content

Commit

Permalink
add energy container to quantum storage controller
Browse files Browse the repository at this point in the history
fix issue with checking the same pos twice
  • Loading branch information
ghzdude committed Dec 21, 2023
1 parent 70d9e5a commit 8a96ecd
Showing 1 changed file with 74 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
package gregtech.common.metatileentities.storage;

import gregtech.api.capability.GregtechCapabilities;
import gregtech.api.capability.IDualHandler;
import gregtech.api.capability.IEnergyContainer;
import gregtech.api.capability.IQuantumController;
import gregtech.api.capability.IQuantumStorage;
import gregtech.api.capability.impl.EnergyContainerHandler;
import gregtech.api.capability.impl.FluidTankList;
import gregtech.api.capability.impl.ItemHandlerList;
import gregtech.api.gui.ModularUI;
import gregtech.api.metatileentity.MetaTileEntity;
import gregtech.api.metatileentity.interfaces.IGregTechTileEntity;
import gregtech.api.util.GTUtility;
import gregtech.client.renderer.texture.Textures;

import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import net.minecraft.client.resources.I18n;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
Expand All @@ -40,34 +41,56 @@
import org.jetbrains.annotations.Nullable;

import java.lang.ref.WeakReference;
import java.util.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.Set;

public class MetaTileEntityQuantumStorageController extends MetaTileEntity implements IQuantumController {

private static final int MAX_DISTANCE_RADIUS = 16;

// somewhat lazily initialized, make sure to call checkStoragePos() before trying to access anything in this
private IEnergyContainer energyContainer;
/** Somewhat lazily initialized, make sure to call {@code getStorage()} before trying to access anything in this */
private Map<BlockPos, WeakReference<IQuantumStorage<?>>> storageInstances = new HashMap<>();

// the "definitive" set of positions of storage instances
/** The "definitive" set of positions of storage instances */
private Set<BlockPos> storagePositions = new HashSet<>();

private long energyConsumption = 0;
private final QuantumControllerHandler handler = new QuantumControllerHandler();

private boolean isDead = false;

public MetaTileEntityQuantumStorageController(ResourceLocation metaTileEntityId) {
super(metaTileEntityId);
reinitializeEnergyContainer();
}

@Override
public MetaTileEntity createMetaTileEntity(IGregTechTileEntity tileEntity) {
return new MetaTileEntityQuantumStorageController(metaTileEntityId);
}

@Override
public void update() {
if (isPowered()) {
energyContainer.removeEnergy(energyConsumption);
}
super.update();
}

public boolean isPowered() {
return energyContainer.getEnergyStored() >= energyConsumption;
}

@Override
public void renderMetaTileEntity(CCRenderState renderState, Matrix4 translation, IVertexOperation[] pipeline) {
for (EnumFacing facing : EnumFacing.VALUES) {
//todo add inactive texture for unpowered state
if (facing == getFrontFacing()) {
Textures.QUANTUM_CONTROLLER_FRONT.renderSided(facing, renderState, translation, pipeline);
Textures.QUANTUM_CHEST_OVERLAY.renderSided(facing, renderState, translation, pipeline);
Expand All @@ -87,11 +110,7 @@ protected boolean openGUIOnRightClick() {
return false;
}

@Override
protected ModularUI createUI(EntityPlayer entityPlayer) {
return null;
}

@SuppressWarnings("SameParameterValue")
private IQuantumStorage<?> getStorage(BlockPos pos, boolean rebuild) {
if (getWorld().isRemote) return null;
if (storageInstances.containsKey(pos)) {
Expand Down Expand Up @@ -154,6 +173,7 @@ public void rebuildNetwork() {

storageInstances = new HashMap<>();
storagePositions = new HashSet<>();
energyConsumption = 0;

Queue<BlockPos> searchQueue = new LinkedList<>();
Set<BlockPos> checked = new HashSet<>();
Expand All @@ -166,6 +186,8 @@ public void rebuildNetwork() {
// while there are blocks to search
while (!searchQueue.isEmpty()) {
BlockPos pos = searchQueue.remove();

if (checked.contains(pos)) continue;
checked.add(pos);

if (!isInRange(pos) || !getWorld().isBlockLoaded(pos, false)) continue;
Expand All @@ -184,14 +206,20 @@ public void rebuildNetwork() {
oldInstances.remove(pos);
oldPositions.remove(pos);

energyConsumption += switch (storage.getType()) {
case ITEM, FLUID -> 8L;
case PROXY -> 2L;
case EXTENDER -> 1L;
default -> 0L;
};

// check against already check posses so we don't recheck a checked pos
for (EnumFacing facing : EnumFacing.VALUES) {
BlockPos offsetPos = pos.offset(facing);
if (!checked.contains(offsetPos) && !getPos().equals(offsetPos)) {
if (checked.contains(offsetPos) || getPos().equals(offsetPos)) continue;

// add a new pos to search
searchQueue.add(offsetPos);
}
// add a new pos to search
searchQueue.add(offsetPos);
}
}

Expand All @@ -216,6 +244,16 @@ public void rebuildNetwork() {
if (storage != null) storage.setDisconnected();
}
handler.rebuildCache();
markDirty();
}

private void reinitializeEnergyContainer() {
long stored = energyContainer == null ? 0L : energyContainer.getEnergyStored();

energyContainer = EnergyContainerHandler.receiverContainer(this, this.energyConsumption * 16L,
Integer.MAX_VALUE, Integer.MAX_VALUE);

if (stored > 0) energyContainer.addEnergy(stored);
}

@Override
Expand All @@ -226,6 +264,7 @@ public NBTTagCompound writeToNBT(NBTTagCompound data) {
list.appendTag(new NBTTagLong(pos.toLong()));
}
tagCompound.setTag("StorageInstances", list);
tagCompound.setLong("EnergyConsumption", energyConsumption);
return tagCompound;
}

Expand All @@ -236,16 +275,23 @@ public void readFromNBT(NBTTagCompound data) {
for (int i = 0; i < list.tagCount(); i++) {
storagePositions.add(BlockPos.fromLong(((NBTTagLong) list.get(i)).getLong()));
}
energyConsumption = data.getLong("EnergyConsumption");
}

@SuppressWarnings("unchecked")
@Override
public <T> T getCapability(Capability<T> capability, EnumFacing side) {
if (capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY && handler.hasItemHandlers()) {
return (T) handler;
} else if (capability == CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY && handler.hasFluidTanks()) {
return (T) handler;
public <T> T getCapability(@NotNull Capability<T> capability, EnumFacing side) {
if (isPowered()) {
if (capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY && handler.hasItemHandlers()) {
return (T) handler;
} else if (capability == CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY && handler.hasFluidTanks()) {
return (T) handler;
}
}

// do not allow the controller to accept energy from other blocks
if (capability == GregtechCapabilities.CAPABILITY_ENERGY_CONTAINER && side != null) return null;

return super.getCapability(capability, side);
}

Expand All @@ -260,6 +306,11 @@ public IDualHandler getHandler() {
return this.handler;
}

@Override
public IEnergyContainer getEnergyContainer() {
return this.energyContainer;
}

private class QuantumControllerHandler implements IDualHandler {

// IFluidHandler saved values
Expand Down Expand Up @@ -302,7 +353,7 @@ public boolean hasItemHandlers() {

// IFluidHandler

private FluidTankList getFluidTanks() {
protected FluidTankList getFluidTanks() {
if (fluidTanks == null) {
rebuildCache();
}
Expand Down Expand Up @@ -333,7 +384,7 @@ public FluidStack drain(int maxDrain, boolean doFill) {

// IItemHandler

private ItemHandlerList getItemHandlers() {
protected ItemHandlerList getItemHandlers() {
if (itemHandlers == null) {
rebuildCache();
}
Expand All @@ -342,7 +393,7 @@ private ItemHandlerList getItemHandlers() {

@Override
public int getSlots() {
return itemHandlers.getSlots();
return getItemHandlers().getSlots();
}

@NotNull
Expand Down

0 comments on commit 8a96ecd

Please sign in to comment.