Skip to content

Commit

Permalink
Add event for commands and make __on_player_message not trigger for…
Browse files Browse the repository at this point in the history
… them (#1525)

* Restoring old behaviour of __on_player_message

Fixes #1511

* Changed to PLAYER_COMMAND

* Updated docs

* Updated event_test.sc

* Updated editor keywords

* Fixed issue
  • Loading branch information
Ghoulboy78 authored Dec 26, 2022
1 parent b63dfa1 commit 43bb844
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 2 deletions.
5 changes: 4 additions & 1 deletion docs/scarpet/api/Events.md
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,10 @@ Triggered when the player has successfully logged in and was placed in the game.
Triggered when a player sends a disconnect package or is forcefully disconnected from the server.

### `__on_player_message(player, message)`
Triggered when a player sends a chat message or runs a command.
Triggered when a player sends a chat message.

### `__on_player_command(player, command)`
Triggered when a player runs a command. Command value is returned without the / in front.

This event can be cancelled by returning `'cancel'`, which prevents the message from being sent.

Expand Down
1 change: 1 addition & 0 deletions docs/scarpet/resources/editors/idea/4.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,6 @@ __on_player_respawns(player) ->
__on_player_disconnects(player, reason) ->
__on_player_connects(player) ->
__on_player_message(player, message) ->
__on_player_command(player, command) ->
__on_explosion(pos, power, source, causer, mode, fire) ->
__on_explosion_outcome(pos, power, source, causer, mode, fire, blocks, entities) ->
1 change: 1 addition & 0 deletions docs/scarpet/resources/editors/npp/scarpet.xml
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@
__on_player_disconnects(player, reason) ->
__on_player_connects(player) ->
__on_player_message(player, message) ->
__on_player_command(player, command) ->
__on_explosion(pos, power, source, causer, mode, fire) ->
__on_explosion_outcome(pos, power, source, causer, mode, fire, blocks, entities) ->
</Keywords>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package carpet.mixins;

import net.minecraft.network.chat.Component;
import net.minecraft.network.protocol.game.ServerboundChatCommandPacket;
import net.minecraft.network.protocol.game.ServerboundChatPacket;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
Expand All @@ -27,6 +27,7 @@
import static carpet.script.CarpetEventServer.Event.PLAYER_SWINGS_HAND;
import static carpet.script.CarpetEventServer.Event.PLAYER_SWITCHES_SLOT;
import static carpet.script.CarpetEventServer.Event.PLAYER_MESSAGE;
import static carpet.script.CarpetEventServer.Event.PLAYER_COMMAND;
import static carpet.script.CarpetEventServer.Event.PLAYER_USES_ITEM;
import static carpet.script.CarpetEventServer.Event.PLAYER_WAKES_UP;

Expand Down Expand Up @@ -302,4 +303,14 @@ private void onChatMessage(ServerboundChatPacket serverboundChatPacket, Callback
if(PLAYER_MESSAGE.onPlayerMessage(player, serverboundChatPacket.message())) ci.cancel();
}
}

@Inject(method = "handleChatCommand(Lnet/minecraft/network/protocol/game/ServerboundChatCommandPacket;)V",
at = @At(value = "HEAD")
)
private void onChatCommandMessage(ServerboundChatCommandPacket serverboundChatCommandPacket, CallbackInfo ci) {
if (PLAYER_COMMAND.isNeeded())
{
PLAYER_COMMAND.onPlayerMessage(player, serverboundChatCommandPacket.command());
}
}
}
7 changes: 7 additions & 0 deletions src/main/java/carpet/script/CarpetEventServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -921,6 +921,13 @@ public boolean onPlayerMessage(ServerPlayer player, String message) {
}
};

public static final Event PLAYER_COMMAND = new Event("player_command", 2, false) {
@Override
public boolean onPlayerMessage(ServerPlayer player, String message) {
return handler.call( () -> Arrays.asList(new EntityValue(player), new StringValue(message)), player::createCommandSourceStack);
}
};

public static final Event STATISTICS = new Event("statistic", 4, false)
{
private <T> ResourceLocation getStatId(Stat<T> stat)
Expand Down
7 changes: 7 additions & 0 deletions src/main/resources/assets/carpet/scripts/event_test.sc
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,13 @@ __on_player_message(player, message) ->
print('Player '+player+' sent message: '+message)
);

__on_player_command(player, command) ->
(
print('');
print('__on_player_command(player, command)');
print('Player '+player+' sent command: '+command)
);

__on_player_chooses_recipe(player, recipe, full_stack) ->
(
print('');
Expand Down

0 comments on commit 43bb844

Please sign in to comment.