Skip to content

Commit

Permalink
Update to 1.20.1
Browse files Browse the repository at this point in the history
  • Loading branch information
IotaBread committed Jul 4, 2023
1 parent 4c2739a commit 5e7353c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,30 @@

import me.bymartrixx.playerevents.api.event.CommandExecutionCallback;
import me.bymartrixx.playerevents.api.event.PlayerLeaveCallback;
import net.minecraft.network.message.MessageSignatureList;
import net.minecraft.network.packet.c2s.play.ChatCommandC2SPacket;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.network.ServerPlayNetworkHandler;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.Text;
import net.minecraft.unmapped.C_mofgmouu;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.LocalCapture;

import java.util.Optional;

@Mixin(ServerPlayNetworkHandler.class)
@SuppressWarnings({"InvalidInjectorMethodSignature", "MixinAnnotationTarget", "UnresolvedMixinReference"})
@SuppressWarnings({"MixinAnnotationTarget", "UnresolvedMixinReference"})
public class ServerPlayNetworkHandlerMixin {
@Shadow
public ServerPlayerEntity player;

// Just after the command is executed
// 1.19
@Inject(at = @At(value = "INVOKE", target = "Lnet/minecraft/server/network/ServerPlayNetworkHandler;checkForSpam()V"),
method = "onChatCommand", locals = LocalCapture.CAPTURE_FAILSOFT, require = 0)
private void onCommandExecuted(ChatCommandC2SPacket packet, CallbackInfo ci, ServerCommandSource source) {
CommandExecutionCallback.EVENT.invoker().onExecuted(packet.command(), source);
}

// 1.19.1/2
@Inject(at = @At(value = "INVOKE", target = "Lnet/minecraft/server/network/ServerPlayNetworkHandler;checkForSpam()V"),
method = "method_44356(Lnet/minecraft/class_7472;)V", require = 0)
private void onCommandExecuted(ChatCommandC2SPacket packet, CallbackInfo ci) {
CommandExecutionCallback.EVENT.invoker().onExecuted(packet.command(), this.player.getCommandSource());
}

// 1.19.3
// 1.20
@Inject(at = @At(value = "INVOKE", target = "Lnet/minecraft/server/network/ServerPlayNetworkHandler;checkForSpam()V"),
method = "method_44356(Lnet/minecraft/class_7472;Ljava/util/Optional;)V", require = 0)
private void onCommandExecuted(ChatCommandC2SPacket packet, Optional<C_mofgmouu> optional, CallbackInfo ci) {
private void onCommandExecuted(ChatCommandC2SPacket packet, Optional<MessageSignatureList> optional, CallbackInfo ci) {
CommandExecutionCallback.EVENT.invoker().onExecuted(packet.command(), this.player.getCommandSource());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,20 @@
import com.mojang.brigadier.tree.LiteralCommandNode;
import me.bymartrixx.playerevents.config.PlayerEventsConfig;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.text.MutableText;
import net.minecraft.text.Text;

import static net.minecraft.server.command.CommandManager.literal;

public class ReloadCommand {

private static final MutableText RELOADING_TEXT = Text.literal("Reloading Player Events config...");

public static LiteralCommandNode<ServerCommandSource> getNode() {
return literal("reload")
.executes(context -> {
ServerCommandSource source = context.getSource();
source.sendFeedback(Text.literal("Reloading Player Events config..."), true);
source.sendFeedback(() -> RELOADING_TEXT, true);
PlayerEventsConfig.Manager.loadConfig();

return 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import net.minecraft.server.function.CommandFunction;
import net.minecraft.server.function.CommandFunctionManager;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.MutableText;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;
import net.minecraft.util.Identifier;
Expand All @@ -34,6 +35,9 @@
import static me.bymartrixx.playerevents.util.PlaceholderReplacingUtil.lazyResolver;

public class PlayerEventsConfig {
private static final String TEST_TEXT_PREFIX = String.valueOf(Formatting.GRAY) + Formatting.ITALIC;
private static final MutableText PLAYER_ONLY_COMMAND_ERROR_TEXT = Text.literal("This command can only be executed by players");

public static class Manager {
private static File configFile;

Expand Down Expand Up @@ -174,7 +178,9 @@ public static void testSimpleAction(ActionList actionList, ServerCommandSource s
if (actionList.pickMessageRandomly()) {
message = message + " [Message picked randomly]";
}
source.sendFeedback(Text.literal("" + Formatting.GRAY + Formatting.ITALIC + message), false);

String finalMessage = message;
source.sendFeedback(() -> Text.literal(TEST_TEXT_PREFIX + finalMessage), false);

Map<String, Object> placeholderArgs = playerPlaceholder(source);

Expand All @@ -191,9 +197,9 @@ public static void testAction(String action, ServerCommandSource source,

if (action.startsWith("/")) {
String command = message.getString();
source.sendFeedback(Text.literal("[COMMAND] " + command), false);
source.sendFeedback(() -> Text.literal("[COMMAND] " + command), false);
} else {
source.sendFeedback(message, false);
source.sendFeedback(() -> message, false);
}
}

Expand Down Expand Up @@ -301,11 +307,7 @@ public void doCustomCommandsActions(String command, ServerCommandSource source)
// Run actions from the event to allow editing the actions without restarting the server
for (CustomCommandActionList customCommand : this.customCommands) {
if (command.trim().equals(customCommand.getCommand())) {
try {
doSimpleAction(customCommand, source.getPlayer());
} catch (CommandSyntaxException e) {
PlayerEvents.LOGGER.error("This should not happen, please report it to the mod author, attaching the logs", e);
}
doSimpleAction(customCommand, source.getPlayer());
}
}
}
Expand All @@ -332,7 +334,7 @@ public void testLeaveActions(ServerCommandSource source) {

public void testKillEntityActions(ServerCommandSource source) {
String message = String.format("Kill entity actions (%s):", this.killEntity.doBroadcastToEveryone() ? "Send to everyone" : "Send only to the player");
source.sendFeedback(Text.literal("" + Formatting.GRAY + Formatting.ITALIC + message), false);
source.sendFeedback(() -> Text.literal(TEST_TEXT_PREFIX + message), false);

Map<String, Object> placeholderArgs = playerPlaceholder(source);
placeholderArgs.put("killedEntity", lazyResolver((subKey) -> switch (subKey) {
Expand All @@ -350,7 +352,7 @@ public void testKillEntityActions(ServerCommandSource source) {

public void testKillPlayerActions(ServerCommandSource source) {
String message = String.format("Kill player actions (%s):", this.killPlayer.doBroadcastToEveryone() ? "Send to everyone" : "Send only to the player");
source.sendFeedback(Text.literal("" + Formatting.GRAY + Formatting.ITALIC + message), false);
source.sendFeedback(() -> Text.literal(TEST_TEXT_PREFIX + message), false);

Map<String, Object> placeholderArgs = playerPlaceholder(source);
placeholderArgs.put("killedPlayer", source);
Expand All @@ -365,7 +367,7 @@ public void testCustomCommandsActions(ServerCommandSource source) {

for (CustomCommandActionList actionList : this.customCommands) {
String message = String.format("'%s' actions ('%s'):", actionList.getCommandStr(), actionList.doBroadcastToEveryone() ? "Send to everyone" : "Send only to the player");
source.sendFeedback(Text.literal("§7§o" + message), false);
source.sendFeedback(() -> Text.literal(TEST_TEXT_PREFIX + message), false);

for (String action : actionList.actions) {
testAction(action, source, placeholderArgs);
Expand Down Expand Up @@ -397,7 +399,7 @@ public void registerCustomCommands(CommandDispatcher<ServerCommandSource> dispat
private int executeCommand(CommandContext<ServerCommandSource> ctx) {
ServerCommandSource source = ctx.getSource();
if (!(source.getEntity() instanceof ServerPlayerEntity)) {
source.sendFeedback(Text.literal("This command can only be executed by players"), false);
source.sendFeedback(() -> PLAYER_ONLY_COMMAND_ERROR_TEXT, false);
}

return 1;
Expand Down

0 comments on commit 5e7353c

Please sign in to comment.