diff options
Diffstat (limited to 'src/main/java/gregtech/api')
6 files changed, 66 insertions, 18 deletions
diff --git a/src/main/java/gregtech/api/gui/GT_GUIColorOverride.java b/src/main/java/gregtech/api/gui/GT_GUIColorOverride.java index f934102eb9..2d6268dbec 100644 --- a/src/main/java/gregtech/api/gui/GT_GUIColorOverride.java +++ b/src/main/java/gregtech/api/gui/GT_GUIColorOverride.java @@ -1,28 +1,69 @@ package gregtech.api.gui; +import com.google.common.cache.CacheBuilder; +import com.google.common.cache.CacheLoader; +import com.google.common.cache.LoadingCache; +import com.google.common.util.concurrent.UncheckedExecutionException; +import cpw.mods.fml.relauncher.FMLLaunchHandler; +import cpw.mods.fml.relauncher.Side; import gregtech.api.GregTech_API; import gregtech.api.util.ColorsMetadataSection; -import java.io.IOException; +import java.util.concurrent.ExecutionException; +import javax.annotation.Nonnull; import net.minecraft.client.Minecraft; import net.minecraft.client.resources.IResource; import net.minecraft.util.ResourceLocation; public class GT_GUIColorOverride { - + private static final Object NOT_FOUND = new Object(); + private static final LoadingCache<ResourceLocation, Object> cache = CacheBuilder.newBuilder() + .softValues() + .build(new CacheLoader<ResourceLocation, Object>() { + @Override + public Object load(@Nonnull ResourceLocation key) throws Exception { + IResource ir = Minecraft.getMinecraft().getResourceManager().getResource(key); + if (ir.hasMetadata()) return ir.getMetadata("colors"); + // return a dummy object because LoadingCache doesn't like null + return NOT_FOUND; + } + }); + private static final GT_GUIColorOverride FALLBACK = new GT_GUIColorOverride(); private ColorsMetadataSection cmSection; + public static GT_GUIColorOverride get(String fullLocation) { + // see other get for more info + if (FMLLaunchHandler.side() != Side.CLIENT) return FALLBACK; + return new GT_GUIColorOverride(new ResourceLocation(fullLocation)); + } + + public static GT_GUIColorOverride get(ResourceLocation path) { + // use dummy fallback if there isn't such thing as a resource pack. + // #side() usually has two possible return value, but since this might be called by test code, it might + // also return null when in test env. Using #isClient will cause a NPE. A plain inequality test won't. + // FMLCommonHandler's #getSide() might trigger a NPE when in test env, so no. + if (FMLLaunchHandler.side() != Side.CLIENT) return FALLBACK; + return new GT_GUIColorOverride(path); + } + + private GT_GUIColorOverride() { + cmSection = null; + } + + /** + * @deprecated use {@link #get(String)} instead. + */ + @Deprecated public GT_GUIColorOverride(String guiTexturePath) { + this(new ResourceLocation(guiTexturePath)); + } + + private GT_GUIColorOverride(ResourceLocation resourceLocation) { try { - // this is dumb, but CombTypeTest causes cascading class load - // and leads to instantiation of GT_CoverBehaviorBase - if (Minecraft.getMinecraft() == null) return; - IResource ir = - Minecraft.getMinecraft().getResourceManager().getResource(new ResourceLocation(guiTexturePath)); - if (ir.hasMetadata()) { - cmSection = (ColorsMetadataSection) ir.getMetadata("colors"); - } - } catch (IOException | NoClassDefFoundError ignore) { - // this is also dumb, but FMLCommonHandler#getEffectiveSide doesn't work during test + Object metadata = cache.get(resourceLocation); + if (metadata != NOT_FOUND) cmSection = (ColorsMetadataSection) metadata; + } catch (ExecutionException | UncheckedExecutionException ignore) { + // make sure it doesn't cache a failing entry + cache.invalidate(resourceLocation); } } @@ -41,4 +82,8 @@ public class GT_GUIColorOverride { public boolean sLoaded() { return cmSection != null; } + + public static void onResourceManagerReload() { + cache.invalidateAll(); + } } diff --git a/src/main/java/gregtech/api/gui/GT_GUIContainer.java b/src/main/java/gregtech/api/gui/GT_GUIContainer.java index 281f910d8c..efed327b46 100644 --- a/src/main/java/gregtech/api/gui/GT_GUIContainer.java +++ b/src/main/java/gregtech/api/gui/GT_GUIContainer.java @@ -27,7 +27,7 @@ public class GT_GUIContainer extends GuiContainer { public GT_GUIContainer(Container aContainer, String aGUIbackground) { super(aContainer); mGUIbackground = new ResourceLocation(mGUIbackgroundPath = aGUIbackground); - colorOverride = new GT_GUIColorOverride(aGUIbackground); + colorOverride = GT_GUIColorOverride.get(aGUIbackground); } protected int getTextColorOrDefault(String textType, int defaultColor) { diff --git a/src/main/java/gregtech/api/gui/GT_GUIScreen.java b/src/main/java/gregtech/api/gui/GT_GUIScreen.java index 470c1045a4..141f51e7c7 100644 --- a/src/main/java/gregtech/api/gui/GT_GUIScreen.java +++ b/src/main/java/gregtech/api/gui/GT_GUIScreen.java @@ -48,7 +48,7 @@ public abstract class GT_GUIScreen extends GuiScreen implements GT_IToolTipRende this.header = header; this.headerIcon = new GT_GuiFakeItemButton(this, 5, 5, null); this.mGUIbackgroundLocation = new ResourceLocation(guiTexturePath); - this.colorOverride = new GT_GUIColorOverride(guiTexturePath); + this.colorOverride = GT_GUIColorOverride.get(guiTexturePath); this.textColor = getTextColorOrDefault("title", 0xFF222222); } diff --git a/src/main/java/gregtech/api/metatileentity/BaseMetaTileEntity.java b/src/main/java/gregtech/api/metatileentity/BaseMetaTileEntity.java index b5efdc8112..8c6076a56e 100644 --- a/src/main/java/gregtech/api/metatileentity/BaseMetaTileEntity.java +++ b/src/main/java/gregtech/api/metatileentity/BaseMetaTileEntity.java @@ -118,7 +118,7 @@ public class BaseMetaTileEntity extends CommonMetaTileEntity oTextureData = 0, oUpdateData = 0, oTexturePage = 0; - private byte oLightValueClient = -1, + private byte oLightValueClient = 0, oLightValue = -1, mLightValue = 0, mOtherUpgrades = 0, diff --git a/src/main/java/gregtech/api/metatileentity/MetaTileEntity.java b/src/main/java/gregtech/api/metatileentity/MetaTileEntity.java index 586b52fd99..21a93307e5 100644 --- a/src/main/java/gregtech/api/metatileentity/MetaTileEntity.java +++ b/src/main/java/gregtech/api/metatileentity/MetaTileEntity.java @@ -138,8 +138,7 @@ public abstract class MetaTileEntity implements IMetaTileEntity, IMachineCallbac mInventory = new ItemStack[aInvSlotCount]; mName = aName; inventoryHandler = new ItemStackHandler(mInventory); - colorOverride = new GT_GUIColorOverride( - getGUITextureSet().getMainBackground().location.getResourcePath()); + colorOverride = GT_GUIColorOverride.get(getGUITextureSet().getMainBackground().location); } /** diff --git a/src/main/java/gregtech/api/util/GT_CoverBehaviorBase.java b/src/main/java/gregtech/api/util/GT_CoverBehaviorBase.java index cfa9b593fa..8991289a78 100644 --- a/src/main/java/gregtech/api/util/GT_CoverBehaviorBase.java +++ b/src/main/java/gregtech/api/util/GT_CoverBehaviorBase.java @@ -45,7 +45,11 @@ public abstract class GT_CoverBehaviorBase<T extends ISerializableObject> { protected GT_CoverBehaviorBase(Class<T> typeToken, ITexture coverTexture) { this.typeToken = typeToken; this.coverFGTexture = coverTexture; - this.colorOverride = new GT_GUIColorOverride(guiTexturePath); + reloadColorOverride(); + } + + public void reloadColorOverride() { + this.colorOverride = GT_GUIColorOverride.get(guiTexturePath); } public abstract T createDataObject(int aLegacyData); |