Guard against null spell HUD skins in all places, fixes #566

This commit is contained in:
Electroblob77
2020-12-20 00:10:16 +00:00
parent 55bb40a684
commit d23c15476a
2 changed files with 51 additions and 40 deletions
@@ -34,12 +34,13 @@ import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.relauncher.Side; import net.minecraftforge.fml.relauncher.Side;
import javax.annotation.Nullable;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.*; import java.util.*;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.nio.charset.StandardCharsets;
@Mod.EventBusSubscriber(Side.CLIENT) @Mod.EventBusSubscriber(Side.CLIENT)
public class GuiSpellDisplay { public class GuiSpellDisplay {
@@ -96,9 +97,28 @@ public class GuiSpellDisplay {
return Collections.unmodifiableMap(skins); return Collections.unmodifiableMap(skins);
} }
/** Returns the skin that corresponds to the given key. */ /** Returns the skin that corresponds to the given key. If no such skin exists, returns the default skin as a
* fallback. If the fallback skin is missing, prints a warning to the console and returns null. */
@Nullable
public static Skin getSkin(String key){ public static Skin getSkin(String key){
return skins.get(key);
Skin skin = skins.get(key);
if(skin == null){
Wizardry.logger.info("The spell HUD skin '" + Wizardry.settings.spellHUDSkin + "' specified in the config"
+ " did not match any of the loaded skins; using the default skin as a fallback.");
skin = skins.get(Settings.DEFAULT_HUD_SKIN_KEY);
if(skin == null){
Wizardry.logger.warn("The default spell HUD skin is missing! A resource pack must have overridden it"
+ " with an invalid JSON file (default.json), please try again without any resource packs.");
return null;
}
}
return skin;
} }
// Normally when extending Gui, you'd have to have an instance to access its methods. However, we're not actually // Normally when extending Gui, you'd have to have an instance to access its methods. However, we're not actually
@@ -212,21 +232,9 @@ public class GuiSpellDisplay {
flipX = flipX == ((mainHand ? player.getPrimaryHand() : player.getPrimaryHand().opposite()) == EnumHandSide.LEFT); flipX = flipX == ((mainHand ? player.getPrimaryHand() : player.getPrimaryHand().opposite()) == EnumHandSide.LEFT);
} }
Skin skin = skins.get(Wizardry.settings.spellHUDSkin); Skin skin = getSkin(Wizardry.settings.spellHUDSkin);
if(skin == null){ if(skin == null) return; // Can't draw anything if there is no skin loaded!
Wizardry.logger.info("The spell HUD skin '" + Wizardry.settings.spellHUDSkin + "' specified in the config"
+ " did not match any of the loaded skins; using the default skin as a fallback.");
skin = skins.get(Settings.DEFAULT_HUD_SKIN_KEY);
if(skin == null){
Wizardry.logger.warn("The default spell HUD skin is missing! A resource pack must have overridden it"
+ " with an invalid JSON file (default.json), please try again without any resource packs.");
return;
}
}
GlStateManager.pushMatrix(); GlStateManager.pushMatrix();
@@ -62,25 +62,28 @@ public class GuiSelectHUDSkin extends GuiSelectString {
Skin skin = GuiSpellDisplay.getSkin((String)this.currentValue); Skin skin = GuiSpellDisplay.getSkin((String)this.currentValue);
float scale = Math.min((previewRight - previewLeft - 2*previewBorder)/(float)skin.getWidth(), if(skin != null){
(previewBottom - previewTop - 2*previewBorder)/(float)skin.getHeight());
float x = (previewLeft + previewRight)/2f - (skin.getWidth()*scale)/2; float scale = Math.min((previewRight - previewLeft - 2 * previewBorder) / (float)skin.getWidth(),
float y = (previewBottom + previewTop)/2f + (skin.getHeight()*scale)/2; (previewBottom - previewTop - 2 * previewBorder) / (float)skin.getHeight());
GlStateManager.pushMatrix(); float x = (previewLeft + previewRight) / 2f - (skin.getWidth() * scale) / 2;
float y = (previewBottom + previewTop) / 2f + (skin.getHeight() * scale) / 2;
GlStateManager.scale(scale, scale, scale); GlStateManager.pushMatrix();
skin.drawBackground((int)(x/scale), (int)(y/scale), false, false, GlStateManager.scale(scale, scale, scale);
Spells.magic_missile.getIcon(), 0.6f, false, false);
skin.drawText((int)(x/scale), (int)(y/scale), false, false, skin.drawBackground((int)(x / scale), (int)(y / scale), false, false,
Spells.none.getDisplayNameWithFormatting(), Spells.magic_missile.getIcon(), 0.6f, false, false);
Spells.magic_missile.getDisplayNameWithFormatting(),
Spells.none.getDisplayNameWithFormatting(), 0);
GlStateManager.popMatrix(); skin.drawText((int)(x / scale), (int)(y / scale), false, false,
Spells.none.getDisplayNameWithFormatting(),
Spells.magic_missile.getDisplayNameWithFormatting(),
Spells.none.getDisplayNameWithFormatting(), 0);
GlStateManager.popMatrix();
}
Skin hovered = getHoveredSkin(mouseX, mouseY); Skin hovered = getHoveredSkin(mouseX, mouseY);