From 0105487e9dd49e0b4dd83d75eb0b2ca7557ee6d3 Mon Sep 17 00:00:00 2001 From: Gegy Date: Tue, 18 Jun 2024 18:51:01 +0200 Subject: [PATCH] Fix: also trigger chat message event for messages from commands --- src/main/java/xyz/nucleoid/stimuli/EventSource.java | 5 +++++ .../java/xyz/nucleoid/stimuli/StimuliInitializer.java | 11 +++++++++++ .../java/xyz/nucleoid/stimuli/StimuliSelector.java | 5 +++++ 3 files changed, 21 insertions(+) diff --git a/src/main/java/xyz/nucleoid/stimuli/EventSource.java b/src/main/java/xyz/nucleoid/stimuli/EventSource.java index 6523eeb..0ba1c0f 100644 --- a/src/main/java/xyz/nucleoid/stimuli/EventSource.java +++ b/src/main/java/xyz/nucleoid/stimuli/EventSource.java @@ -2,6 +2,7 @@ import net.minecraft.entity.Entity; import net.minecraft.registry.RegistryKey; +import net.minecraft.server.command.ServerCommandSource; import net.minecraft.util.math.BlockPos; import net.minecraft.world.World; import org.jetbrains.annotations.Nullable; @@ -65,6 +66,10 @@ public static EventSource forEntityAt(Entity entity, BlockPos pos) { return acquire(entity.getWorld().getRegistryKey(), pos, entity); } + public static EventSource forCommandSource(ServerCommandSource source) { + return acquire(source.getWorld().getRegistryKey(), BlockPos.ofFloored(source.getPosition()), source.getEntity()); + } + static EventSource acquire(RegistryKey dimension, BlockPos pos, @Nullable Entity entity) { var source = POOL.acquire(); source.set(dimension, pos, entity); diff --git a/src/main/java/xyz/nucleoid/stimuli/StimuliInitializer.java b/src/main/java/xyz/nucleoid/stimuli/StimuliInitializer.java index 0cedd84..2d3056d 100644 --- a/src/main/java/xyz/nucleoid/stimuli/StimuliInitializer.java +++ b/src/main/java/xyz/nucleoid/stimuli/StimuliInitializer.java @@ -78,5 +78,16 @@ public void onInitialize() { return result != ActionResult.FAIL; } }); + + ServerMessageEvents.ALLOW_COMMAND_MESSAGE.register((message, source, params) -> { + var player = source.getPlayer(); + if (player == null) { + return true; + } + try (var invokers = Stimuli.select().forCommandSource(source)) { + var result = invokers.get(PlayerChatEvent.EVENT).onSendChatMessage(player, message, params); + return result != ActionResult.FAIL; + } + }); } } diff --git a/src/main/java/xyz/nucleoid/stimuli/StimuliSelector.java b/src/main/java/xyz/nucleoid/stimuli/StimuliSelector.java index 096b6f8..30e064d 100644 --- a/src/main/java/xyz/nucleoid/stimuli/StimuliSelector.java +++ b/src/main/java/xyz/nucleoid/stimuli/StimuliSelector.java @@ -4,6 +4,7 @@ import com.google.common.collect.AbstractIterator; import net.minecraft.entity.Entity; import net.minecraft.server.MinecraftServer; +import net.minecraft.server.command.ServerCommandSource; import net.minecraft.util.math.BlockPos; import net.minecraft.world.World; import org.jetbrains.annotations.Nullable; @@ -32,6 +33,10 @@ public EventInvokers forEntityAt(Entity entity, BlockPos pos) { return this.acquireInvokers(entity.getServer(), EventSource.forEntityAt(entity, pos)); } + public EventInvokers forCommandSource(ServerCommandSource source) { + return this.acquireInvokers(source.getServer(), EventSource.forCommandSource(source)); + } + EventInvokers acquireInvokers(@Nullable MinecraftServer server, EventSource source) { if (server == null) { return NoOPSelectorEventInvokers.INSTANCE;