Skip to content

Commit

Permalink
Rename Config class and try-catch everything
Browse files Browse the repository at this point in the history
  • Loading branch information
mathgeniuszach committed Jan 4, 2023
1 parent 92da651 commit 7c73ef7
Show file tree
Hide file tree
Showing 7 changed files with 128 additions and 108 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ targetCompatibility = "1.8"

String tweaker = 'com.mathgeniuszach.moderninfo.mixin.MixinLoader'

version = "1.1"
version = "1.2"
group = "com.mathgeniuszach.moderninfo"
archivesBaseName = "moderninfo"

Expand Down
71 changes: 38 additions & 33 deletions src/main/java/com/mathgeniuszach/moderninfo/BowChargeGui.java
Original file line number Diff line number Diff line change
@@ -1,48 +1,53 @@
package com.mathgeniuszach.moderninfo;

import com.mathgeniuszach.moderninfo.config.ModernInfoConfig;
import com.mathgeniuszach.moderninfo.config.ConfigData;

import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.Gui;
import net.minecraft.entity.player.EntityPlayer;

public class BowChargeGui extends Gui {
public void render(int screenWidth, int screenHeight) {
Minecraft mc = Minecraft.getMinecraft();
EntityPlayer player = mc.thePlayer;
try {
Minecraft mc = Minecraft.getMinecraft();
EntityPlayer player = mc.thePlayer;

int i = player.getItemInUseDuration();
int i = player.getItemInUseDuration();

float f = (float)i / 20.0F;
f = (f * f + f * 2.0F) / 3.0F;
if (f < 0.0F) f = 0.0F;
if (f > 1.0F) f = 1.0F;
float f = (float)i / 20.0F;
f = (f * f + f * 2.0F) / 3.0F;
if (f < 0.0F) f = 0.0F;
if (f > 1.0F) f = 1.0F;

int color = ModernInfoConfig.defaultColor;
if ((double)f < 0.1D) {
// Bow will not shoot an arrow
color = ModernInfoConfig.disabledColor;
if (ModernInfoConfig.pullingOnly) return;
} else if (f >= 1.0) {
// Critical zone
color = ModernInfoConfig.criticalColor;
}
int color = ConfigData.defaultColor;
if ((double)f < 0.1D) {
// Bow will not shoot an arrow
color = ConfigData.disabledColor;
if (ConfigData.pullingOnly) return;
} else if (f >= 1.0) {
// Critical zone
color = ConfigData.criticalColor;
}

// Outer Rect
drawRect(
screenWidth/2-ModernInfoConfig.width/2-1+ModernInfoConfig.xOffset,
screenHeight/2-1+ModernInfoConfig.yOffset,
screenWidth/2+ModernInfoConfig.width/2+1+ModernInfoConfig.xOffset,
screenHeight/2+ModernInfoConfig.height+1+ModernInfoConfig.yOffset,
ModernInfoConfig.backgroundColor
);
// Inner Rect
drawRect(
screenWidth/2-ModernInfoConfig.width/2+ModernInfoConfig.xOffset,
screenHeight/2+ModernInfoConfig.yOffset,
screenWidth/2-ModernInfoConfig.width/2+ModernInfoConfig.xOffset+(int)(ModernInfoConfig.width*f),
screenHeight/2+ModernInfoConfig.height+ModernInfoConfig.yOffset,
color
);
// Outer Rect
drawRect(
screenWidth/2-ConfigData.width/2-1+ConfigData.xOffset,
screenHeight/2-1+ConfigData.yOffset,
screenWidth/2+ConfigData.width/2+1+ConfigData.xOffset,
screenHeight/2+ConfigData.height+1+ConfigData.yOffset,
ConfigData.backgroundColor
);
// Inner Rect
drawRect(
screenWidth/2-ConfigData.width/2+ConfigData.xOffset,
screenHeight/2+ConfigData.yOffset,
screenWidth/2-ConfigData.width/2+ConfigData.xOffset+(int)(ConfigData.width*f),
screenHeight/2+ConfigData.height+ConfigData.yOffset,
color
);
} catch (Exception e) {
System.err.println("Error in BowChargeGui!!!");
e.printStackTrace();
}
}
}
49 changes: 27 additions & 22 deletions src/main/java/com/mathgeniuszach/moderninfo/ClientEventHandler.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.mathgeniuszach.moderninfo;

import com.mathgeniuszach.moderninfo.config.ModernInfoConfig;
import com.mathgeniuszach.moderninfo.config.ConfigData;

import net.minecraft.client.Minecraft;
import net.minecraft.entity.player.EntityPlayer;
Expand All @@ -24,31 +24,36 @@ public ClientEventHandler() {

@SubscribeEvent(receiveCanceled = true)
public void onEvent(RenderGameOverlayEvent.Pre event) {
if (event.type == ElementType.HOTBAR) {
Minecraft minecraft = Minecraft.getMinecraft();

EntityPlayer player = minecraft.thePlayer;
if (player == null) return;

// Show bow charge bar if holding bow
ItemStack stack = player.getEquipmentInSlot(0);
if (stack != null) {
Item item = stack.getItem();
if (item instanceof ItemBow && !ModernInfoConfig.bowBarDisabled) {
bowChargeGui.render(event.resolution.getScaledWidth(), event.resolution.getScaledHeight());
// Exit, if we're showing the bow bar, no need to show the hit marker
return;
try {
if (event.type == ElementType.CROSSHAIRS) {
Minecraft minecraft = Minecraft.getMinecraft();

EntityPlayer player = minecraft.thePlayer;
if (player == null) return;

// Show bow charge bar if holding bow
ItemStack stack = player.getEquipmentInSlot(0);
if (stack != null) {
Item item = stack.getItem();
if (item instanceof ItemBow && !ConfigData.bowBarDisabled) {
bowChargeGui.render(event.resolution.getScaledWidth(), event.resolution.getScaledHeight());
// Exit, if we're showing the bow bar, no need to show the hit marker
return;
}
}
}

// Show crit marker if looking at entity
if (!ModernInfoConfig.meleeMarkerDisabled) {
MovingObjectPosition mop = minecraft.objectMouseOver;
if (mop == null || mop.entityHit == null) return;
if (!ModernInfoConfig.meleeMarkerOnPlayers && mop.entityHit instanceof EntityPlayer) return;
// Show crit marker if looking at entity
if (!ConfigData.meleeMarkerDisabled) {
MovingObjectPosition mop = minecraft.objectMouseOver;
if (mop == null || mop.entityHit == null) return;
if (!ConfigData.meleeMarkerOnPlayers && mop.entityHit instanceof EntityPlayer) return;

hitGui.render(event.resolution.getScaledWidth(), event.resolution.getScaledHeight());
hitGui.render(event.resolution.getScaledWidth(), event.resolution.getScaledHeight());
}
}
} catch (Exception e) {
System.err.println("Error in ClientEventHandler!!!");
e.printStackTrace();
}
}
}
47 changes: 26 additions & 21 deletions src/main/java/com/mathgeniuszach/moderninfo/HitGui.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.mathgeniuszach.moderninfo;

import com.mathgeniuszach.moderninfo.config.ModernInfoConfig;
import com.mathgeniuszach.moderninfo.config.ConfigData;

import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.FontRenderer;
Expand All @@ -10,27 +10,32 @@

public class HitGui extends Gui {
public void render(int screenWidth, int screenHeight) {
Minecraft mc = Minecraft.getMinecraft();
EntityPlayer player = mc.thePlayer;
try {
Minecraft mc = Minecraft.getMinecraft();
EntityPlayer player = mc.thePlayer;

int color = ModernInfoConfig.meleeMarkerColor;
if (
player.fallDistance > 0.0F &&
!player.onGround &&
!player.isOnLadder() &&
!player.isInWater() &&
player.ridingEntity == null &&
!player.isPotionActive(Potion.blindness)
) {
color = ModernInfoConfig.meleeMarkerCritColor;
}
int color = ConfigData.meleeMarkerColor;
if (
player.fallDistance > 0.0F &&
!player.onGround &&
!player.isOnLadder() &&
!player.isInWater() &&
player.ridingEntity == null &&
!player.isPotionActive(Potion.blindness)
) {
color = ConfigData.meleeMarkerCritColor;
}

FontRenderer fr = mc.fontRendererObj;
fr.drawString(
ModernInfoConfig.meleeMarker,
screenWidth/2+ModernInfoConfig.meleeXOffset-fr.getStringWidth(ModernInfoConfig.meleeMarker)/2+1,
screenHeight/2+ModernInfoConfig.meleeYOffset,
color
);
FontRenderer fr = mc.fontRendererObj;
fr.drawString(
ConfigData.meleeMarker,
screenWidth/2+ConfigData.meleeXOffset-fr.getStringWidth(ConfigData.meleeMarker)/2+1,
screenHeight/2+ConfigData.meleeYOffset,
color
);
} catch (Exception e) {
System.err.println("Error in HitGui!!!");
e.printStackTrace();
}
}
}
8 changes: 4 additions & 4 deletions src/main/java/com/mathgeniuszach/moderninfo/ModernInfo.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.mathgeniuszach.moderninfo;

import com.mathgeniuszach.moderninfo.config.ModernInfoConfig;
import com.mathgeniuszach.moderninfo.config.ConfigData;
import com.mathgeniuszach.moderninfo.proxy.CommonProxy;

import net.minecraftforge.fml.common.Mod;
Expand All @@ -16,7 +16,7 @@
public class ModernInfo
{
public static final String MODID = "moderninfo";
public static final String VERSION = "1.1";
public static final String VERSION = "1.2";

@Mod.Instance(ModernInfo.MODID)
public static ModernInfo INSTANCE;
Expand All @@ -27,11 +27,11 @@ public class ModernInfo
)
public static CommonProxy PROXY;

public static ModernInfoConfig CONFIG;
public static ConfigData CONFIG;

@EventHandler
public void preInit(FMLPreInitializationEvent event) {
CONFIG = new ModernInfoConfig(event.getSuggestedConfigurationFile());
CONFIG = new ConfigData(event.getSuggestedConfigurationFile());
PROXY.preInit(event);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import net.minecraftforge.fml.relauncher.SideOnly;
import net.minecraftforge.fml.client.event.ConfigChangedEvent.OnConfigChangedEvent;

public class ModernInfoConfig {
public class ConfigData {
public static Configuration CONFIG;

public static int xOffset;
Expand Down Expand Up @@ -49,47 +49,52 @@ public class ModernInfoConfig {

public static boolean customNamesVisible;

public ModernInfoConfig(File file) {
public ConfigData(File file) {
MinecraftForge.EVENT_BUS.register(this);
CONFIG = new Configuration(file);
CONFIG.load();
loadConfig();
}

public static void loadConfig() {
xOffset = CONFIG.get("bow", "barXOffset", 0).getInt();
yOffset = CONFIG.get("bow", "barYOffset", -20).getInt();
width = CONFIG.get("bow", "barWidth", 50).getInt();
height = CONFIG.get("bow", "barHeight", 5).getInt();
try {
xOffset = CONFIG.get("bow", "barXOffset", 0).getInt();
yOffset = CONFIG.get("bow", "barYOffset", -20).getInt();
width = CONFIG.get("bow", "barWidth", 50).getInt();
height = CONFIG.get("bow", "barHeight", 5).getInt();

backgroundColor = getConfigColor("bow", "colorBackground", "80000000");
defaultColor = getConfigColor("bow", "colorDefault", "80EEEEEE");
disabledColor = getConfigColor("bow", "colorDisabled", "80808080");
criticalColor = getConfigColor("bow", "colorCritical", "80FF0000");
backgroundColor = getConfigColor("bow", "colorBackground", "80000000");
defaultColor = getConfigColor("bow", "colorDefault", "80EEEEEE");
disabledColor = getConfigColor("bow", "colorDisabled", "80808080");
criticalColor = getConfigColor("bow", "colorCritical", "80FF0000");

pullingOnly = CONFIG.get("bow", "pullingOnly", false).getBoolean();
bowBarDisabled = CONFIG.get("bow", "barDisabled", false).getBoolean();
pullingOnly = CONFIG.get("bow", "pullingOnly", false).getBoolean();
bowBarDisabled = CONFIG.get("bow", "barDisabled", false).getBoolean();



meleeMarker = CONFIG.get("melee", "markerText", "-+-").getString();
meleeMarker = CONFIG.get("melee", "markerText", "-+-").getString();

meleeXOffset = CONFIG.get("melee", "markerXOffset", 0).getInt();
meleeYOffset = CONFIG.get("melee", "markerYOffset", -20).getInt();
meleeXOffset = CONFIG.get("melee", "markerXOffset", 0).getInt();
meleeYOffset = CONFIG.get("melee", "markerYOffset", -20).getInt();

meleeMarkerColor = getConfigColor("melee", "markerDefaultColor", "FFFFFF");
meleeMarkerCritColor = getConfigColor("melee", "markerCritColor", "FF0000");
meleeMarkerColor = getConfigColor("melee", "markerDefaultColor", "FFFFFF");
meleeMarkerCritColor = getConfigColor("melee", "markerCritColor", "FF0000");

meleeMarkerOnPlayers = CONFIG.get("melee", "showOnPlayers", false).getBoolean();
meleeMarkerDisabled = CONFIG.get("melee", "disabled", false).getBoolean();
meleeMarkerOnPlayers = CONFIG.get("melee", "showOnPlayers", false).getBoolean();
meleeMarkerDisabled = CONFIG.get("melee", "disabled", false).getBoolean();



customNamesVisible = CONFIG.get("other", "customNamesVisible", true).getBoolean();
customNamesVisible = CONFIG.get("other", "customNamesVisible", true).getBoolean();



if (CONFIG.hasChanged()) CONFIG.save();
if (CONFIG.hasChanged()) CONFIG.save();
} catch (Exception e) {
System.err.println("Error in ConfigData!!!");
e.printStackTrace();
}
}

public static int getConfigColor(String category, String key, String defColor) {
Expand Down Expand Up @@ -137,9 +142,9 @@ public static int getConfigColor(String category, String key, String defColor) {
@SideOnly(Side.CLIENT)
public List<IConfigElement> getClientGuiElements() {
List<IConfigElement> list = new ArrayList<IConfigElement>();
list.add(new ConfigElement(ModernInfoConfig.CONFIG.getCategory("bow")));
list.add(new ConfigElement(ModernInfoConfig.CONFIG.getCategory("melee")));
list.add(new ConfigElement(ModernInfoConfig.CONFIG.getCategory("other")));
list.add(new ConfigElement(ConfigData.CONFIG.getCategory("bow")));
list.add(new ConfigElement(ConfigData.CONFIG.getCategory("melee")));
list.add(new ConfigElement(ConfigData.CONFIG.getCategory("other")));
return list;
}

Expand All @@ -150,7 +155,7 @@ public String getFileName(){
@SubscribeEvent
public void onConfigChanged(OnConfigChangedEvent event) {
if (event.modID.equalsIgnoreCase(ModernInfo.MODID)) {
ModernInfoConfig.loadConfig();
ConfigData.loadConfig();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

import com.mathgeniuszach.moderninfo.config.ModernInfoConfig;
import com.mathgeniuszach.moderninfo.config.ConfigData;

import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
Expand All @@ -17,7 +17,7 @@ public abstract class EntityLivingBaseMixin extends Entity {

@Inject(method = "getAlwaysRenderNameTagForRender", at = @At("HEAD"), cancellable = true)
private void getAlwaysRenderNameTag(CallbackInfoReturnable<Boolean> ci) {
if (ModernInfoConfig.customNamesVisible && this.hasCustomName()) {
if (ConfigData.customNamesVisible && this.hasCustomName()) {
ci.setReturnValue(true);
}
}
Expand Down

0 comments on commit 7c73ef7

Please sign in to comment.