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 * Copyright (C) 2021 Markil 3
* <p> * <p>
* This program is free software: you can redistribute it and/or modify it under * 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; 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 * 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 * 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 * Determines whether or not to draw the crosshair, adjusting the alpha as
* the change checks are made and adjustments or overrides are done. * needed.
* *
* @param event - event data * @return If true, then cancel drawing the crosshair.
*
* @since 0.2-1.16.4-forge
*/ */
@SubscribeEvent private static boolean drawCrosshair()
public static void onGUIDraw(final RenderGameOverlayEvent event)
{ {
/*
* 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(); Minecraft mc = Minecraft.getInstance();
boolean changed = false; boolean changed = false;
int hotbarSlot; boolean canceled = false;
Item item, handItem = null;
switch (event.getType())
{
case CROSSHAIRS:
if (event instanceof RenderGameOverlayEvent.Pre)
{
if (mc.objectMouseOver != null && mc.objectMouseOver.getType() != RayTraceResult.Type.MISS) if (mc.objectMouseOver != null && mc.objectMouseOver.getType() != RayTraceResult.Type.MISS)
{ {
if (mc.objectMouseOver.getType() == RayTraceResult.Type.ENTITY) if (mc.objectMouseOver.getType() == RayTraceResult.Type.ENTITY)
@@ -358,33 +361,39 @@ public class EventBus
} }
if (changed || mainHandLock || offHandLock || crosshairTime > 0) if (changed || mainHandLock || offHandLock || crosshairTime > 0)
{ {
RenderSystem.color4f(1.0F, setAlpha(Main.getAlpha(crosshairTime > 0 ?
1.0F,
1.0F,
RenderUtils.getAlpha(crosshairTime > 0 ?
crosshairTime : crosshairTime :
VISUAL_TIME)); VISUAL_TIME));
} }
else else
{ {
event.setCanceled(true); canceled = true;
} }
if (crosshairTime > 0) if (crosshairTime > 0)
{ {
crosshairTime--; crosshairTime--;
} }
return canceled;
} }
else if (event instanceof RenderGameOverlayEvent.Post)
{ /**
RenderSystem.color4f(1.0F, 1.0F, 1.0F, 1.0F); * Updates several fields relating to what items are being held by the
} * player.
break; *
case HOTBAR: * @return If true, then there have been changes in the hotbar.
if (event instanceof RenderGameOverlayEvent.Pre) *
* @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; mainHandLock = offHandLock = false;
mapLock = 0; mapLock = 0;
event.setCanceled(true);
for (int i = 0, l = Hand.values().length; i < l; i++) for (int i = 0, l = Hand.values().length; i < l; i++)
{ {
item = item =
@@ -454,7 +463,7 @@ public class EventBus
} }
} }
} }
/** /*
* Items that have the crosshairs enabled for as long * Items that have the crosshairs enabled for as long
* as the item is actively being used. * as the item is actively being used.
*/ */
@@ -474,7 +483,7 @@ public class EventBus
} }
} }
} }
/** /*
* Items that have the crosshairs enabled for as long * Items that have the crosshairs enabled for as long
* as the item is being held at all. * as the item is being held at all.
*/ */
@@ -490,7 +499,7 @@ public class EventBus
break; break;
} }
} }
/** /*
* Maps get special treatment. The hand is locked, * Maps get special treatment. The hand is locked,
* but the crosshairs are not enabled. * but the crosshairs are not enabled.
*/ */
@@ -507,7 +516,7 @@ public class EventBus
{ {
handItem = mainHandItem; handItem = mainHandItem;
} }
else if (i == 1) else
{ {
handItem = offHandItem; 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. * Checks for a change in what slot is used.
*/ */
@@ -555,23 +580,35 @@ public class EventBus
{ {
hotbarTime--; hotbarTime--;
} }
RenderUtils.renderHotbar(mc, mc.ingameGUI,
event.getMatrixStack(), return hotbarTime == 0;
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);
} }
/** /**
* 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. * Checks for a change in current or max health.
*/ */
if (health != mc.player.getHealth()) if (health != mc.player.getHealth())
@@ -596,32 +633,32 @@ public class EventBus
* Only makes a change if the player is healthy. Otherwise, * Only makes a change if the player is healthy. Otherwise,
* the bar is shown. * the bar is shown.
*/ */
if (health / maxHealth > CONST_BOUNDARY) if (health / maxHealth > HEALTH_BOUNDARY)
{ {
RenderSystem.color4f(1.0F, setAlpha(Main.getAlpha(healthTime));
1.0F,
1.0F,
RenderUtils.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) private static boolean drawHunger()
{
RenderSystem.color4f(1.0F, 1.0F, 1.0F, 1.0F);
}
break;
case FOOD:
if (event instanceof RenderGameOverlayEvent.Pre)
{ {
/* /*
* 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) final int HUNGER_BOUNDARY = 15;
{ Minecraft mc = Minecraft.getInstance();
event.setCanceled(true); boolean canceled = hungerTime == 0 && hunger > HUNGER_BOUNDARY;
} boolean changed = false;
if (hunger != mc.player.getFoodStats().getFoodLevel()) if (hunger != mc.player.getFoodStats().getFoodLevel())
{ {
@@ -641,10 +678,7 @@ public class EventBus
if (changed) if (changed)
{ {
hungerTime = HEALTH_TIME; hungerTime = HEALTH_TIME;
RenderSystem.color4f(1.0F, setAlpha(Main.getAlpha(hungerTime));
1.0F,
1.0F,
RenderUtils.getAlpha(hungerTime));
} }
else if (hungerTime > 0) else if (hungerTime > 0)
{ {
@@ -654,45 +688,43 @@ public class EventBus
* Only makes a change if the player is satisfied. Otherwise, * Only makes a change if the player is satisfied. Otherwise,
* the bar is shown. * 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) private static boolean drawArmor()
{
RenderSystem.color4f(1.0F, 1.0F, 1.0F, 1.0F);
}
break;
case ARMOR:
if (event instanceof RenderGameOverlayEvent.Pre)
{ {
/* /*
* Renders armor along with health. * Renders armor along with health.
*/ */
if (healthTime > 0) if (healthTime > 0)
{ {
RenderSystem.color4f(1.0F, setAlpha(Main.getAlpha(healthTime));
1.0F, return false;
1.0F,
RenderUtils.getAlpha(healthTime));
} }
else return true;
{
event.setCanceled(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) private static boolean drawMountHealth()
{
RenderSystem.color4f(1.0F, 1.0F, 1.0F, 1.0F);
}
break;
case HEALTHMOUNT:
if (event instanceof RenderGameOverlayEvent.Pre)
{ {
Minecraft mc = Minecraft.getInstance();
Entity tmp = mc.player.getRidingEntity(); Entity tmp = mc.player.getRidingEntity();
boolean changed = false;
if (tmp == null) if (tmp == null)
{ {
mountHealth = -1; mountHealth = -1;
@@ -720,23 +752,26 @@ public class EventBus
{ {
mountTime--; 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, private static boolean drawJumpbar()
1.0F,
1.0F,
RenderUtils.getAlpha(mountTime));
}
else if (event instanceof RenderGameOverlayEvent.Post)
{ {
RenderSystem.color4f(1.0F, 1.0F, 1.0F, 1.0F); Minecraft mc = Minecraft.getInstance();
}
break;
case JUMPBAR:
if (event instanceof RenderGameOverlayEvent.Pre)
{
event.setCanceled(true);
if (mc.player.getHorseJumpPower() > 0) if (mc.player.getHorseJumpPower() > 0)
{ {
jumpTime = VISUAL_TIME; jumpTime = VISUAL_TIME;
@@ -745,14 +780,22 @@ public class EventBus
{ {
jumpTime--; jumpTime--;
} }
RenderUtils.renderHorseJumpBar(mc, mc.ingameGUI, return jumpTime == 0;
event.getMatrixStack(), jumpTime);
} }
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) if (experienceProgress != mc.player.experience)
{ {
experienceProgress = mc.player.experience; experienceProgress = mc.player.experience;
@@ -766,9 +809,128 @@ public class EventBus
{ {
experienceTime--; 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, RenderUtils.renderExperience(mc, mc.ingameGUI,
event.getMatrixStack(), experienceTime); event.getMatrixStack(), experienceTime);
} }
}
break; break;
} }
} }

View File

@@ -49,4 +49,19 @@ public class Main
.of(() -> FMLNetworkConstants.IGNORESERVERONLY, .of(() -> FMLNetworkConstants.IGNORESERVERONLY,
(a, b) -> true)); (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. * with some tweaks to make the mod work.
* *
* @author Mojang Studios * @author Mojang Studios
* @author Markil 3
* @version 0.1-1.16.4-forge * @version 0.1-1.16.4-forge
*/ */
public class RenderUtils 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. * Renders the horse jump bar.
* *
@@ -87,7 +71,7 @@ public class RenderUtils
return; return;
} }
float alpha = getAlpha(renderTime); float alpha = Main.getAlpha(renderTime);
int scaledWidth = mc.getMainWindow().getScaledWidth(); int scaledWidth = mc.getMainWindow().getScaledWidth();
int scaledHeight = mc.getMainWindow().getScaledHeight(); int scaledHeight = mc.getMainWindow().getScaledHeight();
int xPosition = scaledWidth / 2 - 91; int xPosition = scaledWidth / 2 - 91;
@@ -132,7 +116,7 @@ public class RenderUtils
return; return;
} }
float alpha = getAlpha(renderTime); float alpha = Main.getAlpha(renderTime);
RenderSystem.color4f(1.0F, 1.0F, 1.0F, alpha); RenderSystem.color4f(1.0F, 1.0F, 1.0F, alpha);
if (mc.playerController.gameIsSurvivalOrAdventure()) if (mc.playerController.gameIsSurvivalOrAdventure())
@@ -230,7 +214,7 @@ public class RenderUtils
return; return;
} }
float alpha = getAlpha(renderTime); float alpha = Main.getAlpha(renderTime);
int scaledWidth = mc.getMainWindow().getScaledWidth(); int scaledWidth = mc.getMainWindow().getScaledWidth();
int scaledHeight = mc.getMainWindow().getScaledHeight(); int scaledHeight = mc.getMainWindow().getScaledHeight();