Small code cleanups and Javadoc improvements
This commit is contained in:
parent
826a8b4b72
commit
514071346a
8 changed files with 46 additions and 22 deletions
|
@ -43,7 +43,7 @@ public class BetterPingDisplayMod implements ModInitializer {
|
|||
}
|
||||
|
||||
public Config getConfig() {
|
||||
return this.config;
|
||||
return config;
|
||||
}
|
||||
|
||||
public static BetterPingDisplayMod instance() {
|
||||
|
|
|
@ -13,15 +13,15 @@ public class Config {
|
|||
private static final int DEFAULT_PING_TEXT_COLOR = 0xA0A0A0;
|
||||
private static final String DEFAULT_PING_TEXT_FORMAT = "%dms";
|
||||
|
||||
private boolean autoColorPingText;
|
||||
private boolean renderPingBars;
|
||||
private final boolean autoColorPingText;
|
||||
private final boolean renderPingBars;
|
||||
private int textColor = DEFAULT_PING_TEXT_COLOR;
|
||||
private String textFormatString = DEFAULT_PING_TEXT_FORMAT;
|
||||
|
||||
public Config(ConfigData confileFileFormat) {
|
||||
if (confileFileFormat.pingTextColor.startsWith("#")) {
|
||||
public Config(ConfigData configFileFormat) {
|
||||
if (configFileFormat.pingTextColor.startsWith("#")) {
|
||||
try {
|
||||
textColor = Integer.parseInt(confileFileFormat.pingTextColor.substring(1), 16);
|
||||
textColor = Integer.parseInt(configFileFormat.pingTextColor.substring(1), 16);
|
||||
}
|
||||
catch (NumberFormatException ex) {
|
||||
BetterPingDisplayMod.LOGGER.error("Config option 'pingTextColor' is invalid - it must be a hex color code");
|
||||
|
@ -31,15 +31,15 @@ public class Config {
|
|||
BetterPingDisplayMod.LOGGER.error("Config option 'pingTextColor' is invalid - it must be a hex color code");
|
||||
}
|
||||
|
||||
if (confileFileFormat.pingTextFormatString.contains("%d")) {
|
||||
textFormatString = confileFileFormat.pingTextFormatString;
|
||||
if (configFileFormat.pingTextFormatString.contains("%d")) {
|
||||
textFormatString = configFileFormat.pingTextFormatString;
|
||||
}
|
||||
else {
|
||||
BetterPingDisplayMod.LOGGER.error("Config option 'pingTextFormatString' is invalid - it needs to contain %d");
|
||||
}
|
||||
|
||||
autoColorPingText = confileFileFormat.autoColorPingText;
|
||||
renderPingBars = confileFileFormat.renderPingBars;
|
||||
autoColorPingText = configFileFormat.autoColorPingText;
|
||||
renderPingBars = configFileFormat.renderPingBars;
|
||||
}
|
||||
|
||||
public Config() {
|
||||
|
|
|
@ -1,7 +1,8 @@
|
|||
package com.vladmarica.betterpingdisplay.hud;
|
||||
|
||||
public class ColorUtil {
|
||||
import com.google.common.annotations.VisibleForTesting;
|
||||
|
||||
public final class ColorUtil {
|
||||
public static int interpolate(int colorStart, int colorEnd, float offset) {
|
||||
if (offset < 0 || offset > 1) {
|
||||
throw new IllegalArgumentException("Offset must be between 0.0 and 1.0");
|
||||
|
@ -18,15 +19,20 @@ public class ColorUtil {
|
|||
return (newRed << 16) | (newGreen << 8) | newBlue;
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
static int getRed(int color) {
|
||||
return (color >> 16) & 0xFF;
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
static int getGreen(int color) {
|
||||
return (color >> 8) & 0xFF;
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
static int getBlue(int color) {
|
||||
return color & 0xFF;
|
||||
}
|
||||
|
||||
private ColorUtil() {}
|
||||
}
|
||||
|
|
|
@ -15,22 +15,24 @@ public final class CustomPlayerListHud {
|
|||
private static final int PING_BARS_WIDTH = 11;
|
||||
private static final Config config = BetterPingDisplayMod.instance().getConfig();
|
||||
|
||||
public static void render(MinecraftClient client, PlayerListHud hud, MatrixStack matrixStack, int width, int x, int y, PlayerListEntry player) {
|
||||
public static void renderPingDisplay(
|
||||
MinecraftClient client, PlayerListHud hud, MatrixStack matrixStack, int width, int x, int y, PlayerListEntry player) {
|
||||
TextRenderer textRenderer = client.textRenderer;
|
||||
|
||||
String pingString = String.format(config.getTextFormatString(), player.getLatency());
|
||||
int pingStringWidth = textRenderer.getWidth(pingString);
|
||||
|
||||
int pingTextColor = config.shouldAutoColorPingText() ? PingColors.getColor(player.getLatency()) : config.getTextColor();
|
||||
int textX = width + x - pingStringWidth + PING_TEXT_RENDER_OFFSET;
|
||||
|
||||
if (!config.shouldRenderPingBars()) {
|
||||
textX += PING_BARS_WIDTH;
|
||||
}
|
||||
|
||||
int pingTextColor = config.shouldAutoColorPingText() ? PingColors.getColor(player.getLatency()) : config.getTextColor();
|
||||
// Draw the ping text for the given player
|
||||
textRenderer.drawWithShadow(matrixStack, pingString, (float) textX, (float) y, pingTextColor);
|
||||
|
||||
if (config.shouldRenderPingBars()) {
|
||||
((PlayerListHudInvoker) hud).renderLatencyText(matrixStack, width, x, y, player);
|
||||
((PlayerListHudInvoker) hud).renderLatencyIcon(matrixStack, width, x, y, player);
|
||||
} else {
|
||||
// If we don't render ping bars, we need to reset the render system color so the rest
|
||||
// of the player list renders properly
|
||||
|
|
|
@ -2,7 +2,7 @@ package com.vladmarica.betterpingdisplay.hud;
|
|||
|
||||
import net.minecraft.util.math.MathHelper;
|
||||
|
||||
public class PingColors {
|
||||
public final class PingColors {
|
||||
public static final int PING_START = 0;
|
||||
public static final int PING_MID = 150;
|
||||
public static final int PING_END = 300;
|
||||
|
@ -30,8 +30,10 @@ public class PingColors {
|
|||
computeOffset(PING_MID, PING_END, Math.min(ping, PING_END)));
|
||||
}
|
||||
|
||||
static float computeOffset(int start, int end, int value) {
|
||||
private static float computeOffset(int start, int end, int value) {
|
||||
float offset = (value - start) / (float) ( end - start);
|
||||
return MathHelper.clamp(offset, 0.0F, 1.0F);
|
||||
}
|
||||
|
||||
private PingColors() {}
|
||||
}
|
||||
|
|
|
@ -9,5 +9,5 @@ import org.spongepowered.asm.mixin.gen.Invoker;
|
|||
@Mixin(PlayerListHud.class)
|
||||
public interface PlayerListHudInvoker {
|
||||
@Invoker("renderLatencyIcon")
|
||||
void renderLatencyText(MatrixStack matrices, int width, int x, int y, PlayerListEntry entry);
|
||||
void renderLatencyIcon(MatrixStack matrices, int width, int x, int y, PlayerListEntry entry);
|
||||
}
|
||||
|
|
|
@ -5,6 +5,8 @@ import net.minecraft.client.MinecraftClient;
|
|||
import net.minecraft.client.gui.hud.PlayerListHud;
|
||||
import net.minecraft.client.network.PlayerListEntry;
|
||||
import net.minecraft.client.util.math.MatrixStack;
|
||||
import net.minecraft.scoreboard.Scoreboard;
|
||||
import net.minecraft.scoreboard.ScoreboardObjective;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.spongepowered.asm.mixin.Final;
|
||||
import org.spongepowered.asm.mixin.Mixin;
|
||||
|
@ -25,13 +27,24 @@ public abstract class PlayerListHudMixin {
|
|||
@Final
|
||||
private MinecraftClient client;
|
||||
|
||||
/**
|
||||
* Increases the int constant {@code 13} in the {@link PlayerListHud#render} method by
|
||||
* {@value #PLAYER_SLOT_EXTRA_WIDTH}. This constant is used to define the width of the "slots" in the player list.
|
||||
* In order to fit the ping text, this needs to be increased.
|
||||
*/
|
||||
@ModifyConstant(method = "render", constant = @Constant(intValue = 13))
|
||||
private int on13(int original) {
|
||||
private int modifySlotWidthConstant(int original) {
|
||||
return original + PLAYER_SLOT_EXTRA_WIDTH;
|
||||
}
|
||||
|
||||
@Redirect(method = "render", at = @At(value = "INVOKE", target = "net/minecraft/client/gui/hud/PlayerListHud.renderLatencyIcon(Lnet/minecraft/client/util/math/MatrixStack;IIILnet/minecraft/client/network/PlayerListEntry;)V"))
|
||||
private void redirectLatencyDrawCall(PlayerListHud instance, MatrixStack matrices, int width, int x, int y, @NotNull PlayerListEntry entry) {
|
||||
CustomPlayerListHud.render(this.client, instance, matrices, width, x, y, entry);
|
||||
/**
|
||||
* Redirects the call to {@code renderLatencyIcon} in {@link PlayerListHud#render} to instead call
|
||||
* {@link CustomPlayerListHud#renderPingDisplay}.
|
||||
*/
|
||||
@Redirect(method = "render",
|
||||
at = @At(value = "INVOKE", target = "net/minecraft/client/gui/hud/PlayerListHud.renderLatencyIcon(Lnet/minecraft/client/util/math/MatrixStack;IIILnet/minecraft/client/network/PlayerListEntry;)V"))
|
||||
private void redirectRenderLatencyIconCall(
|
||||
PlayerListHud instance, MatrixStack matrices, int width, int x, int y, @NotNull PlayerListEntry entry) {
|
||||
CustomPlayerListHud.renderPingDisplay(client, instance, matrices, width, x, y, entry);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -9,6 +9,7 @@ import org.junit.Test;
|
|||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.JUnit4;
|
||||
|
||||
/** Unit tests for {@link ColorUtil} */
|
||||
@RunWith(JUnit4.class)
|
||||
public class ColorUtilTest {
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue