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