Moved most of the timer logic to their own methods.

Not only does this make onGUIDraw a little more managable, but it also makes it more similar to the Fabric implementation.
This commit is contained in:
Markil3
2021-02-27 08:41:53 -07:00
parent 5b4c6c40c2
commit 2a0b4446bc
3 changed files with 528 additions and 367 deletions

View File

@@ -1,4 +1,4 @@
/**
/*
* Copyright (C) 2021 Markil 3
* <p>
* This program is free software: you can redistribute it and/or modify it under
@@ -187,6 +187,26 @@ public class EventBus
*/
private static float experienceProgress = -1;
/**
* Sets the transparency level of the next drawn element.
*
* @param alpha - The transparency, from 0 to 1.
*/
private static void setAlpha(float alpha)
{
RenderSystem.color4f(1.0F, 1.0F, 1.0F, alpha);
}
/**
* Resets the transparency level.
*
* @see #setAlpha(float)
*/
private static void resetAlpha()
{
setAlpha(1.0F);
}
/**
* Run whenever the player clicks a mouse button. This brings the hands into
* view, and briefly brings the health and hunger into view if the held item
@@ -311,36 +331,19 @@ public class EventBus
}
/**
* Called whenever we are drawing a part of the GUI. This is where most of
* the change checks are made and adjustments or overrides are done.
* Determines whether or not to draw the crosshair, adjusting the alpha as
* needed.
*
* @param event - event data
* @return If true, then cancel drawing the crosshair.
*
* @since 0.2-1.16.4-forge
*/
@SubscribeEvent
public static void onGUIDraw(final RenderGameOverlayEvent event)
private static boolean drawCrosshair()
{
/*
* When the health percentage falls to this level or below, the
* health bar won't disappear, allowing the player to be constantly
* reminded of their low health.
*/
final float CONST_BOUNDARY = 0.5F;
/*
* When hunger falls to this level or below, the hunger bar won't
* disappear, allowing the player to be constantly reminded of their
* low hunger.
*/
final int HUNGER_BOUNDARY = 15;
Minecraft mc = Minecraft.getInstance();
boolean changed = false;
int hotbarSlot;
Item item, handItem = null;
switch (event.getType())
{
case CROSSHAIRS:
if (event instanceof RenderGameOverlayEvent.Pre)
{
boolean canceled = false;
if (mc.objectMouseOver != null && mc.objectMouseOver.getType() != RayTraceResult.Type.MISS)
{
if (mc.objectMouseOver.getType() == RayTraceResult.Type.ENTITY)
@@ -358,33 +361,39 @@ public class EventBus
}
if (changed || mainHandLock || offHandLock || crosshairTime > 0)
{
RenderSystem.color4f(1.0F,
1.0F,
1.0F,
RenderUtils.getAlpha(crosshairTime > 0 ?
setAlpha(Main.getAlpha(crosshairTime > 0 ?
crosshairTime :
VISUAL_TIME));
}
else
{
event.setCanceled(true);
canceled = true;
}
if (crosshairTime > 0)
{
crosshairTime--;
}
return canceled;
}
else if (event instanceof RenderGameOverlayEvent.Post)
{
RenderSystem.color4f(1.0F, 1.0F, 1.0F, 1.0F);
}
break;
case HOTBAR:
if (event instanceof RenderGameOverlayEvent.Pre)
/**
* Updates several fields relating to what items are being held by the
* player.
*
* @return If true, then there have been changes in the hotbar.
*
* @see #drawHotbar()
* @since 0.2-1.16.4-forge
*/
private static boolean updateHotbar()
{
Minecraft mc = Minecraft.getInstance();
Item item, handItem;
boolean changed = false;
mainHandLock = offHandLock = false;
mapLock = 0;
event.setCanceled(true);
for (int i = 0, l = Hand.values().length; i < l; i++)
{
item =
@@ -454,7 +463,7 @@ public class EventBus
}
}
}
/**
/*
* Items that have the crosshairs enabled for as long
* as the item is actively being used.
*/
@@ -474,7 +483,7 @@ public class EventBus
}
}
}
/**
/*
* Items that have the crosshairs enabled for as long
* as the item is being held at all.
*/
@@ -490,7 +499,7 @@ public class EventBus
break;
}
}
/**
/*
* Maps get special treatment. The hand is locked,
* but the crosshairs are not enabled.
*/
@@ -507,7 +516,7 @@ public class EventBus
{
handItem = mainHandItem;
}
else if (i == 1)
else
{
handItem = offHandItem;
}
@@ -535,7 +544,23 @@ public class EventBus
}
}
}
return changed;
}
/**
* Determines whether or not to draw the hotbar, adjusting the alpha as
* needed.
*
* @return If true, then cancel drawing the hotbar.
*
* @since 0.2-1.16.4-forge
*/
private static boolean drawHotbar()
{
Minecraft mc = Minecraft.getInstance();
int hotbarSlot;
boolean changed = updateHotbar();
/*
* Checks for a change in what slot is used.
*/
@@ -555,23 +580,35 @@ public class EventBus
{
hotbarTime--;
}
RenderUtils.renderHotbar(mc, mc.ingameGUI,
event.getMatrixStack(),
event.getPartialTicks(), hotbarTime);
}
break;
case HEALTH:
if (event instanceof RenderGameOverlayEvent.Pre)
{
/*
* Skip healthy bars that haven't updated in awhile.
*/
if (healthTime == 0 && health / maxHealth > CONST_BOUNDARY)
{
event.setCanceled(true);
return hotbarTime == 0;
}
/**
* Determines whether or not to draw the health bar, adjusting the alpha as
* needed.
*
* @return If true, then cancel drawing the health.
*
* @since 0.2-1.16.4-forge
*/
private static boolean drawHealth()
{
/*
* When the health percentage falls to this level or below, the
* health bar won't disappear, allowing the player to be constantly
* reminded of their low health.
*/
final float HEALTH_BOUNDARY = 0.5F;
Minecraft mc = Minecraft.getInstance();
boolean changed = false;
/*
* Skip healthy bars that haven't updated in awhile.
*/
boolean canceled =
healthTime == 0 && health / maxHealth > HEALTH_BOUNDARY;
/*
* Checks for a change in current or max health.
*/
if (health != mc.player.getHealth())
@@ -596,32 +633,32 @@ public class EventBus
* Only makes a change if the player is healthy. Otherwise,
* the bar is shown.
*/
if (health / maxHealth > CONST_BOUNDARY)
if (health / maxHealth > HEALTH_BOUNDARY)
{
RenderSystem.color4f(1.0F,
1.0F,
1.0F,
RenderUtils.getAlpha(healthTime));
setAlpha(Main.getAlpha(healthTime));
}
return canceled;
}
/*
* Reset the transparency.
/**
* Determines whether or not to draw the hunger bar, adjusting the alpha as
* needed.
*
* @return If true, then cancel drawing the hunger bar.
*
* @since 0.2-1.16.4-forge
*/
else if (event instanceof RenderGameOverlayEvent.Post)
{
RenderSystem.color4f(1.0F, 1.0F, 1.0F, 1.0F);
}
break;
case FOOD:
if (event instanceof RenderGameOverlayEvent.Pre)
private static boolean drawHunger()
{
/*
* Skip satisfied bars that haven't updated in awhile.
* When hunger falls to this level or below, the hunger bar won't
* disappear, allowing the player to be constantly reminded of their
* low hunger.
*/
if (hungerTime == 0 && hunger > HUNGER_BOUNDARY)
{
event.setCanceled(true);
}
final int HUNGER_BOUNDARY = 15;
Minecraft mc = Minecraft.getInstance();
boolean canceled = hungerTime == 0 && hunger > HUNGER_BOUNDARY;
boolean changed = false;
if (hunger != mc.player.getFoodStats().getFoodLevel())
{
@@ -641,10 +678,7 @@ public class EventBus
if (changed)
{
hungerTime = HEALTH_TIME;
RenderSystem.color4f(1.0F,
1.0F,
1.0F,
RenderUtils.getAlpha(hungerTime));
setAlpha(Main.getAlpha(hungerTime));
}
else if (hungerTime > 0)
{
@@ -654,45 +688,43 @@ public class EventBus
* Only makes a change if the player is satisfied. Otherwise,
* the bar is shown.
*/
return canceled;
}
/*
* Reset the transparency.
/**
* Determines whether or not to draw the armor bar, adjusting the alpha as
* needed.
*
* @return If true, then cancel drawing the armor bar.
*
* @since 0.2-1.16.4-forge
*/
else if (event instanceof RenderGameOverlayEvent.Post)
{
RenderSystem.color4f(1.0F, 1.0F, 1.0F, 1.0F);
}
break;
case ARMOR:
if (event instanceof RenderGameOverlayEvent.Pre)
private static boolean drawArmor()
{
/*
* Renders armor along with health.
*/
if (healthTime > 0)
{
RenderSystem.color4f(1.0F,
1.0F,
1.0F,
RenderUtils.getAlpha(healthTime));
setAlpha(Main.getAlpha(healthTime));
return false;
}
else
{
event.setCanceled(true);
return true;
}
}
/*
* Reset the transparency.
/**
* Determines whether or not to draw the mount's health, adjusting the alpha
* as needed.
*
* @return If true, then cancel drawing the mount health bar.
*
* @since 0.2-1.16.4-forge
*/
else if (event instanceof RenderGameOverlayEvent.Post)
{
RenderSystem.color4f(1.0F, 1.0F, 1.0F, 1.0F);
}
break;
case HEALTHMOUNT:
if (event instanceof RenderGameOverlayEvent.Pre)
private static boolean drawMountHealth()
{
Minecraft mc = Minecraft.getInstance();
Entity tmp = mc.player.getRidingEntity();
boolean changed = false;
if (tmp == null)
{
mountHealth = -1;
@@ -720,23 +752,26 @@ public class EventBus
{
mountTime--;
}
/*
* Renders it along with health.
if (mountTime > 0)
{
setAlpha(Main.getAlpha(mountTime));
return false;
}
return true;
}
/**
* Determines whether or not to draw the horse jump bar, adjusting the alpha
* as needed.
*
* @return If true, then cancel drawing the jump bar.
*
* @since 0.2-1.16.4-forge
*/
RenderSystem.color4f(1.0F,
1.0F,
1.0F,
RenderUtils.getAlpha(mountTime));
}
else if (event instanceof RenderGameOverlayEvent.Post)
private static boolean drawJumpbar()
{
RenderSystem.color4f(1.0F, 1.0F, 1.0F, 1.0F);
}
break;
case JUMPBAR:
if (event instanceof RenderGameOverlayEvent.Pre)
{
event.setCanceled(true);
Minecraft mc = Minecraft.getInstance();
if (mc.player.getHorseJumpPower() > 0)
{
jumpTime = VISUAL_TIME;
@@ -745,14 +780,22 @@ public class EventBus
{
jumpTime--;
}
RenderUtils.renderHorseJumpBar(mc, mc.ingameGUI,
event.getMatrixStack(), jumpTime);
return jumpTime == 0;
}
break;
case EXPERIENCE:
if (event instanceof RenderGameOverlayEvent.Pre)
/**
* Determines whether or not to draw the experience bar, adjusting the alpha
* as needed.
*
* @return If true, then cancel drawing the experience bar.
*
* @since 0.2-1.16.4-forge
*/
private static boolean drawExperience()
{
event.setCanceled(true);
Minecraft mc = Minecraft.getInstance();
boolean changed = false;
if (experienceProgress != mc.player.experience)
{
experienceProgress = mc.player.experience;
@@ -766,9 +809,128 @@ public class EventBus
{
experienceTime--;
}
return experienceProgress == 0;
}
/**
* Called whenever we are drawing a part of the GUI. This is where most of
* the change checks are made and adjustments or overrides are done.
*
* @param event - event data
*/
@SubscribeEvent
public static void onGUIDraw(final RenderGameOverlayEvent event)
{
Minecraft mc = Minecraft.getInstance();
switch (event.getType())
{
case CROSSHAIRS:
if (event instanceof RenderGameOverlayEvent.Pre)
{
if (drawCrosshair())
{
event.setCanceled(true);
}
}
/*
* Reset the transparency.
*/
else if (event instanceof RenderGameOverlayEvent.Post)
{
resetAlpha();
}
break;
case HOTBAR:
if (event instanceof RenderGameOverlayEvent.Pre)
{
event.setCanceled(true);
if (!drawHotbar())
{
RenderUtils.renderHotbar(mc, mc.ingameGUI,
event.getMatrixStack(),
event.getPartialTicks(), hotbarTime);
}
}
break;
case HEALTH:
if (event instanceof RenderGameOverlayEvent.Pre)
{
if (drawHealth())
{
event.setCanceled(true);
}
}
else if (event instanceof RenderGameOverlayEvent.Post)
{
resetAlpha();
}
break;
case FOOD:
if (event instanceof RenderGameOverlayEvent.Pre)
{
if (drawHunger())
{
event.setCanceled(true);
}
}
/*
* Reset the transparency.
*/
else if (event instanceof RenderGameOverlayEvent.Post)
{
resetAlpha();
}
break;
case ARMOR:
if (event instanceof RenderGameOverlayEvent.Pre)
{
if (drawArmor())
{
event.setCanceled(true);
}
}
/*
* Reset the transparency.
*/
else if (event instanceof RenderGameOverlayEvent.Post)
{
resetAlpha();
}
break;
case HEALTHMOUNT:
if (event instanceof RenderGameOverlayEvent.Pre)
{
if (drawMountHealth())
{
event.setCanceled(true);
}
}
else if (event instanceof RenderGameOverlayEvent.Post)
{
resetAlpha();
}
break;
case JUMPBAR:
if (event instanceof RenderGameOverlayEvent.Pre)
{
event.setCanceled(true);
if (!drawJumpbar())
{
RenderUtils.renderHorseJumpBar(mc, mc.ingameGUI,
event.getMatrixStack(), jumpTime);
}
}
break;
case EXPERIENCE:
if (event instanceof RenderGameOverlayEvent.Pre)
{
event.setCanceled(true);
if (!drawExperience())
{
RenderUtils.renderExperience(mc, mc.ingameGUI,
event.getMatrixStack(), experienceTime);
}
}
break;
}
}

View File

@@ -49,4 +49,19 @@ public class Main
.of(() -> FMLNetworkConstants.IGNORESERVERONLY,
(a, b) -> true));
}
/**
* A utility method to handle the transparency timing. It will (for the most
* part), make
*
* @param renderTime - How many ticks until the element entirely
* disappears.
*
* @return The proper transparency to render something.
*/
static float getAlpha(int renderTime)
{
return Math.min(renderTime / 20.0F,
1.0F);
}
}

View File

@@ -46,26 +46,10 @@ import net.minecraft.world.World;
* with some tweaks to make the mod work.
*
* @author Mojang Studios
* @author Markil 3
* @version 0.1-1.16.4-forge
*/
public class RenderUtils
{
/**
* A utility method to handle the transparency timing. It will (for the most
* part), make
*
* @param renderTime - How many ticks until the element entirely
* disappears.
*
* @author Markil 3
*/
static float getAlpha(int renderTime)
{
return Math.min(renderTime / 20.0F,
1.0F);
}
/**
* Renders the horse jump bar.
*
@@ -87,7 +71,7 @@ public class RenderUtils
return;
}
float alpha = getAlpha(renderTime);
float alpha = Main.getAlpha(renderTime);
int scaledWidth = mc.getMainWindow().getScaledWidth();
int scaledHeight = mc.getMainWindow().getScaledHeight();
int xPosition = scaledWidth / 2 - 91;
@@ -132,7 +116,7 @@ public class RenderUtils
return;
}
float alpha = getAlpha(renderTime);
float alpha = Main.getAlpha(renderTime);
RenderSystem.color4f(1.0F, 1.0F, 1.0F, alpha);
if (mc.playerController.gameIsSurvivalOrAdventure())
@@ -230,7 +214,7 @@ public class RenderUtils
return;
}
float alpha = getAlpha(renderTime);
float alpha = Main.getAlpha(renderTime);
int scaledWidth = mc.getMainWindow().getScaledWidth();
int scaledHeight = mc.getMainWindow().getScaledHeight();