Skip to content

Commit

Permalink
Improved client reset when detecting server change.
Browse files Browse the repository at this point in the history
Refactored manager state reset class.
Improved boss bar rendering, to avoid breaking OpenGL context and to fix rendering issues.
  • Loading branch information
brunoxkk0 committed Feb 25, 2025
1 parent 335c62a commit 8cf795f
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 53 deletions.
23 changes: 23 additions & 0 deletions src/main/java/io/github/cruciblemc/necrotempus/NecroTempus.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
package io.github.cruciblemc.necrotempus;

import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.common.Mod;
import cpw.mods.fml.common.SidedProxy;
import cpw.mods.fml.common.event.FMLInitializationEvent;
import cpw.mods.fml.common.event.FMLPostInitializationEvent;
import cpw.mods.fml.common.event.FMLPreInitializationEvent;
import cpw.mods.fml.common.event.FMLServerStartingEvent;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import cpw.mods.fml.common.network.FMLNetworkEvent;
import cpw.mods.fml.common.network.NetworkRegistry;
import cpw.mods.fml.common.network.simpleimpl.SimpleNetworkWrapper;
import cpw.mods.fml.relauncher.Side;
import io.github.cruciblemc.necrotempus.modules.features.actionbar.network.ActionBarPacket;
import io.github.cruciblemc.necrotempus.modules.features.actionbar.network.ActionBarPacketHandler;
import io.github.cruciblemc.necrotempus.modules.features.bossbar.network.BossBarPacket;
import io.github.cruciblemc.necrotempus.modules.features.bossbar.network.BossBarPacketHandler;
import io.github.cruciblemc.necrotempus.modules.features.core.ClientResetState;
import io.github.cruciblemc.necrotempus.modules.features.packet.NTClientPacket;
import io.github.cruciblemc.necrotempus.modules.features.packet.NTClientPacketHandler;
import io.github.cruciblemc.necrotempus.modules.features.playertab.network.PlayerTabPacket;
Expand All @@ -22,6 +26,7 @@
import io.github.cruciblemc.necrotempus.proxy.CommonProxy;
import io.github.cruciblemc.omniconfig.api.OmniconfigAPI;
import lombok.Getter;
import net.minecraft.network.NetHandlerPlayServer;
import org.apache.logging.log4j.Logger;


Expand Down Expand Up @@ -56,6 +61,7 @@ public void preInit(FMLPreInitializationEvent event) {
logger = event.getModLog();
OmniconfigAPI.registerAnnotationConfig(NecroTempusConfig.class);
proxy.preInit(event);
FMLCommonHandler.instance().bus().register(this);
}

@Mod.EventHandler
Expand All @@ -73,4 +79,21 @@ public void serverStarting(FMLServerStartingEvent event) {
proxy.serverStarting(event);
}

@SuppressWarnings("rawtypes")
@SubscribeEvent
public void onNetworkRegister(FMLNetworkEvent.CustomPacketRegistrationEvent event) {

if (event.operation.equals("REGISTER")) {

boolean hasNecroTempus = event.registrations.contains(Tags.MODID + ":main");

if (!(event.handler instanceof NetHandlerPlayServer) && !hasNecroTempus) {
NecroTempus.getInstance().getLogger().info("Connected to a server that does not have NecroTempus, resetting client managers.");
ClientResetState.resetRender();
}

}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,13 @@ public static int size() {
return BOSS_BARS_ENTRIES.size();
}

public static void clear(){
BOSS_BARS_ENTRIES.clear();
}

@SubscribeEvent
public void onPlayerQuit(FMLNetworkEvent.ClientDisconnectionFromServerEvent event) {
BOSS_BARS_ENTRIES.clear();
clear();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -40,75 +40,59 @@ private BossBarGui() {
}

private void render(ScaledResolution scaledResolution) {

TextureManager textureManager = minecraft.getTextureManager();

if (!ClientBossBarManager.isEmpty()) {

int width = scaledResolution.getScaledWidth();
int y = 12;

Iterator<BossBar> iterator = ClientBossBarManager.iterator();

render:
{
while (iterator.hasNext()) {

BossBar bar = iterator.next();

if (bar.getCreationTime() != 0 && (bar.getCreationTime() + 2000) < System.currentTimeMillis()) {
iterator.remove();
continue;
}

if (bar.isVisible()) {

int x = (width / 2) - BAR_SIZE / 2;
GL11.glPushAttrib(GL11.GL_ALL_ATTRIB_BITS);
GL11.glDisable(GL11.GL_DEPTH_TEST);
GL11.glEnable(GL11.GL_BLEND);
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);

GL11.glPushMatrix();
while (iterator.hasNext()) {
BossBar bar = iterator.next();

drawBossBar(textureManager, x, y, bar);

String t = bar.getText().getFormattedText();

int textWidth = minecraft.fontRenderer.getStringWidth(t);

int textX = (width / 2) - (textWidth / 2);
int textY = y - 9;
if (bar.getCreationTime() != 0 && (bar.getCreationTime() + 2000) < System.currentTimeMillis()) {
iterator.remove();
continue;
}

minecraft.fontRenderer.drawStringWithShadow(t, textX, textY, 16777215);
if (bar.isVisible()) {
int x = (width / 2) - BAR_SIZE / 2;
drawBossBar(textureManager, x, y, bar);

GL11.glPopMatrix();
String t = bar.getText().getFormattedText();
int textWidth = minecraft.fontRenderer.getStringWidth(t);
int textX = (width / 2) - (textWidth / 2);
int textY = y - 9;

y += BAR_MARGIN;
minecraft.fontRenderer.drawStringWithShadow(t, textX, textY, 16777215);
y += BAR_MARGIN;

if (y >= scaledResolution.getScaledHeight() / 3) {
break render;
}
if (y >= scaledResolution.getScaledHeight() / 3) {
break;
}
}
}

GL11.glPopAttrib();
}
}

private void drawBossBar(TextureManager textureManager, int x, int y, BossBarComponent bar) {
minecraft.mcProfiler.startSection("necroTimeBossBar");
GL11.glPushMatrix();

int color = bar.getColor().intValue();

if (bar.getLazyColor() != -1) {
color = bar.getLazyColor();
}

int color = bar.getLazyColor() != -1 ? bar.getLazyColor() : bar.getColor().intValue();
float RED = ((color >> 16) & 0xFF) / 255F;
float GREEN = ((color >> 8) & 0xFF) / 255F;
float BLUE = ((color) & 0xFF) / 255F;

GL11.glColor4f(RED, GREEN, BLUE, 1F);

minecraft.mcProfiler.startSection("bars");

textureManager.bindTexture(BARS_TEXTURES);

drawTexturedModalRect(x, y, 0, 0, BAR_SIZE, BAR_SEGMENT_HEIGHT);
Expand All @@ -117,24 +101,16 @@ private void drawBossBar(TextureManager textureManager, int x, int y, BossBarCom
drawTexturedModalRect(x, y, 0, DECORATION_GAP + (bar.getType().ordinal() - 1) * BAR_SEGMENT_HEIGHT * 2, BAR_SIZE, BAR_SEGMENT_HEIGHT);
}

minecraft.mcProfiler.endSection();

minecraft.mcProfiler.startSection("barsValues");

int percentage = (int) (bar.getPercentage() * BAR_SIZE);

if (percentage > 0) {

drawTexturedModalRect(x, y, 0, BAR_SEGMENT_HEIGHT, percentage, BAR_SEGMENT_HEIGHT);

if (bar.getType() != BossBarType.FLAT) {
drawTexturedModalRect(x, y, 0, (DECORATION_GAP + (bar.getType().ordinal() - 1) * BAR_SEGMENT_HEIGHT * 2 + BAR_SEGMENT_HEIGHT), percentage, BAR_SEGMENT_HEIGHT);
}

}

minecraft.mcProfiler.endSection();

GL11.glColor4f(1F, 1F, 1F, 1F);
GL11.glPopMatrix();
minecraft.mcProfiler.endSection();
Expand All @@ -145,5 +121,4 @@ public void onRenderGui(RenderGameOverlayEvent.Pre event) {
if (event.type == RenderGameOverlayEvent.ElementType.EXPERIENCE)
instance.render(event.resolution);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@

public class ClientResetState {

public static void resetRender(){
public static void resetRender() {
ClientGuiIngameModifier.clearActionbar();
ClientBossBarManager.getInstance().onPlayerQuit(null);
ClientBossBarManager.clear();
ClientPlayerTabManager.setPlayerTab(null);
ClientTitleManager.getInstance().onPlayerQuit(null);
ClientTitleManager.getInstance().clear();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,8 @@ public void onPlayerQuit(FMLNetworkEvent.ClientDisconnectionFromServerEvent even
currentTitle = null;
}

public void clear(){
currentTitle = null;
}

}

0 comments on commit 8cf795f

Please sign in to comment.