cache frequent enum.values() calls (#59)

This commit is contained in:
Alexdoru authored and GitHub committed 2026-03-04 16:28:06 +00:00
1 parent 9ce571f464
commit 7c38b1101a
17 files changed
+108 -59

No files matched your search

@@ -38,7 +38,7 @@ public class ItemCapsule extends Item {
public String getItemStackDisplayName(ItemStack itemStack) {
return String.format(
this.capsuleType.getLocalizedName(),
FluidType.values()[itemStack.getItemDamage()].getDisplayName());
FluidType.VALUES[itemStack.getItemDamage()].getDisplayName());
}
public ItemStack getCapsuleForLiquid(FluidType l) {
@@ -49,7 +49,7 @@ public class ItemCapsule extends Item {
@Override
@SideOnly(Side.CLIENT)
public void getSubItems(Item item, CreativeTabs tab, List itemList) {
for (FluidType l : FluidType.values()) {
for (FluidType l : FluidType.VALUES) {
if (l.available) {
itemList.add(new ItemStack(this, 1, l.ordinal()));
}
@@ -68,7 +68,7 @@ public class ItemCapsule extends Item {
CommonProxy.DOMAIN + ":capsule"
+ this.capsuleType.getName().substring(0, 1).toUpperCase()
+ this.capsuleType.getName().substring(1));
for (FluidType t : FluidType.values()) {
for (FluidType t : FluidType.VALUES) {
if (t != FluidType.EMPTY) {
t.liquidIcon = iconRegister.registerIcon(CommonProxy.DOMAIN + ":liquids/" + t.liquidID);
}
@@ -80,7 +80,7 @@ public class ItemCapsule extends Item {
public IIcon getIconFromDamageForRenderPass(int metadata, int pass) {
IIcon i = this.itemIcon;
if (metadata != 0 && pass == 0) {
i = FluidType.values()[metadata % FluidType.values().length].liquidIcon;
i = FluidType.VALUES[metadata % FluidType.VALUES.length].liquidIcon;
}
return i;
}
+6 -6
View File
@@ -38,7 +38,7 @@ public class ItemComb extends Item {
@Override
@SideOnly(Side.CLIENT)
public void getSubItems(Item item, CreativeTabs tabs, List list) {
for (CombType type : CombType.values()) {
for (CombType type : CombType.VALUES) {
if (type.showInList || Config.forestryDebugEnabled) {
list.add(this.getStackForType(type));
}
@@ -73,11 +73,11 @@ public class ItemComb extends Item {
@Override
@SideOnly(Side.CLIENT)
public int getColorFromItemStack(ItemStack stack, int pass) {
int meta = Math.max(0, Math.min(CombType.values().length - 1, stack.getItemDamage()));
int colour = CombType.values()[meta].getColours()[0];
int meta = Math.max(0, Math.min(CombType.VALUES.length - 1, stack.getItemDamage()));
int colour = CombType.VALUES[meta].getColours()[0];
if (pass >= 1) {
colour = CombType.values()[meta].getColours()[1];
colour = CombType.VALUES[meta].getColours()[1];
}
return colour;
@@ -85,12 +85,12 @@ public class ItemComb extends Item {
@Override
public String getItemStackDisplayName(ItemStack stack) {
return CombType.values()[stack.getItemDamage()].getName();
return CombType.VALUES[stack.getItemDamage()].getName();
}
@Override
public int getDamage(ItemStack stack) {
if (CombType.values().length < super.getDamage(stack)) {
if (CombType.VALUES.length < super.getDamage(stack)) {
stack.setItemDamage(0);
}
return super.getDamage(stack);
@@ -17,7 +17,7 @@ import magicbees.main.utils.TabMagicBees;
public class ItemMiscResources extends Item {
private IIcon[] icons = new IIcon[ResourceType.values().length];
private final IIcon[] icons = new IIcon[ResourceType.VALUES.length];
public ItemMiscResources() {
super();
@@ -39,7 +39,7 @@ public class ItemMiscResources extends Item {
@SideOnly(Side.CLIENT)
@SuppressWarnings({ "unchecked", "rawtypes" })
public void getSubItems(Item item, CreativeTabs tabs, List list) {
for (ResourceType type : ResourceType.values()) {
for (ResourceType type : ResourceType.VALUES) {
if (type.showInList) {
list.add(this.getStackForType(type));
}
@@ -48,9 +48,8 @@ public class ItemMiscResources extends Item {
@SideOnly(Side.CLIENT)
public void registerIcons(IIconRegister par1IconRegister) {
for (int i = 0; i < ResourceType.values().length; i++) {
this.icons[i] = par1IconRegister
.registerIcon(CommonProxy.DOMAIN + ":" + ResourceType.values()[i].getName());
for (int i = 0; i < ResourceType.VALUES.length; i++) {
this.icons[i] = par1IconRegister.registerIcon(CommonProxy.DOMAIN + ":" + ResourceType.VALUES[i].getName());
}
}
@@ -62,6 +61,6 @@ public class ItemMiscResources extends Item {
@Override
public String getItemStackDisplayName(ItemStack stack) {
return ResourceType.values()[stack.getItemDamage()].getLocalizedName();
return ResourceType.VALUES[stack.getItemDamage()].getLocalizedName();
}
}
+5 -5
View File
@@ -37,7 +37,7 @@ public class ItemPollen extends Item {
@SideOnly(Side.CLIENT)
@SuppressWarnings({ "unchecked", "rawtypes" })
public void getSubItems(Item item, CreativeTabs tabs, List list) {
for (PollenType type : PollenType.values()) {
for (PollenType type : PollenType.VALUES) {
list.add(this.getStackForType(type));
}
}
@@ -70,11 +70,11 @@ public class ItemPollen extends Item {
@Override
@SideOnly(Side.CLIENT)
public int getColorFromItemStack(ItemStack stack, int pass) {
int meta = Math.max(0, Math.min(PollenType.values().length - 1, stack.getItemDamage()));
int colour = PollenType.values()[meta].colour[0];
int meta = Math.max(0, Math.min(PollenType.VALUES.length - 1, stack.getItemDamage()));
int colour = PollenType.VALUES[meta].colourA;
if (pass >= 1) {
colour = PollenType.values()[meta].colour[1];
colour = PollenType.VALUES[meta].colourB;
}
return colour;
@@ -82,6 +82,6 @@ public class ItemPollen extends Item {
@Override
public String getItemStackDisplayName(ItemStack stack) {
return PollenType.values()[stack.getItemDamage()].getName();
return PollenType.VALUES[stack.getItemDamage()].getName();
}
}
@@ -39,7 +39,11 @@ public enum CombType {
TE_LUX("TElux", false),
TE_ENDEARING("TEendearing", false),;
private static int[][] colours = new int[][] { { 0xFF9859, 0xFFC58E }, { 0xcc3333, 0x1E160E },
/**
* Cached values() array for frequent read-only operations, the array should NOT be mutated.
*/
public static final CombType[] VALUES = values();
private static final int[][] colours = new int[][] { { 0xFF9859, 0xFFC58E }, { 0xcc3333, 0x1E160E },
{ 0x9872FF, 0x2D2D2D }, { 0x3EE0D8, 0x3A3820 }, { 0xE5425D, 0x323291 }, { 0xBCA664, 0x35332E },
{ 0x7F7171, 0x876D53 }, { 0xB7ACB7, 0x636363 }, { 0x0092e9, 0x618fff }, { 0x2F9381, 0x773C31 },
{ 0xB191D8, 0x35443B }, { 0xffff7e, 0x606308 }, { 0xff3C01, 0x5B0D10 }, { 0x0090ff, 0x102F6B },
@@ -47,10 +51,11 @@ public enum CombType {
{ 0x28D328, 0x333004 }, { 0xDDDDFF, 0x9D9DB5 }, { 0x555577, 0x2D2D56 }, { 0xCC8604, 0x41046F },
{ 0xCCCC04, 0x41046F }, { 0xCC002C, 0x6B0118 }, { 0x454545, 0x0F0F0F }, { 0xF5F3A4, 0xC9C87D },
{ 0x12E3D9, 0x069E97 }, };
private final String name;
public boolean showInList;
private CombType(String pName, boolean show) {
CombType(String pName, boolean show) {
this.name = pName;
this.showInList = show;
}
@@ -58,9 +63,6 @@ public enum CombType {
this.showInList = false;
}
private String name;
public boolean showInList;
public String getName() {
return LocalizationManager.getLocalizedString("comb." + this.name);
}
@@ -48,12 +48,16 @@ public enum FluidType {
MANA("mana"),
COAL("coal"),;
public String liquidID;
/**
* Cached values() array for frequent read-only operations, the array should NOT be mutated.
*/
public static final FluidType[] VALUES = values();
public final String liquidID;
public int iconIdx;
public boolean available = false;
public IIcon liquidIcon;
private FluidType(String l) {
FluidType(String l) {
this.liquidID = l;
}
@@ -5,17 +5,22 @@ import magicbees.main.utils.LocalizationManager;
public enum PollenType {
UNUSUAL("unusual", 0xD8417B, 0xA03059),
PHASED("phased", 0x4974B4, 0x456BA5),;
PHASED("phased", 0x4974B4, 0x456BA5);
private PollenType(String pName, int colourA, int colourB) {
/**
* Cached values() array for frequent read-only operations, the array should NOT be mutated.
*/
public static final PollenType[] VALUES = values();
private final String name;
public final int colourA;
public final int colourB;
PollenType(String pName, int colourA, int colourB) {
this.name = pName;
this.colour[0] = colourA;
this.colour[1] = colourB;
this.colourA = colourA;
this.colourB = colourB;
}
private String name;
public int[] colour = new int[2];
public String getName() {
return LocalizationManager.getLocalizedString("pollen." + this.name);
}
@@ -32,16 +32,20 @@ public enum ResourceType {
// Dummy items for Thaumanomicon research icons.
// These won't ever actually exist in-game, and so they can be moved around.
RESEARCH_STARTNODE("startNode", false),
RESEARCH_BEEINFUSION("beeInfusion", false),;
RESEARCH_BEEINFUSION("beeInfusion", false);
private ResourceType(String n, boolean show) {
/**
* Cached values() array for frequent read-only operations, the array should NOT be mutated.
*/
public static final ResourceType[] VALUES = values();
private final String name;
public boolean showInList;
ResourceType(String n, boolean show) {
this.name = n;
this.showInList = show;
}
private String name;
public boolean showInList;
public void setHidden() {
this.showInList = false;
}
@@ -822,7 +822,7 @@ public class CraftingManager {
ItemStack filled;
FluidStack liquid = null;
for (FluidType fluidType : FluidType.values()) {
for (FluidType fluidType : FluidType.VALUES) {
switch (fluidType) {
case EMPTY:
liquid = null;