Skip to content

Commit

Permalink
Changelog:
Browse files Browse the repository at this point in the history
- Replaced inaccurate chat colorizing
- Added XYZCommandActor#wrap(...) methods
- Exposed CommandActor to CommandExceptionHandler#handleException
- Removed #getActor() from exceptions
- Added support for Sponge's TextMessageException.
- Created a default exception handler for Velocity
- SendableExceptions no longer require a CommandActor to be supplied.
- Bump versions
  • Loading branch information
Revxrsal committed Sep 21, 2021
1 parent b1271a6 commit 0e23b2b
Show file tree
Hide file tree
Showing 71 changed files with 398 additions and 538 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ subprojects {
apply plugin: 'maven'

group 'io.github.revxrsal'
version '1.0'
version '1.2'

repositories {
mavenCentral()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import revxrsal.commands.bukkit.core.BukkitActor;
import revxrsal.commands.bukkit.exception.SenderNotConsoleException;
import revxrsal.commands.bukkit.exception.SenderNotPlayerException;
import revxrsal.commands.command.CommandActor;
Expand Down Expand Up @@ -61,4 +62,14 @@ public interface BukkitCommandActor extends CommandActor {
*/
@NotNull ConsoleCommandSender requireConsole() throws SenderNotConsoleException;

/**
* Creates a new {@link BukkitCommandActor} that wraps the given {@link CommandSender}.
*
* @param sender Command sender to wrap
* @return The wrapping {@link BukkitCommandActor}.
*/
static @NotNull BukkitCommandActor wrap(@NotNull CommandSender sender) {
return new BukkitActor(sender);
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package revxrsal.commands.bukkit.core;

import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.entity.Player;
Expand All @@ -12,12 +11,15 @@

import java.util.UUID;

final class BukkitActor implements BukkitCommandActor {
import static revxrsal.commands.util.Preconditions.notNull;
import static revxrsal.commands.util.Strings.colorize;

public final class BukkitActor implements BukkitCommandActor {

private final CommandSender sender;

public BukkitActor(CommandSender sender) {
this.sender = sender;
this.sender = notNull(sender, "sender");
}

@Override public CommandSender getSender() {
Expand All @@ -38,13 +40,13 @@ public BukkitActor(CommandSender sender) {

@Override public @NotNull Player requirePlayer() {
if (!isPlayer())
throw new SenderNotPlayerException(this);
throw new SenderNotPlayerException();
return (Player) sender;
}

@Override public @NotNull ConsoleCommandSender requireConsole() {
if (!isConsole())
throw new SenderNotConsoleException(this);
throw new SenderNotConsoleException();
return (ConsoleCommandSender) sender;
}

Expand All @@ -57,12 +59,12 @@ public BukkitActor(CommandSender sender) {
}

@Override public void reply(@NotNull String message) {
message = ChatColor.translateAlternateColorCodes('&', message);
sender.sendMessage(message);
notNull(message, "message");
sender.sendMessage(colorize(message));
}

@Override public void error(@NotNull String message) {
message = ChatColor.translateAlternateColorCodes('&', "&c" + message);
sender.sendMessage(message);
notNull(message, "message");
sender.sendMessage(colorize("&c" + message));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public BukkitHandler(@NotNull Plugin plugin) {
return ((BukkitCommandActor) actor).requirePlayer();
Player player = Bukkit.getPlayer(value);
if (player == null)
throw new InvalidPlayerException(parameter, value, actor);
throw new InvalidPlayerException(parameter, value);
return player;
});
registerValueResolver(OfflinePlayer.class, (arguments, actor, parameter, command) -> {
Expand All @@ -57,7 +57,7 @@ public BukkitHandler(@NotNull Plugin plugin) {
//noinspection deprecation
OfflinePlayer player = Bukkit.getOfflinePlayer(value);
if (!player.hasPlayedBefore())
throw new InvalidPlayerException(parameter, value, actor);
throw new InvalidPlayerException(parameter, value);
return player;
});
registerValueResolver(World.class, (arguments, actor, parameter, command) -> {
Expand All @@ -66,7 +66,7 @@ public BukkitHandler(@NotNull Plugin plugin) {
return ((BukkitCommandActor) actor).requirePlayer().getWorld();
World world = Bukkit.getWorld(value);
if (world == null)
throw new InvalidWorldException(parameter, value, actor);
throw new InvalidWorldException(parameter, value);
return world;
});
registerValueResolver(PlayerSelector.class, PlayerSelectorResolver.INSTANCE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ enum PlayerSelectorResolver implements ValueResolver<PlayerSelector> {
default: {
Player player = Bukkit.getPlayer(value);
if (player == null)
throw new InvalidPlayerException(parameter, value, actor);
throw new InvalidPlayerException(parameter, value);
coll.add(player);
return coll::iterator;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,42 +1,43 @@
package revxrsal.commands.bukkit.exception;

import org.jetbrains.annotations.NotNull;
import revxrsal.commands.command.CommandActor;
import revxrsal.commands.exception.DefaultExceptionHandler;

public /*final*/ class BukkitExceptionAdapter extends DefaultExceptionHandler {

public static final BukkitExceptionAdapter INSTANCE = new BukkitExceptionAdapter();

@Override protected final void handleUnknown(@NotNull Throwable throwable) {
@Override protected final void handleUnknown(@NotNull CommandActor actor, @NotNull Throwable throwable) {
if (throwable instanceof SenderNotPlayerException)
senderNotPlayer((SenderNotPlayerException) throwable);
senderNotPlayer(actor, (SenderNotPlayerException) throwable);
else if (throwable instanceof SenderNotConsoleException)
senderNotConsole((SenderNotConsoleException) throwable);
senderNotConsole(actor, (SenderNotConsoleException) throwable);
else if (throwable instanceof InvalidPlayerException)
invalidPlayer((InvalidPlayerException) throwable);
invalidPlayer(actor, (InvalidPlayerException) throwable);
else if (throwable instanceof InvalidWorldException)
invalidWorld((InvalidWorldException) throwable);
invalidWorld(actor, (InvalidWorldException) throwable);
else
handleUnknownThrowable(throwable);
handleUnknownThrowable(actor, throwable);
}

protected void senderNotPlayer(@NotNull SenderNotPlayerException exception) {
exception.getActor().error("You must be a player to use this command!");
protected void senderNotPlayer(@NotNull CommandActor actor, @NotNull SenderNotPlayerException exception) {
actor.error("You must be a player to use this command!");
}

protected void senderNotConsole(@NotNull SenderNotConsoleException exception) {
exception.getActor().error("This command can only be used on console!");
protected void senderNotConsole(@NotNull CommandActor actor, @NotNull SenderNotConsoleException exception) {
actor.error("This command can only be used on console!");
}

protected void invalidPlayer(@NotNull InvalidPlayerException exception) {
exception.getActor().error("Invalid player: &e" + exception.getInput());
protected void invalidPlayer(@NotNull CommandActor actor, @NotNull InvalidPlayerException exception) {
actor.error("Invalid player: &e" + exception.getInput());
}

protected void invalidWorld(@NotNull InvalidWorldException exception) {
exception.getActor().error("Invalid world: &e" + exception.getInput());
protected void invalidWorld(@NotNull CommandActor actor, @NotNull InvalidWorldException exception) {
actor.error("Invalid world: &e" + exception.getInput());
}

protected void handleUnknownThrowable(@NotNull Throwable throwable) {
protected void handleUnknownThrowable(@NotNull CommandActor actor, @NotNull Throwable throwable) {
throwable.printStackTrace();
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package revxrsal.commands.bukkit.exception;

import org.jetbrains.annotations.NotNull;
import revxrsal.commands.bukkit.BukkitCommandActor;
import revxrsal.commands.command.CommandActor;
import revxrsal.commands.command.CommandParameter;
import revxrsal.commands.exception.InvalidValueException;

Expand All @@ -12,11 +10,7 @@
*/
public class InvalidPlayerException extends InvalidValueException {

public InvalidPlayerException(@NotNull CommandParameter parameter, @NotNull String input, @NotNull CommandActor actor) {
super(parameter, input, actor);
}

@Override public @NotNull BukkitCommandActor getActor() {
return super.getActor().as(BukkitCommandActor.class);
public InvalidPlayerException(@NotNull CommandParameter parameter, @NotNull String input) {
super(parameter, input);
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package revxrsal.commands.bukkit.exception;

import org.jetbrains.annotations.NotNull;
import revxrsal.commands.bukkit.BukkitCommandActor;
import revxrsal.commands.command.CommandActor;
import revxrsal.commands.command.CommandParameter;
import revxrsal.commands.exception.InvalidValueException;

Expand All @@ -12,11 +10,7 @@
*/
public class InvalidWorldException extends InvalidValueException {

public InvalidWorldException(@NotNull CommandParameter parameter, @NotNull String input, @NotNull CommandActor actor) {
super(parameter, input, actor);
}

@Override public @NotNull BukkitCommandActor getActor() {
return super.getActor().as(BukkitCommandActor.class);
public InvalidWorldException(@NotNull CommandParameter parameter, @NotNull String input) {
super(parameter, input);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import lombok.AllArgsConstructor;
import lombok.Getter;
import org.jetbrains.annotations.NotNull;
import revxrsal.commands.bukkit.BukkitCommandActor;

/**
* Thrown when a console-only command is executed by a non-console
Expand All @@ -12,9 +10,4 @@
@AllArgsConstructor
public class SenderNotConsoleException extends RuntimeException {

/**
* The command actor that failed to be a console.
*/
private final @NotNull BukkitCommandActor actor;

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import lombok.AllArgsConstructor;
import lombok.Getter;
import org.jetbrains.annotations.NotNull;
import revxrsal.commands.bukkit.BukkitCommandActor;

/**
* Thrown when a player-only command is executed by a non-player
Expand All @@ -12,9 +10,4 @@
@AllArgsConstructor
public class SenderNotPlayerException extends RuntimeException {

/**
* The command actor that failed to be a player.
*/
private final @NotNull BukkitCommandActor actor;

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import net.md_5.bungee.api.connection.ProxiedPlayer;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import revxrsal.commands.bungee.core.BungeeActor;
import revxrsal.commands.bungee.exception.SenderNotPlayerException;
import revxrsal.commands.command.CommandActor;

Expand Down Expand Up @@ -41,4 +42,14 @@ public interface BungeeCommandActor extends CommandActor {
*/
@NotNull ProxiedPlayer requirePlayer() throws SenderNotPlayerException;

/**
* Creates a new {@link BungeeCommandActor} that wraps the given {@link CommandSender}.
*
* @param sender Command sender to wrap
* @return The wrapping {@link BungeeCommandActor}.
*/
static @NotNull BungeeCommandActor wrap(@NotNull CommandSender sender) {
return new BungeeActor(sender);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

import java.util.UUID;

final class BungeeActor implements BungeeCommandActor {
public final class BungeeActor implements BungeeCommandActor {

private static final UUID CONSOLE_UUID = new UUID(0, 0);

Expand All @@ -35,7 +35,7 @@ public BungeeActor(CommandSender sender) {

@Override public @NotNull ProxiedPlayer requirePlayer() throws SenderNotPlayerException {
if (!isPlayer())
throw new SenderNotPlayerException(this);
throw new SenderNotPlayerException();
return (ProxiedPlayer) sender;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public BungeeHandler(Plugin plugin) {
if (name.equalsIgnoreCase("me") || name.equalsIgnoreCase("self"))
return ((BungeeCommandActor) actor).requirePlayer();
ProxiedPlayer player = ProxyServer.getInstance().getPlayer(name);
if (player == null) throw new InvalidPlayerException(parameter, name, actor);
if (player == null) throw new InvalidPlayerException(parameter, name);
return player;
});
registerValueResolver(PlayerSelector.class, PlayerSelectorResolver.INSTANCE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ enum PlayerSelectorResolver implements ValueResolver<PlayerSelector> {
default: {
ProxiedPlayer player = ProxyServer.getInstance().getPlayer(value);
if (player == null)
throw new InvalidPlayerException(parameter, value, actor);
throw new InvalidPlayerException(parameter, value);
coll.add(player);
return coll::iterator;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,36 +1,37 @@
package revxrsal.commands.bungee.exception;

import org.jetbrains.annotations.NotNull;
import revxrsal.commands.command.CommandActor;
import revxrsal.commands.exception.DefaultExceptionHandler;

public /*final*/ class BungeeExceptionAdapter extends DefaultExceptionHandler {

public static final BungeeExceptionAdapter INSTANCE = new BungeeExceptionAdapter();

@Override protected final void handleUnknown(@NotNull Throwable throwable) {
@Override protected final void handleUnknown(@NotNull CommandActor actor, @NotNull Throwable throwable) {
if (throwable instanceof SenderNotPlayerException)
senderNotPlayer((SenderNotPlayerException) throwable);
senderNotPlayer(actor, (SenderNotPlayerException) throwable);
else if (throwable instanceof SenderNotConsoleException)
senderNotConsole((SenderNotConsoleException) throwable);
senderNotConsole(actor, (SenderNotConsoleException) throwable);
else if (throwable instanceof InvalidPlayerException)
invalidPlayer((InvalidPlayerException) throwable);
invalidPlayer(actor, (InvalidPlayerException) throwable);
else
handleUnknownThrowable(throwable);
handleUnknownThrowable(actor, throwable);
}

protected void senderNotPlayer(@NotNull SenderNotPlayerException exception) {
exception.getActor().error("You must be a player to use this command!");
protected void senderNotPlayer(@NotNull CommandActor actor, @NotNull SenderNotPlayerException exception) {
actor.error("You must be a player to use this command!");
}

protected void senderNotConsole(@NotNull SenderNotConsoleException exception) {
exception.getActor().error("This command can only be used on console!");
protected void senderNotConsole(@NotNull CommandActor actor, @NotNull SenderNotConsoleException exception) {
actor.error("This command can only be used on console!");
}

protected void invalidPlayer(@NotNull InvalidPlayerException exception) {
exception.getActor().error("Invalid player: &e" + exception.getInput());
protected void invalidPlayer(@NotNull CommandActor actor, @NotNull InvalidPlayerException exception) {
actor.error("Invalid player: &e" + exception.getInput());
}

protected void handleUnknownThrowable(@NotNull Throwable throwable) {
protected void handleUnknownThrowable(@NotNull CommandActor actor, @NotNull Throwable throwable) {
throwable.printStackTrace();
}
}
Loading

0 comments on commit 0e23b2b

Please sign in to comment.