Created
August 29, 2014 16:51
-
-
Save SupaHam/f4dd5cc7b798e3d445b8 to your computer and use it in GitHub Desktop.
This class manages all the commands. WorldEdit's is a tad bit different than this, but this makes it easier to directly register it to bukkit's cmd map.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.supaham.powerjuice.commands.arena; | |
import java.util.Set; | |
import java.util.stream.Collectors; | |
import com.sk89q.worldedit.Vector; | |
import com.sk89q.worldedit.regions.Region; | |
import com.supaham.powerjuice.PJException; | |
import com.supaham.powerjuice.PowerJuicePlugin; | |
import com.supaham.powerjuice.arena.Arena; | |
import com.supaham.powerjuice.arena.ArenaManager; | |
import com.supaham.powerjuice.commands.PJCommand; | |
import com.supaham.powerjuice.util.StringUtil; | |
import org.bukkit.Location; | |
import org.bukkit.World; | |
import org.bukkit.entity.Player; | |
import org.jetbrains.annotations.Contract; | |
import org.jetbrains.annotations.NotNull; | |
import org.jetbrains.annotations.Nullable; | |
import static com.supaham.powerjuice.util.Language.Command.Arena.NOT_FOUND; | |
import static com.supaham.powerjuice.util.Language.Command.ILLEGAL_CHARS; | |
public abstract class ArenaCommand extends PJCommand { | |
public final ArenaManager arenaManager; | |
protected ArenaCommand(@NotNull PowerJuicePlugin plugin, @NotNull ArenaManager arenaManager) { | |
super(plugin); | |
this.arenaManager = arenaManager; | |
} | |
public void isValidName(@NotNull String name) throws PJException { | |
if (!StringUtil.isASCII(name)) { | |
throw new PJException(ILLEGAL_CHARS.getParsedMessage(name)); | |
} | |
} | |
/** | |
* Finds an {@link Arena} that a {@link Player} is standing in. | |
* | |
* @param player player to check | |
* @return the one Arena the player is standing in | |
* @throws PJException thrown if the {@code player} is standing in none/multiple arenas | |
*/ | |
public Arena findArenaStandingIn(Player player) throws PJException { | |
return findArenaByLocation(player.getLocation(), | |
"You're not standing in an arena.", "You're standing in multiple arenas... "); | |
} | |
/** | |
* Finds an {@link Arena} that a {@link Player} is standing in. | |
* | |
* @param location location to check arena in | |
* @param nonFound the exception message if no arenas were found | |
* @param moreThanOne the exception message if two or more arenas were found | |
* @return the one Arena the player is standing in | |
* @throws PJException thrown if the {@code player} is standing in none/multiple arenas | |
*/ | |
public Arena findArenaByLocation(@NotNull Location location, String nonFound, | |
String moreThanOne) throws PJException { | |
Set<Arena> arenas = arenaManager.getArenas().values().stream().filter(arena -> arena.contains(location)) | |
.collect(Collectors.toSet()); | |
if (arenas.size() == 0) { | |
throw new PJException(nonFound); | |
} else if (arenas.size() > 1) { | |
StringBuilder sb = new StringBuilder(); | |
for (Arena arena : arenas) { | |
sb.append(arena.getName()).append(", "); | |
} | |
sb.setLength(sb.length() - 2); | |
throw new PJException(moreThanOne + sb.toString()); | |
} | |
return arenas.iterator().next(); | |
} | |
public Arena getArena(@NotNull World world, @NotNull Region region, @Nullable String arenaName) | |
throws PJException { | |
Vector min = region.getMinimumPoint(); | |
Location location = new Location(world, min.getX(), min.getY(), min.getZ()); // meh | |
return arenaName == null ? | |
findArenaByLocation(location, "No Arenas found in your selection", | |
"There are more than one Arenas in your selection...") : getArena(arenaName); | |
} | |
public Arena getArena(String arenaName) throws PJException { | |
return getArena(arenaName, true); | |
} | |
/** | |
* Gets an {@link Arena} by name. | |
* | |
* @param arenaName name of the {@link Arena} to get. | |
* @param throwIfNull whether to throw a {@link PJException} if the arena doesn't exist. | |
* @return the retrieved arena | |
* @throws PJException thrown if {@code throwIfNull} is true and the retrieved arena is null | |
*/ | |
@Contract | |
public Arena getArena(@NotNull String arenaName, boolean throwIfNull) throws PJException { | |
isValidName(arenaName); | |
Arena arena = arenaManager.getArena(arenaName); | |
if (throwIfNull && arena == null) { | |
throw new PJException(NOT_FOUND.getParsedMessage(arenaName)); | |
} | |
return arena; | |
} | |
/** | |
* Checks if an Arena exists in the {@link ArenaManager}. | |
* | |
* @param arenaName arena name to check | |
* @return whether the {@code arena} exists in the {@link ArenaManager} | |
*/ | |
public boolean exists(String arenaName) { | |
return arenaManager.hasArena(arenaName); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.supaham.powerjuice.commands.arena; | |
import com.sk89q.minecraft.util.commands.Command; | |
import com.sk89q.minecraft.util.commands.CommandPermissions; | |
import com.sk89q.worldedit.Vector; | |
import com.sk89q.worldedit.bukkit.selections.CuboidSelection; | |
import com.sk89q.worldedit.regions.Region; | |
import com.supaham.powerjuice.PJException; | |
import com.supaham.powerjuice.PowerJuicePlugin; | |
import com.supaham.powerjuice.arena.Arena; | |
import com.supaham.powerjuice.players.PJPlayer; | |
import com.supaham.powerjuice.worldedit.annotations.Selection; | |
import org.jetbrains.annotations.NotNull; | |
import pluginbase.minecraft.location.Locations; | |
import static com.supaham.powerjuice.util.Language.Command.Arena.ALREADY_EXISTS; | |
import static com.supaham.powerjuice.util.Language.Command.Arena.CREATE_SUCCESS; | |
import static com.supaham.powerjuice.util.Language.Command.Arena.REDEFINE_SUCCESS; | |
/** | |
* Arena commands. | |
*/ | |
public class ArenaCommands extends ArenaCommand { | |
public ArenaCommands(@NotNull PowerJuicePlugin plugin) { | |
super(plugin, plugin.getArenaManager()); | |
} | |
@Command( | |
aliases = {"create"}, | |
desc = "Creates an Arena.", | |
usage = "<name>", | |
help = "Creates an Arena with the boundaries of command sender's WorldEdit region and the name provided " + | |
"as the first argument.", | |
min = 1, | |
max = 1 | |
) | |
@CommandPermissions("wm.arena.create") | |
public void create(PJPlayer sender, @Selection Region region, String arenaName) throws PJException { | |
arenaName = arenaName.toLowerCase(); | |
isValidName(arenaName); | |
if (exists(arenaName)) { | |
throw new PJException(ALREADY_EXISTS.getParsedMessage(arenaName)); | |
} | |
Arena arena = plugin.getArenaManager().createArena(arenaName); | |
Vector min = region.getMinimumPoint(); | |
Vector max = region.getMaximumPoint(); | |
arena.setBoundaries(Locations.getCoordinates(min.getX(), min.getY(), min.getZ()), | |
Locations.getCoordinates(max.getX(), max.getY(), max.getZ())); | |
arena.save(); | |
CREATE_SUCCESS.send(sender.getPlayer(), arena.getName()); | |
} | |
@Command( | |
aliases = {"redefine", "selection", "sel", "re"}, | |
desc = "Redefines an Arena's boundaries.", | |
usage = "<arena>", | |
help = "Redefines an Arena's boundaries. Only supports cuboid selections", | |
min = 1, | |
max = 1 | |
) | |
@CommandPermissions("wm.arena.redefine") | |
public void redefine(PJPlayer sender, @Selection(CuboidSelection.class) Region region, String arenaName) | |
throws PJException { | |
arenaName = arenaName.toLowerCase(); | |
Arena arena = getArena(arenaName); | |
Vector min = region.getMinimumPoint(); | |
Vector max = region.getMaximumPoint(); | |
arena.setBoundaries(Locations.getCoordinates(min.getX(), min.getY(), min.getZ()), | |
Locations.getCoordinates(max.getX(), max.getY(), max.getZ())); | |
arena.save(); | |
REDEFINE_SUCCESS.send(sender.getPlayer(), arena.getName()); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.supaham.powerjuice.commands; | |
import com.sk89q.bukkit.util.CommandInspector; | |
import com.sk89q.minecraft.util.commands.CommandLocals; | |
import com.sk89q.worldedit.util.command.CommandMapping; | |
import com.sk89q.worldedit.util.command.Description; | |
import com.sk89q.worldedit.util.command.Dispatcher; | |
import com.supaham.powerjuice.PowerJuicePlugin; | |
import org.bukkit.command.Command; | |
import org.bukkit.command.CommandSender; | |
import org.jetbrains.annotations.NotNull; | |
class BukkitCommandInspector implements CommandInspector { | |
private final PowerJuicePlugin plugin; | |
private final Dispatcher dispatcher; | |
public BukkitCommandInspector(@NotNull PowerJuicePlugin plugin, @NotNull Dispatcher dispatcher) { | |
this.plugin = plugin; | |
this.dispatcher = dispatcher; | |
} | |
@Override | |
public String getShortText(Command command) { | |
CommandMapping mapping = dispatcher.get(command.getName()); | |
if (mapping != null) { | |
return mapping.getDescription().getShortDescription(); | |
} else { | |
plugin.getLog().warning("BukkitCommandInspector doesn't know how about the command '" + command + "'"); | |
return "Help text not available"; | |
} | |
} | |
@Override | |
public String getFullText(Command command) { | |
CommandMapping mapping = dispatcher.get(command.getName()); | |
if (mapping != null) { | |
Description description = mapping.getDescription(); | |
return "Usage: " + description.getUsage() + (description.getHelp() != null ? "\n" + description.getHelp() : ""); | |
} else { | |
plugin.getLog().warning("BukkitCommandInspector doesn't know about the command '" + command + "'"); | |
return "Help text not available"; | |
} | |
} | |
@Override | |
public boolean testPermission(CommandSender sender, Command command) { | |
CommandMapping mapping = dispatcher.get(command.getName()); | |
if (mapping != null) { | |
CommandLocals locals = new CommandLocals(); | |
locals.put(CommandSender.class, sender); | |
return mapping.getCallable().testPermission(locals); | |
} else { | |
plugin.getLog().warning("BukkitCommandInspector doesn't know about the command '" + command + "'"); | |
return false; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.supaham.powerjuice.commands; | |
import java.util.ArrayList; | |
import java.util.Collections; | |
import java.util.List; | |
import com.google.common.base.Joiner; | |
import com.sk89q.bukkit.util.CommandInfo; | |
import com.sk89q.bukkit.util.CommandRegistration; | |
import com.sk89q.minecraft.util.commands.CommandException; | |
import com.sk89q.minecraft.util.commands.CommandLocals; | |
import com.sk89q.minecraft.util.commands.CommandPermissionsException; | |
import com.sk89q.minecraft.util.commands.WrappedCommandException; | |
import com.sk89q.worldedit.util.command.CommandMapping; | |
import com.sk89q.worldedit.util.command.Description; | |
import com.sk89q.worldedit.util.command.Dispatcher; | |
import com.sk89q.worldedit.util.command.InvalidUsageException; | |
import com.sk89q.worldedit.util.command.fluent.CommandGraph; | |
import com.sk89q.worldedit.util.command.parametric.LegacyCommandsHandler; | |
import com.sk89q.worldedit.util.command.parametric.ParametricBuilder; | |
import com.sk89q.worldedit.util.formatting.ColorCodeBuilder; | |
import com.sk89q.worldedit.util.formatting.component.CommandUsageBox; | |
import com.supaham.powerjuice.PowerJuicePlugin; | |
import com.supaham.powerjuice.commands.arena.ArenaAddCommands; | |
import com.supaham.powerjuice.commands.arena.ArenaCommands; | |
import com.supaham.powerjuice.commands.arena.ArenaRemoveCommands; | |
import com.supaham.powerjuice.commands.arena.ArenaSetCommands; | |
import com.supaham.powerjuice.commands.game.GameCommands; | |
import com.supaham.powerjuice.commands.gamersession.GamerSessionCommands; | |
import com.supaham.powerjuice.commands.lobby.LobbyAddCommands; | |
import com.supaham.powerjuice.commands.lobby.LobbyCommands; | |
import com.supaham.powerjuice.commands.lobby.LobbyRemoveCommands; | |
import com.supaham.powerjuice.commands.lobby.LobbySetCommands; | |
import com.supaham.powerjuice.players.PJPlayer; | |
import org.bukkit.ChatColor; | |
import org.bukkit.command.CommandSender; | |
import org.bukkit.entity.Player; | |
import org.jetbrains.annotations.NotNull; | |
/** | |
* Handles the registration and invocation of commands. | |
*/ | |
public class CommandsManager { | |
private final PowerJuicePlugin plugin; | |
private final Dispatcher dispatcher; | |
private CommandRegistration dynamicCommands; | |
public CommandsManager(@NotNull PowerJuicePlugin plugin) { | |
this.plugin = plugin; | |
dynamicCommands = new CommandRegistration(plugin); | |
ParametricBuilder builder = new ParametricBuilder(); | |
builder.setAuthorizer(new CommandAuthorizer()); | |
builder.setDefaultCompleter(new PlayerCommandCompleter()); | |
builder.addBinding(new PJBinding(plugin)); | |
builder.addExceptionConverter(new PJExceptionConverter(plugin)); | |
builder.addInvokeListener(new LegacyCommandsHandler()); | |
// @formatter:off | |
this.dispatcher = new CommandGraph() | |
.builder(builder) | |
.commands() | |
.registerMethods(new GeneralCommands(plugin)) | |
.group("arena", "ar") | |
.describeAs("Arena management commands.") | |
.registerMethods(new ArenaCommands(plugin)) | |
// Add | |
.group("add") | |
.describeAs("Arena add commands.") | |
.registerMethods(new ArenaAddCommands(plugin)) | |
.parent() | |
// Remove | |
.group("remove") | |
.describeAs("Arena remove commands.") | |
.registerMethods(new ArenaRemoveCommands(plugin)) | |
.parent() // arena group | |
// Set | |
.group("set") | |
.describeAs("Arena set commands.") | |
.registerMethods(new ArenaSetCommands(plugin)) | |
.parent() // arena group | |
.parent() // root | |
.group("lobby", "l") | |
.describeAs("Lobby management commands.") | |
.registerMethods(new LobbyCommands(plugin)) | |
// Add | |
.group("add") | |
.describeAs("Lobby add commands.") | |
.registerMethods(new LobbyAddCommands(plugin)) | |
.parent() // lobby group | |
// Remove | |
.group("remove") | |
.describeAs("Lobby remove commands.") | |
.registerMethods(new LobbyRemoveCommands(plugin)) | |
.parent() // lobby group | |
// Set | |
.group("set") | |
.describeAs("Lobby set commands.") | |
.registerMethods(new LobbySetCommands(plugin)) | |
.parent() // lobby group | |
.parent() // root | |
.group("game", "ga") | |
.describeAs("Game management commands.") | |
.registerMethods(new GameCommands(plugin)) | |
.parent() // root | |
.group("gamersession", "gs") | |
.describeAs("GamerSession management commands.") | |
.registerMethods(new GamerSessionCommands(plugin)) | |
.parent() // root | |
.graph().getDispatcher(); | |
// @formatter:on | |
} | |
public void handleCommand(CommandSender sender, String arguments) { | |
String[] split = arguments.split(" "); | |
// No command found! | |
if (!dispatcher.contains(split[0])) { | |
return; | |
} | |
CommandLocals locals = new CommandLocals(); | |
boolean isPlayer = sender instanceof Player; | |
String RED = isPlayer ? ChatColor.RED.toString() : ""; | |
locals.put(CommandSender.class, sender); | |
if (isPlayer) { | |
locals.put(Player.class, sender); | |
locals.put(PJPlayer.class, plugin.getPlayerManager().getPJPlayer(sender)); | |
} | |
try { | |
dispatcher.call(Joiner.on(" ").join(split), locals, new String[0]); | |
} catch (CommandPermissionsException e) { | |
sender.sendMessage(RED + "You don't have permission to do this."); | |
} catch (InvalidUsageException e) { | |
if (e.isFullHelpSuggested()) { | |
sender.sendMessage(ColorCodeBuilder.asColorCodes( | |
new CommandUsageBox(e.getCommand(), e.getCommandUsed("/", ""), locals))); | |
String message = e.getMessage(); | |
if (message != null) { | |
sender.sendMessage(RED + message); | |
} | |
} else { | |
String message = e.getMessage(); | |
sender.sendMessage(RED + (message != null ? message : | |
"The command was not used properly (no more help available).")); | |
sender.sendMessage(RED + "Usage: " + e.getSimpleUsageString("/")); | |
} | |
} catch (WrappedCommandException e) { | |
Throwable t = e.getCause(); | |
sender.sendMessage(RED + "Please report this error: [See console]"); | |
sender.sendMessage(RED + t.getClass().getName() + ": " + t.getMessage()); | |
plugin.getLog().severe("An unexpected error while handling a WatchMobs command"); | |
t.printStackTrace(); | |
} catch (CommandException e) { | |
String message = e.getMessage(); | |
if (message != null) { | |
sender.sendMessage(RED + e.getMessage()); | |
} else { | |
if (isPlayer) { | |
sender.sendMessage(RED + "An unknown error has occurred! Please report this to a staff " + | |
"member immediately."); | |
} | |
plugin.getLog().severe("An unknown error occurred: "); | |
e.printStackTrace(); | |
} | |
} | |
} | |
public List<String> handleCommandSuggestion(CommandSender sender, String arguments) { | |
try { | |
return new PlayerCommandCompleter().getSuggestions(arguments, new CommandLocals()); | |
} catch (CommandException e) { | |
sender.sendMessage(((sender instanceof Player) ? ChatColor.RED : "") + e.getMessage()); | |
return Collections.emptyList(); | |
} | |
} | |
public void registerCommands() { | |
List<CommandInfo> toRegister = new ArrayList<CommandInfo>(); | |
BukkitCommandInspector inspector = new BukkitCommandInspector(plugin, dispatcher); | |
for (CommandMapping command : dispatcher.getCommands()) { | |
Description description = command.getDescription(); | |
List<String> permissions = description.getPermissions(); | |
String[] permissionsArray = new String[permissions.size()]; | |
permissions.toArray(permissionsArray); | |
toRegister.add(new CommandInfo(description.getUsage(), description.getShortDescription(), | |
command.getAllAliases(), inspector, permissionsArray)); | |
} | |
dynamicCommands.register(toRegister); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.supaham.powerjuice.commands; | |
import com.sk89q.minecraft.util.commands.Command; | |
import com.sk89q.minecraft.util.commands.CommandPermissions; | |
import com.sk89q.worldedit.util.command.parametric.Optional; | |
import com.supaham.powerjuice.PJException; | |
import com.supaham.powerjuice.PowerJuicePlugin; | |
import com.supaham.powerjuice.players.PJPlayer; | |
import org.jetbrains.annotations.NotNull; | |
import static com.supaham.powerjuice.util.Language.Command.General.IGNORE_FALSE; | |
import static com.supaham.powerjuice.util.Language.Command.General.IGNORE_TRUE; | |
public class GeneralCommands extends PJCommand { | |
protected GeneralCommands(@NotNull PowerJuicePlugin plugin) { | |
super(plugin); | |
} | |
@Command( | |
aliases = {"ignore"}, | |
desc = "Ignores a player from the game.", | |
help = "Ignores a player from the game.", | |
max = 0 | |
) | |
@CommandPermissions("pj.ignore") | |
public void ignore(PJPlayer sender, @Optional Boolean ignore) throws PJException { | |
if (ignore == null) { | |
ignore = !sender.isIgnored(); | |
} | |
if (ignore) { | |
sender.ignore(); | |
sender.send(IGNORE_TRUE); | |
} else { | |
sender.unignore(); | |
sender.send(IGNORE_FALSE); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.supaham.powerjuice.commands; | |
import com.sk89q.worldedit.IncompleteRegionException; | |
import com.sk89q.worldedit.regions.Region; | |
import com.sk89q.worldedit.util.command.parametric.ArgumentStack; | |
import com.sk89q.worldedit.util.command.parametric.BindingBehavior; | |
import com.sk89q.worldedit.util.command.parametric.BindingHelper; | |
import com.sk89q.worldedit.util.command.parametric.BindingMatch; | |
import com.sk89q.worldedit.util.command.parametric.ParameterException; | |
import com.supaham.powerjuice.PJException; | |
import com.supaham.powerjuice.PowerJuicePlugin; | |
import com.supaham.powerjuice.arena.Arena; | |
import com.supaham.powerjuice.game.GameManager; | |
import com.supaham.powerjuice.game.GamerSession; | |
import com.supaham.powerjuice.players.PJPlayer; | |
import com.supaham.powerjuice.worldedit.annotations.Selection; | |
import org.bukkit.command.CommandSender; | |
import org.bukkit.entity.Player; | |
import org.jetbrains.annotations.NotNull; | |
import org.jetbrains.annotations.Nullable; | |
public class PJBinding extends BindingHelper { | |
private final PowerJuicePlugin plugin; | |
/** | |
* Create a new instance. | |
* | |
* @param plugin the WorldEdit instance to bind to | |
*/ | |
public PJBinding(@NotNull PowerJuicePlugin plugin) { | |
this.plugin = plugin; | |
} | |
/** | |
* Gets an {@link GamerSession} from an {@link ArgumentStack}. | |
* | |
* @param context the context | |
* @return a {@link GamerSession} | |
* @throws ParameterException on error | |
*/ | |
@BindingMatch(type = GamerSession.class, | |
behavior = BindingBehavior.PROVIDES) | |
@Nullable | |
public GamerSession getGamerSession(ArgumentStack context) throws ParameterException { | |
GameManager mgr = plugin.getGameManager(); | |
if (mgr == null) return null; | |
// TODO check game state | |
return mgr.getCurrentGame().getSession(getPlayer(context)); | |
} | |
/** | |
* Gets an {@link PJPlayer} from an {@link ArgumentStack}. | |
* | |
* @param context the context | |
* @return a {@link PJPlayer} | |
* @throws ParameterException on error | |
*/ | |
@BindingMatch(type = PJPlayer.class, | |
behavior = BindingBehavior.PROVIDES) | |
public PJPlayer getPJPlayer(ArgumentStack context) throws ParameterException { | |
return context.getContext().getLocals().get(PJPlayer.class); | |
} | |
/** | |
* Gets an {@link Player} from an {@link ArgumentStack}. | |
* | |
* @param context the context | |
* @return a {@link Player} | |
* @throws ParameterException on error | |
*/ | |
@BindingMatch(type = Player.class, | |
behavior = BindingBehavior.PROVIDES) | |
public Player getPlayer(ArgumentStack context) throws ParameterException { | |
CommandSender sender = getCommandSender(context); | |
if (sender == null) { | |
throw new ParameterException("No player to get."); | |
} else if (sender instanceof Player) { | |
return (Player) sender; | |
} else { | |
throw new ParameterException("Sender is not a player."); | |
} | |
} | |
/** | |
* Gets an {@link Player} from an {@link ArgumentStack}. | |
* | |
* @param context the context | |
* @return a {@link Player} | |
* @throws ParameterException on error | |
*/ | |
@BindingMatch(type = CommandSender.class, | |
behavior = BindingBehavior.PROVIDES) | |
public CommandSender getCommandSender(ArgumentStack context) throws ParameterException { | |
return context.getContext().getLocals().get(CommandSender.class); | |
} | |
/** | |
* Gets an {@link Player} from an {@link ArgumentStack}. | |
* | |
* @param context the context | |
* @return a {@link Player} | |
* @throws ParameterException on error | |
*/ | |
@BindingMatch(type = Arena.class, | |
behavior = BindingBehavior.CONSUMES, | |
consumedCount = 1) | |
public Arena getArena(ArgumentStack context) throws ParameterException { | |
String input = checkNotNull(context.next(), "Please specify an arena."); | |
return checkNotNull(plugin.getArenaManager().getArena(input), "'" + input + "' is not a valid arena."); | |
} | |
// WORLDEDIT | |
/** | |
* Gets a selection from a {@link ArgumentStack}. | |
* | |
* @param context the context | |
* @param selection the annotation | |
* @return a selection | |
* @throws IncompleteRegionException if no selection is available | |
* @throws ParameterException on other error | |
*/ | |
@BindingMatch(classifier = Selection.class, | |
type = Region.class, | |
behavior = BindingBehavior.PROVIDES) | |
public Object getSelection(ArgumentStack context, Selection selection) | |
throws ParameterException, IncompleteRegionException, PJException { | |
Player player = getPlayer(context); | |
com.sk89q.worldedit.bukkit.selections.Selection sel = plugin.getWorldEdit().getSelection(player); | |
if (sel == null) { | |
throw new IncompleteRegionException(); | |
} | |
if(selection.value() != null && selection.value().length > 0) { | |
boolean found = false; | |
for (Class<? extends com.sk89q.worldedit.bukkit.selections.Selection> clazz : selection.value()) { | |
if(sel.getClass().isAssignableFrom(clazz)) found = true; | |
} | |
if(!found) { | |
throw new PJException("Your selection is not supported."); | |
} | |
} | |
return sel.getRegionSelector().getRegion(); | |
} | |
public static <T> T checkNotNull(T object, String message) throws ParameterException { | |
if (object == null) { | |
throw new ParameterException(message); | |
} | |
return object; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.supaham.powerjuice.commands; | |
import com.supaham.powerjuice.PJException; | |
import com.supaham.powerjuice.PowerJuicePlugin; | |
import com.supaham.powerjuice.players.PJPlayer; | |
import org.jetbrains.annotations.NotNull; | |
import pluginbase.messages.messaging.Messager; | |
import static com.supaham.powerjuice.util.Language.PLAYER_NOT_ONLINE; | |
/** | |
* Represents a Base command for {@link PowerJuicePlugin}. | |
*/ | |
public abstract class PJCommand { | |
protected PowerJuicePlugin plugin; | |
protected PJCommand(@NotNull PowerJuicePlugin plugin) { | |
this.plugin = plugin; | |
} | |
public Messager getMessager() { | |
return plugin.getPluginBase().getMessager(); | |
} | |
public PJPlayer getPJPlayer(String playerName) throws PJException { | |
PJPlayer pjPlayer = plugin.getPlayerManager().getPJPlayer(playerName); | |
if(pjPlayer == null) { | |
throw new PJException(PLAYER_NOT_ONLINE.getParsedMessage(playerName)); | |
} | |
return pjPlayer; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.supaham.powerjuice.commands; | |
import java.util.regex.Matcher; | |
import java.util.regex.Pattern; | |
import com.sk89q.minecraft.util.commands.CommandException; | |
import com.sk89q.worldedit.IncompleteRegionException; | |
import com.sk89q.worldedit.util.command.parametric.ExceptionConverterHelper; | |
import com.sk89q.worldedit.util.command.parametric.ExceptionMatch; | |
import com.supaham.powerjuice.PJException; | |
import com.supaham.powerjuice.PowerJuicePlugin; | |
import org.jetbrains.annotations.NotNull; | |
/** | |
* Converts non {@link CommandException}s into CommandExceptions. | |
*/ | |
public class PJExceptionConverter extends ExceptionConverterHelper { | |
private static final Pattern numberFormat = Pattern.compile("^For input string: \"(.*)\"$"); | |
private final PowerJuicePlugin plugin; | |
public PJExceptionConverter(@NotNull PowerJuicePlugin plugin) { | |
this.plugin = plugin; | |
} | |
@ExceptionMatch | |
public void convert(NumberFormatException e) throws CommandException { | |
final Matcher matcher = numberFormat.matcher(e.getMessage()); | |
if (matcher.matches()) { | |
throw new CommandException("Number expected; string \"" + matcher.group(1) + "\" given."); | |
} else { | |
throw new CommandException("Number expected; string given."); | |
} | |
} | |
@ExceptionMatch | |
public void convert(IncompleteRegionException e) throws CommandException { | |
throw new CommandException("Make a region selection first."); | |
} | |
@ExceptionMatch | |
public void convert(PJException e) throws CommandException { | |
throw new CommandException(e.getMessage(), e); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.supaham.powerjuice.commands; | |
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.stream.Collectors; | |
import com.sk89q.minecraft.util.commands.CommandException; | |
import com.sk89q.minecraft.util.commands.CommandLocals; | |
import com.sk89q.worldedit.util.command.CommandCompleter; | |
import org.bukkit.Bukkit; | |
import org.bukkit.entity.Player; | |
/** | |
* Provides the names of connected {@link Player}s as suggestions. | |
*/ | |
public class PlayerCommandCompleter implements CommandCompleter { | |
@Override | |
public List<String> getSuggestions(String arguments, CommandLocals locals) throws CommandException { | |
List<String> suggestions = new ArrayList<>(); | |
String l = arguments.toLowerCase().trim(); | |
suggestions.addAll(Bukkit.getOnlinePlayers().stream() | |
.filter(player -> player.getName().toLowerCase().startsWith(l)).map(Player::getName) | |
.collect(Collectors.toList())); | |
return suggestions; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment