Skip to content

Commit

Permalink
Fix #200 - Crash When Placing a Comparator Reading a Large Energy Sto…
Browse files Browse the repository at this point in the history
…rage (#202)
  • Loading branch information
aderoian authored Dec 11, 2024
1 parent 13474fd commit 3c66e61
Show file tree
Hide file tree
Showing 13 changed files with 54 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import net.minecraft.world.World;
import org.jetbrains.annotations.Nullable;
import rearth.oritech.block.entity.accelerator.AcceleratorSensorBlockEntity;
import rearth.oritech.util.ComparatorOutputProvider;

import java.util.List;

Expand All @@ -31,7 +32,7 @@ protected boolean hasComparatorOutput(BlockState state) {

@Override
protected int getComparatorOutput(BlockState state, World world, BlockPos pos) {
return ((AcceleratorSensorBlockEntity) world.getBlockEntity(pos)).getComparatorOutput();
return ((ComparatorOutputProvider) world.getBlockEntity(pos)).getComparatorOutput();
}

@Nullable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import rearth.oritech.block.entity.addons.RedstoneAddonBlockEntity;
import rearth.oritech.util.ComparatorOutputProvider;

public class RedstoneAddonBlock extends MachineAddonBlock {

Expand All @@ -44,7 +45,7 @@ protected boolean hasComparatorOutput(BlockState state) {

@Override
protected int getComparatorOutput(BlockState state, World world, BlockPos pos) {
return ((RedstoneAddonBlockEntity) world.getBlockEntity(pos)).currentOutput;
return ((ComparatorOutputProvider) world.getBlockEntity(pos)).getComparatorOutput();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import net.minecraft.world.World;
import org.jetbrains.annotations.Nullable;
import rearth.oritech.block.entity.arcane.EnchantmentCatalystBlockEntity;
import rearth.oritech.util.ComparatorOutputProvider;

import java.util.List;
import java.util.Objects;
Expand All @@ -41,7 +42,7 @@ protected boolean hasComparatorOutput(BlockState state) {

@Override
protected int getComparatorOutput(BlockState state, World world, BlockPos pos) {
return ((EnchantmentCatalystBlockEntity) world.getBlockEntity(pos)).getComparatorOutput();
return ((ComparatorOutputProvider) world.getBlockEntity(pos)).getComparatorOutput();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.jetbrains.annotations.Nullable;
import rearth.oritech.block.entity.storage.SmallFluidTankEntity;
import rearth.oritech.init.BlockContent;
import rearth.oritech.util.ComparatorOutputProvider;

import java.util.List;

Expand Down Expand Up @@ -64,7 +65,7 @@ protected boolean hasComparatorOutput(BlockState state) {

@Override
protected int getComparatorOutput(BlockState state, World world, BlockPos pos) {
return ((SmallFluidTankEntity) world.getBlockEntity(pos)).getComparatorOutput();
return ((ComparatorOutputProvider) world.getBlockEntity(pos)).getComparatorOutput();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import rearth.oritech.block.base.entity.ExpandableEnergyStorageBlockEntity;
import rearth.oritech.block.entity.storage.SmallStorageBlockEntity;
import rearth.oritech.init.BlockContent;
import rearth.oritech.util.ComparatorOutputProvider;
import rearth.oritech.util.MachineAddonController;

import java.util.List;
Expand Down Expand Up @@ -79,7 +80,7 @@ protected boolean hasComparatorOutput(BlockState state) {

@Override
protected int getComparatorOutput(BlockState state, World world, BlockPos pos) {
return ((SmallStorageBlockEntity) world.getBlockEntity(pos)).getComparatorOutput();
return ((ComparatorOutputProvider) world.getBlockEntity(pos)).getComparatorOutput();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import rearth.oritech.init.BlockEntitiesContent;
import rearth.oritech.util.ComparatorOutputProvider;

public class AcceleratorSensorBlockEntity extends BlockEntity implements BlockEntityTicker<AcceleratorSensorBlockEntity> {
public class AcceleratorSensorBlockEntity extends BlockEntity implements BlockEntityTicker<AcceleratorSensorBlockEntity>, ComparatorOutputProvider {

private float measuredSpeed;
private long measuredTime;
Expand Down Expand Up @@ -42,7 +43,8 @@ public void measureParticle(AcceleratorParticleLogic.ActiveParticle particle) {
this.measuredTime = world.getTime();
dirty = true;
}


@Override
public int getComparatorOutput() {
if (measuredSpeed <= 0) {
return 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@
import rearth.oritech.client.ui.RedstoneAddonScreenHandler;
import rearth.oritech.init.BlockEntitiesContent;
import rearth.oritech.network.NetworkContent;
import rearth.oritech.util.ComparatorOutputProvider;

public class RedstoneAddonBlockEntity extends AddonBlockEntity implements BlockEntityTicker<RedstoneAddonBlockEntity>, ExtendedScreenHandlerFactory {
public class RedstoneAddonBlockEntity extends AddonBlockEntity implements BlockEntityTicker<RedstoneAddonBlockEntity>, ExtendedScreenHandlerFactory, ComparatorOutputProvider {

private RedstoneControllable cachedController;
public RedstoneMode activeMode = RedstoneMode.INPUT_CONTROL;
Expand Down Expand Up @@ -99,7 +100,12 @@ public void setRedstonePowered(boolean isPowered) {
cachedController.onRedstoneEvent(isPowered);

}


@Override
public int getComparatorOutput() {
return currentOutput;
}

@Override
public Object getScreenOpeningData(ServerPlayerEntity player) {
sendDataToClient();
Expand Down Expand Up @@ -133,12 +139,21 @@ public enum RedstoneMode {
OUTPUT_POWER, OUTPUT_SLOT, OUTPUT_PROGRESS, OUTPUT_ACTIVE, INPUT_CONTROL
}

public interface RedstoneControllable {
public interface RedstoneControllable extends ComparatorOutputProvider {
int getComparatorEnergyAmount();
int getComparatorSlotAmount(int slot);
int getComparatorProgress();
int getComparatorActiveState();
void onRedstoneEvent(boolean isPowered);

/**
* A redstone controllable machine only outputs a readable comparator signal from the controller addon block.
* @return 0
*/
@Override
default int getComparatorOutput() {
return 0;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
import java.util.List;

public class EnchantmentCatalystBlockEntity extends BaseSoulCollectionEntity
implements InventoryProvider, EnergyApi.BlockProvider, ScreenProvider, GeoBlockEntity, BlockEntityTicker<EnchantmentCatalystBlockEntity>, ExtendedScreenHandlerFactory<ModScreens.BasicData> {
implements InventoryProvider, EnergyApi.BlockProvider, ScreenProvider, ComparatorOutputProvider, GeoBlockEntity, BlockEntityTicker<EnchantmentCatalystBlockEntity>, ExtendedScreenHandlerFactory<ModScreens.BasicData> {

public static final RawAnimation IDLE = RawAnimation.begin().thenLoop("idle");
public static final RawAnimation STABILIZED = RawAnimation.begin().thenLoop("stabilized");
Expand Down Expand Up @@ -275,7 +275,8 @@ public void onSoulIncoming(Vec3d source) {
public boolean canAcceptSoul() {
return collectedSouls < maxSouls;
}


@Override
public int getComparatorOutput() {
return calculateComparatorLevel();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@
import rearth.oritech.init.BlockContent;
import rearth.oritech.init.BlockEntitiesContent;
import rearth.oritech.network.NetworkContent;
import rearth.oritech.util.ComparatorOutputProvider;

public class SpawnerControllerBlockEntity extends BaseSoulCollectionEntity implements BlockEntityTicker<SpawnerControllerBlockEntity> {
public class SpawnerControllerBlockEntity extends BaseSoulCollectionEntity implements BlockEntityTicker<SpawnerControllerBlockEntity>, ComparatorOutputProvider {

public int maxSouls = 100_000;
public int collectedSouls = 0;
Expand Down Expand Up @@ -169,7 +170,8 @@ private void updateComparator() {
}

}


@Override
public int getComparatorOutput() {
if (spawnedMob == null || maxSouls == 0) return 0;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
import net.minecraft.world.World;
import rearth.oritech.block.base.entity.ExpandableEnergyStorageBlockEntity;
import rearth.oritech.init.BlockEntitiesContent;
import rearth.oritech.util.ComparatorOutputProvider;

import java.util.List;

public class CreativeStorageBlockEntity extends ExpandableEnergyStorageBlockEntity {
public class CreativeStorageBlockEntity extends ExpandableEnergyStorageBlockEntity implements ComparatorOutputProvider {

public CreativeStorageBlockEntity(BlockPos pos, BlockState state) {
super(BlockEntitiesContent.CREATIVE_STORAGE_ENTITY, pos, state);
Expand All @@ -35,6 +36,7 @@ public long getDefaultExtractionRate() {
return Integer.MAX_VALUE;
}

@Override
public int getComparatorOutput() {
if (energyStorage.amount == 0) return 0;
return (int) (1 + ((energyStorage.amount / (float) energyStorage.capacity) * 14));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@

import java.util.List;

public class SmallFluidTankEntity extends BlockEntity implements FluidProvider, InventoryProvider, ScreenProvider, ExtendedScreenHandlerFactory, BlockEntityTicker<SmallFluidTankEntity> {
public class SmallFluidTankEntity extends BlockEntity implements FluidProvider, InventoryProvider, ComparatorOutputProvider, ScreenProvider, ExtendedScreenHandlerFactory, BlockEntityTicker<SmallFluidTankEntity> {

private boolean netDirty = false;
private int lastComparatorOutput = 0;
Expand Down Expand Up @@ -219,7 +219,8 @@ private boolean outputCanAcceptBucket(ItemStack bucket) {
var slot = inventory.getStack(1);
return (slot.isEmpty() || (slot.isStackable() && ItemStack.areItemsAndComponentsEqual(slot, bucket) && slot.getCount() < slot.getMaxCount()));
}


@Override
public int getComparatorOutput() {
if (fluidStorage.isResourceBlank()) return 0;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
import rearth.oritech.Oritech;
import rearth.oritech.block.base.entity.ExpandableEnergyStorageBlockEntity;
import rearth.oritech.init.BlockEntitiesContent;
import rearth.oritech.util.ComparatorOutputProvider;

import java.util.List;

public class SmallStorageBlockEntity extends ExpandableEnergyStorageBlockEntity {
public class SmallStorageBlockEntity extends ExpandableEnergyStorageBlockEntity implements ComparatorOutputProvider {

public SmallStorageBlockEntity(BlockPos pos, BlockState state) {
super(BlockEntitiesContent.SMALL_STORAGE_ENTITY, pos, state);
Expand Down Expand Up @@ -38,6 +39,7 @@ public long getDefaultExtractionRate() {
return Oritech.CONFIG.smallEnergyStorage.maxEnergyExtraction();
}

@Override
public int getComparatorOutput() {
if (energyStorage.amount == 0) return 0;
return (int) (1 + ((energyStorage.amount / (float) energyStorage.capacity) * 14));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package rearth.oritech.util;

public interface ComparatorOutputProvider {

int getComparatorOutput();
}

0 comments on commit 3c66e61

Please sign in to comment.