aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/gregtech/api/gui
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/gregtech/api/gui')
-rw-r--r--src/main/java/gregtech/api/gui/GT_GUIColorOverride.java69
-rw-r--r--src/main/java/gregtech/api/gui/GT_GUIContainer.java2
-rw-r--r--src/main/java/gregtech/api/gui/GT_GUIScreen.java2
3 files changed, 59 insertions, 14 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);
}