Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix chat events #299

Open
wants to merge 2 commits into
base: 1.19
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@

package dev.architectury.event.fabric;

import dev.architectury.event.EventResult;
import dev.architectury.event.events.client.ClientGuiEvent;
import dev.architectury.event.events.client.ClientLifecycleEvent;
import dev.architectury.event.events.client.ClientTickEvent;
import dev.architectury.event.events.client.ClientTooltipEvent;
import dev.architectury.event.events.common.CommandRegistrationEvent;
import dev.architectury.event.events.common.InteractionEvent;
import dev.architectury.event.events.common.LifecycleEvent;
import dev.architectury.event.events.common.TickEvent;
import dev.architectury.event.events.common.*;
import dev.architectury.impl.fabric.ChatComponentImpl;
import dev.architectury.impl.fabric.EventChatDecorator;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientLifecycleEvents;
Expand All @@ -40,6 +40,9 @@
import net.fabricmc.fabric.api.event.player.AttackBlockCallback;
import net.fabricmc.fabric.api.event.player.UseBlockCallback;
import net.fabricmc.fabric.api.event.player.UseItemCallback;
import net.fabricmc.fabric.api.message.v1.ServerMessageDecoratorEvent;

import java.util.concurrent.CompletableFuture;

public class EventHandlerImpl {
@Environment(EnvType.CLIENT)
Expand Down Expand Up @@ -75,6 +78,20 @@ public static void registerCommon() {
UseItemCallback.EVENT.register((player, world, hand) -> InteractionEvent.RIGHT_CLICK_ITEM.invoker().click(player, hand).asMinecraft());
UseBlockCallback.EVENT.register((player, world, hand, hitResult) -> InteractionEvent.RIGHT_CLICK_BLOCK.invoker().click(player, hand, hitResult.getBlockPos(), hitResult.getDirection()).asMinecraft());
AttackBlockCallback.EVENT.register((player, world, hand, pos, face) -> InteractionEvent.LEFT_CLICK_BLOCK.invoker().click(player, hand, pos, face).asMinecraft());

ServerMessageDecoratorEvent.EVENT.register(ServerMessageDecoratorEvent.CONTENT_PHASE, (player, component) -> {
ChatEvent.ChatComponent chatComponent = new ChatComponentImpl(component, component);
EventResult result = ChatEvent.SERVER.invoker().process(player, chatComponent);
if (result.isPresent()) {
if (result.isFalse()) {
return CompletableFuture.completedFuture(EventChatDecorator.CANCELLING_COMPONENT);
} else {
return CompletableFuture.completedFuture(chatComponent.getFiltered());
}
}

return CompletableFuture.completedFuture(component);
});
}

@Environment(EnvType.SERVER)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,68 +19,8 @@

package dev.architectury.impl.fabric;

import dev.architectury.event.events.common.ChatEvent;
import net.minecraft.network.chat.ChatDecorator;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.MessageSignature;
import net.minecraft.network.chat.PlayerChatMessage;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.server.network.FilteredText;
import org.jetbrains.annotations.Nullable;

import java.util.concurrent.CompletableFuture;

public class EventChatDecorator implements ChatDecorator {
public class EventChatDecorator {
public static final Component CANCELLING_COMPONENT = Component.literal("THIS SHOULDN'T BE DISPLAYED, ARCHITECTURY SPECIFIC STRING DO NOT IMITATE THIS");
MaxNeedsSnacks marked this conversation as resolved.
Show resolved Hide resolved
private final ChatDecorator parent;
private final ChatProcessor processor;

public EventChatDecorator(ChatDecorator parent, ChatProcessor processor) {
this.parent = parent;
this.processor = processor;
}

@Override
public CompletableFuture<Component> decorate(@Nullable ServerPlayer player, Component component) {
return parent.decorate(player, component).thenApply(c -> {
return processor.process(player, FilteredText.fullyFiltered(c)).raw();
}).exceptionally(throwable -> {
throwable.printStackTrace();
return component;
});
}

@Override
public CompletableFuture<FilteredText<Component>> decorateFiltered(@Nullable ServerPlayer player, FilteredText<Component> message) {
return parent.decorateFiltered(player, message).thenApply(newMessage -> {
FilteredText<Component> newContent = processor.process(player, newMessage);
if (!newContent.equals(newMessage)) {
return newContent;
}
return newMessage;
}).exceptionally(throwable -> {
throwable.printStackTrace();
return message;
});
}

@Override
public CompletableFuture<FilteredText<PlayerChatMessage>> decorateChat(@Nullable ServerPlayer player, FilteredText<Component> component, MessageSignature signature, boolean signedPreview) {
return parent.decorateChat(player, component, signature, signedPreview).thenApply(message -> {
FilteredText<Component> newComponent = processor.process(player, message.map(PlayerChatMessage::signedContent));
FilteredText<PlayerChatMessage> newMessage = PlayerChatMessage.filteredSigned(component, newComponent, signature, signedPreview);
if (!newMessage.equals(message)) {
return newMessage;
}
return message;
}).exceptionally(throwable -> {
throwable.printStackTrace();
return PlayerChatMessage.filteredSigned(component, component, signature, signedPreview);
});
}

@FunctionalInterface
public interface ChatProcessor {
FilteredText<Component> process(@Nullable ServerPlayer player, FilteredText<Component> text);
}
}

This file was deleted.

1 change: 0 additions & 1 deletion fabric/src/main/resources/architectury.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
"MixinInventory",
"MixinItemEntity",
"MixinLivingEntity",
"MixinMinecraftServer",
"MixinNaturalSpawner",
"MixinOcelot",
"MixinPatrolSpawner",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,6 @@ public static void event(net.minecraftforge.client.event.ClientChatEvent event)
event.setCanceled(true);
else {
event.setMessage(processor.getMessage());

if (process.isTrue()) {
event.setCanceled(true);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public static void debugEvents() {
if (message.getRaw().getString().contains("shit")) {
return EventResult.interruptFalse();
}
message.modifyBoth(component -> component.copy().withStyle(ChatFormatting.AQUA));
message.modifyBoth(component -> component.copy().withStyle(ChatFormatting.AQUA).append(" + new text"));
return EventResult.interruptTrue();
});
CommandPerformEvent.EVENT.register(event -> {
Expand Down Expand Up @@ -255,6 +255,10 @@ public static void debugEventsClient() {
});
ClientChatEvent.PROCESS.register((message) -> {
TestMod.SINK.accept("Client chat sent: " + message.getMessage());
if (message.getMessage().contains("error")) {
message.setMessage("Error: " + message.getMessage());
return EventResult.interruptTrue();
}
return EventResult.pass();
});
ClientChatEvent.RECEIVED.register((type, message, sender) -> {
Expand Down