-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Major refactor + better error handling
- Loading branch information
Showing
11 changed files
with
231 additions
and
230 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package com.fwloopins.tabby.client; | ||
|
||
import com.fwloopins.tabby.client.config.TabbyConfig; | ||
import com.fwloopins.tabby.client.config.DataManager; | ||
import me.shedaniel.autoconfig.AutoConfig; | ||
import me.shedaniel.autoconfig.serializer.GsonConfigSerializer; | ||
import net.fabricmc.api.ClientModInitializer; | ||
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents; | ||
import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper; | ||
import net.minecraft.client.option.KeyBinding; | ||
import net.minecraft.client.util.InputUtil; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class TabbyClient implements ClientModInitializer { | ||
public static final Logger LOGGER = LoggerFactory.getLogger("Tabby"); | ||
|
||
@Override | ||
public void onInitializeClient() { | ||
AutoConfig.register(TabbyConfig.class, GsonConfigSerializer::new); | ||
|
||
DataManager.initFiles(); | ||
DataManager.cacheJson(); | ||
|
||
reload(); | ||
|
||
logInfo("Tabby initialised"); | ||
} | ||
|
||
public static void logInfo(String msg) { | ||
LOGGER.info("[Tabby] " + msg); | ||
} | ||
|
||
public static void logError(String msg) { | ||
LOGGER.error("[Tabby] " + msg); | ||
} | ||
|
||
public static void reload() { | ||
TabbyConfig config = AutoConfig.getConfigHolder(TabbyConfig.class).getConfig(); | ||
|
||
KeyBinding reloadBinding = KeyBindingHelper.registerKeyBinding(new KeyBinding( | ||
"key.reload", | ||
InputUtil.Type.KEYSYM, | ||
config.misc.reloadKey, | ||
"category.tabby" | ||
)); | ||
|
||
ClientTickEvents.END_CLIENT_TICK.register(client -> { | ||
while (reloadBinding.wasPressed()) { | ||
DataManager.initFiles(); | ||
DataManager.cacheJson(); | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...pins/tabby/config/ModMenuIntegration.java → ...bby/client/config/ModMenuIntegration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...m/fwloopins/tabby/config/TabbyConfig.java → ...pins/tabby/client/config/TabbyConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
165 changes: 165 additions & 0 deletions
165
src/main/java/com/fwloopins/tabby/client/mixin/PlayerListHudMixin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
package com.fwloopins.tabby.client.mixin; | ||
|
||
import com.fwloopins.tabby.client.TabbyClient; | ||
import com.fwloopins.tabby.client.config.DataManager; | ||
import com.fwloopins.tabby.client.config.TabbyConfig; | ||
import com.google.gson.JsonArray; | ||
import com.google.gson.JsonElement; | ||
import com.google.gson.JsonObject; | ||
import me.shedaniel.autoconfig.AutoConfig; | ||
import net.minecraft.client.MinecraftClient; | ||
import net.minecraft.client.gui.hud.PlayerListHud; | ||
import net.minecraft.client.network.PlayerListEntry; | ||
import net.minecraft.text.MutableText; | ||
import net.minecraft.text.Style; | ||
import net.minecraft.text.Text; | ||
import net.minecraft.util.Formatting; | ||
import org.jetbrains.annotations.Nullable; | ||
import org.spongepowered.asm.mixin.Final; | ||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.Shadow; | ||
import org.spongepowered.asm.mixin.Unique; | ||
import org.spongepowered.asm.mixin.injection.*; | ||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; | ||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
@Mixin(PlayerListHud.class) | ||
public abstract class PlayerListHudMixin { | ||
@Shadow @Final private MinecraftClient client; | ||
@Unique final List<String> colours = new ArrayList<>(Arrays.asList("DARK_RED", "RED", "GOLD", "YELLOW", "DARK_GREEN", "GREEN", "AQUA", "DARK_AQUA", "DARK_BLUE", "BLUE", "LIGHT_PURPLE", "DARK_PURPLE", "WHITE", "GRAY", "DARK_GRAY", "BLACK")); | ||
@Shadow @Nullable private Text footer; | ||
@Shadow @Nullable private Text header; | ||
@Unique TabbyConfig config = AutoConfig.getConfigHolder(TabbyConfig.class).getConfig(); | ||
|
||
@ModifyConstant(constant = @Constant(longValue = 80L), method = "collectPlayerEntries") | ||
private long modifyCount(long count) { | ||
if (config.general.maxCount <= 0 && client.getNetworkHandler() != null) | ||
return client.getNetworkHandler().getListedPlayerListEntries().size(); | ||
|
||
return config.general.maxCount; | ||
} | ||
|
||
@ModifyConstant(constant = @Constant(intValue = 20), method = "render") | ||
private int modifyMaxRows(int MAX_ROWS) { | ||
if (!config.general.adaptive) | ||
return Math.max(1, config.general.maxRows); | ||
|
||
if (config.general.maxCount <= 0 && client.getNetworkHandler() != null) { | ||
int onlinePlayers = client.getNetworkHandler().getListedPlayerListEntries().size(); | ||
return Math.max(1, onlinePlayers / config.general.adaptiveDivisor); | ||
} | ||
|
||
return (int) Math.max(1, config.general.maxCount / config.general.adaptiveDivisor); | ||
} | ||
|
||
@Inject(method = "applyGameModeFormatting", at = @At("HEAD")) | ||
private void modifyNameColour(PlayerListEntry entry, MutableText name, CallbackInfoReturnable<Text> cir) { | ||
if (config.misc.customColours) { | ||
String text = name.getString(); | ||
|
||
String uuid; | ||
try { | ||
if (client.getNetworkHandler() == null) | ||
throw new NullPointerException(); | ||
|
||
PlayerListEntry playerListEntry = client.getNetworkHandler().getPlayerListEntry(text); | ||
|
||
if (playerListEntry != null) { | ||
uuid = playerListEntry.getProfile().getId().toString(); | ||
} else { | ||
throw new NullPointerException(); | ||
} | ||
} catch (NullPointerException e) { | ||
uuid = ""; | ||
} | ||
|
||
JsonArray jsonArray; | ||
try { | ||
jsonArray = DataManager.getCachedJson().getAsJsonArray(); | ||
} catch (Exception e) { | ||
if (config.misc.debug) | ||
TabbyClient.logError("Failed to read groups.json"); | ||
|
||
return; | ||
} | ||
|
||
try { | ||
for (JsonElement group : jsonArray) { | ||
JsonArray usernamesArray = group.getAsJsonObject().getAsJsonArray("usernames"); | ||
|
||
for (JsonElement username : usernamesArray) { | ||
if (username.getAsString().equals(text) || username.getAsString().equals(uuid)) { | ||
JsonObject jsonObject = group.getAsJsonObject(); | ||
Style style = getStyle(jsonObject, text); | ||
|
||
name.setStyle(style); | ||
} | ||
} | ||
} | ||
} catch (Exception e) { | ||
if (config.misc.debug) | ||
TabbyClient.logError("Failed to parse groups.json, likely malformed JSON"); | ||
} | ||
} | ||
} | ||
|
||
@Inject(method = "setHeader", at = @At("TAIL")) | ||
private void setHeader(Text header, CallbackInfo ci) { | ||
if (!config.misc.customHeader.isEmpty()) { | ||
if (config.misc.customHeader.equals("null")) { | ||
this.header = null; | ||
} else { | ||
this.header = Text.of(config.misc.customHeader); | ||
} | ||
} | ||
} | ||
|
||
@Inject(method = "setFooter", at = @At("TAIL")) | ||
private void setFooter(Text footer, CallbackInfo ci) { | ||
if (!config.misc.customFooter.isEmpty()) { | ||
if (config.misc.customFooter.equals("null")) { | ||
this.footer = null; | ||
} else { | ||
this.footer = Text.of(config.misc.customFooter); | ||
} | ||
} | ||
} | ||
|
||
@Unique | ||
private Style getStyle(JsonObject jsonObject, String username) { | ||
Style style = Style.EMPTY; | ||
|
||
String colour = jsonObject.has("colour") ? jsonObject.get("colour").getAsString() : null; | ||
|
||
boolean bold = jsonObject.has("bold") && jsonObject.get("bold").getAsBoolean(); | ||
boolean italic = jsonObject.has("italic") && jsonObject.get("italic").getAsBoolean(); | ||
boolean underline = jsonObject.has("underline") && jsonObject.get("underline").getAsBoolean(); | ||
boolean strikethrough = jsonObject.has("strikethrough") && jsonObject.get("strikethrough").getAsBoolean(); | ||
boolean obfuscated = jsonObject.has("obfuscated") && jsonObject.get("obfuscated").getAsBoolean(); | ||
|
||
style.withBold(bold).withItalic(italic).withUnderline(underline).withStrikethrough(strikethrough).withObfuscated(obfuscated); | ||
|
||
if (colour == null) { | ||
return style; | ||
} | ||
|
||
if (colours.contains(colour)) { | ||
return style.withColor(Formatting.byName(colour)); | ||
} else { | ||
try { | ||
int rgb = Integer.parseInt(colour, 16); | ||
|
||
return style.withColor(rgb); | ||
} catch (NumberFormatException e) { | ||
if (config.misc.debug) | ||
TabbyClient.logError("Invalid value for colour found while setting style for " + username); | ||
|
||
return style; | ||
} | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.