Skip to content

Commit 9b29778

Browse files
committed
allow server resource packs to disable chat heads via "assets/chat_heads/disable" file
1 parent 1f3c40d commit 9b29778

File tree

5 files changed

+66
-21
lines changed

5 files changed

+66
-21
lines changed

common/src/main/java/dzwdz/chat_heads/ChatHeads.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
public class ChatHeads {
5858
public static final String MOD_ID = "chat_heads";
5959
public static final String NON_NAME_REGEX = "(§.)|[^\\w]";
60+
public static final ResourceLocation DISABLE_RESOURCE = new ResourceLocation("chat_heads", "disable");
6061

6162
public static ChatHeadsConfig CONFIG = new ChatHeadsConfigDefaults();
6263

@@ -75,7 +76,8 @@ public class ChatHeads {
7576
public static int lastY = 0;
7677
public static float lastOpacity = 0.0f;
7778
public static int lastChatOffset;
78-
public static boolean serverSentUuid = false;
79+
public static volatile boolean serverSentUuid = false;
80+
public static volatile boolean serverDisabledChatHeads = false;
7981

8082
public static final Set<ResourceLocation> blendedHeadTextures = new HashSet<>();
8183

@@ -92,6 +94,11 @@ public static void resetLineOwner() {
9294
}
9395

9496
public static void handleAddedMessage(Component message, @Nullable ChatType.Bound bound, @Nullable PlayerInfo playerInfo) {
97+
if (ChatHeads.serverDisabledChatHeads) {
98+
ChatHeads.lastSender = null;
99+
return;
100+
}
101+
95102
if (ChatHeads.CONFIG.senderDetection() != HEURISTIC_ONLY) {
96103
if (playerInfo != null) {
97104
ChatHeads.lastSender = playerInfo;
@@ -117,7 +124,7 @@ public static int getChatOffset(@NotNull GuiMessage.Line guiMessage) {
117124
}
118125

119126
public static int getChatOffset(@Nullable PlayerInfo owner) {
120-
if (owner != null || ChatHeads.CONFIG.offsetNonPlayerText()) {
127+
if (owner != null || (ChatHeads.CONFIG.offsetNonPlayerText() && !ChatHeads.serverDisabledChatHeads)) {
121128
return 10;
122129
} else {
123130
return 0;
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package dzwdz.chat_heads.mixin;
2+
3+
import dzwdz.chat_heads.ChatHeads;
4+
import net.minecraft.network.Connection;
5+
import net.minecraft.network.protocol.login.ClientLoginPacketListener;
6+
import org.spongepowered.asm.mixin.Mixin;
7+
import org.spongepowered.asm.mixin.injection.At;
8+
import org.spongepowered.asm.mixin.injection.Inject;
9+
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
10+
11+
@Mixin(Connection.class)
12+
public abstract class ConnectionMixin {
13+
// note: can run on different threads
14+
@Inject(method = "initiateServerboundPlayConnection", at = @At("HEAD"))
15+
public void chatheads$resetServerKnowledge(String string, int i, ClientLoginPacketListener clientLoginPacketListener, CallbackInfo ci) {
16+
// reset every time we build a connection, be it singleplayer or multiplayer
17+
ChatHeads.serverSentUuid = false;
18+
ChatHeads.serverDisabledChatHeads = false;
19+
}
20+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package dzwdz.chat_heads.mixin;
2+
3+
import dzwdz.chat_heads.ChatHeads;
4+
import net.minecraft.client.resources.DownloadedPackSource;
5+
import net.minecraft.server.packs.PackResources;
6+
import net.minecraft.server.packs.PackType;
7+
import net.minecraft.server.packs.repository.Pack;
8+
import net.minecraft.server.packs.repository.PackSource;
9+
import org.jetbrains.annotations.Nullable;
10+
import org.spongepowered.asm.mixin.Mixin;
11+
import org.spongepowered.asm.mixin.Shadow;
12+
import org.spongepowered.asm.mixin.injection.At;
13+
import org.spongepowered.asm.mixin.injection.Inject;
14+
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
15+
16+
import java.io.File;
17+
import java.util.concurrent.CompletableFuture;
18+
19+
@Mixin(DownloadedPackSource.class)
20+
public abstract class DownloadedPackSourceMixin {
21+
@Shadow @Nullable
22+
private Pack serverPack;
23+
24+
// note: runs on Netty Client IO thread
25+
@Inject(method = "setServerPack", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/Minecraft;delayTextureReload()Ljava/util/concurrent/CompletableFuture;"))
26+
public void f(File file, PackSource packSource, CallbackInfoReturnable<CompletableFuture<Void>> cir) {
27+
if (serverPack == null) return; // should never be null
28+
29+
try (PackResources resources = serverPack.open()) {
30+
if (resources.getResource(PackType.CLIENT_RESOURCES, ChatHeads.DISABLE_RESOURCE) != null) {
31+
ChatHeads.serverDisabledChatHeads = true;
32+
}
33+
}
34+
}
35+
}

common/src/main/java/dzwdz/chat_heads/mixin/MinecraftMixin.java

Lines changed: 0 additions & 18 deletions
This file was deleted.

common/src/main/resources/chat_heads.mixins.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@
99
"ChatListenerMixin",
1010
"ClientPacketListenerMixin",
1111
"CommandSuggestionSuggestionsListMixin",
12+
"ConnectionMixin",
13+
"DownloadedPackSourceMixin",
1214
"GuiMessageLineMixin",
1315
"GuiMessageMixin",
1416
"HttpTextureMixin",
15-
"MinecraftMixin",
1617
"PlayerChatMessageMixin",
1718
"SkinManagerMixin"
1819
],

0 commit comments

Comments
 (0)