-
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: losing energy when using wrench dismantling
- Loading branch information
1 parent
a1cc6d2
commit 3c6874b
Showing
12 changed files
with
126 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
...om/refinedmods/refinedstorage2/platform/common/support/energy/ItemBlockEnergyStorage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters