Skip to content

Commit

Permalink
feat: guard against use of storages linked to invalidated block entities
Browse files Browse the repository at this point in the history
  • Loading branch information
marcus8448 committed Aug 21, 2024
1 parent 5362b8c commit fd19d2e
Show file tree
Hide file tree
Showing 20 changed files with 87 additions and 39 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
uses: actions/checkout@v4

- name: Validate Gradle wrapper
uses: gradle/actions/wrapper-validation@v3
uses: gradle/actions/wrapper-validation@v4

- name: Setup JDK 21
uses: actions/setup-java@v4
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/commit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
uses: actions/checkout@v4

- name: Validate Gradle wrapper
uses: gradle/actions/wrapper-validation@v3
uses: gradle/actions/wrapper-validation@v4

- name: Setup JDK 21
uses: actions/setup-java@v4
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/pr_check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
uses: actions/checkout@v4

- name: Validate Gradle wrapper
uses: gradle/actions/wrapper-validation@v3
uses: gradle/actions/wrapper-validation@v4

- name: Setup JDK 21
uses: actions/setup-java@v4
Expand Down
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ plugins {
id("fabric-loom") version("1.7-SNAPSHOT")
id("org.cadixdev.licenser") version("0.6.1")
id("org.ajoberstar.grgit") version("5.2.2")
id("dev.galacticraft.mojarn") version("0.4.0+9")
id("dev.galacticraft.mojarn") version("0.4.0+10")
}

group = "dev.galacticraft"
Expand Down
14 changes: 7 additions & 7 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@ mod.name=MachineLib
mod.version=0.7.0

# Minecraft and Fabric Loader
minecraft.version=1.21
minecraft.version=1.21.1
loader.version=0.15.11
yarn.build=8
yarn.build=3

# Mod Dependencies
badpackets.version=0.8.1
energy.version=4.1.0
fabric.version=0.100.6+1.21
fabric.version=0.102.1+1.21.1

# Optional Mod Dependencies
cloth.config.version=15.0.127
cloth.config.version=15.0.130
modmenu.version=11.0.1
rei.version=16.0.729
architectury.version=13.0.3
wthit.version=12.2.2
rei.version=16.0.754
architectury.version=13.0.6
wthit.version=12.3.0
Binary file modified gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionSha256Sum=a4b4158601f8636cdeeab09bd76afb640030bb5b144aafe261a5e8af027dc612
distributionUrl=https\://services.gradle.org/distributions/gradle-8.8-bin.zip
distributionSha256Sum=5b9c5eb3f9fc2c94abaea57d90bd78747ca117ddbbf96c859d3741181a12bf2a
distributionUrl=https\://services.gradle.org/distributions/gradle-8.10-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
Expand Down
5 changes: 4 additions & 1 deletion gradlew
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0
#

##############################################################################
#
Expand Down Expand Up @@ -84,7 +86,8 @@ done
# shellcheck disable=SC2034
APP_BASE_NAME=${0##*/}
# Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036)
APP_HOME=$( cd "${APP_HOME:-./}" > /dev/null && pwd -P ) || exit
APP_HOME=$( cd -P "${APP_HOME:-./}" > /dev/null && printf '%s
' "$PWD" ) || exit

# Use the maximum available, or set MAX_FD != -1 to use that value.
MAX_FD=maximum
Expand Down
2 changes: 2 additions & 0 deletions gradlew.bat
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
@rem See the License for the specific language governing permissions and
@rem limitations under the License.
@rem
@rem SPDX-License-Identifier: Apache-2.0
@rem

@if "%DEBUG%"=="" @echo off
@rem ##########################################################################
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

package dev.galacticraft.machinelib.api.compat.transfer;

import dev.galacticraft.machinelib.api.storage.MachineEnergyStorage;
import dev.galacticraft.machinelib.impl.storage.exposed.ExposedEnergyStorageImpl;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
Expand All @@ -40,7 +41,7 @@ public interface ExposedEnergyStorage extends EnergyStorage {
* @return A new exposed energy storage.
*/
@Contract("_, _, _ -> new")
static @NotNull ExposedEnergyStorage create(@NotNull EnergyStorage parent, long maxInsertion, long maxExtraction) {
static @NotNull ExposedEnergyStorage create(@NotNull MachineEnergyStorage parent, long maxInsertion, long maxExtraction) {
return new ExposedEnergyStorageImpl(parent, maxInsertion, maxExtraction);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import net.fabricmc.fabric.api.transfer.v1.storage.StoragePreconditions;
import net.fabricmc.fabric.api.transfer.v1.transaction.TransactionContext;
import net.minecraft.nbt.LongTag;
import net.minecraft.world.level.block.entity.BlockEntity;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -204,10 +205,15 @@ public interface MachineEnergyStorage extends EnergyStorage, Serializable<LongTa
long externalExtractionRate();

/**
* Sets the listener (called when the energy storage changes). Internal use only.
* Sets the parent of this energy storage (notified when the energy storage changes). Internal use only.
*/
@ApiStatus.Internal
void setListener(Runnable listener);
void setParent(BlockEntity parent);

/**
* {@return whether the energy storage should still be interacted with}
*/
boolean isValid();

record Spec(long capacity, long insertion, long extraction) {
public Spec {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import net.fabricmc.fabric.api.transfer.v1.storage.TransferVariant;
import net.minecraft.nbt.ListTag;
import net.minecraft.network.RegistryFriendlyByteBuf;
import net.minecraft.world.level.block.entity.BlockEntity;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand All @@ -44,11 +45,13 @@
*/
public interface ResourceStorage<Resource, Slot extends ResourceSlot<Resource>> extends Iterable<Slot>, MutableModifiable, SlottedStorageAccess<Resource, Slot>, Serializable<ListTag>, DeltaPacketSerializable<RegistryFriendlyByteBuf, long[]>, PacketSerializable<RegistryFriendlyByteBuf> {
/**
* Set the listener for this storage. This listener will be called whenever the storage is modified.
* Set the parent for this storage. This parent will be notified whenever the storage is modified.
*
* @param listener the listener to set.
* @param parent the parent of this storage.
*/
void setListener(Runnable listener);
void setParent(BlockEntity parent);

boolean isValid();

/**
* Create an exposed storage for this storage.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@
* @see StorageAccess
*/
public interface ResourceSlot<Resource> extends StorageAccess<Resource>, MutableModifiable, Serializable<CompoundTag>, PacketSerializable<RegistryFriendlyByteBuf> {
/**
* {@return whether this slot should still be interacted with}
*/
boolean isValid();

/**
* {@return the way this slot is allowed to be interacted with}
* Governs in-world (block-to-block) and player (menu) interactions.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,24 +50,24 @@ public ExposedSlotImpl(@NotNull ResourceSlot<Resource> slot, @NotNull ResourceFl

@Override
public long insert(Variant variant, long maxAmount, TransactionContext transaction) {
return this.insertion && this.slot.getFilter().test(variant.getObject(), variant.getComponents()) ?
return this.supportsInsertion() && this.slot.getFilter().test(variant.getObject(), variant.getComponents()) ?
this.slot.insert(variant.getObject(), variant.getComponents(), maxAmount, transaction)
: 0;
}

@Override
public long extract(Variant variant, long maxAmount, TransactionContext transaction) {
return this.extraction ? this.slot.extract(variant.getObject(), variant.getComponents(), maxAmount, transaction) : 0;
return this.supportsExtraction() ? this.slot.extract(variant.getObject(), variant.getComponents(), maxAmount, transaction) : 0;
}

@Override
public boolean supportsInsertion() {
return this.insertion;
return this.insertion && this.slot.isValid() ;
}

@Override
public boolean supportsExtraction() {
return this.extraction;
return this.extraction && this.slot.isValid() ;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public ExposedStorageImpl(ResourceStorage<Resource, ?> storage, ExposedSlot<Reso

@Override
public boolean supportsInsertion() {
if (!this.storage.isValid()) return false;
for (ExposedSlot<Resource, Variant> slot : this.slots) {
if (slot.supportsInsertion()) {
return true;
Expand All @@ -54,6 +55,7 @@ public boolean supportsInsertion() {

@Override
public boolean supportsExtraction() {
if (!this.storage.isValid()) return false;
for (ExposedSlot<Resource, Variant> slot : this.slots) {
if (slot.supportsExtraction()) {
return true;
Expand All @@ -64,6 +66,7 @@ public boolean supportsExtraction() {

@Override
public long insert(Variant variant, long maxAmount, TransactionContext transaction) {
if (!this.storage.isValid()) return 0;
long requested = maxAmount;
for (ExposedSlot<Resource, Variant> slot : this.slots) {
if (maxAmount == 0) return requested;
Expand All @@ -74,6 +77,7 @@ public long insert(Variant variant, long maxAmount, TransactionContext transacti

@Override
public long extract(Variant variant, long maxAmount, TransactionContext transaction) {
if (!this.storage.isValid()) return 0;
long requested = maxAmount;
for (ExposedSlot<Resource, Variant> slot : this.slots) {
if (maxAmount == 0) return requested;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import io.netty.buffer.ByteBuf;
import net.fabricmc.fabric.api.transfer.v1.transaction.TransactionContext;
import net.minecraft.nbt.LongTag;
import net.minecraft.world.level.block.entity.BlockEntity;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import team.reborn.energy.api.EnergyStorage;
Expand Down Expand Up @@ -138,7 +139,12 @@ public long externalExtractionRate() {
}

@Override
public void setListener(Runnable listener) {
public void setParent(BlockEntity parent) {
}

@Override
public boolean isValid() {
return true;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import net.fabricmc.fabric.api.transfer.v1.transaction.TransactionContext;
import net.fabricmc.fabric.api.transfer.v1.transaction.base.SnapshotParticipant;
import net.minecraft.nbt.LongTag;
import net.minecraft.world.level.block.entity.BlockEntity;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand All @@ -41,7 +42,7 @@ public final class MachineEnergyStorageImpl extends SnapshotParticipant<Long> im
private final long maxOutput;
private final @Nullable EnergyStorage[] exposedStorages = new EnergyStorage[3];
public long amount = 0;
private Runnable listener;
private BlockEntity parent;

public MachineEnergyStorageImpl(long capacity, long maxInput, long maxOutput) {
this.capacity = capacity;
Expand Down Expand Up @@ -199,8 +200,13 @@ public long externalExtractionRate() {
}

@Override
public void setListener(Runnable listener) {
this.listener = listener;
public void setParent(BlockEntity parent) {
this.parent = parent;
}

@Override
public boolean isValid() {
return this.parent == null || !this.parent.isRemoved();
}

@Override
Expand Down Expand Up @@ -235,7 +241,7 @@ public long getModifications() {
}

private void markModified() {
if (this.listener != null) this.listener.run();
if (this.parent != null) this.parent.setChanged();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,14 @@
import net.fabricmc.fabric.api.transfer.v1.transaction.TransactionContext;
import net.minecraft.nbt.ListTag;
import net.minecraft.network.RegistryFriendlyByteBuf;
import net.minecraft.world.level.block.entity.BlockEntity;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public abstract class ResourceStorageImpl<Resource, Slot extends ResourceSlot<Resource>> extends BaseSlottedStorage<Resource, Slot> implements ResourceStorage<Resource, Slot>, TransactionContext.CloseCallback {
private final LongList transactions = new LongArrayList();
private long modifications = 1;
private Runnable listener;
private @Nullable BlockEntity parent;

public ResourceStorageImpl(@NotNull Slot @NotNull [] slots) {
super(slots);
Expand All @@ -45,8 +46,13 @@ public ResourceStorageImpl(@NotNull Slot @NotNull [] slots) {
}

@Override
public void setListener(Runnable listener) {
this.listener = listener;
public void setParent(@Nullable BlockEntity parent) {
this.parent = parent;
}

@Override
public boolean isValid() {
return this.parent == null || !this.parent.isRemoved();
}

@Override
Expand All @@ -57,7 +63,7 @@ public long getModifications() {
@Override
public void markModified() {
this.modifications++;
if (this.listener != null) this.listener.run();
if (this.parent != null) this.parent.setChanged();
}

@Override
Expand Down Expand Up @@ -92,7 +98,7 @@ public void onClose(TransactionContext transaction, TransactionContext.Result re
this.transactions.clear();
transaction.addOuterCloseCallback((res) -> {
assert res.wasCommitted();
if (this.listener != null) this.listener.run();
if (this.parent != null) this.parent.setChanged();
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
package dev.galacticraft.machinelib.impl.storage.exposed;

import dev.galacticraft.machinelib.api.compat.transfer.ExposedEnergyStorage;
import dev.galacticraft.machinelib.api.storage.MachineEnergyStorage;
import net.fabricmc.fabric.api.transfer.v1.transaction.TransactionContext;
import org.jetbrains.annotations.NotNull;
import team.reborn.energy.api.EnergyStorage;
Expand All @@ -35,29 +36,29 @@
* @param maxExtraction The maximum amount of energy that can be extracted in one transaction.
* @see EnergyStorage
*/
public record ExposedEnergyStorageImpl(@NotNull EnergyStorage parent, long maxInsertion,
public record ExposedEnergyStorageImpl(@NotNull MachineEnergyStorage parent, long maxInsertion,
long maxExtraction) implements ExposedEnergyStorage {
@Override
public boolean supportsInsertion() {
return this.maxInsertion > 0;
return this.parent.isValid() && this.maxInsertion > 0;
}

@Override
public long insert(long maxAmount, TransactionContext transaction) {
if (this.maxInsertion > 0) {
if (this.supportsInsertion()) {
return this.parent.insert(Math.min(this.maxInsertion, maxAmount), transaction);
}
return 0;
}

@Override
public boolean supportsExtraction() {
return this.maxExtraction > 0;
return this.parent.isValid() && this.maxExtraction > 0;
}

@Override
public long extract(long maxAmount, TransactionContext transaction) {
if (this.maxExtraction > 0) {
if (this.supportsExtraction()) {
return this.parent.extract(Math.min(this.maxExtraction, maxAmount), transaction);
}
return 0;
Expand Down
Loading

0 comments on commit fd19d2e

Please sign in to comment.