aboutsummaryrefslogtreecommitdiff
path: root/src/main/java
diff options
context:
space:
mode:
authormiozune <miozune@gmail.com>2022-12-28 19:16:48 +0900
committerGitHub <noreply@github.com>2022-12-28 11:16:48 +0100
commitca871fd068d23fee4e4d02fdc6291790c71918be (patch)
tree0fb61e6f6f630c38f6492df2afe7473ec9ccc0b8 /src/main/java
parent196075f63a402c9742fae0801878d13adab1a3a9 (diff)
downloadGT5-Unofficial-ca871fd068d23fee4e4d02fdc6291790c71918be.tar.gz
GT5-Unofficial-ca871fd068d23fee4e4d02fdc6291790c71918be.tar.bz2
GT5-Unofficial-ca871fd068d23fee4e4d02fdc6291790c71918be.zip
Allow resource packs to override individual progressbar textures (#1596)
* Allow resource packs to override individual progressbar textures * spell: every + singular * Fix potential crash on server with FallbackableSteamTexture
Diffstat (limited to 'src/main/java')
-rw-r--r--src/main/java/gregtech/api/gui/modularui/FallbackableSteamTexture.java84
-rw-r--r--src/main/java/gregtech/api/metatileentity/implementations/GT_MetaTileEntity_BasicMachine.java4
-rw-r--r--src/main/java/gregtech/api/util/GT_Recipe.java44
-rw-r--r--src/main/java/gregtech/common/GT_Client.java2
4 files changed, 126 insertions, 8 deletions
diff --git a/src/main/java/gregtech/api/gui/modularui/FallbackableSteamTexture.java b/src/main/java/gregtech/api/gui/modularui/FallbackableSteamTexture.java
new file mode 100644
index 0000000000..9216d1d224
--- /dev/null
+++ b/src/main/java/gregtech/api/gui/modularui/FallbackableSteamTexture.java
@@ -0,0 +1,84 @@
+package gregtech.api.gui.modularui;
+
+import com.gtnewhorizons.modularui.api.drawable.UITexture;
+import com.gtnewhorizons.modularui.common.internal.network.NetworkUtils;
+import gregtech.api.enums.SteamVariant;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import net.minecraft.client.Minecraft;
+
+public class FallbackableSteamTexture {
+
+ private final SteamTexture candidate;
+ private final Object fallback;
+ private final Map<SteamVariant, Boolean> useFallbackMap = new HashMap<>();
+
+ private static final List<FallbackableSteamTexture> ALL_INSTANCES = new ArrayList<>();
+
+ public FallbackableSteamTexture(SteamTexture candidate, SteamTexture fallback) {
+ this(candidate, (Object) fallback);
+ }
+
+ public FallbackableSteamTexture(SteamTexture candidate, FallbackableSteamTexture fallback) {
+ this(candidate, (Object) fallback);
+ }
+
+ public FallbackableSteamTexture(SteamTexture fallback) {
+ this(null, fallback);
+ }
+
+ private FallbackableSteamTexture(SteamTexture candidate, Object fallback) {
+ this.candidate = candidate;
+ this.fallback = fallback;
+ ALL_INSTANCES.add(this);
+ }
+
+ public UITexture get(SteamVariant steamVariant) {
+ verifyCandidate(steamVariant);
+ if (useFallbackMap.get(steamVariant)) {
+ return castFallback(steamVariant);
+ } else {
+ return candidate.get(steamVariant);
+ }
+ }
+
+ private void verifyCandidate(SteamVariant steamVariant) {
+ if (useFallbackMap.get(steamVariant) == null) {
+ boolean useFallback;
+ if (NetworkUtils.isDedicatedClient()) {
+ if (candidate == null) {
+ useFallback = true;
+ } else {
+ try {
+ Minecraft.getMinecraft().getResourceManager().getResource(candidate.get(steamVariant).location);
+ useFallback = false;
+ } catch (IOException e) {
+ useFallback = true;
+ }
+ }
+ } else {
+ useFallback = true;
+ }
+ useFallbackMap.put(steamVariant, useFallback);
+ }
+ }
+
+ private UITexture castFallback(SteamVariant steamVariant) {
+ if (fallback instanceof SteamTexture) {
+ return ((SteamTexture) fallback).get(steamVariant);
+ } else if (fallback instanceof FallbackableSteamTexture) {
+ return ((FallbackableSteamTexture) fallback).get(steamVariant);
+ } else {
+ throw new RuntimeException("Unexpected type found for fallback: " + fallback.getClass());
+ }
+ }
+
+ public static void reload() {
+ for (FallbackableSteamTexture t : ALL_INSTANCES) {
+ t.useFallbackMap.clear();
+ }
+ }
+}
diff --git a/src/main/java/gregtech/api/metatileentity/implementations/GT_MetaTileEntity_BasicMachine.java b/src/main/java/gregtech/api/metatileentity/implementations/GT_MetaTileEntity_BasicMachine.java
index cb1118a70d..bbe20b6be6 100644
--- a/src/main/java/gregtech/api/metatileentity/implementations/GT_MetaTileEntity_BasicMachine.java
+++ b/src/main/java/gregtech/api/metatileentity/implementations/GT_MetaTileEntity_BasicMachine.java
@@ -1341,8 +1341,8 @@ public abstract class GT_MetaTileEntity_BasicMachine extends GT_MetaTileEntity_B
builder.widget(setNEITransferRect(
createProgressBar(
isSteampowered()
- ? getRecipeList().progressBarTextureSteam.get(getSteamVariant())
- : getRecipeList().progressBarTexture,
+ ? getRecipeList().getProgressBarTextureSteam(getSteamVariant())
+ : getRecipeList().getProgressBarTexture(),
getRecipeList().getProgressBarImageSize(),
getRecipeList().progressBarDirection,
getRecipeList().progressBarPos,
diff --git a/src/main/java/gregtech/api/util/GT_Recipe.java b/src/main/java/gregtech/api/util/GT_Recipe.java
index b13d164109..81937c42f8 100644
--- a/src/main/java/gregtech/api/util/GT_Recipe.java
+++ b/src/main/java/gregtech/api/util/GT_Recipe.java
@@ -4,6 +4,7 @@ import static gregtech.api.enums.GT_Values.*;
import codechicken.nei.PositionedStack;
import com.gtnewhorizons.modularui.api.ModularUITextures;
+import com.gtnewhorizons.modularui.api.drawable.FallbackableUITexture;
import com.gtnewhorizons.modularui.api.drawable.IDrawable;
import com.gtnewhorizons.modularui.api.drawable.UITexture;
import com.gtnewhorizons.modularui.api.forge.IItemHandlerModifiable;
@@ -21,6 +22,7 @@ import gregtech.GT_Mod;
import gregtech.api.GregTech_API;
import gregtech.api.enums.*;
import gregtech.api.enums.SteamVariant;
+import gregtech.api.gui.modularui.FallbackableSteamTexture;
import gregtech.api.gui.modularui.GT_UITextures;
import gregtech.api.gui.modularui.SteamTexture;
import gregtech.api.interfaces.tileentity.IGregTechTileEntity;
@@ -2579,7 +2581,7 @@ public class GT_Recipe implements Comparable<GT_Recipe> {
* First is (20, 18) size of "empty" image at the top,
* Second is (20, 18) size of "filled" image at the bottom.
*/
- public UITexture progressBarTexture = GT_UITextures.PROGRESSBAR_ARROW;
+ private FallbackableUITexture progressBarTexture;
/**
* Progressbar used for steam machine GUI and/or NEI.
@@ -2587,7 +2589,7 @@ public class GT_Recipe implements Comparable<GT_Recipe> {
* First is (20, 18) size of "empty" image at the top,
* Second is (20, 18) size of "filled" image at the bottom.
*/
- public SteamTexture progressBarTextureSteam;
+ private FallbackableSteamTexture progressBarTextureSteam;
public ProgressBar.Direction progressBarDirection = ProgressBar.Direction.RIGHT;
@@ -2678,6 +2680,9 @@ public class GT_Recipe implements Comparable<GT_Recipe> {
aUsualOutputCount,
aMinimalInputFluids,
aMinimalInputItems);
+ progressBarTexture = new FallbackableUITexture(
+ UITexture.fullImage("gregtech", "gui/progressbar/" + mUnlocalizedName),
+ GT_UITextures.PROGRESSBAR_ARROW);
if (sIndexedMappings.put(mUniqueIdentifier, this) != null)
throw new IllegalArgumentException("Duplicate recipe map registered: " + mUniqueIdentifier);
}
@@ -2766,17 +2771,36 @@ public class GT_Recipe implements Comparable<GT_Recipe> {
}
public GT_Recipe_Map setProgressBar(UITexture progressBarTexture, ProgressBar.Direction progressBarDirection) {
+ return setProgressBarWithFallback(
+ new FallbackableUITexture(
+ UITexture.fullImage("gregtech", "gui/progressbar/" + mUnlocalizedName), progressBarTexture),
+ progressBarDirection);
+ }
+
+ public GT_Recipe_Map setProgressBar(UITexture progressBarTexture) {
+ return setProgressBar(progressBarTexture, ProgressBar.Direction.RIGHT);
+ }
+
+ /**
+ * Some resource packs want to use custom progress bar textures even for plain arrow.
+ * This method allows them to add unique textures, yet other packs don't need to make textures
+ * for every recipemap.
+ */
+ public GT_Recipe_Map setProgressBarWithFallback(
+ FallbackableUITexture progressBarTexture, ProgressBar.Direction progressBarDirection) {
useModularUI(true);
this.progressBarTexture = progressBarTexture;
this.progressBarDirection = progressBarDirection;
return this;
}
- public GT_Recipe_Map setProgressBar(UITexture progressBarTexture) {
- return setProgressBar(progressBarTexture, ProgressBar.Direction.RIGHT);
+ public GT_Recipe_Map setProgressBarSteam(SteamTexture progressBarTexture) {
+ return setProgressBarSteamWithFallback(new FallbackableSteamTexture(
+ SteamTexture.fullImage("gregtech", "gui/progressbar/" + mUnlocalizedName + "_%s"),
+ progressBarTexture));
}
- public GT_Recipe_Map setProgressBarSteam(SteamTexture progressBarTexture) {
+ public GT_Recipe_Map setProgressBarSteamWithFallback(FallbackableSteamTexture progressBarTexture) {
this.progressBarTextureSteam = progressBarTexture;
return this;
}
@@ -3332,6 +3356,14 @@ public class GT_Recipe implements Comparable<GT_Recipe> {
return null;
}
+ public UITexture getProgressBarTexture() {
+ return progressBarTexture.get();
+ }
+
+ public UITexture getProgressBarTextureSteam(SteamVariant steamVariant) {
+ return progressBarTextureSteam.get(steamVariant);
+ }
+
public int getProgressBarImageSize() {
if (progressBarImageSize != 0) {
return progressBarImageSize;
@@ -3411,7 +3443,7 @@ public class GT_Recipe implements Comparable<GT_Recipe> {
public void addProgressBarUI(
ModularWindow.Builder builder, Supplier<Float> progressSupplier, Pos2d windowOffset) {
builder.widget(new ProgressBar()
- .setTexture(progressBarTexture, 20)
+ .setTexture(getProgressBarTexture(), 20)
.setDirection(progressBarDirection)
.setProgress(progressSupplier)
.setSynced(false, false)
diff --git a/src/main/java/gregtech/common/GT_Client.java b/src/main/java/gregtech/common/GT_Client.java
index a4150f8220..3a8f511122 100644
--- a/src/main/java/gregtech/common/GT_Client.java
+++ b/src/main/java/gregtech/common/GT_Client.java
@@ -23,6 +23,7 @@ import cpw.mods.fml.common.network.FMLNetworkEvent;
import gregtech.api.GregTech_API;
import gregtech.api.enums.*;
import gregtech.api.gui.GT_GUIColorOverride;
+import gregtech.api.gui.modularui.FallbackableSteamTexture;
import gregtech.api.interfaces.IHasFluidDisplayItem;
import gregtech.api.interfaces.tileentity.ICoverable;
import gregtech.api.interfaces.tileentity.IGregTechTileEntity;
@@ -612,6 +613,7 @@ public class GT_Client extends GT_Proxy implements Runnable {
@Override
public void onResourceManagerReload(IResourceManager l) {
GT_GUIColorOverride.onResourceManagerReload();
+ FallbackableSteamTexture.reload();
GregTech_API.sCoverBehaviors.values().forEach(GT_CoverBehaviorBase::reloadColorOverride);
}
});