Skip to content

Commit

Permalink
Cache most values() calls due to memory usage
Browse files Browse the repository at this point in the history
  • Loading branch information
Roadhog360 committed Nov 16, 2023
1 parent bcd1915 commit 56dd2fe
Show file tree
Hide file tree
Showing 30 changed files with 101 additions and 76 deletions.
2 changes: 2 additions & 0 deletions src/main/java/ganymedes01/etfuturum/ModBlocks.java
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,8 @@ public enum ModBlocks {
@Deprecated
public static final Block red_sandstone = RED_SANDSTONE.get();

public static final ModBlocks[] VALUES = values();

public static void init() {
for (ModBlocks block : values()) {
if (block.isEnabled()) {
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/ganymedes01/etfuturum/ModItems.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ public enum ModItems {
@Deprecated
public static final Item sweet_berries = SWEET_BERRIES.get();

public static final ModItems[] VALUES = values();

public static void init() {
for (ModItems item : values()) {
if (item.isEnabled()) { //Honestly what do you think it's doing lmfao
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public Item getItemDropped(int p_149650_1_, Random p_149650_2_, int p_149650_3_)
@Override
public void updateTick(World world, int x, int y, int z, Random rand) {
if (rand.nextInt(5) == 0) {
EnumFacing facing = EnumFacing.getFront(rand.nextInt(EnumFacing.values().length));
EnumFacing facing = EnumFacing.getFront(rand.nextInt(Utils.ENUM_FACING_VALUES.length));
Block block = world.getBlock(x + facing.getFrontOffsetX(), y + facing.getFrontOffsetY(), z + facing.getFrontOffsetZ());
int meta = world.getBlockMetadata(x + facing.getFrontOffsetX(), y + facing.getFrontOffsetY(), z + facing.getFrontOffsetZ());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public boolean canSurviveAt(World world, int x, int y, int z) {
boolean flag = world.isAirBlock(x, y + 1, z);
boolean flag1 = world.isAirBlock(x, y - 1, z);

for (EnumFacing enumfacing : EnumFacing.values()) {
for (EnumFacing enumfacing : Utils.ENUM_FACING_VALUES) {
if (enumfacing.getFrontOffsetY() != 0) continue;

Block block = world.getBlock(x + enumfacing.getFrontOffsetX(), y, z + enumfacing.getFrontOffsetZ());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public void onNeighborBlockChange(World world, int x, int y, int z, Block block)
}

private boolean setBlock(World world, int x, int y, int z) {
for (ForgeDirection dir : ForgeDirection.values()) {
for (ForgeDirection dir : ForgeDirection.VALID_DIRECTIONS) {
if (dir != ForgeDirection.DOWN && world.getBlock(x + dir.offsetX, y + dir.offsetY, z + dir.offsetZ).getMaterial() == Material.water) {
world.setBlock(x, y, z, ModBlocks.CONCRETE.get(), world.getBlockMetadata(x, y, z), 3);
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public int getMobilityFlag() {
public void onBlockPlacedBy(World world, int x, int y, int z, EntityLivingBase entity, ItemStack stack) {
TileEntityShulkerBox box = (TileEntityShulkerBox) world.getTileEntity(x, y, z);
if (stack.hasTagCompound()) {
box.type = ShulkerBoxType.values()[stack.getTagCompound().getByte("Type")];
box.type = ShulkerBoxType.VALUES[stack.getTagCompound().getByte("Type")];
box.chestContents = new ItemStack[box.getSizeInventory()];

NBTTagList nbttaglist = stack.getTagCompound().getTagList("Items", 10);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import ganymedes01.etfuturum.lib.EnumColour;
import ganymedes01.etfuturum.lib.EnumColor;
import ganymedes01.etfuturum.recipes.ModRecipes;
import ganymedes01.etfuturum.tileentities.TileEntityBanner;
import ganymedes01.etfuturum.tileentities.TileEntityBanner.EnumBannerPattern;
Expand Down Expand Up @@ -105,7 +105,7 @@ public void addInformation(ItemStack stack, EntityPlayer playerIn, List tooltip,

for (int i = 0; i < nbttaglist.tagCount() && i < 6; i++) {
NBTTagCompound nbt = nbttaglist.getCompoundTagAt(i);
EnumColour color = EnumColour.fromDamage(nbt.getInteger("Color"));
EnumColor color = EnumColor.fromDamage(nbt.getInteger("Color"));
EnumBannerPattern pattern = EnumBannerPattern.getPatternByID(nbt.getString("Pattern"));

if (pattern != null) {
Expand All @@ -121,7 +121,7 @@ public int getColorFromItemStack(ItemStack stack, int renderPass) {
if (renderPass == 0) {
return 0xFFFFFF;
}
EnumColour EnumColour = getBaseColor(stack);
EnumColor EnumColour = getBaseColor(stack);
return EnumColour.getRGB();
}

Expand All @@ -133,14 +133,14 @@ public void getSubItems(Item item, CreativeTabs tab, List subItems) {
}
}

private EnumColour getBaseColor(ItemStack stack) {
private EnumColor getBaseColor(ItemStack stack) {
NBTTagCompound nbttagcompound = getSubTag(stack, "BlockEntityTag", false);
EnumColour color;
EnumColor color;

if (nbttagcompound != null && nbttagcompound.hasKey("Base")) {
color = EnumColour.fromDamage(nbttagcompound.getInteger("Base"));
color = EnumColor.fromDamage(nbttagcompound.getInteger("Base"));
} else {
color = EnumColour.fromDamage(stack.getItemDamage());
color = EnumColor.fromDamage(stack.getItemDamage());
}

return color;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import ganymedes01.etfuturum.lib.EnumColour;
import ganymedes01.etfuturum.lib.EnumColor;
import net.minecraft.block.material.MapColor;
import net.minecraft.client.renderer.texture.AbstractTexture;
import net.minecraft.client.renderer.texture.TextureUtil;
Expand All @@ -28,9 +28,9 @@ public class LayeredColorMaskTexture extends AbstractTexture {
*/
private final ResourceLocation textureLocation;
private final List<String> field_174949_h;
private final List<EnumColour> field_174950_i;
private final List<EnumColor> field_174950_i;

public LayeredColorMaskTexture(ResourceLocation textureLocationIn, List<String> p_i46101_2_, List<EnumColour> p_i46101_3_) {
public LayeredColorMaskTexture(ResourceLocation textureLocationIn, List<String> p_i46101_2_, List<EnumColor> p_i46101_3_) {
textureLocation = textureLocationIn;
field_174949_h = p_i46101_2_;
field_174950_i = p_i46101_3_;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

public class GuiShulkerBox extends GuiContainer {

private static final ResourceLocation[] backgrounds = Arrays.stream(TileEntityShulkerBox.ShulkerBoxType.values())
private static final ResourceLocation[] backgrounds = Arrays.stream(ShulkerBoxType.VALUES)
.map(t -> t.getGuiTextureName() == null ? new ResourceLocation("textures/gui/container/generic_54.png") :
new ResourceLocation(String.format("etfuturum:textures/gui/container/ironshulkerbox/%s.png", t.getGuiTextureName())))
.toArray(ResourceLocation[]::new);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public boolean shouldUseRenderHelper(ItemRenderType type, ItemStack item, ItemRe
public void renderItem(ItemRenderType type, ItemStack item, Object... data) {

box.color = item.hasTagCompound() ? item.getTagCompound().getByte("Color") : 0;
box.type = TileEntityShulkerBox.ShulkerBoxType.values()[item.hasTagCompound() ? item.getTagCompound().getByte("Type") : 0];
box.type = TileEntityShulkerBox.ShulkerBoxType.VALUES[item.hasTagCompound() ? item.getTagCompound().getByte("Type") : 0];

OpenGLHelper.pushMatrix();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import ganymedes01.etfuturum.client.LayeredColorMaskTexture;
import ganymedes01.etfuturum.client.OpenGLHelper;
import ganymedes01.etfuturum.client.model.ModelBanner;
import ganymedes01.etfuturum.lib.EnumColour;
import ganymedes01.etfuturum.lib.EnumColor;
import ganymedes01.etfuturum.tileentities.TileEntityBanner;
import ganymedes01.etfuturum.tileentities.TileEntityBanner.EnumBannerPattern;
import net.minecraft.client.Minecraft;
Expand Down Expand Up @@ -54,7 +54,7 @@ private ResourceLocation getTexture(TileEntityBanner banner) {
}

List<EnumBannerPattern> list1 = banner.getPatternList();
List<EnumColour> list = banner.getColorList();
List<EnumColor> list = banner.getColorList();
ArrayList<String> arraylist = Lists.newArrayList();
Iterator<EnumBannerPattern> patters = list1.iterator();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public void renderTileEntityAt(TileEntityShulkerBox te, double x, double y, doub

if (te.hasWorldObj()) {
int facing = te.facing;
enumfacing = ForgeDirection.values()[facing];
enumfacing = ForgeDirection.VALID_DIRECTIONS[facing];
}

GL11.glPushAttrib(GL11.GL_ENABLE_BIT);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public void loadConfig() {
API.addItemListEntry(firework);
}

for (ModBlocks mb : ModBlocks.values()) {
for (ModBlocks mb : ModBlocks.VALUES) {
if (mb.isEnabled() && mb.get() instanceof BlockSlab && mb.name().toLowerCase().contains("double")) {
API.hideItem(mb.newItemStack(1, OreDictionary.WILDCARD_VALUE));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import ganymedes01.etfuturum.ModBlocks;
import ganymedes01.etfuturum.blocks.itemblocks.ItemBlockBanner;
import ganymedes01.etfuturum.core.utils.Utils;
import ganymedes01.etfuturum.lib.EnumColour;
import ganymedes01.etfuturum.lib.EnumColor;
import ganymedes01.etfuturum.recipes.ModRecipes;
import ganymedes01.etfuturum.tileentities.TileEntityBanner;
import ganymedes01.etfuturum.tileentities.TileEntityBanner.EnumBannerPattern;
Expand Down Expand Up @@ -57,7 +57,7 @@ public void loadCraftingRecipes(String outputId, Object... results) {
return;
}

for (EnumBannerPattern pattern : EnumBannerPattern.values()) {
for (EnumBannerPattern pattern : EnumBannerPattern.VALUES) {
if (!pattern.hasValidCrafting())
continue;
if (pattern.hasCraftingStack()) {
Expand Down Expand Up @@ -94,7 +94,7 @@ public void loadCraftingRecipes(ItemStack result) {

ItemStack copy = ModBlocks.BANNER.newItemStack(1, result.getItemDamage());
copy.setTagCompound(null);
EnumColour colour = patternNBT.hasKey("Color") ? EnumColour.fromDamage(patternNBT.getInteger("Color")) : null;
EnumColor colour = patternNBT.hasKey("Color") ? EnumColor.fromDamage(patternNBT.getInteger("Color")) : null;
if (!pattern.hasValidCrafting())
continue;
if (pattern.hasCraftingStack()) {
Expand All @@ -121,7 +121,7 @@ public void loadCraftingRecipes(ItemStack result) {
@Override
public void loadUsageRecipes(ItemStack ingredient) {
if (ingredient.getItem() == Item.getItemFromBlock(ModBlocks.BANNER.get()) && TileEntityBanner.getPatterns(ingredient) < 6)
for (EnumBannerPattern pattern : EnumBannerPattern.values()) {
for (EnumBannerPattern pattern : EnumBannerPattern.VALUES) {
if (!pattern.hasValidCrafting())
continue;
else if (pattern.hasCraftingStack()) {
Expand All @@ -145,7 +145,7 @@ else if (pattern.hasCraftingStack()) {
}
}
else
for (EnumBannerPattern pattern : EnumBannerPattern.values())
for (EnumBannerPattern pattern : EnumBannerPattern.VALUES)
if (!pattern.hasValidCrafting())
continue;
else if (pattern.hasCraftingStack() && OreDictionary.itemMatches(pattern.getCraftingStack(), ingredient, false)) {
Expand Down Expand Up @@ -178,9 +178,9 @@ else if (pattern.hasCraftingStack() && OreDictionary.itemMatches(pattern.getCraf
}
}

EnumColour getEnumColour(ItemStack stack) {
EnumColor getEnumColour(ItemStack stack) {
for (String ore : Utils.getOreNames(stack))
for (EnumColour colour : EnumColour.values())
for (EnumColor colour : EnumColor.VALUES)
if (ore.equals(colour.getOreName()))
return colour;
return null;
Expand Down Expand Up @@ -234,7 +234,7 @@ public List<PositionedStack> getIngredients() {

@Override
public PositionedStack getResult() {
EnumColour colour = null;
EnumColor colour = null;
ItemStack banner = null;
for (PositionedStack stack : getIngredients()) {
if (stack.item.getItem() == Item.getItemFromBlock(ModBlocks.BANNER.get()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import ganymedes01.etfuturum.client.sound.ModSounds;
import ganymedes01.etfuturum.configuration.configs.*;
import ganymedes01.etfuturum.core.utils.ExternalContent;
import ganymedes01.etfuturum.core.utils.Utils;
import ganymedes01.etfuturum.elytra.IElytraEntityTrackerEntry;
import ganymedes01.etfuturum.elytra.IElytraPlayer;
import ganymedes01.etfuturum.entities.*;
Expand Down Expand Up @@ -1222,7 +1223,7 @@ public void naturalSpawnEvent(SpecialSpawn event) {
if (ConfigTweaks.spawnAnywhereShulkerColors) {
World world = event.world;

for (EnumFacing facing : EnumFacing.values()) {
for (EnumFacing facing : Utils.ENUM_FACING_VALUES) {
Block block = world.getBlock(x + facing.getFrontOffsetX(), y + facing.getFrontOffsetY(), z + facing.getFrontOffsetZ());
byte color = -1;
int meta = world.getBlockMetadata(x + facing.getFrontOffsetX(), y + facing.getFrontOffsetY(), z + facing.getFrontOffsetZ());
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/ganymedes01/etfuturum/core/utils/Rotation.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ public enum Rotation {
COUNTERCLOCKWISE_90("rotate_270");//West

private final String name;
private static final String[] rotationNames = new String[values().length];
private static final String[] ROTATION_NAMES = new String[values().length];
public static final Rotation[] VALUES = values();

Rotation(String nameIn) {
this.name = nameIn;
Expand Down Expand Up @@ -77,7 +78,7 @@ public EnumFacing rotate(EnumFacing facing) {
return rotateY(facing);

case CLOCKWISE_180:
return EnumFacing.values()[ForgeDirection.values()[facing.ordinal()].getOpposite().ordinal()];
return Utils.ENUM_FACING_VALUES[ForgeDirection.VALID_DIRECTIONS[facing.ordinal()].getOpposite().ordinal()];

case COUNTERCLOCKWISE_90:
return rotateYCCW(facing);
Expand Down Expand Up @@ -145,7 +146,7 @@ public int rotate(int p_185833_1_, int p_185833_2_) {
int i = 0;

for (Rotation rotation : values()) {
rotationNames[i++] = rotation.name;
ROTATION_NAMES[i++] = rotation.name;
}
}
}
6 changes: 6 additions & 0 deletions src/main/java/ganymedes01/etfuturum/core/utils/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,18 @@
import net.minecraft.util.*;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import net.minecraftforge.common.util.ForgeDirection;
import net.minecraftforge.oredict.OreDictionary;

import java.util.*;

public class Utils {

/**
* Note: Includes UNKNOWN, use ForgeDirection.VALID_DIRECTIONS to exclude it
*/
public static final ForgeDirection[] FORGE_DIRECTIONS = ForgeDirection.values();
public static final EnumFacing[] ENUM_FACING_VALUES = EnumFacing.values();
public static final float SQRT_2 = MathHelper.sqrt_float(2.0F);

public static String getUnlocalisedName(String name) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ protected ItemStack dispenseStack(IBlockSource coords, ItemStack stack) {
TileEntityShulkerBox box = (TileEntityShulkerBox) coords.getWorld().getTileEntity(x, y, z);
box.facing = enumfacing != EnumFacing.UP && coords.getWorld().getBlock(x, y - 1, z) == Blocks.air ? (byte) enumfacing.ordinal() : 1;
if (stack.hasTagCompound()) {
box.type = TileEntityShulkerBox.ShulkerBoxType.values()[stack.getTagCompound().getByte("Type")];
box.type = TileEntityShulkerBox.ShulkerBoxType.VALUES[stack.getTagCompound().getByte("Type")];

NBTTagList nbttaglist = stack.getTagCompound().getTagList("Items", 10);
box.chestContents = new ItemStack[box.getSizeInventory()];
Expand Down
14 changes: 8 additions & 6 deletions src/main/java/ganymedes01/etfuturum/entities/EntityNewBoat.java
Original file line number Diff line number Diff line change
Expand Up @@ -1015,7 +1015,7 @@ public int getForwardDirection() {
}

public void setBoatType(EntityNewBoat.Type boatType) {
getDataWatcher().updateObject(20, Integer.valueOf(boatType.ordinal()));
getDataWatcher().updateObject(20, boatType.ordinal());
}

public EntityNewBoat.Type getBoatType() {
Expand Down Expand Up @@ -1053,6 +1053,8 @@ public enum Type {
ACACIA(4, "acacia"),
DARK_OAK(5, "dark_oak");

public static final Type[] VALUES = values();

private final String name;
private final int metadata;

Expand All @@ -1074,17 +1076,17 @@ public String toString() {
}

public static EntityNewBoat.Type byId(int id) {
return values()[MathHelper.clamp_int(id, 0, values().length - 1)];
return VALUES[MathHelper.clamp_int(id, 0, VALUES.length - 1)];
}

public static EntityNewBoat.Type getTypeFromString(String nameIn) {
for (int i = 0; i < values().length; ++i) {
if (values()[i].getName().equals(nameIn)) {
return values()[i];
for (Type value : VALUES) {
if (value.getName().equals(nameIn)) {
return value;
}
}

return values()[0];
return VALUES[0];
}
}
}
Loading

0 comments on commit 56dd2fe

Please sign in to comment.