Skip to content

Commit

Permalink
Fix: also trigger chat message event for messages from commands
Browse files Browse the repository at this point in the history
  • Loading branch information
Gegy committed Jun 18, 2024
1 parent 49504c0 commit 79aa517
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/main/java/xyz/nucleoid/stimuli/EventSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<World> dimension, BlockPos pos, @Nullable Entity entity) {
var source = POOL.acquire();
source.set(dimension, pos, entity);
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/xyz/nucleoid/stimuli/StimuliInitializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
});
}
}
5 changes: 5 additions & 0 deletions src/main/java/xyz/nucleoid/stimuli/StimuliSelector.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,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;
Expand Down Expand Up @@ -31,6 +32,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;
Expand Down

0 comments on commit 79aa517

Please sign in to comment.