From 9cc9c5fa31e53c1b4346f074f9ee057c9fa21b52 Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 18 Mar 2024 23:08:57 +0100 Subject: [PATCH] If redstone is supplied by neighbors dont accpet energy --- .../energymeter/component/SidedEnergyStorage.java | 5 +++++ .../energymeter/core/Constants.java | 2 +- .../energymeter/meter/MeterBlock.java | 5 +++++ .../energymeter/meter/MeterEntity.java | 15 +++++++++++++++ 4 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/github/almostreliable/energymeter/component/SidedEnergyStorage.java b/src/main/java/com/github/almostreliable/energymeter/component/SidedEnergyStorage.java index 25cc6c9..f6bddb1 100644 --- a/src/main/java/com/github/almostreliable/energymeter/component/SidedEnergyStorage.java +++ b/src/main/java/com/github/almostreliable/energymeter/component/SidedEnergyStorage.java @@ -55,6 +55,11 @@ public boolean canExtract() { @Override public boolean canReceive() { + + //Abort if redstone enabled + if(parent.getIsRedstoneDisabled()) + return false; + if (parent.getMode() == MODE.CONSUMER) { return parent.getSideConfig().get(side) == IO_SETTING.IN; } diff --git a/src/main/java/com/github/almostreliable/energymeter/core/Constants.java b/src/main/java/com/github/almostreliable/energymeter/core/Constants.java index 9820fa5..eb14e2a 100644 --- a/src/main/java/com/github/almostreliable/energymeter/core/Constants.java +++ b/src/main/java/com/github/almostreliable/energymeter/core/Constants.java @@ -26,7 +26,7 @@ public enum Constants { public static final String IO_SCREEN_ID = "screen"; public static final String INTERVAL_ID = "interval"; public static final String THRESHOLD_ID = "threshold"; - + public static final String REDSTONE_DISABLED_ID = "redstone_disabled"; /** * Decimal color values to ensure consistent color values. */ diff --git a/src/main/java/com/github/almostreliable/energymeter/meter/MeterBlock.java b/src/main/java/com/github/almostreliable/energymeter/meter/MeterBlock.java index 1b7eb52..efd842b 100644 --- a/src/main/java/com/github/almostreliable/energymeter/meter/MeterBlock.java +++ b/src/main/java/com/github/almostreliable/energymeter/meter/MeterBlock.java @@ -26,6 +26,7 @@ import net.minecraft.world.phys.BlockHitResult; import net.minecraftforge.network.NetworkHooks; + import javax.annotation.Nullable; import static com.github.almostreliable.energymeter.core.Constants.IO_STATE_ID; @@ -75,6 +76,10 @@ public void neighborChanged( // resolve tile entity from block position if (!state.hasBlockEntity()) return; if (level.getBlockEntity(pos) instanceof MeterEntity entity) { + //Check neighbor redstone level + var isDisabled = level.hasNeighborSignal(pos); + entity.setIsRedstoneDisabled(isDisabled); + // ensure valid neighbor var neighborState = level.getBlockState(neighbor); // noinspection deprecation diff --git a/src/main/java/com/github/almostreliable/energymeter/meter/MeterEntity.java b/src/main/java/com/github/almostreliable/energymeter/meter/MeterEntity.java index f1ca3c0..3946e23 100644 --- a/src/main/java/com/github/almostreliable/energymeter/meter/MeterEntity.java +++ b/src/main/java/com/github/almostreliable/energymeter/meter/MeterEntity.java @@ -60,6 +60,8 @@ public class MeterEntity extends BlockEntity implements MenuProvider { private int threshold = REFRESH_RATE; private double zeroThreshold; + private boolean isRedstoneDisabled = false; + @SuppressWarnings("ThisEscapedInObjectConstruction") public MeterEntity(BlockPos pos, BlockState state) { super(Entities.METER.get(), pos, state); @@ -171,6 +173,8 @@ public void load(CompoundTag tag) { if (tag.contains(ACCURACY_ID)) accuracy = ACCURACY.values()[tag.getInt(ACCURACY_ID)]; if (tag.contains(INTERVAL_ID)) interval = tag.getInt(INTERVAL_ID); if (tag.contains(THRESHOLD_ID)) threshold = tag.getInt(THRESHOLD_ID); + if (tag.contains(REDSTONE_DISABLED_ID)) isRedstoneDisabled = tag.getBoolean(REDSTONE_DISABLED_ID); + } @Override @@ -182,6 +186,7 @@ public void saveAdditional(CompoundTag tag) { tag.putInt(ACCURACY_ID, accuracy.ordinal()); tag.putInt(INTERVAL_ID, interval); tag.putInt(THRESHOLD_ID, threshold); + tag.putBoolean(REDSTONE_DISABLED_ID, isRedstoneDisabled); } @Override @@ -195,6 +200,7 @@ public CompoundTag getUpdateTag() { tag.putInt(ACCURACY_ID, accuracy.ordinal()); tag.putInt(INTERVAL_ID, interval); tag.putInt(THRESHOLD_ID, threshold); + tag.putBoolean(REDSTONE_DISABLED_ID, isRedstoneDisabled); return tag; } @@ -216,6 +222,7 @@ public void handleUpdateTag(CompoundTag tag) { accuracy = ACCURACY.values()[tag.getInt(ACCURACY_ID)]; interval = tag.getInt(INTERVAL_ID); threshold = tag.getInt(THRESHOLD_ID); + isRedstoneDisabled = tag.getBoolean(REDSTONE_DISABLED_ID); } public int receiveEnergy(int energy, boolean simulate) { @@ -583,4 +590,12 @@ public void setMode(MODE mode) { public Component getDisplayName() { return TextUtils.translate(TRANSLATE_TYPE.CONTAINER, METER_ID); } + + public void setIsRedstoneDisabled(boolean isDisabled) { + isRedstoneDisabled = isDisabled; + } + + public boolean getIsRedstoneDisabled() { + return isRedstoneDisabled; + } }