Skip to content

Commit

Permalink
Fix the QE's energy tab not updating while in the GUI
Browse files Browse the repository at this point in the history
  • Loading branch information
pupnewfster committed Mar 8, 2024
1 parent 2ed429e commit 5c125a6
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 18 deletions.
Expand Up @@ -214,7 +214,7 @@ protected void dropSelfWithContents(Collection<? extends Holder<Block>> blockPro
CopyContainersLootFunction.Builder containerBuilder = CopyContainersLootFunction.builder();
for (ContainerType<?, ?, ?> type : ContainerType.TYPES) {
AttachedContainers<?> attachment = type.getAttachment(stack);
List<?> containers = tileEntity.handles(type) ? type.getContainers(tileEntity) : List.of();
List<?> containers = tileEntity.persists(type) ? type.getContainers(tileEntity) : List.of();
List<?> attachmentContainers = attachment == null ? List.of() : attachment.getContainers();
if (containers.size() == attachmentContainers.size()) {
if (!containers.isEmpty()) {
Expand Down
Expand Up @@ -32,10 +32,10 @@ public boolean canBeMaster() {
}

@Override
public boolean handles(ContainerType<?, ?, ?> type) {
public boolean persists(ContainerType<?, ?, ?> type) {
if (type == ContainerType.GAS || type == ContainerType.FLUID || type == ContainerType.HEAT) {
return false;
}
return super.handles(type);
return super.persists(type);
}
}
Expand Up @@ -3,15 +3,20 @@
import java.util.Collections;
import java.util.List;
import mekanism.api.energy.IEnergyContainer;
import mekanism.common.capabilities.energy.BasicEnergyContainer;
import mekanism.common.capabilities.holder.QuantumEntangloporterConfigHolder;
import mekanism.common.config.MekanismConfig;
import mekanism.common.lib.transmitter.TransmissionType;
import mekanism.common.tile.TileEntityQuantumEntangloporter;
import net.minecraft.core.Direction;
import net.neoforged.neoforge.common.util.Lazy;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class QuantumEntangloporterEnergyContainerHolder extends QuantumEntangloporterConfigHolder<IEnergyContainer> implements IEnergyContainerHolder {

private final Lazy<List<IEnergyContainer>> clientContainer = Lazy.of(() -> Collections.singletonList(BasicEnergyContainer.create(MekanismConfig.general.entangloporterEnergyBuffer.get(), null)));

public QuantumEntangloporterEnergyContainerHolder(TileEntityQuantumEntangloporter entangloporter) {
super(entangloporter);
}
Expand All @@ -24,6 +29,11 @@ protected TransmissionType getTransmissionType() {
@NotNull
@Override
public List<IEnergyContainer> getEnergyContainers(@Nullable Direction side) {
return entangloporter.hasFrequency() ? entangloporter.getFreq().getEnergyContainers(side) : Collections.emptyList();
if (entangloporter.hasFrequency()) {
return entangloporter.getFreq().getEnergyContainers(side);
} else if (entangloporter.isRemote()) {
return clientContainer.get();
}
return Collections.emptyList();
}
}
Expand Up @@ -19,12 +19,14 @@
import mekanism.api.chemical.slurry.ISlurryTank;
import mekanism.api.chemical.slurry.Slurry;
import mekanism.api.chemical.slurry.SlurryStack;
import mekanism.api.energy.IEnergyContainer;
import mekanism.api.fluid.IExtendedFluidTank;
import mekanism.api.heat.HeatAPI.HeatTransfer;
import mekanism.api.heat.IHeatCapacitor;
import mekanism.api.heat.IHeatHandler;
import mekanism.common.attachments.containers.ContainerType;
import mekanism.api.math.FloatingLong;
import mekanism.api.security.SecurityMode;
import mekanism.common.attachments.containers.ContainerType;
import mekanism.common.capabilities.Capabilities;
import mekanism.common.capabilities.heat.CachedAmbientTemperature;
import mekanism.common.capabilities.holder.chemical.IChemicalTankHolder;
Expand All @@ -45,6 +47,7 @@
import mekanism.common.integration.computer.annotation.WrappingComputerMethod;
import mekanism.common.inventory.container.MekanismContainer;
import mekanism.common.inventory.container.sync.SyncableDouble;
import mekanism.common.inventory.container.sync.SyncableFloatingLong;
import mekanism.common.lib.chunkloading.IChunkLoader;
import mekanism.common.lib.frequency.Frequency;
import mekanism.common.lib.frequency.Frequency.FrequencyIdentity;
Expand Down Expand Up @@ -249,6 +252,17 @@ public void addContainerTrackers(MekanismContainer container) {
super.addContainerTrackers(container);
container.track(SyncableDouble.create(this::getLastTransferLoss, value -> lastTransferLoss = value));
container.track(SyncableDouble.create(this::getLastEnvironmentLoss, value -> lastEnvironmentLoss = value));
//Note: We have to manually sync the energy container as we don't sync it in super and don't even always have one
trackLastEnergy(container);
container.track(SyncableFloatingLong.create(() -> {
List<IEnergyContainer> energyContainers = getEnergyContainers(null);
return energyContainers.isEmpty() ? FloatingLong.ZERO : energyContainers.get(0).getEnergy();
}, energy -> {
List<IEnergyContainer> energyContainers = getEnergyContainers(null);
if (!energyContainers.isEmpty()) {
energyContainers.get(0).setEnergy(energy);
}
}));
}

//Methods relating to IComputerTile
Expand Down
30 changes: 17 additions & 13 deletions src/main/java/mekanism/common/tile/base/TileEntityMekanism.java
Expand Up @@ -331,16 +331,16 @@ public ResourceLocation getBlockTypeRegistryName() {
}

/**
* Should data related to the given type be persisted in this tile save
* Should data related to the given type be persisted in this tile save and transferred to the item
*/
public boolean persists(ContainerType<?, ?, ?> type) {
return type.canHandle(this);
}

/**
* Should data related to the given type be saved to the item and synced to the client in the GUI
* Should data related to the given type be synced to the client in the GUI
*/
public boolean handles(ContainerType<?, ?, ?> type) {
public boolean syncs(ContainerType<?, ?, ?> type) {
return persists(type);
}

Expand Down Expand Up @@ -755,7 +755,7 @@ public void readFromStack(ItemStack stack) {
stack.getExistingData(MekanismAttachmentTypes.UPGRADES).ifPresent(storedUpgrades -> storedUpgrades.copyTo(getComponent()));
}
for (ContainerType<?, ?, ?> type : ContainerType.TYPES) {
if (handles(type)) {
if (persists(type)) {
type.copyFrom(stack, this);
}
}
Expand Down Expand Up @@ -798,7 +798,7 @@ public void writeToStack(ItemStack stack) {
stack.setData(MekanismAttachmentTypes.REDSTONE_CONTROL, controlType);
}
for (ContainerType<?, ?, ?> type : ContainerType.TYPES) {
if (handles(type)) {
if (persists(type)) {
type.copyTo(this, stack);
}
}
Expand All @@ -816,37 +816,37 @@ public void addContainerTrackers(MekanismContainer container) {
container.track(SyncableEnum.create(RedstoneControl::byIndexStatic, RedstoneControl.DISABLED, () -> controlType, value -> controlType = value));
}
boolean isClient = isRemote();
if (canHandleGas() && handles(ContainerType.GAS)) {
if (canHandleGas() && syncs(ContainerType.GAS)) {
List<IGasTank> gasTanks = getGasTanks(null);
for (IGasTank gasTank : gasTanks) {
container.track(SyncableGasStack.create(gasTank, isClient));
}
}
if (canHandleInfusion() && handles(ContainerType.INFUSION)) {
if (canHandleInfusion() && syncs(ContainerType.INFUSION)) {
List<IInfusionTank> infusionTanks = getInfusionTanks(null);
for (IInfusionTank infusionTank : infusionTanks) {
container.track(SyncableInfusionStack.create(infusionTank, isClient));
}
}
if (canHandlePigment() && handles(ContainerType.PIGMENT)) {
if (canHandlePigment() && syncs(ContainerType.PIGMENT)) {
List<IPigmentTank> pigmentTanks = getPigmentTanks(null);
for (IPigmentTank pigmentTank : pigmentTanks) {
container.track(SyncablePigmentStack.create(pigmentTank, isClient));
}
}
if (canHandleSlurry() && handles(ContainerType.SLURRY)) {
if (canHandleSlurry() && syncs(ContainerType.SLURRY)) {
List<ISlurryTank> slurryTanks = getSlurryTanks(null);
for (ISlurryTank slurryTank : slurryTanks) {
container.track(SyncableSlurryStack.create(slurryTank, isClient));
}
}
if (canHandleFluid() && handles(ContainerType.FLUID)) {
if (canHandleFluid() && syncs(ContainerType.FLUID)) {
List<IExtendedFluidTank> fluidTanks = getFluidTanks(null);
for (IExtendedFluidTank fluidTank : fluidTanks) {
container.track(SyncableFluidStack.create(fluidTank, isClient));
}
}
if (canHandleHeat() && handles(ContainerType.HEAT)) {
if (canHandleHeat() && syncs(ContainerType.HEAT)) {
List<IHeatCapacitor> heatCapacitors = getHeatCapacitors(null);
for (IHeatCapacitor capacitor : heatCapacitors) {
container.track(SyncableDouble.create(capacitor::getHeat, capacitor::setHeat));
Expand All @@ -855,8 +855,8 @@ public void addContainerTrackers(MekanismContainer container) {
}
}
}
if (canHandleEnergy() && handles(ContainerType.ENERGY)) {
container.track(SyncableFloatingLong.create(lastEnergyTracker::getLastEnergyReceived, lastEnergyTracker::setLastEnergyReceived));
if (canHandleEnergy() && syncs(ContainerType.ENERGY)) {
trackLastEnergy(container);
List<IEnergyContainer> energyContainers = getEnergyContainers(null);
for (IEnergyContainer energyContainer : energyContainers) {
container.track(SyncableFloatingLong.create(energyContainer::getEnergy, energyContainer::setEnergy));
Expand All @@ -870,6 +870,10 @@ public void addContainerTrackers(MekanismContainer container) {
}
}

protected void trackLastEnergy(MekanismContainer container) {
container.track(SyncableFloatingLong.create(lastEnergyTracker::getLastEnergyReceived, lastEnergyTracker::setLastEnergyReceived));
}

@NotNull
@Override
public CompoundTag getReducedUpdateTag() {
Expand Down

0 comments on commit 5c125a6

Please sign in to comment.