Skip to content

Commit

Permalink
Remove SharedVariables.java
Browse files Browse the repository at this point in the history
  • Loading branch information
mrbuilder1961 committed Dec 31, 2023
1 parent 05de3c2 commit 5fd271c
Show file tree
Hide file tree
Showing 10 changed files with 28 additions and 33 deletions.
3 changes: 3 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

## Chat Patches `202.6.2` for Minecraft 1.20.2 on Fabric
- Removed `SharedVariables` class and redistributed its one useful variable to `ChatPatches`

## Chat Patches `202.6.1` for Minecraft 1.20.2 on Fabric
- Remove Gradle integration system, it was completely unnecessary and unused
- Fixed messages starting with a newline not being copyable
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/obro1961/chatpatches/ChatPatches.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import obro1961.chatpatches.accessor.ChatHudAccessor;
import obro1961.chatpatches.chatlog.ChatLog;
import obro1961.chatpatches.config.Config;
import obro1961.chatpatches.util.ChatUtils;
import obro1961.chatpatches.util.Flags;
import obro1961.chatpatches.util.MiscUtils;

Expand All @@ -19,6 +20,8 @@ public class ChatPatches implements ClientModInitializer {
public static final String MOD_ID = "chatpatches";

public static Config config = Config.newConfig(false);
/** Contains the sender and timestamp data of the last received chat message. */
public static ChatUtils.MessageData msgData = ChatUtils.NIL_MSG_DATA;

private static String lastWorld = "";

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/obro1961/chatpatches/chatlog/ChatLog.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
import com.google.gson.InstanceCreator;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonSerializer;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.hud.MessageIndicator;
import net.minecraft.client.resource.language.I18n;
import net.minecraft.text.Text;
import obro1961.chatpatches.ChatPatches;
import obro1961.chatpatches.config.Config;
import obro1961.chatpatches.util.Flags;
import obro1961.chatpatches.util.SharedVariables;

import java.io.IOException;
import java.nio.charset.Charset;
Expand All @@ -29,7 +29,7 @@
* Represents the chat log file in the run directory located at {@link ChatLog#CHATLOG_PATH}.
*/
public class ChatLog {
public static final String CHATLOG_PATH = SharedVariables.FABRIC_LOADER.getGameDir().toString() + separator + "logs" + separator + "chatlog.json";
public static final String CHATLOG_PATH = FabricLoader.getInstance().getGameDir().toString() + separator + "logs" + separator + "chatlog.json";
public static final MessageIndicator RESTORED_TEXT = new MessageIndicator(0x382fb5, null, null, I18n.translate("text.chatpatches.restored"));

private static final Path file = Path.of(CHATLOG_PATH);
Expand Down
9 changes: 4 additions & 5 deletions src/main/java/obro1961/chatpatches/config/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.google.gson.JsonIOException;
import com.google.gson.JsonSyntaxException;
import com.mojang.authlib.GameProfile;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.entity.EntityType;
import net.minecraft.text.*;
Expand All @@ -24,12 +25,11 @@
import static java.io.File.separator;
import static obro1961.chatpatches.ChatPatches.LOGGER;
import static obro1961.chatpatches.ChatPatches.config;
import static obro1961.chatpatches.util.SharedVariables.FABRIC_LOADER;
import static obro1961.chatpatches.util.StringTextUtils.fillVars;
import static obro1961.chatpatches.util.StringTextUtils.toText;

public class Config {
public static final String CONFIG_PATH = FABRIC_LOADER.getConfigDir().toString() + separator + "chatpatches.json";
public static final String CONFIG_PATH = FabricLoader.getInstance().getConfigDir().toString() + separator + "chatpatches.json";
private static final Config DEFAULTS = new Config();

// categories: time, hover, counter, counter.compact, boundary, chatlog, chat.hud, chat.screen, copy
Expand All @@ -46,9 +46,8 @@ public class Config {

/** Creates a new Config or YACLConfig depending on installed mods. */
public static Config newConfig(boolean reset) {
config = (FABRIC_LOADER.isModLoaded("modmenu") && FABRIC_LOADER.isModLoaded("yet_another_config_lib_v3"))
? new YACLConfig()
: new Config();
FabricLoader fbr = FabricLoader.getInstance();
config = (fbr.isModLoaded("modmenu") && fbr.isModLoaded("yet_another_config_lib_v3")) ? new YACLConfig() : new Config();

if(!reset)
read();
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/obro1961/chatpatches/config/YACLConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import dev.isxander.yacl3.api.*;
import dev.isxander.yacl3.api.controller.*;
import dev.isxander.yacl3.gui.YACLScreen;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.resource.language.I18n;
Expand All @@ -16,7 +17,6 @@
import obro1961.chatpatches.ChatPatches;
import obro1961.chatpatches.chatlog.ChatLog;
import obro1961.chatpatches.util.Flags;
import obro1961.chatpatches.util.SharedVariables;

import java.awt.*;
import java.io.File;
Expand Down Expand Up @@ -142,7 +142,7 @@ public void set(Object value) {
});

// debug options
if(SharedVariables.FABRIC_LOADER.isDevelopmentEnvironment()) {
if(FabricLoader.getInstance().isDevelopmentEnvironment()) {
builder.category(
category(
"debug",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
import net.minecraft.text.Text;
import net.minecraft.text.TextVisitFactory;
import net.minecraft.util.Util;
import obro1961.chatpatches.ChatPatches;
import obro1961.chatpatches.mixin.gui.ChatHudMixin;
import obro1961.chatpatches.util.ChatUtils;
import obro1961.chatpatches.util.SharedVariables;
import org.apache.commons.lang3.StringUtils;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
Expand Down Expand Up @@ -46,9 +46,9 @@ public abstract class MessageHandlerMixin {
private void cacheChatData(SignedMessage message, GameProfile sender, MessageType.Parameters params, CallbackInfo ci) {
// only logs the metadata if it was a player-sent message (otherwise tries to format some commands like /msg and /me)
if( params.type().chat().translationKey().equals("chat.type.text") )
SharedVariables.lastMsg = new ChatUtils.MessageData(sender, Date.from(message.getTimestamp()), true);
ChatPatches.msgData = new ChatUtils.MessageData(sender, Date.from(message.getTimestamp()), true);
else
SharedVariables.lastMsg = ChatUtils.NIL_MSG_DATA;
ChatPatches.msgData = ChatUtils.NIL_MSG_DATA;
}

/**
Expand All @@ -61,7 +61,7 @@ private void cacheGameData(Text message, boolean overlay, CallbackInfo ci) {
String name = ChatUtils.VANILLA_MESSAGE.matcher(string).matches() ? StringUtils.substringBetween(string, "<", ">") : null;
UUID uuid = name == null ? Util.NIL_UUID : client.getSocialInteractionsManager().getUuid(name);

SharedVariables.lastMsg = !uuid.equals(Util.NIL_UUID)
ChatPatches.msgData = !uuid.equals(Util.NIL_UUID)
? new ChatUtils.MessageData(new GameProfile(uuid, name), new Date(), true)
: ChatUtils.NIL_MSG_DATA;
}
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/obro1961/chatpatches/mixin/gui/ChatHudMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@
import java.util.List;

import static obro1961.chatpatches.ChatPatches.config;
import static obro1961.chatpatches.ChatPatches.msgData;
import static obro1961.chatpatches.util.ChatUtils.MSG_INDEX;
import static obro1961.chatpatches.util.SharedVariables.lastMsg;

/**
* The main entrypoint mixin for most chat modifications.
Expand Down Expand Up @@ -147,9 +147,9 @@ private Text modifyMessage(Text m, @Local(argsOnly = true) boolean refreshing) {
return addCounter(m, refreshing); // cancels modifications when loading the chatlog or regenerating visibles

final Style style = m.getStyle();
boolean lastEmpty = lastMsg.equals(ChatUtils.NIL_MSG_DATA);
boolean lastEmpty = msgData.equals(ChatUtils.NIL_MSG_DATA);
boolean boundary = Flags.BOUNDARY_LINE.isRaised() && config.boundary && !config.vanillaClearing;
Date now = lastEmpty ? new Date() : lastMsg.timestamp();
Date now = lastEmpty ? new Date() : msgData.timestamp();
String nowTime = String.valueOf( now.getTime() ); // for copy menu and storing timestamp data! only affects the timestamp


Expand All @@ -161,9 +161,9 @@ private Text modifyMessage(Text m, @Local(argsOnly = true) boolean refreshing) {
: Text.empty().setStyle( Style.EMPTY.withInsertion(nowTime) )
)
.append(
!lastEmpty && !boundary && Config.getOption("chatNameFormat").changed() && lastMsg.vanilla()
!lastEmpty && !boundary && Config.getOption("chatNameFormat").changed() && msgData.vanilla()
? Text.empty().setStyle(style)
.append( config.formatPlayername( lastMsg.sender() ) ) // add formatted name
.append( config.formatPlayername( msgData.sender() ) ) // add formatted name
.append( // add first part of message (depending on the Style and how it was constructed)
Util.make(() -> {
if(m.getContent() instanceof TranslatableTextContent ttc) { // most vanilla chat messages
Expand Down Expand Up @@ -228,7 +228,7 @@ private Text modifyMessage(Text m, @Local(argsOnly = true) boolean refreshing) {

modified = addCounter(modified, false);
ChatLog.addMessage(modified);
lastMsg = ChatUtils.NIL_MSG_DATA; // fixes messages that get around MessageHandlerMixin's data caching, usually thru ChatHud#addMessage (ex. open-to-lan message)
msgData = ChatUtils.NIL_MSG_DATA; // fixes messages that get around MessageHandlerMixin's data caching, usually thru ChatHud#addMessage (ex. open-to-lan message)
return modified;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.font.TextRenderer;
import net.minecraft.client.gui.DrawContext;
Expand Down Expand Up @@ -61,7 +62,6 @@
import static obro1961.chatpatches.gui.MenuButtonWidget.anchor;
import static obro1961.chatpatches.gui.MenuButtonWidget.of;
import static obro1961.chatpatches.util.RenderUtils.NIL_HUD_LINE;
import static obro1961.chatpatches.util.SharedVariables.FABRIC_LOADER;
import static obro1961.chatpatches.util.StringTextUtils.delAll;

/**
Expand Down Expand Up @@ -123,7 +123,7 @@ public abstract class ChatScreenMixin extends Screen implements ChatScreenAccess
private void chatScreenInit(String originalChatText, CallbackInfo ci) {
if(config.messageDrafting && !messageDraft.isBlank()) {
// if message drafting is enabled, a draft exists, and SMWYG sent an item message, clear the draft to avoid crashing
if(FABRIC_LOADER.isModLoaded("smwyg") && originalChatText.matches("^\\[[\\w\\s]+]$"))
if(FabricLoader.getInstance().isModLoaded("smwyg") && originalChatText.matches("^\\[[\\w\\s]+]$"))
messageDraft = originalChatText;
// otherwise if message drafting is enabled, a draft exists and this is not triggered by command key, update the draft
else if(!originalChatText.equals("/"))
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/obro1961/chatpatches/util/Flags.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ public void toggle() {

/** Removes this flag's bits in {@link #flags}. */
public void lower() {
if( isRaised())
if(isRaised())
toggle();
}

/** Returns true if this flag has its bit(s) set in {@link #flags}. */
public boolean isRaised() {
return (flags & value) == value;
}
}
}
10 changes: 0 additions & 10 deletions src/main/java/obro1961/chatpatches/util/SharedVariables.java

This file was deleted.

0 comments on commit 5fd271c

Please sign in to comment.