diff --git a/gradle.properties b/gradle.properties index 4e68d86..c6ffb18 100644 --- a/gradle.properties +++ b/gradle.properties @@ -8,9 +8,9 @@ org.gradle.jvmargs=-Xmx1G loader_version=0.14.22 # Mod Properties - mod_version = 1.1.1 + mod_version = 1.2.1 maven_group = com.vladmarica archives_base_name = BetterPingDisplay-Fabric # Dependencies - fabric_version=0.89.3+1.20.2 + fabric_version=0.90.7+1.20.2 \ No newline at end of file diff --git a/src/main/java/com/vladmarica/betterpingdisplay/Config.java b/src/main/java/com/vladmarica/betterpingdisplay/Config.java index 90373a8..3ca3ad9 100644 --- a/src/main/java/com/vladmarica/betterpingdisplay/Config.java +++ b/src/main/java/com/vladmarica/betterpingdisplay/Config.java @@ -17,6 +17,8 @@ public class Config { private final boolean renderPingBars; private int textColor = DEFAULT_PING_TEXT_COLOR; private String textFormatString = DEFAULT_PING_TEXT_FORMAT; + private String[] blacklistServers = {}; + private String[] blacklistPlayers = {}; public Config(ConfigData configFileFormat) { if (configFileFormat.pingTextColor.startsWith("#")) { @@ -40,6 +42,8 @@ public Config(ConfigData configFileFormat) { autoColorPingText = configFileFormat.autoColorPingText; renderPingBars = configFileFormat.renderPingBars; + blacklistServers = configFileFormat.blacklistServers; + blacklistPlayers = configFileFormat.blacklistPlayers; } public Config() { @@ -62,6 +66,14 @@ public boolean shouldRenderPingBars() { return this.renderPingBars; } + public String[] getBlacklistServers() { + return this.blacklistServers; + } + + public String[] getBlacklistPlayers() { + return this.blacklistPlayers; + } + public static ConfigData loadConfigFile(File configFile) throws IOException { FileReader reader = null; try { @@ -101,5 +113,9 @@ public static class ConfigData implements Serializable { @Expose private String pingTextFormatString = "%dms"; + @Expose + private String[] blacklistServers = {}; + @Expose + private String[] blacklistPlayers = {}; } } diff --git a/src/main/java/com/vladmarica/betterpingdisplay/mixin/PlayerListHudMixin.java b/src/main/java/com/vladmarica/betterpingdisplay/mixin/PlayerListHudMixin.java index e31ceb0..806dd19 100644 --- a/src/main/java/com/vladmarica/betterpingdisplay/mixin/PlayerListHudMixin.java +++ b/src/main/java/com/vladmarica/betterpingdisplay/mixin/PlayerListHudMixin.java @@ -1,10 +1,14 @@ package com.vladmarica.betterpingdisplay.mixin; +import com.mojang.authlib.GameProfile; +import com.vladmarica.betterpingdisplay.BetterPingDisplayMod; +import com.vladmarica.betterpingdisplay.Config; import com.vladmarica.betterpingdisplay.hud.CustomPlayerListHud; import net.minecraft.client.MinecraftClient; import net.minecraft.client.gui.DrawContext; import net.minecraft.client.gui.hud.PlayerListHud; import net.minecraft.client.network.PlayerListEntry; +import net.minecraft.client.network.ServerInfo; import net.minecraft.client.util.math.MatrixStack; import net.minecraft.scoreboard.Scoreboard; import net.minecraft.scoreboard.ScoreboardObjective; @@ -13,10 +17,9 @@ import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.Unique; -import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.Constant; -import org.spongepowered.asm.mixin.injection.ModifyConstant; -import org.spongepowered.asm.mixin.injection.Redirect; +import org.spongepowered.asm.mixin.injection.*; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; +import java.util.Arrays; @Mixin(PlayerListHud.class) public abstract class PlayerListHudMixin { @@ -24,28 +27,42 @@ public abstract class PlayerListHudMixin { @Final private static final int PLAYER_SLOT_EXTRA_WIDTH = 45; + @Unique + @Final + private final Config config = BetterPingDisplayMod.instance().getConfig(); + @Shadow @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. - */ + * 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 modifySlotWidthConstant(int original) { + ServerInfo d = MinecraftClient.getInstance().getCurrentServerEntry(); + String serverIp = d == null ? "*" : d.address; + if (Arrays.asList(config.getBlacklistServers()).contains(serverIp)) return original; return original + PLAYER_SLOT_EXTRA_WIDTH; } /** - * 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/gui/DrawContext;IIILnet/minecraft/client/network/PlayerListEntry;)V")) - private void redirectRenderLatencyIconCall( - PlayerListHud instance, DrawContext context, int width, int x, int y, @NotNull PlayerListEntry entry) { - CustomPlayerListHud.renderPingDisplay(client, instance, context, width, x, y, entry); + * Redirects the call to {@code renderLatencyIcon} in {@link PlayerListHud#render} to instead call + * {@link CustomPlayerListHud#renderPingDisplay}. + */ + @Inject(method = "renderLatencyIcon", at = @At(value = "HEAD"), cancellable = true) + private void redirectRenderLatencyIconCall(DrawContext context, int width, int x, int y, PlayerListEntry entry, CallbackInfo ci) { + GameProfile userProfile = entry.getProfile(); + String username = userProfile == null ? "*" : userProfile.getName(); + if (Arrays.asList(config.getBlacklistPlayers()).contains(username)) return; + + ServerInfo d = MinecraftClient.getInstance().getCurrentServerEntry(); + String serverIp = d == null ? "*" : d.address; + if (Arrays.asList(config.getBlacklistServers()).contains(serverIp)) return; + + CustomPlayerListHud.renderPingDisplay(client, (PlayerListHud) (Object) this, context, width, x, y, entry); + ci.cancel(); } }