Skip to content

Commit

Permalink
fix: losing energy when using wrench dismantling
Browse files Browse the repository at this point in the history
  • Loading branch information
raoulvdberge committed Feb 19, 2024
1 parent a1cc6d2 commit 3c6874b
Show file tree
Hide file tree
Showing 12 changed files with 126 additions and 41 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
### Fixed

- Fixed losing disk when using Wrench dismantling on the Portable Grid.
- Fixed losing energy when using Wrench dismantling on the Portable Grid and the Controller.

## [2.0.0-milestone.3.3] - 2024-02-17

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.entity.BlockEntityType;
import org.apiguardian.api.API;

@API(status = API.Status.STABLE, since = "2.0.0-milestone.1.0")
Expand Down Expand Up @@ -165,6 +166,12 @@ GridScrollingStrategy createGridScrollingStrategy(AbstractContainerMenu containe

EnergyStorage asItemEnergyStorage(EnergyStorage energyStorage, ItemStack stack);

EnergyStorage asBlockItemEnergyStorage(
EnergyStorage energyStorage,
ItemStack stack,
BlockEntityType<?> blockEntityType
);

NetworkBoundItemHelper getNetworkBoundItemHelper();

PlatformRegistry<SlotReferenceFactory> getSlotReferenceFactoryRegistry();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.entity.BlockEntityType;

public class PlatformApiProxy implements PlatformApi {
@Nullable
Expand Down Expand Up @@ -324,6 +325,13 @@ public EnergyStorage asItemEnergyStorage(final EnergyStorage energyStorage, fina
return ensureLoaded().asItemEnergyStorage(energyStorage, stack);
}

@Override
public EnergyStorage asBlockItemEnergyStorage(final EnergyStorage energyStorage,
final ItemStack stack,
final BlockEntityType<?> blockEntityType) {
return ensureLoaded().asBlockItemEnergyStorage(energyStorage, stack, blockEntityType);
}

@Override
public NetworkBoundItemHelper getNetworkBoundItemHelper() {
return ensureLoaded().getNetworkBoundItemHelper();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
import com.refinedmods.refinedstorage2.platform.common.storagemonitor.CompositeStorageMonitorExtractionStrategy;
import com.refinedmods.refinedstorage2.platform.common.storagemonitor.CompositeStorageMonitorInsertionStrategy;
import com.refinedmods.refinedstorage2.platform.common.support.energy.EnergyItemHelperImpl;
import com.refinedmods.refinedstorage2.platform.common.support.energy.ItemBlockEnergyStorage;
import com.refinedmods.refinedstorage2.platform.common.support.energy.ItemEnergyStorage;
import com.refinedmods.refinedstorage2.platform.common.support.network.ConnectionProviderImpl;
import com.refinedmods.refinedstorage2.platform.common.support.network.bounditem.CompositeSlotReferenceProvider;
Expand Down Expand Up @@ -101,6 +102,7 @@
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraft.world.level.saveddata.SavedData;

import static com.refinedmods.refinedstorage2.platform.common.util.IdentifierUtil.createIdentifier;
Expand Down Expand Up @@ -468,6 +470,13 @@ public EnergyStorage asItemEnergyStorage(final EnergyStorage energyStorage,
return new ItemEnergyStorage(stack, energyStorage);
}

@Override
public EnergyStorage asBlockItemEnergyStorage(final EnergyStorage energyStorage,
final ItemStack stack,
final BlockEntityType<?> blockEntityType) {
return new ItemBlockEnergyStorage(energyStorage, stack, blockEntityType);
}

@Override
public NetworkBoundItemHelper getNetworkBoundItemHelper() {
return networkBoundItemHelper;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.refinedmods.refinedstorage2.platform.common.controller;

import com.refinedmods.refinedstorage2.api.core.Action;
import com.refinedmods.refinedstorage2.api.network.energy.EnergyStorage;
import com.refinedmods.refinedstorage2.api.network.impl.node.controller.ControllerNetworkNode;
import com.refinedmods.refinedstorage2.platform.api.support.energy.TransferableBlockEntityEnergy;
Expand All @@ -10,6 +9,7 @@
import com.refinedmods.refinedstorage2.platform.common.support.containermenu.ExtendedMenuProvider;
import com.refinedmods.refinedstorage2.platform.common.support.energy.BlockEntityEnergyStorage;
import com.refinedmods.refinedstorage2.platform.common.support.energy.CreativeEnergyStorage;
import com.refinedmods.refinedstorage2.platform.common.support.energy.ItemBlockEnergyStorage;
import com.refinedmods.refinedstorage2.platform.common.support.network.AbstractRedstoneModeNetworkNodeContainerBlockEntity;

import com.google.common.util.concurrent.RateLimiter;
Expand All @@ -31,7 +31,6 @@ public class ControllerBlockEntity extends AbstractRedstoneModeNetworkNodeContai
implements ExtendedMenuProvider, TransferableBlockEntityEnergy {
private static final Logger LOGGER = LoggerFactory.getLogger(ControllerBlockEntity.class);

private static final String TAG_STORED = "stored";
private static final String TAG_CAPACITY = "capacity";

private final ControllerType type;
Expand Down Expand Up @@ -78,7 +77,7 @@ public void updateEnergyTypeInLevel(final BlockState state) {
@Override
public void saveAdditional(final CompoundTag tag) {
super.saveAdditional(tag);
tag.putLong(TAG_STORED, getNode().getActualStored());
ItemBlockEnergyStorage.writeToTag(tag, getNode().getActualStored());
saveRenderingInfo(tag);
}

Expand All @@ -89,9 +88,7 @@ private void saveRenderingInfo(final CompoundTag tag) {
@Override
public void load(final CompoundTag tag) {
super.load(tag);
if (tag.contains(TAG_STORED)) {
energyStorage.receive(tag.getLong(TAG_STORED), Action.EXECUTE);
}
ItemBlockEnergyStorage.readFromTag(energyStorage, tag);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.refinedmods.refinedstorage2.platform.api.support.HelpTooltipComponent;
import com.refinedmods.refinedstorage2.platform.api.support.energy.AbstractEnergyBlockItem;
import com.refinedmods.refinedstorage2.platform.common.Platform;
import com.refinedmods.refinedstorage2.platform.common.content.BlockEntities;

import java.util.Optional;

Expand Down Expand Up @@ -44,6 +45,10 @@ public EnergyStorage createEnergyStorage(final ItemStack stack) {
final EnergyStorage energyStorage = new EnergyStorageImpl(
Platform.INSTANCE.getConfig().getController().getEnergyCapacity()
);
return PlatformApi.INSTANCE.asItemEnergyStorage(energyStorage, stack);
return PlatformApi.INSTANCE.asBlockItemEnergyStorage(
energyStorage,
stack,
BlockEntities.INSTANCE.getController()
);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.refinedmods.refinedstorage2.platform.common.storage.portablegrid;

import com.refinedmods.refinedstorage2.api.core.Action;
import com.refinedmods.refinedstorage2.api.network.energy.EnergyStorage;
import com.refinedmods.refinedstorage2.platform.api.PlatformApi;
import com.refinedmods.refinedstorage2.platform.api.configurationcard.ConfigurationCardTarget;
Expand All @@ -18,6 +17,7 @@
import com.refinedmods.refinedstorage2.platform.common.support.containermenu.ExtendedMenuProvider;
import com.refinedmods.refinedstorage2.platform.common.support.energy.BlockEntityEnergyStorage;
import com.refinedmods.refinedstorage2.platform.common.support.energy.CreativeEnergyStorage;
import com.refinedmods.refinedstorage2.platform.common.support.energy.ItemBlockEnergyStorage;
import com.refinedmods.refinedstorage2.platform.common.util.ContainerUtil;

import javax.annotation.Nullable;
Expand Down Expand Up @@ -48,7 +48,6 @@ public abstract class AbstractPortableGridBlockEntity extends BlockEntity implem

private static final String TAG_DISK_INVENTORY = "inv";
private static final String TAG_DISKS = "disks";
private static final String TAG_STORED = "stored";
private static final String TAG_REDSTONE_MODE = "rm";

@Nullable
Expand Down Expand Up @@ -156,9 +155,7 @@ private void initialize(final Level level) {
public void load(final CompoundTag tag) {
fromClientTag(tag);
readDiskInventory(tag, diskInventory);
if (tag.contains(TAG_STORED)) {
energyStorage.receive(tag.getLong(TAG_STORED), Action.EXECUTE);
}
ItemBlockEnergyStorage.readFromTag(energyStorage, tag);
readConfiguration(tag);
super.load(tag);
}
Expand Down Expand Up @@ -186,7 +183,7 @@ protected void onClientDriveStateUpdated() {
public void saveAdditional(final CompoundTag tag) {
super.saveAdditional(tag);
writeDiskInventory(tag, diskInventory);
tag.putLong(TAG_STORED, energyStorage.getStored());
ItemBlockEnergyStorage.writeToTag(tag, energyStorage.getStored());
writeConfiguration(tag);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import com.refinedmods.refinedstorage2.platform.common.storage.Disk;
import com.refinedmods.refinedstorage2.platform.common.storage.DiskInventory;
import com.refinedmods.refinedstorage2.platform.common.support.energy.CreativeEnergyStorage;
import com.refinedmods.refinedstorage2.platform.common.support.energy.ItemEnergyStorage;

import javax.annotation.Nullable;

Expand All @@ -41,7 +40,7 @@ public PortableGridBlockItem(final Block block, final PortableGridType type) {
public static PortableGridBlockItemRenderInfo getRenderInfo(final ItemStack stack,
@Nullable final Level level) {
final boolean creative = isCreative(stack);
final boolean hasEnergy = creative || ItemEnergyStorage.getStored(stack) > 0;
final boolean hasEnergy = creative || createEnergyStorage(stack).getStored() > 0;
final ItemStack diskStack = getDisk(stack);
final boolean active = hasEnergy && !diskStack.isEmpty();
final Disk disk = new Disk(
Expand Down Expand Up @@ -91,11 +90,15 @@ static void setDiskInventory(final ItemStack stack, final DiskInventory diskInve
);
}

public EnergyStorage createEnergyStorage(final ItemStack stack) {
public static EnergyStorage createEnergyStorage(final ItemStack stack) {
final EnergyStorage energyStorage = new EnergyStorageImpl(
Platform.INSTANCE.getConfig().getPortableGrid().getEnergyCapacity()
);
return PlatformApi.INSTANCE.asItemEnergyStorage(energyStorage, stack);
return PlatformApi.INSTANCE.asBlockItemEnergyStorage(
energyStorage,
stack,
BlockEntities.INSTANCE.getPortableGrid()
);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package com.refinedmods.refinedstorage2.platform.common.support.energy;

import com.refinedmods.refinedstorage2.api.core.Action;
import com.refinedmods.refinedstorage2.api.network.energy.EnergyStorage;
import com.refinedmods.refinedstorage2.api.network.impl.energy.AbstractProxyEnergyStorage;

import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.item.BlockItem;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.block.entity.BlockEntityType;

public class ItemBlockEnergyStorage extends AbstractProxyEnergyStorage {
private static final String TAG_STORED = "stored";

private final ItemStack stack;
private final BlockEntityType<?> blockEntityType;

public ItemBlockEnergyStorage(final EnergyStorage energyStorage,
final ItemStack stack,
final BlockEntityType<?> blockEntityType) {
super(energyStorage);
this.stack = stack;
this.blockEntityType = blockEntityType;
final CompoundTag tag = BlockItem.getBlockEntityData(stack);
if (tag != null) {
readFromTag(energyStorage, tag);
}
}

@Override
public long receive(final long amount, final Action action) {
final long received = super.receive(amount, action);
if (received > 0 && action == Action.EXECUTE) {
updateStored();
}
return received;
}

@Override
public long extract(final long amount, final Action action) {
final long extracted = super.extract(amount, action);
if (extracted > 0 && action == Action.EXECUTE) {
updateStored();
}
return extracted;
}

private void updateStored() {
CompoundTag tag = BlockItem.getBlockEntityData(stack);
if (tag == null) {
tag = new CompoundTag();
}
writeToTag(tag, getStored());
BlockItem.setBlockEntityData(stack, blockEntityType, tag);
}

public static void writeToTag(final CompoundTag tag, final long stored) {
tag.putLong(TAG_STORED, stored);
}

public static void readFromTag(final EnergyStorage energyStorage, final CompoundTag tag) {
final long stored = tag.getLong(TAG_STORED);
energyStorage.receive(stored, Action.EXECUTE);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,50 +2,43 @@

import com.refinedmods.refinedstorage2.api.core.Action;
import com.refinedmods.refinedstorage2.api.network.energy.EnergyStorage;
import com.refinedmods.refinedstorage2.api.network.impl.energy.AbstractProxyEnergyStorage;

import net.minecraft.world.item.ItemStack;

public class ItemEnergyStorage implements EnergyStorage {
public class ItemEnergyStorage extends AbstractProxyEnergyStorage {
private static final String TAG_STORED = "stored";

private final ItemStack stack;
private final EnergyStorage delegate;

public ItemEnergyStorage(final ItemStack stack, final EnergyStorage delegate) {
super(delegate);
this.stack = stack;
this.delegate = delegate;
delegate.receive(getStored(stack), Action.EXECUTE);
}

public static long getStored(final ItemStack stack) {
return stack.getTag() != null ? stack.getTag().getLong(TAG_STORED) : 0;
}

@Override
public long getStored() {
return delegate.getStored();
}

@Override
public long getCapacity() {
return delegate.getCapacity();
final long stored = stack.getTag() != null ? stack.getTag().getLong(TAG_STORED) : 0;
if (stored > 0) {
delegate.receive(stored, Action.EXECUTE);
}
}

@Override
public long receive(final long amount, final Action action) {
final long received = delegate.receive(amount, action);
final long received = super.receive(amount, action);
if (received > 0 && action == Action.EXECUTE) {
stack.getOrCreateTag().putLong(TAG_STORED, getStored());
updateStored();
}
return received;
}

@Override
public long extract(final long amount, final Action action) {
final long extracted = delegate.extract(amount, action);
final long extracted = super.extract(amount, action);
if (extracted > 0 && action == Action.EXECUTE) {
stack.getOrCreateTag().putLong(TAG_STORED, getStored());
updateStored();
}
return extracted;
}

private void updateStored() {
stack.getOrCreateTag().putLong(TAG_STORED, getStored());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ private void registerEnergyItemProviders() {
controller.get()
));
EnergyStorage.ITEM.registerForItems(
(stack, context) -> new EnergyStorageAdapter(Items.INSTANCE.getPortableGrid().createEnergyStorage(stack)),
(stack, context) -> new EnergyStorageAdapter(PortableGridBlockItem.createEnergyStorage(stack)),
Items.INSTANCE.getPortableGrid()
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ private void registerCapabilities(final RegisterCapabilitiesEvent event) {
));
event.registerItem(
Capabilities.EnergyStorage.ITEM,
(stack, ctx) -> new EnergyStorageAdapter(Items.INSTANCE.getPortableGrid().createEnergyStorage(stack)),
(stack, ctx) -> new EnergyStorageAdapter(PortableGridBlockItem.createEnergyStorage(stack)),
Items.INSTANCE.getPortableGrid()
);
}
Expand Down

0 comments on commit 3c6874b

Please sign in to comment.