Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Player and Server blacklisting #28

Open
wants to merge 3 commits into
base: 1.20.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
16 changes: 16 additions & 0 deletions src/main/java/com/vladmarica/betterpingdisplay/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -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("#")) {
Expand All @@ -40,6 +42,8 @@ public Config(ConfigData configFileFormat) {

autoColorPingText = configFileFormat.autoColorPingText;
renderPingBars = configFileFormat.renderPingBars;
blacklistServers = configFileFormat.blacklistServers;
blacklistPlayers = configFileFormat.blacklistPlayers;
}

public Config() {
Expand All @@ -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 {
Expand Down Expand Up @@ -101,5 +113,9 @@ public static class ConfigData implements Serializable {

@Expose
private String pingTextFormatString = "%dms";
@Expose
private String[] blacklistServers = {};
@Expose
private String[] blacklistPlayers = {};
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -13,39 +17,52 @@
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 {
@Unique
@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();
}
}