Skip to content

Commit

Permalink
Customizable unknown and internal error messages
Browse files Browse the repository at this point in the history
Signed-off-by: VytskaLT <[email protected]>
  • Loading branch information
vytskalt authored Mar 19, 2021
1 parent 06a15d6 commit 6546757
Show file tree
Hide file tree
Showing 3 changed files with 181 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
From cf4e5aab74629c3d8e822c36d96190c87fc72f47 Mon Sep 17 00:00:00 2001
From: VytskaLT <[email protected]>
Date: Fri, 4 Dec 2020 13:56:52 +0200
Subject: [PATCH] Make unknown and internal command messages customisable in
PlayerCommandPreprocessEvent


diff --git a/src/main/java/org/bukkit/event/player/PlayerCommandPreprocessEvent.java b/src/main/java/org/bukkit/event/player/PlayerCommandPreprocessEvent.java
index 1ec81732..649b5654 100644
--- a/src/main/java/org/bukkit/event/player/PlayerCommandPreprocessEvent.java
+++ b/src/main/java/org/bukkit/event/player/PlayerCommandPreprocessEvent.java
@@ -48,7 +48,7 @@ import org.bukkit.event.HandlerList;
public class PlayerCommandPreprocessEvent extends PlayerEvent implements Cancellable {
private static final HandlerList handlers = new HandlerList();
private boolean cancel = false;
- private String message;
+ private String message, unknownCommandMessage, internalErrorMessage;
private String format = "<%1$s> %2$s";
private final Set<Player> recipients;

@@ -64,6 +64,12 @@ public class PlayerCommandPreprocessEvent extends PlayerEvent implements Cancell
this.message = message;
}

+ public PlayerCommandPreprocessEvent(final Player player, final String message, final Set<Player> recipients, final String unknownCommandMessage, final String internalErrorMessage) {
+ this(player, message, recipients);
+ this.unknownCommandMessage = unknownCommandMessage;
+ this.internalErrorMessage = internalErrorMessage;
+ }
+
public boolean isCancelled() {
return cancel;
}
@@ -99,6 +105,48 @@ public class PlayerCommandPreprocessEvent extends PlayerEvent implements Cancell
this.message = command;
}

+ /**
+ * Gets the message that the player will receive if the command wasn't found.
+ *
+ * @return message that the player will receive if the command wasn't found.
+ */
+ public String getUnknownCommandMessage() {
+ return unknownCommandMessage;
+ }
+
+ /**
+ * Sets the message that the player will receive if the command wasn't found.
+ *
+ * @param unknownCommandMessage new message that the player will receive if the command wasn't found.
+ * @throws IllegalArgumentException if unknown command message is null or empty
+ */
+ public void setUnknownCommandMessage(String unknownCommandMessage) throws IllegalArgumentException {
+ Validate.notNull(unknownCommandMessage, "Unknown command message cannot be null");
+ Validate.notEmpty(unknownCommandMessage, "Unknown command message cannot be empty");
+ this.unknownCommandMessage = unknownCommandMessage;
+ }
+
+ /**
+ * Gets the message that the player will receive if an internal error occurred.
+ *
+ * @return message that the player will receive if an internal error occurred.
+ */
+ public String getInternalErrorMessage() {
+ return internalErrorMessage;
+ }
+
+ /**
+ * Sets the message that the player will receive if an internal error occurred.
+ *
+ * @param internalErrorMessage new message that the player will receive if an internal error occurred.
+ * @throws IllegalArgumentException if internal error message is null or empty
+ */
+ public void setInternalErrorMessage(String internalErrorMessage) throws IllegalArgumentException {
+ Validate.notNull(internalErrorMessage, "Internal error message cannot be null");
+ Validate.notEmpty(internalErrorMessage, "Internal error message cannot be empty");
+ this.internalErrorMessage = internalErrorMessage;
+ }
+
/**
* Sets the player that this command will be executed as.
*
--
2.25.1

Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
From 950e920f95c5399732e7106022222cc34ef7ffbd Mon Sep 17 00:00:00 2001
From: VytskaLT <[email protected]>
Date: Fri, 4 Dec 2020 13:56:51 +0200
Subject: [PATCH] Make unknown and internal command messages customisable in
PlayerCommandPreprocessEvent


diff --git a/src/main/java/net/minecraft/server/PlayerConnection.java b/src/main/java/net/minecraft/server/PlayerConnection.java
index 21747b92..5267250a 100644
--- a/src/main/java/net/minecraft/server/PlayerConnection.java
+++ b/src/main/java/net/minecraft/server/PlayerConnection.java
@@ -1319,7 +1319,10 @@ public class PlayerConnection implements PacketListenerPlayIn, IUpdatePlayerList

CraftPlayer player = this.getPlayer();

- PlayerCommandPreprocessEvent event = new PlayerCommandPreprocessEvent(player, s, new LazyPlayerSet());
+ // SportPaper - make unknown command and internal error messages customisable
+ PlayerCommandPreprocessEvent event = new PlayerCommandPreprocessEvent(player, s, new LazyPlayerSet(),
+ org.spigotmc.SpigotConfig.unknownCommandMessage,
+ org.spigotmc.SpigotConfig.internalErrorMessage);
this.server.getPluginManager().callEvent(event);

if (event.isCancelled()) {
@@ -1328,12 +1331,14 @@ public class PlayerConnection implements PacketListenerPlayIn, IUpdatePlayerList
}

try {
- if (this.server.dispatchCommand(event.getPlayer(), event.getMessage().substring(1))) {
+ if (this.server.dispatchCommand(event.getPlayer(), event.getMessage().substring(1), event.getUnknownCommandMessage())) { // SportPaper
SpigotTimings.playerCommandTimer.stopTiming(); // Spigot
return;
}
} catch (org.bukkit.command.CommandException ex) {
- player.sendMessage(org.bukkit.ChatColor.RED + "An internal error occurred while attempting to perform this command");
+ // SportPaper start
+ player.sendMessage(event.getInternalErrorMessage());
+ // SportPaper end
java.util.logging.Logger.getLogger(PlayerConnection.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
SpigotTimings.playerCommandTimer.stopTiming(); // Spigot
return;
diff --git a/src/main/java/org/bukkit/craftbukkit/CraftServer.java b/src/main/java/org/bukkit/craftbukkit/CraftServer.java
index 613582cc..c90241cd 100644
--- a/src/main/java/org/bukkit/craftbukkit/CraftServer.java
+++ b/src/main/java/org/bukkit/craftbukkit/CraftServer.java
@@ -619,8 +619,14 @@ public final class CraftServer implements Server {
}
}

+ // SportPaper start
@Override
public boolean dispatchCommand(CommandSender sender, String commandLine) {
+ return dispatchCommand(sender, commandLine, org.spigotmc.SpigotConfig.unknownCommandMessage);
+ }
+ // SportPaper end
+
+ public boolean dispatchCommand(CommandSender sender, String commandLine, String unknownCommandMessage) { // SportPaper - make unknown command message customisable
Validate.notNull(sender, "Sender cannot be null");
Validate.notNull(commandLine, "CommandLine cannot be null");

@@ -651,7 +657,11 @@ public final class CraftServer implements Server {
return true;
}

- sender.sendMessage(org.spigotmc.SpigotConfig.unknownCommandMessage);
+ // SportPaper start
+ if (unknownCommandMessage != null) {
+ sender.sendMessage(unknownCommandMessage);
+ }
+ // SportPaper end

return false;
}
diff --git a/src/main/java/org/spigotmc/SpigotConfig.java b/src/main/java/org/spigotmc/SpigotConfig.java
index ec995293..2b9358b2 100644
--- a/src/main/java/org/spigotmc/SpigotConfig.java
+++ b/src/main/java/org/spigotmc/SpigotConfig.java
@@ -84,6 +84,7 @@ public class SpigotConfig

public static String whitelistMessage;
public static String unknownCommandMessage;
+ public static String internalErrorMessage;
public static String serverFullMessage;
public static String outdatedClientMessage = "Outdated client! Please use {0}";
public static String outdatedServerMessage = "Outdated server! I\'m still on {0}";
@@ -95,6 +96,7 @@ public class SpigotConfig
{
whitelistMessage = transform( getString( "messages.whitelist", "You are not whitelisted on this server!" ) );
unknownCommandMessage = transform( getString( "messages.unknown-command", "Unknown command. Type \"/help\" for help." ) );
+ internalErrorMessage = transform( getString( "messages.internal-error", "&cAn internal error occurred while attempting to perform this command" ) );
serverFullMessage = transform( getString( "messages.server-full", "The server is full!" ) );
outdatedClientMessage = transform( getString( "messages.outdated-client", outdatedClientMessage ) );
outdatedServerMessage = transform( getString( "messages.outdated-server", outdatedServerMessage ) );
--
2.25.1

1 change: 1 addition & 0 deletions sportpaper.yml
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ spigot:
restart: Server is restarting!
whitelist: You are not whitelisted on this server!
unknown-command: Unknown command. Type "/help" for help.
internal-error: '&cAn internal error occurred while attempting to perform this command'
server-full: The server is full!
outdated-client: Outdated client! Please use {0}
outdated-server: Outdated server! Server is on {0}
Expand Down

0 comments on commit 6546757

Please sign in to comment.