mirror of
https://github.com/darksaid98/Carbon.git
synced 2026-12-02 06:28:54 +00:00
fabric: register chat type normally
This commit is contained in:
8 files changed
+40
-92
No files matched your search
@@ -45,10 +45,6 @@ import net.fabricmc.fabric.api.message.v1.ServerMessageEvents;
|
||||
import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents;
|
||||
import net.fabricmc.loader.api.FabricLoader;
|
||||
import net.fabricmc.loader.api.entrypoint.EntrypointContainer;
|
||||
import net.minecraft.core.registries.Registries;
|
||||
import net.minecraft.network.chat.ChatType;
|
||||
import net.minecraft.resources.ResourceKey;
|
||||
import net.minecraft.resources.ResourceLocation;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.checkerframework.framework.qual.DefaultQualifier;
|
||||
@@ -57,8 +53,6 @@ import org.checkerframework.framework.qual.DefaultQualifier;
|
||||
@Singleton
|
||||
public final class CarbonChatFabric extends CarbonChatInternal {
|
||||
|
||||
public static ResourceKey<ChatType> CHAT_TYPE = ResourceKey.create(Registries.CHAT_TYPE, new ResourceLocation("carbon", "chat"));
|
||||
|
||||
@Inject
|
||||
private CarbonChatFabric(
|
||||
final Injector injector,
|
||||
|
||||
@@ -20,6 +20,7 @@
|
||||
package net.draycia.carbon.fabric.listeners;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
import java.util.Optional;
|
||||
import net.draycia.carbon.api.users.CarbonPlayer;
|
||||
import net.draycia.carbon.common.config.ConfigManager;
|
||||
import net.draycia.carbon.common.event.events.CarbonChatEventImpl;
|
||||
@@ -31,16 +32,25 @@ import net.fabricmc.fabric.api.message.v1.ServerMessageEvents;
|
||||
import net.kyori.adventure.platform.fabric.FabricAudiences;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.minecraft.commands.CommandSourceStack;
|
||||
import net.minecraft.core.Registry;
|
||||
import net.minecraft.core.RegistryAccess;
|
||||
import net.minecraft.core.registries.Registries;
|
||||
import net.minecraft.network.chat.ChatType;
|
||||
import net.minecraft.network.chat.FilterMask;
|
||||
import net.minecraft.network.chat.OutgoingChatMessage;
|
||||
import net.minecraft.network.chat.PlayerChatMessage;
|
||||
import net.minecraft.resources.ResourceKey;
|
||||
import net.minecraft.resources.ResourceLocation;
|
||||
import net.minecraft.server.level.ServerPlayer;
|
||||
import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public class FabricChatHandler extends ChatListenerInternal implements ServerMessageEvents.AllowChatMessage {
|
||||
|
||||
private static final ResourceLocation CHAT_TYPE_KEY = new ResourceLocation("carbonchat", "chat");
|
||||
|
||||
private final CarbonChatFabric carbonChat;
|
||||
private @MonotonicNonNull ResourceKey<ChatType> chatTypeResourceKey;
|
||||
|
||||
@Inject
|
||||
public FabricChatHandler(
|
||||
@@ -72,7 +82,12 @@ public class FabricChatHandler extends ChatListenerInternal implements ServerMes
|
||||
|
||||
final net.minecraft.network.chat.Component nativeMessage = FabricAudiences.nonWrappingSerializer().serialize(finishedMessage);
|
||||
final PlayerChatMessage customChatMessage = new PlayerChatMessage(chatMessage.link(), chatMessage.signature(), chatMessage.signedBody(), nativeMessage, FilterMask.FULLY_FILTERED);
|
||||
final ChatType.Bound customBound = ChatType.bind(CarbonChatFabric.CHAT_TYPE, serverPlayer.level().registryAccess(), nativeMessage);
|
||||
final RegistryAccess registryAccess = serverPlayer.level().registryAccess();
|
||||
if (this.chatTypeResourceKey == null) {
|
||||
final Registry<ChatType> reg = registryAccess.registryOrThrow(Registries.CHAT_TYPE);
|
||||
this.chatTypeResourceKey = Optional.ofNullable(reg.get(CHAT_TYPE_KEY)).flatMap(reg::getResourceKey).orElseThrow();
|
||||
}
|
||||
final ChatType.Bound customBound = ChatType.bind(this.chatTypeResourceKey, registryAccess, nativeMessage);
|
||||
|
||||
if (recipient instanceof CommandSourceStack recipientSource) {
|
||||
recipientSource.sendChatMessage(new OutgoingChatMessage.Player(customChatMessage), false, customBound);
|
||||
|
||||
@@ -20,7 +20,6 @@
|
||||
package net.draycia.carbon.fabric.mixin;
|
||||
|
||||
import cloud.commandframework.types.tuples.Pair;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import net.draycia.carbon.fabric.callback.PlayerStatusMessageEvents;
|
||||
@@ -35,6 +34,7 @@ import org.checkerframework.checker.nullness.qual.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.At;
|
||||
import org.spongepowered.asm.mixin.injection.Inject;
|
||||
import org.spongepowered.asm.mixin.injection.Redirect;
|
||||
@@ -48,9 +48,7 @@ abstract class PlayerListMixin {
|
||||
@Shadow
|
||||
public abstract void broadcastSystemMessage(Component component, boolean bool);
|
||||
|
||||
@Shadow @Final private List<ServerPlayer> players;
|
||||
|
||||
public Map<Thread, Pair<Component, Boolean>> carbon$joinMsg = new ConcurrentHashMap<>();
|
||||
@Unique private Map<Thread, Pair<Component, Boolean>> joinMsg = new ConcurrentHashMap<>();
|
||||
|
||||
@Redirect(
|
||||
method = "placeNewPlayer",
|
||||
@@ -61,7 +59,7 @@ abstract class PlayerListMixin {
|
||||
)
|
||||
public void redirectJoinMessage(final PlayerList instance, final Component component, final boolean bool) {
|
||||
// move to after player is added to playerlist and world
|
||||
this.carbon$joinMsg.put(Thread.currentThread(), Pair.of(component, bool));
|
||||
this.joinMsg.put(Thread.currentThread(), Pair.of(component, bool));
|
||||
}
|
||||
|
||||
@Inject(
|
||||
@@ -69,7 +67,7 @@ abstract class PlayerListMixin {
|
||||
at = @At("RETURN")
|
||||
)
|
||||
public void injectJoin(final Connection connection, final ServerPlayer serverPlayer, final CommonListenerCookie cookie, final CallbackInfo ci) {
|
||||
final @Nullable Pair<Component, Boolean> remove = this.carbon$joinMsg.remove(Thread.currentThread());
|
||||
final @Nullable Pair<Component, Boolean> remove = this.joinMsg.remove(Thread.currentThread());
|
||||
if (remove != null) {
|
||||
final PlayerStatusMessageEvents.MessageEvent event = PlayerStatusMessageEvents.MessageEvent.of(
|
||||
serverPlayer, remove.getFirst().asComponent()
|
||||
|
||||
@@ -1,76 +0,0 @@
|
||||
/*
|
||||
* CarbonChat
|
||||
*
|
||||
* Copyright (c) 2023 Josua Parks (Vicarious)
|
||||
* Contributors
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package net.draycia.carbon.fabric.mixin;
|
||||
|
||||
import com.mojang.datafixers.util.Pair;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import net.draycia.carbon.fabric.CarbonChatFabric;
|
||||
import net.minecraft.core.Registry;
|
||||
import net.minecraft.core.RegistryAccess;
|
||||
import net.minecraft.core.WritableRegistry;
|
||||
import net.minecraft.core.registries.Registries;
|
||||
import net.minecraft.network.chat.ChatType;
|
||||
import net.minecraft.network.chat.ChatTypeDecoration;
|
||||
import net.minecraft.resources.RegistryDataLoader;
|
||||
import net.minecraft.server.packs.resources.ResourceManager;
|
||||
import org.spongepowered.asm.mixin.Mixin;
|
||||
import org.spongepowered.asm.mixin.Unique;
|
||||
import org.spongepowered.asm.mixin.injection.At;
|
||||
import org.spongepowered.asm.mixin.injection.Inject;
|
||||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
|
||||
import org.spongepowered.asm.mixin.injection.callback.LocalCapture;
|
||||
|
||||
@Mixin(RegistryDataLoader.class)
|
||||
abstract class RegistryDataLoaderMixin {
|
||||
|
||||
@Inject(
|
||||
method = "load",
|
||||
at = @At(
|
||||
value = "INVOKE",
|
||||
target = "Ljava/util/List;forEach(Ljava/util/function/Consumer;)V",
|
||||
ordinal = 0,
|
||||
shift = At.Shift.AFTER
|
||||
),
|
||||
locals = LocalCapture.CAPTURE_FAILEXCEPTION
|
||||
)
|
||||
@SuppressWarnings({"unchecked", "rawtypes"})
|
||||
private static void carbon$injectMessageTypes(
|
||||
final ResourceManager resourceManager,
|
||||
final RegistryAccess registryAccess,
|
||||
final List<RegistryDataLoader.RegistryData<?>> data,
|
||||
final CallbackInfoReturnable<RegistryAccess.Frozen> cir,
|
||||
final Map map,
|
||||
final List<Pair<WritableRegistry<?>, ?>> list
|
||||
) {
|
||||
for (final Pair<WritableRegistry<?>, ?> pair : list) {
|
||||
final WritableRegistry<?> registryKey = pair.getFirst();
|
||||
if (registryKey.key() == Registries.CHAT_TYPE) {
|
||||
Registry.register((Registry<ChatType>) pair.getFirst(), CarbonChatFabric.CHAT_TYPE, carbonChatType());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Unique
|
||||
private static ChatType carbonChatType() {
|
||||
return new ChatType(ChatTypeDecoration.withSender("%s"), ChatTypeDecoration.withSender("%s"));
|
||||
}
|
||||
|
||||
}
|
||||
-2
@@ -39,8 +39,6 @@ import org.spongepowered.asm.mixin.injection.Redirect;
|
||||
@Mixin(ServerGamePacketListenerImpl.class)
|
||||
abstract class ServerGamePacketListenerImplMixin extends ServerCommonPacketListenerImpl implements ServerGamePacketListener {
|
||||
|
||||
//@Shadow @Final private MinecraftServer server;
|
||||
|
||||
@Shadow public ServerPlayer player;
|
||||
|
||||
ServerGamePacketListenerImplMixin(final MinecraftServer minecraftServer, final Connection connection, final CommonListenerCookie commonListenerCookie) {
|
||||
|
||||
@@ -6,7 +6,6 @@
|
||||
"mixins": [
|
||||
"MessageSignatureCacheAccessor",
|
||||
"PlayerListMixin",
|
||||
"RegistryDataLoaderMixin",
|
||||
"ServerGamePacketListenerImplMixin",
|
||||
"ServerPlayerMixin"
|
||||
],
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
{
|
||||
"chat": {
|
||||
"parameters": [
|
||||
"content"
|
||||
],
|
||||
"translation_key": "%s"
|
||||
},
|
||||
"narration": {
|
||||
"parameters": [
|
||||
"content"
|
||||
],
|
||||
"translation_key": "%s"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"pack": {
|
||||
"pack_format": 18,
|
||||
"description": "CarbonChat Data"
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user