Skip to content

Commit

Permalink
Refactor detector covers (#1659)
Browse files Browse the repository at this point in the history
* Add base class for detector covers

* Fix toggle not toggling

* Add screwdriver handling to CoverDetectorBase

* Call super on initialSyncData in CoverDetectorBase even though parent functions are empty

* Update Fluid Detector Covers to extend CoverDetectorBase

* Update Item Detector Covers to extend CoverDetectorBase

* Update Energy Detector Covers to extend CoverDetectorBase - with added compatibility check for inverted NBT key

* Update Activity Detector Covers to extend CoverDetectorBase

* Rename detector cover for activity to follow naming system on rest of cover detectors

* Extract function to compute redstone from basic detector covers

* Remove unused lang keys for detector messages

* Update Advanced Activity detector to use utility function for calculating redstone signal strength

* Make toggle function in CoverDetectorBase private as it is not needed anywhere else

* Clean up

* Move redstone logic related functions to own util file

* Add used network key for updating inverted on Detector covers to GregtechDataCodes
  • Loading branch information
LAGIdiot authored Apr 8, 2023
1 parent 4e859f8 commit ed3142b
Show file tree
Hide file tree
Showing 18 changed files with 293 additions and 512 deletions.
3 changes: 3 additions & 0 deletions src/main/java/gregtech/api/capability/GregtechDataCodes.java
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,9 @@ public class GregtechDataCodes {
// Quantum Chest
public static final int UPDATE_ITEM_COUNT = 14;

// Detector Covers
public static final int UPDATE_INVERTED = 100;

// NBT Keys

// From MetaTileEntityHolder
Expand Down
56 changes: 8 additions & 48 deletions src/main/java/gregtech/api/util/GTUtility.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public class GTUtility {
private static final NumberFormat NUMBER_FORMAT = NumberFormat.getInstance();
private static final DecimalFormat TWO_PLACES_FORMAT = new DecimalFormat("#.##");

private static TreeMap<Integer, String> romanNumeralConversions = new TreeMap<>();
private static final TreeMap<Integer, String> romanNumeralConversions = new TreeMap<>();

private static final NavigableMap<Long, Byte> tierByVoltage = new TreeMap<>();

Expand Down Expand Up @@ -1084,6 +1084,7 @@ public static boolean isBlockSnowLayer(@Nonnull IBlockState blockState) {

/**
* Attempt to break a (single) snow layer at the given BlockPos.
*
* @return true if the passed IBlockState was a snow layer
*/
public static boolean tryBreakSnowLayer(World world, BlockPos pos, @Nonnull IBlockState blockState, boolean playSound) {
Expand All @@ -1109,11 +1110,12 @@ public static Pattern getForwardNewLineRegex() {

/**
* Tries to parse a string into an int, returning a default value if it fails.
* @param val string to parse
*
* @param val string to parse
* @param defaultValue default value to return
* @return returns an int from the parsed string, otherwise the default value
*/
public static int tryParseInt(String val, int defaultValue){
public static int tryParseInt(String val, int defaultValue) {
try {
return Integer.parseInt(val);
} catch (NumberFormatException e) {
Expand All @@ -1124,11 +1126,12 @@ public static int tryParseInt(String val, int defaultValue){

/**
* Tries to parse a string into a long, returning a default value if it fails.
* @param val string to parse
*
* @param val string to parse
* @param defaultValue default value to return
* @return returns a long from the parsed string, otherwise the default value
*/
public static long tryParseLong(String val, long defaultValue){
public static long tryParseLong(String val, long defaultValue) {
try {
return Long.parseLong(val);
} catch (NumberFormatException e) {
Expand All @@ -1137,48 +1140,6 @@ public static long tryParseLong(String val, long defaultValue){
return defaultValue;
}

/**
* Compares a value against a min and max, with an option to invert the logic
* @param value value to be compared
* @param maxValue the max that the value can be
* @param minValue the min that the value can be
* @param isInverted whether to invert the logic of this method
* @return an int from 0 (value <= min) to 15 (value >= max) normally, with a ratio when the value is between min and max
*/
public static int computeRedstoneBetweenValues(int value, float maxValue, float minValue, boolean isInverted) {
if (value >= maxValue) {
return isInverted ? 0 : 15; // value above maxValue should normally be 15, otherwise 0
} else if (value <= minValue) {
return isInverted ? 15 : 0; // value below minValue should normally be 0, otherwise 15
}

float ratio;
if (!isInverted) {
ratio = 15 * (value - minValue) / (maxValue - minValue); // value closer to max results in higher output
} else {
ratio = 15 * (maxValue - value) / (maxValue - minValue); // value closer to min results in higher output
}

return Math.round(ratio);
}

/**
* Compares a value against a min and max, with an option to invert the logic. Has latching functionality.
* @param value value to be compared
* @param maxValue the max that the value can be
* @param minValue the min that the value can be
* @param output the output value the function modifies
* @return returns the modified output value
*/
public static int computeLatchedRedstoneBetweenValues(float value, float maxValue, float minValue, boolean isInverted, int output) {
if (value >= maxValue) {
output = !isInverted ? 0 : 15; // value above maxValue should normally be 0, otherwise 15
} else if (value <= minValue) {
output = !isInverted ? 15 : 0; // value below minValue should normally be 15, otherwise 0
}
return output;
}

/**
* @param fluidHandler the handler to drain from
* @param doDrain if the handler should be actually drained
Expand Down Expand Up @@ -1243,7 +1204,6 @@ public static Set<ItemStack> getAllSubItems(@Nonnull ItemStack stack) {
* @param height The height of the box
* @param pointX The X value of the point to check
* @param pointY The Y value of the point to check
*
* @return True if the provided (X,Y) point is within the described box, else false
*/
public static boolean isPointWithinRange(int initialX, int initialY, int width, int height, int pointX, int pointY) {
Expand Down
67 changes: 67 additions & 0 deletions src/main/java/gregtech/api/util/RedstoneUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package gregtech.api.util;

public class RedstoneUtil {

/**
* Compares a value against a min and max, with an option to invert the logic
*
* @param value value to be compared
* @param maxValue the max that the value can be
* @param minValue the min that the value can be
* @param isInverted whether to invert the logic of this method
* @return an int from 0 (value <= min) to 15 (value >= max) normally, with a ratio when the value is between min and max
*/
public static int computeRedstoneBetweenValues(int value, float maxValue, float minValue, boolean isInverted) {
if (value >= maxValue) {
return isInverted ? 0 : 15; // value above maxValue should normally be 15, otherwise 0
} else if (value <= minValue) {
return isInverted ? 15 : 0; // value below minValue should normally be 0, otherwise 15
}

float ratio;
if (!isInverted) {
ratio = 15 * (value - minValue) / (maxValue - minValue); // value closer to max results in higher output
} else {
ratio = 15 * (maxValue - value) / (maxValue - minValue); // value closer to min results in higher output
}

return Math.round(ratio);
}

/**
* Compares a value against a min and max, with an option to invert the logic. Has latching functionality.
*
* @param value value to be compared
* @param maxValue the max that the value can be
* @param minValue the min that the value can be
* @param output the output value the function modifies
* @return returns the modified output value
*/
public static int computeLatchedRedstoneBetweenValues(float value, float maxValue, float minValue, boolean isInverted, int output) {
if (value >= maxValue) {
output = !isInverted ? 0 : 15; // value above maxValue should normally be 0, otherwise 15
} else if (value <= minValue) {
output = !isInverted ? 15 : 0; // value below minValue should normally be 15, otherwise 0
}
return output;
}

/**
* Compares a current against max, with an option to invert the logic.
*
* @param current value to be compared
* @param max the max that value can be
* @param isInverted whether to invert the logic of this method
* @return value 0 to 15
* @throws ArithmeticException when max is 0
*/
public static int computeRedstoneValue(long current, long max, boolean isInverted) throws ArithmeticException {
int outputAmount = (int) (15 * current / max);

if (isInverted) {
outputAmount = 15 - outputAmount;
}

return outputAmount;
}
}
4 changes: 2 additions & 2 deletions src/main/java/gregtech/common/covers/CoverBehaviors.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ public static void init() {
registerBehavior(new ResourceLocation(GTValues.MODID, "fluid_detector_advanced"), MetaItems.COVER_FLUID_DETECTOR_ADVANCED, CoverDetectorFluidAdvanced::new);
registerBehavior(new ResourceLocation(GTValues.MODID, "item_detector"), MetaItems.COVER_ITEM_DETECTOR, CoverDetectorItem::new);
registerBehavior(new ResourceLocation(GTValues.MODID, "item_detector_advanced"), MetaItems.COVER_ITEM_DETECTOR_ADVANCED, CoverDetectorItemAdvanced::new);
registerBehavior(new ResourceLocation(GTValues.MODID, "activity_detector"), MetaItems.COVER_ACTIVITY_DETECTOR, CoverActivityDetector::new);
registerBehavior(new ResourceLocation(GTValues.MODID, "activity_detector_advanced"), MetaItems.COVER_ACTIVITY_DETECTOR_ADVANCED, CoverActivityDetectorAdvanced::new);
registerBehavior(new ResourceLocation(GTValues.MODID, "activity_detector"), MetaItems.COVER_ACTIVITY_DETECTOR, CoverDetectorActivity::new);
registerBehavior(new ResourceLocation(GTValues.MODID, "activity_detector_advanced"), MetaItems.COVER_ACTIVITY_DETECTOR_ADVANCED, CoverDetectorActivityAdvanced::new);
registerBehavior(new ResourceLocation(GTValues.MODID, "crafting_table"), MetaItems.COVER_CRAFTING, CoverCraftingTable::new);
registerBehavior(new ResourceLocation(GTValues.MODID, "infinite_water"), MetaItems.COVER_INFINITE_WATER, CoverInfiniteWater::new);
registerBehavior(new ResourceLocation(GTValues.MODID, "ender_fluid_link"), MetaItems.COVER_ENDER_FLUID_LINK, CoverEnderFluidLink::new);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package gregtech.common.covers.detector;

import codechicken.lib.render.CCRenderState;
import codechicken.lib.render.pipeline.IVertexOperation;
import codechicken.lib.vec.Cuboid6;
import codechicken.lib.vec.Matrix4;
import gregtech.api.capability.GregtechTileCapabilities;
import gregtech.api.capability.IWorkable;
import gregtech.api.cover.ICoverable;
import gregtech.client.renderer.texture.Textures;
import net.minecraft.util.BlockRenderLayer;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.ITickable;

public class CoverDetectorActivity extends CoverDetectorBase implements ITickable {
public CoverDetectorActivity(ICoverable coverHolder, EnumFacing attachedSide) {
super(coverHolder, attachedSide);
}

@Override
public boolean canAttach() {
return coverHolder.getCapability(GregtechTileCapabilities.CAPABILITY_WORKABLE, null) != null;
}

@Override
public void renderCover(CCRenderState renderState, Matrix4 translation, IVertexOperation[] pipeline, Cuboid6 plateBox, BlockRenderLayer layer) {
Textures.DETECTOR_ACTIVITY.renderSided(attachedSide, plateBox, renderState, pipeline, translation);
}

@Override
public void update() {
if (this.coverHolder.getOffsetTimer() % 20 != 0)
return;

IWorkable workable = coverHolder.getCapability(GregtechTileCapabilities.CAPABILITY_WORKABLE, null);
if (workable == null)
return;

if (isInverted()) setRedstoneSignalOutput(workable.isActive() && workable.isWorkingEnabled() ? 0 : 15);
else setRedstoneSignalOutput(workable.isActive() && workable.isWorkingEnabled() ? 15 : 0);
}
}
Loading

0 comments on commit ed3142b

Please sign in to comment.