diff options
author | chill <chill.gtnh@outlook.com> | 2023-06-05 06:51:28 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-06-05 06:51:28 +0200 |
commit | db089891a20e5696096907864578e39586035e6e (patch) | |
tree | e56d68eb64c3b8025193930b68e731b23d0f60d9 /src/main/java/gregtech/api/items | |
parent | dd9bbe334a00a0435502ac502d4155bfa12d123d (diff) | |
download | GT5-Unofficial-db089891a20e5696096907864578e39586035e6e.tar.gz GT5-Unofficial-db089891a20e5696096907864578e39586035e6e.tar.bz2 GT5-Unofficial-db089891a20e5696096907864578e39586035e6e.zip |
Code cleanup (#2040)
* remove redundant suppressions
* prettify commented code
* improve comments
The integer comment contradicted the code, so I deleted it.
* delete commented-out code
* update bitwise int flip from XOR to the dedicated tilda operator
The flip was a 32-bit XOR, which is an int-flip.
That XOR was replaced with an equivalent tilda operator.
* convert a field to inline
This field was used only once, so put it straight to where it is used.
* remove fluid fix since no-one uses Forge version 1355 or earlier
* unwrap switches where fitting
In some places, we suppress IntelliJ's inspection
RedundantLabeledSwitchRuleCodeBlock - we don't want to unwrap some of
the suggested cases because we want to keep the consistency in a
switch statement for the sake of readability.
* fuse "collect" into Stream API
* fix javadocs
* remove unnecessary public modifiers from an interface
Members of an interface are public by default.
* move common parts outside of if
* suppress OverrideOnly warning in a javadoc
* remove unused lock
* suppress warnings about unchecked casts
These warnings require non-trivial fixes that are yet to arrive.
For now, let's suppress them to reduce the warning-bloat.
* remove outdated comment
* remove final modifier from private methods
Because they are private, it is hard to accidentally overwrite them.
Therefore, the final modifier is redundant in this case.
* refactor getIcon
The first 'if' doesn't cover only tMeta == 9 && mConnectedMachineTextures,
therefore the second if can be unrolled.
The last switch is redundant because all tMeta values are covered by
switches, but let's keep SOLID_STEEL as a fallback just in case.
* explain what the casings are and why block casings are split
* suppress switch-to-if suggestion
* remove unnecessary null check
The null is handled in doGenerateItem()
* address redundant local variables
* rename variables in onTick
* suppress warning about accessing static member via instance
* rephrase exception
* rephrase javadoc
* address field-can-be-final warnings
* remove empty methods
* enum cannot inherit, so protected is redundant
* remove redundant throws
* update imports to be not wildcard
* remove unnecessary try-catch
* update for loop
* remove redundant code in order to use the diamond operator
* update instanceof to use pattern variables
* replace blank lines with <p> in javadocs
* fix dangling javadocs
* suppress warning about unreachable methods in javadocs
* remove redundant operation
* add the descriptions of parameters in javadocs
Also fix javadocs along the way.
* relax returned type in javadoc
That was done in order to make the docs reflect the code more often.
Otherwise, people may forget to change the returned type again with
another change.
* make long conversion explicit
Integer multiplication can give a wrong result if one of the parts is
not explicitly cast to long. Let's cast one of the parts where
applicable.
* remove unary plus
* simplify unary minus
* use addAll instead of forEach,add
It was suggested by IntelliJ to replace the iteration with a bulk operation
to improve performance.
* replace .asList with .singletonList for consistency
* simplify toArray calls
Explanation from IntelliJ:
There are two styles to convert a collection to an array:
* A pre-sized array, for example, c.toArray(new String[c.size()])
* An empty array, for example, c.toArray(new String[0])
In older Java versions, using a pre-sized array was recommended, as the
reflection call necessary to create an array of proper size was quite slow.
However, since late updates of OpenJDK 6, this call was intrinsified, making
the performance of the empty array version the same, and sometimes even better,
compared to the pre-sized version. Also, passing a pre-sized array is dangerous
for a concurrent or synchronized collection as a data race is possible between
the size and toArray calls. This may result in extra nulls at the end of the
array if the collection was concurrently shrunk during the operation.
* split StringBuilder append
Explanation by IntelliJ:
Reports String concatenation used as the argument to appends.
Such calls may profitably be turned into chained append calls on the existing
StringBuilder saving the cost of an extra StringBuilder allocation. This
inspection ignores compile-time evaluated String concatenations,
in which case the conversion would only worsen performance.
* annotate overriding methods with @Nonnull where needed
The method that was overridden has @Nonnull so the method that is overriding
should also have @Nonnull.
* remove set adding itself to itself
* remove null check because findField either works or blows up
cpw.mods.fml.relauncher.ReflectionHelper::findField either returns a non-null
value or throws a RuntimeException, so no need to check of null.
* remove slot comparison with tInventory.length
slot max value is 127 when tInventory.length is set to 256, which results in
that the condition is always true and unnecessary.
* remove aOutput2 null check
As GT_Values.NI is null, there is no need to check aOutput2 for null again
* suppress the suggestion to delete tMeta < 13
mConnectedMachineTextures can change, so tMeta range is not guaranteed
* remove aCoverVariable % 3 < 2
the if above already limits the result of % 3 to "2", so the condition
"less than 2" is always false.
* unwrap "if" because bonus is unchanged
Unwrap if because even if the bonus is a variable, it hasn't been changed for
the past 8 years and is unlikely to be changed in the future.
* reformat javadoc
* improve ignoring an exception
Make them either more clear or concise
* fixup fix typo
* update deprecated calls of newInstance()
* remove testing BaseMetaTileEntity
GregTech_API.constructBaseMetaTileEntity() checks the creation by itself,
logging and throwing a runtime error if failed.
* unwrap hatch-fill for do_SolarSalt
To reach this branch, do_coolant needs to be false and we need to still be in
the function, which means that do_SolarSalt was set to true in the previous
top-tier "if".
* remove always-false input-bus checks of MTE PlasmaForge
size() is non-negative, and the values it is compared to are final and 0.
* length and size are non-negative
Therefore, there's no need to check their negativity
* aOutput is guaranteed to be positive
* tThereWasARecipe is always false when reached in its first occurrence
* convert an assert into if
Only tStack 2 is checked for null because if it isn't null then
tStack1 also isn't null based on the "if" above.
Also IntelliJ was sure that tStack is not null for some reason.
On a side note, assertions work only either with a specified flag
or in debug runs. Therefore, it is dangerous to rely on them.
* simplify stone-gravel-cobble if
tBlock != Blocks.stone because of the if at the start of the method.
for the last else-if, tBlock == Blocks.gravel because of the check slightly
above the change.
* interDimensional is always true because of the first if
* convert an example to javadoc
* remove always-false statements
* replace referential string equality with equals
If we compare strings by "==", we compare references to them, which is not
what we usually want. I wasn't sure if String Pool works here, so I played
it save with equals().
* use Automatic Resource Management for AutoCloseable ByteBufOutputStream
* add todo to swap from sleep to event bus
* null is checked by instanceof
* merge switch branches
* add a TODO to use clamp()
* new String declaration is redundant
* use getOrDefault for a map
* replace StringBuilder with concatenation where fitting
Using a StringBuilder to concatenate two string will not make the program
faster or more understandable, so I swapped it to concatenation.
* remove unnecessary continue
* flip if
* remove redundant returns
* unwrap ifs
It's checked at the top "if" that aType == IItemRenderer.ItemRenderType.INVENTORY,
so all aType.equals(IItemRenderer.ItemRenderType.INVENTORY below are always true.
* remove checking all GT VERSIONs except the API one
* remove version check from GT_Mod and delete respective VERSION fields
Aside from GregTech_API.VERSION, these fields are not used anywhere
in the project, so only GregTech_API.VERSION was kept.
The idea and the usage check were done by miozune.
Diffstat (limited to 'src/main/java/gregtech/api/items')
9 files changed, 13 insertions, 23 deletions
diff --git a/src/main/java/gregtech/api/items/GT_CoolantCell_Item.java b/src/main/java/gregtech/api/items/GT_CoolantCell_Item.java index 352448b6b1..6ed7dae3b0 100644 --- a/src/main/java/gregtech/api/items/GT_CoolantCell_Item.java +++ b/src/main/java/gregtech/api/items/GT_CoolantCell_Item.java @@ -13,7 +13,7 @@ import ic2.core.util.StackUtil; public class GT_CoolantCell_Item extends GT_Generic_Item { - protected int heatStorage; + protected final int heatStorage; public GT_CoolantCell_Item(String aUnlocalized, String aEnglish, int aMaxStore) { super(aUnlocalized, aEnglish, null); diff --git a/src/main/java/gregtech/api/items/GT_EnergyArmor_Item.java b/src/main/java/gregtech/api/items/GT_EnergyArmor_Item.java index cec7a86240..9c7b7c173c 100644 --- a/src/main/java/gregtech/api/items/GT_EnergyArmor_Item.java +++ b/src/main/java/gregtech/api/items/GT_EnergyArmor_Item.java @@ -140,7 +140,7 @@ public class GT_EnergyArmor_Item extends ItemArmor implements ISpecialArmor { } if (!aPlayer.worldObj.isRemote && (mSpecials & 128) != 0) { - float jumpCharge = jumpChargeMap.containsKey(aPlayer) ? (Float) jumpChargeMap.get(aPlayer) : 1.0F; + float jumpCharge = jumpChargeMap.getOrDefault(aPlayer, 1.0F); if (GT_ModHandler.canUseElectricItem(aStack, 1000) && aPlayer.onGround && jumpCharge < 1.0F) { jumpCharge = 1.0F; @@ -180,9 +180,7 @@ public class GT_EnergyArmor_Item extends ItemArmor implements ISpecialArmor { } } - if (bonus > 0.0F) { - aPlayer.moveFlying(0.0F, 1.0F, bonus); - } + aPlayer.moveFlying(0.0F, 1.0F, bonus); } } @@ -333,10 +331,9 @@ public class GT_EnergyArmor_Item extends ItemArmor implements ISpecialArmor { private double getBaseAbsorptionRatio() { if (mArmorAbsorbtionPercentage <= 0) return 0.00; return switch (this.armorType) { - case 0 -> 0.15; + case 0, 3 -> 0.15; case 1 -> 0.40; case 2 -> 0.30; - case 3 -> 0.15; default -> 0.00; }; } diff --git a/src/main/java/gregtech/api/items/GT_Generic_Item.java b/src/main/java/gregtech/api/items/GT_Generic_Item.java index 26eafe5130..4410eccc26 100644 --- a/src/main/java/gregtech/api/items/GT_Generic_Item.java +++ b/src/main/java/gregtech/api/items/GT_Generic_Item.java @@ -91,7 +91,6 @@ public class GT_Generic_Item extends Item implements IProjectileItem { return 0; } - @SuppressWarnings("unchecked") @Override public void addInformation(ItemStack aStack, EntityPlayer aPlayer, List<String> aList, boolean aF3_H) { if (getMaxDamage() > 0 && !getHasSubtypes()) diff --git a/src/main/java/gregtech/api/items/GT_MetaBase_Item.java b/src/main/java/gregtech/api/items/GT_MetaBase_Item.java index 4b3c713f22..8a65fbc841 100644 --- a/src/main/java/gregtech/api/items/GT_MetaBase_Item.java +++ b/src/main/java/gregtech/api/items/GT_MetaBase_Item.java @@ -297,10 +297,6 @@ public abstract class GT_MetaBase_Item extends GT_Generic_Item if (tStats == null || tStats[2] > aTier || !(tStats[3] == -1 || tStats[3] == -3 || (tStats[3] < 0 && aCharge == Integer.MAX_VALUE)) || aStack.stackSize != 1) return 0; - // REALLY?? THIS IS THE CULPRIT THAT CHARGES ITEMS AT INSTANT!!! - // long tChargeBefore = getRealCharge(aStack), tNewCharge = aCharge == Integer.MAX_VALUE ? Long.MAX_VALUE : - // Math.min(Math.abs(tStats[0]), tChargeBefore + (aIgnoreTransferLimit ? (long) aCharge : Math.min(tStats[1], - // (long) aCharge))); long tTransfer = aIgnoreTransferLimit ? (long) aCharge : Math.min(tStats[1], (long) aCharge); long tChargeBefore = getRealCharge(aStack), tNewCharge = Math.min( Math.abs(tStats[0]), diff --git a/src/main/java/gregtech/api/items/GT_MetaGenerated_Item.java b/src/main/java/gregtech/api/items/GT_MetaGenerated_Item.java index 677ad639c7..14da8254ef 100644 --- a/src/main/java/gregtech/api/items/GT_MetaGenerated_Item.java +++ b/src/main/java/gregtech/api/items/GT_MetaGenerated_Item.java @@ -134,7 +134,6 @@ public abstract class GT_MetaGenerated_Item extends GT_MetaBase_Item implements } if (tRandomData == SubTag.NO_UNIFICATION) { GT_OreDictUnificator.addToBlacklist(rStack); - continue; } } // now check for the rest @@ -161,6 +160,8 @@ public abstract class GT_MetaGenerated_Item extends GT_MetaBase_Item implements tUseOreDict = false; } if (tRandomData instanceof IItemBehaviour) { + // The cast below from is not safe. If you know how to make it safe, please do. + // noinspection unchecked addItemBehavior(mOffset + aID, (IItemBehaviour<GT_MetaBase_Item>) tRandomData); tUseOreDict = false; } @@ -182,7 +183,6 @@ public abstract class GT_MetaGenerated_Item extends GT_MetaBase_Item implements } if (tUseOreDict) { GT_OreDictUnificator.registerOre(tRandomData, rStack); - continue; } } if (GregTech_API.sThaumcraftCompat != null) diff --git a/src/main/java/gregtech/api/items/GT_MetaGenerated_Item_X32.java b/src/main/java/gregtech/api/items/GT_MetaGenerated_Item_X32.java index ac5af06ef4..a06a4a7a63 100644 --- a/src/main/java/gregtech/api/items/GT_MetaGenerated_Item_X32.java +++ b/src/main/java/gregtech/api/items/GT_MetaGenerated_Item_X32.java @@ -100,7 +100,7 @@ public abstract class GT_MetaGenerated_Item_X32 extends GT_MetaGenerated_Item { // You have to check for at least these Conditions in every Case! So add a super Call like the following for // this before executing your Code: // if (!super.doesMaterialAllowGeneration(aPrefix, aMaterial)) return false; - return aPrefix != null && aMaterial != null && aPrefix.doGenerateItem(aMaterial); + return aPrefix != null && aPrefix.doGenerateItem(aMaterial); } /* ---------- OVERRIDEABLE FUNCTIONS ---------- */ diff --git a/src/main/java/gregtech/api/items/GT_MetaGenerated_Tool.java b/src/main/java/gregtech/api/items/GT_MetaGenerated_Tool.java index 9483d401db..ecbea37697 100644 --- a/src/main/java/gregtech/api/items/GT_MetaGenerated_Tool.java +++ b/src/main/java/gregtech/api/items/GT_MetaGenerated_Tool.java @@ -236,7 +236,7 @@ public abstract class GT_MetaGenerated_Tool extends GT_MetaBase_Item IToolStats tStats = getToolStats(aStack); if (isItemStackUsable(aStack) && getDigSpeed(aStack, aBlock, aMetaData) > 0.0F) doDamage( aStack, - tStats + (long) tStats .convertBlockDrops(aDrops, aStack, aPlayer, aBlock, aX, aY, aZ, aMetaData, aFortune, aSilkTouch, aEvent) * tStats.getToolDamagePerDropConversion()); } @@ -404,6 +404,10 @@ public abstract class GT_MetaGenerated_Tool extends GT_MetaBase_Item // EU/t -> toolCombatDamage, toolSpeed // Overflow Tier -> toolQuality float aBaseEff = (5f + getToolCombatDamage(aStack)) * 1000f; + + // It was noted by IntelliJ that replacing ((GT_MetaGenerated_Tool) aStack.getItem()) with + // GT_MetaGenerated_Tool can have side effects. This refactoring will need tests. + @SuppressWarnings("AccessStaticViaInstance") float aOptFlow = (Math.max( Float.MIN_NORMAL, ((GT_MetaGenerated_Tool) aStack.getItem()).getToolStats(aStack) @@ -615,7 +619,7 @@ public abstract class GT_MetaGenerated_Tool extends GT_MetaBase_Item @Override public final boolean doDamageToItem(ItemStack aStack, int aVanillaDamage) { - return doDamage(aStack, aVanillaDamage * 100); + return doDamage(aStack, aVanillaDamage * 100L); } public final boolean doDamage(ItemStack aStack, long aAmount) { diff --git a/src/main/java/gregtech/api/items/GT_SolderingTool_Item.java b/src/main/java/gregtech/api/items/GT_SolderingTool_Item.java index 6ec06c91a5..6d84a64e0a 100644 --- a/src/main/java/gregtech/api/items/GT_SolderingTool_Item.java +++ b/src/main/java/gregtech/api/items/GT_SolderingTool_Item.java @@ -28,10 +28,6 @@ public class GT_SolderingTool_Item extends GT_Tool_Item { aDischargedGTID); GT_OreDictUnificator.registerOre(ToolDictNames.craftingToolSolderingIron, new ItemStack(this, 1, W)); GregTech_API.registerSolderingTool(new ItemStack(this, 1, W)); - // setCraftingSound(GregTech_API.sSoundList.get(103)); - // setBreakingSound(GregTech_API.sSoundList.get(103)); - // setEntityHitSound(GregTech_API.sSoundList.get(103)); - // setUsageAmounts(1, 1, 1); } @Override diff --git a/src/main/java/gregtech/api/items/GT_Spray_Pepper_Item.java b/src/main/java/gregtech/api/items/GT_Spray_Pepper_Item.java index 29f6348d82..81517daa0e 100644 --- a/src/main/java/gregtech/api/items/GT_Spray_Pepper_Item.java +++ b/src/main/java/gregtech/api/items/GT_Spray_Pepper_Item.java @@ -52,8 +52,6 @@ public class GT_Spray_Pepper_Item extends GT_Tool_Item { return false; } Block aBlock = aWorld.getBlock(aX, aY, aZ); - // byte aMeta = (byte)aWorld.getBlockMetadata(aX, aY, aZ); - // TileEntity aTileEntity = aWorld.getTileEntity(aX, aY, aZ); return false; } |