-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: use a light-way method to implement
Chat module
(better c…
…ompatibility, slightly performance improvement)
- Loading branch information
1 parent
fbd9f27
commit 95c8bef
Showing
13 changed files
with
154 additions
and
102 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 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 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 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 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 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
59 changes: 59 additions & 0 deletions
59
src/main/java/io/github/sakurawald/module/mixin/chat/RegistryLoaderMixin.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,59 @@ | ||
package io.github.sakurawald.module.mixin.chat; | ||
|
||
import io.github.sakurawald.module.initializer.resource_world.interfaces.SimpleRegistryMixinInterface; | ||
import io.github.sakurawald.module.mixin.resource_world.registry.SimpleRegistryMixin; | ||
import lombok.extern.slf4j.Slf4j; | ||
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents; | ||
import net.minecraft.network.message.MessageType; | ||
import net.minecraft.registry.*; | ||
import net.minecraft.text.Decoration; | ||
import net.minecraft.text.Style; | ||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.injection.At; | ||
import org.spongepowered.asm.mixin.injection.Coerce; | ||
import org.spongepowered.asm.mixin.injection.Inject; | ||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; | ||
import org.spongepowered.asm.mixin.injection.callback.LocalCapture; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
@Mixin(value = RegistryLoader.class, priority = 900) | ||
@Slf4j | ||
public class RegistryLoaderMixin { | ||
|
||
/** | ||
* This amazing mixin is written by Patbox. | ||
* And the source is inside: <a href="https://github.dev/Patbox/StyledChat/blob/1.21/src/main/java/eu/pb4/styledchat/mixin/MessageArgumentTypeMixin.java"> | ||
* Thanks to his great work. | ||
* <p> | ||
* I modified the code to override the vanilla MessageType.CHAT format. | ||
* | ||
* @see net.minecraft.registry.SimpleRegistry#add(net.minecraft.registry.RegistryKey, java.lang.Object, net.minecraft.registry.entry.RegistryEntryInfo) | ||
*/ | ||
@Inject(method = "load(Lnet/minecraft/registry/RegistryLoader$RegistryLoadable;Lnet/minecraft/registry/DynamicRegistryManager;Ljava/util/List;)Lnet/minecraft/registry/DynamicRegistryManager$Immutable;" | ||
, at = @At(value = "INVOKE", target = "Ljava/util/List;forEach(Ljava/util/function/Consumer;)V", ordinal = 0, shift = At.Shift.AFTER), locals = LocalCapture.CAPTURE_FAILEXCEPTION) | ||
private static void styledChat$injectMessageTypes(@Coerce Object registryLoadable, DynamicRegistryManager dynamicRegistryManager, List<RegistryLoader.Entry<?>> entries, CallbackInfoReturnable<DynamicRegistryManager.Immutable> cir, Map map, | ||
List<RegistryLoader.Loader<?>> loaders, RegistryOps.RegistryInfoGetter registryInfoGetter) { | ||
Decoration firstDecoration = new Decoration("%s", List.of(Decoration.Parameter.CONTENT), Style.EMPTY); | ||
Decoration secondDecoration = Decoration.ofChat("chat.type.text.narrate"); | ||
|
||
for (RegistryLoader.Loader<?> entry : loaders) { | ||
MutableRegistry<?> registry = entry.comp_2246(); | ||
RegistryKey<? extends Registry<?>> registryKey = registry.getKey(); | ||
|
||
if (registryKey.equals(RegistryKeys.MESSAGE_TYPE)) { | ||
Registry<MessageType> registryForMessageType = (Registry<MessageType>) registry; | ||
|
||
// The code is tricky, we need to register it later to override the vanilla registerKey. | ||
ServerLifecycleEvents.SERVER_STARTED.register(server -> { | ||
SimpleRegistryMixinInterface<MessageType> ex = (SimpleRegistryMixinInterface<MessageType>) registry; | ||
ex.fuji$setFrozen(false); | ||
Registry.register(registryForMessageType, MessageType.CHAT, new MessageType(firstDecoration, secondDecoration)); | ||
ex.fuji$setFrozen(true); | ||
}); | ||
|
||
} | ||
} | ||
} | ||
} |
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 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 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 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 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 |
---|---|---|
@@ -1,15 +1,12 @@ | ||
accessWidener v2 named | ||
|
||
# accessible field net/minecraft/registry/entry/RegistryEntry$Reference value Ljava/lang/Object; | ||
|
||
accessible method net/minecraft/server/world/ServerChunkManager getChunkHolder (J)Lnet/minecraft/server/world/ChunkHolder; | ||
|
||
accessible method net/minecraft/util/UserCache$Entry getProfile ()Lcom/mojang/authlib/GameProfile; | ||
|
||
accessible field net/minecraft/server/world/ChunkHolder UNLOADED_WORLD_CHUNK Lnet/minecraft/server/world/OptionalChunk; | ||
accessible field net/minecraft/server/world/ChunkHolder UNLOADED_WORLD_CHUNK_FUTURE Ljava/util/concurrent/CompletableFuture; | ||
|
||
accessible field net/minecraft/registry/entry/RegistryEntry$Reference value Ljava/lang/Object; | ||
|
||
accessible field net/minecraft/server/MinecraftServer worlds Ljava/util/Map; | ||
accessible field net/minecraft/server/MinecraftServer session Lnet/minecraft/world/level/storage/LevelStorage$Session; | ||
|
||
accessible class net/minecraft/registry/RegistryLoader$Loader |
Oops, something went wrong.