Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor detector covers #1659

Merged
merged 16 commits into from
Apr 8, 2023
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 34 additions & 11 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 @@ -1139,9 +1142,10 @@ public static long tryParseLong(String val, long 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 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
*/
Expand All @@ -1164,10 +1168,11 @@ public static int computeRedstoneBetweenValues(int value, float maxValue, float

/**
* 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 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
* @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) {
Expand All @@ -1179,6 +1184,25 @@ public static int computeLatchedRedstoneBetweenValues(float value, float maxValu
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 {
LAGIdiot marked this conversation as resolved.
Show resolved Hide resolved
int outputAmount = (int) (15 * current / max);

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

return outputAmount;
}

/**
* @param fluidHandler the handler to drain from
* @param doDrain if the handler should be actually drained
Expand Down Expand Up @@ -1243,7 +1267,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
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);
}
}
Original file line number Diff line number Diff line change
@@ -1,24 +1,19 @@
package gregtech.common.covers.detector;

import codechicken.lib.raytracer.CuboidRayTraceResult;
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.api.util.GTUtility;
import gregtech.client.renderer.texture.Textures;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.util.BlockRenderLayer;
import net.minecraft.util.EnumActionResult;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand;
import net.minecraft.util.text.TextComponentTranslation;

public class CoverActivityDetectorAdvanced extends CoverActivityDetector {

public CoverActivityDetectorAdvanced(ICoverable coverHolder, EnumFacing attachedSide) {
public class CoverDetectorActivityAdvanced extends CoverDetectorActivity {
public CoverDetectorActivityAdvanced(ICoverable coverHolder, EnumFacing attachedSide) {
super(coverHolder, attachedSide);
}

Expand All @@ -27,22 +22,6 @@ public void renderCover(CCRenderState renderState, Matrix4 translation, IVertexO
Textures.DETECTOR_ACTIVITY_ADVANCED.renderSided(attachedSide, plateBox, renderState, pipeline, translation);
}

@Override
public EnumActionResult onScrewdriverClick(EntityPlayer playerIn, EnumHand hand, CuboidRayTraceResult hitResult) {
if (this.coverHolder.getWorld().isRemote) {
return EnumActionResult.SUCCESS;
}

if (this.isInverted) {
this.setInverted();
playerIn.sendMessage(new TextComponentTranslation("gregtech.cover.activity_detector_advanced.message_activity_normal"));
} else {
this.setInverted();
playerIn.sendMessage(new TextComponentTranslation("gregtech.cover.activity_detector_advanced.message_activity_inverted"));
}
return EnumActionResult.SUCCESS;
}

@Override
public void update() {
if (this.coverHolder.getOffsetTimer() % 20 != 0)
Expand All @@ -52,12 +31,14 @@ public void update() {
if (workable == null)
return;

int outputAmount = (int) (15.0 * workable.getProgress() / workable.getMaxProgress());
if (workable.getMaxProgress() == 0)
return;

int outputAmount = GTUtility.computeRedstoneValue(workable.getProgress(), workable.getMaxProgress(), isInverted());

//nonstandard logic for handling off state
if (!workable.isWorkingEnabled())
outputAmount = 0;
else if (this.isInverted)
outputAmount = 15 - outputAmount;

setRedstoneSignalOutput(outputAmount);
}
Expand Down
Loading