CommandsAPI - command now asks if the sender is a player (option) and if it has the right permission (option)

Added some methods in User class
CommandsAPI is still WIP
This commit is contained in:
Florian CUNY 2017-12-17 11:02:49 +01:00
parent cd40405c30
commit ed6899c989
9 changed files with 133 additions and 80 deletions

View File

@ -21,6 +21,8 @@ public abstract class CommandArgument {
public BSkyBlock plugin = BSkyBlock.getPlugin();
private String label;
private List<String> aliases;
private String permission;
private boolean onlyPlayer;
private Map<String, CommandArgument> subCommands;
public CommandArgument(String label, String... aliases) {
@ -69,7 +71,23 @@ public abstract class CommandArgument {
public void replaceSubCommand(CommandArgument subCommand) {
subCommands.put(subCommand.getLabel(), subCommand);
}
public String getPermission() {
return permission;
}
public void setPermission(String permission) {
this.permission = permission;
}
public boolean isOnlyPlayer() {
return onlyPlayer;
}
public void setOnlyPlayer(boolean onlyPlayer) {
this.onlyPlayer = onlyPlayer;
}
// These methods below just neaten up the code in the commands so "plugin." isn't always used
/**
@ -116,18 +134,4 @@ public abstract class CommandArgument {
protected IslandsManager getIslands() {
return plugin.getIslands();
}
/**
* @param sender
* @return Locale for sender
*/
protected BSBLocale getLocale(CommandSender sender) {
return plugin.getLocale(sender);
}
/**
* @param uuid
* @return Locale for UUID
*/
protected BSBLocale getLocale(UUID uuid) {
return plugin.getLocale(uuid);
}
}

View File

@ -15,12 +15,13 @@ public abstract class CompositeCommand extends Command implements PluginIdentifi
private BSkyBlock plugin = BSkyBlock.getPlugin();
private String permission = null;
private boolean onlyPlayer = false;
private Map<String, CommandArgument> subCommands;
public CompositeCommand(String label, String description){
super(label);
this.setDescription(description);
this.subCommands = new LinkedHashMap<>();
this.setup();
@ -37,7 +38,7 @@ public abstract class CompositeCommand extends Command implements PluginIdentifi
}
public abstract void setup();
public abstract boolean execute(CommandSender sender, String[] args);
public abstract boolean execute(User user, String[] args);
public Map<String, CommandArgument> getSubCommands() {
return subCommands;
@ -63,32 +64,67 @@ public abstract class CompositeCommand extends Command implements PluginIdentifi
subCommands.put(subCommand.getLabel(), subCommand);
}
@Override
public void setPermission(String permission) {
this.permission = permission;
}
@Override
public String getPermission() {
return this.permission;
}
public boolean isOnlyPlayer() {
return onlyPlayer;
}
public void setOnlyPlayer(boolean onlyPlayer) {
this.onlyPlayer = onlyPlayer;
}
@Override
public boolean execute(CommandSender sender, String label, String[] args) {
if (args.length >= 1) {
// Store the latest subCommand found
CommandArgument subCommand = null;
User user = User.getInstance(sender);
if (permission != null && user.hasPermission(permission)) {
if (onlyPlayer && user.isPlayer()) {
if (args.length >= 1) {
// Store the latest subCommand found
CommandArgument subCommand = null;
for (int i = 0 ; i < args.length ; i++) {
// get the subcommand corresponding to the label
if (subCommand == null) subCommand = getSubCommand(args[i]);
else subCommand = subCommand.getSubCommand(args[i]);
for (int i = 0; i < args.length; i++) {
// get the subcommand corresponding to the label
if (subCommand == null) subCommand = getSubCommand(args[i]);
else subCommand = subCommand.getSubCommand(args[i]);
if (subCommand != null) { // check if this subcommand exists
if (!subCommand.hasSubCommmands()) { // if it has not any subcommands
subCommand.execute(User.getInstance(sender), args); //TODO: "cut" the args to only send the needed ones
if (subCommand != null) { // check if this subcommand exists
if (!subCommand.hasSubCommmands()) { // if it has not any subcommands
if (subCommand.getPermission() != null && user.hasPermission(subCommand.getPermission())) {
if (onlyPlayer && user.isPlayer()) {
subCommand.execute(user, args); //TODO: "cut" the args to only send the needed ones
} else {
user.sendMessage("general.errors.use-in-game");
}
} else {
user.sendMessage("general.errors.no-permission");
}
}
// else continue the loop
// TODO: adapt this part to make it works with arguments that are not subcommands
}
// TODO: get the help
else {
//TODO: say "unknown command"
}
}
// else continue the loop
// TODO: adapt this part to make it works with arguments that are not subcommands
}
// TODO: get the help
else {
//TODO: say "unknown command"
} else {
// No args : execute the default behaviour
this.execute(user, args);
}
} else {
user.sendMessage("general.errors.use-in-game");
}
} else {
// No args : execute the default behaviour
this.execute(sender, args);
user.sendMessage("general.errors.no-permission");
}
return true;

View File

@ -92,7 +92,7 @@ public class User {
}
public Set<PermissionAttachmentInfo> getEffectivePermissions() {
return player.getEffectivePermissions();
return sender.getEffectivePermissions();
}
public PlayerInventory getInventory() {
@ -114,6 +114,10 @@ public class User {
return player;
}
public boolean isPlayer() {
return player != null;
}
public CommandSender getSender() {
return sender;
}
@ -123,23 +127,23 @@ public class User {
}
public boolean hasPermission(String string) {
return player.hasPermission(string);
return sender.hasPermission(string);
}
public boolean isOnline() {
return player == null ? false : player.isOnline();
return player != null && player.isOnline();
}
public boolean isOp() {
return player.isOp();
return sender.isOp();
}
/**
* Send a message to sender if message is not empty. Does not include color codes or spaces
* @param reference - language file reference. May be preceded with color codes.
* Send a message to sender if message is not empty. Does not include color codes or spaces.
* @param reference - language file reference
* @param variables - CharSequence target, replacement pairs
*/
public void sendMessage(String reference, String... variables ) {
public void sendMessage(String reference, String... variables) {
String message = ChatColor.getLastColors(reference) + plugin.getLocale(sender).get(ChatColor.stripColor(reference));
if (variables.length > 1) {
for (int i = 0; i < variables.length; i+=2) {
@ -156,13 +160,20 @@ public class User {
}
}
/**
* Sends a message to sender without any modification (colors, multi-lines, placeholders).
* Should only be used for debug purposes.
* @param message - the message to send
*/
public void sendLegacyMessage(String message) {
sender.sendMessage(message);
}
public void setGameMode(GameMode mode) {
player.setGameMode(mode);
}
public void teleport(Location location) {
player.teleport(location);
}
}

View File

@ -3,6 +3,7 @@ package us.tastybento.bskyblock.commands;
import org.bukkit.command.CommandSender;
import us.tastybento.bskyblock.api.commands.CompositeCommand;
import us.tastybento.bskyblock.api.commands.User;
import us.tastybento.bskyblock.commands.admin.AdminVersionCommand;
import us.tastybento.bskyblock.config.Settings;
@ -18,7 +19,7 @@ public class AdminCommand extends CompositeCommand {
}
@Override
public boolean execute(CommandSender sender, String[] args) {
public boolean execute(User user, String[] args) {
return false;
}
}

View File

@ -3,6 +3,7 @@ package us.tastybento.bskyblock.commands;
import org.bukkit.command.CommandSender;
import us.tastybento.bskyblock.api.commands.CompositeCommand;
import us.tastybento.bskyblock.api.commands.User;
import us.tastybento.bskyblock.commands.island.IslandAboutCommand;
import us.tastybento.bskyblock.commands.island.IslandCreateCommand;
import us.tastybento.bskyblock.commands.island.IslandGoCommand;
@ -32,8 +33,8 @@ public class IslandCommand extends CompositeCommand {
}
@Override
public boolean execute(CommandSender sender, String[] args) {
sender.sendMessage("You successfully did /is !");
public boolean execute(User user, String[] args) {
user.sendLegacyMessage("You successfully did /is !");
return true;
}
}

View File

@ -14,35 +14,35 @@ public class IslandAboutCommand extends CommandArgument {
@Override
public boolean execute(User user, String[] args) {
user.sendMessage("About " + BSkyBlock.getPlugin().getDescription().getName() + " v" + BSkyBlock.getPlugin().getDescription().getVersion() + ":");
user.sendMessage("Copyright (c) 2017 - 2018 tastybento, Poslovitch");
user.sendMessage("All rights reserved.");
user.sendMessage("");
user.sendMessage("Redistribution and use in source and binary forms, with or without");
user.sendMessage("modification, are permitted provided that the following conditions are met:");
user.sendLegacyMessage("About " + BSkyBlock.getPlugin().getDescription().getName() + " v" + BSkyBlock.getPlugin().getDescription().getVersion() + ":");
user.sendLegacyMessage("Copyright (c) 2017 - 2018 tastybento, Poslovitch");
user.sendLegacyMessage("All rights reserved.");
user.sendLegacyMessage("");
user.sendLegacyMessage("Redistribution and use in source and binary forms, with or without");
user.sendLegacyMessage("modification, are permitted provided that the following conditions are met:");
user.sendMessage(" * Redistributions of source code must retain the above copyright notice,");
user.sendMessage(" this list of conditions and the following disclaimer.");
user.sendLegacyMessage(" * Redistributions of source code must retain the above copyright notice,");
user.sendLegacyMessage(" this list of conditions and the following disclaimer.");
user.sendMessage(" * Redistributions in binary form must reproduce the above copyright");
user.sendMessage(" notice, this list of conditions and the following disclaimer in the");
user.sendMessage(" documentation and/or other materials provided with the distribution.");
user.sendLegacyMessage(" * Redistributions in binary form must reproduce the above copyright");
user.sendLegacyMessage(" notice, this list of conditions and the following disclaimer in the");
user.sendLegacyMessage(" documentation and/or other materials provided with the distribution.");
user.sendMessage(" * Neither the name of the BSkyBlock team nor the names of its");
user.sendMessage(" contributors may be used to endorse or promote products derived from");
user.sendMessage(" this software without specific prior written permission.");
user.sendLegacyMessage(" * Neither the name of the BSkyBlock team nor the names of its");
user.sendLegacyMessage(" contributors may be used to endorse or promote products derived from");
user.sendLegacyMessage(" this software without specific prior written permission.");
user.sendMessage("THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"");
user.sendMessage("AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE");
user.sendMessage("IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE");
user.sendMessage("ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE");
user.sendMessage("LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR");
user.sendMessage("CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF");
user.sendMessage("SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS");
user.sendMessage("INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN");
user.sendMessage("CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)");
user.sendMessage("ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE");
user.sendMessage("POSSIBILITY OF SUCH DAMAGE. ");
user.sendLegacyMessage("THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"");
user.sendLegacyMessage("AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE");
user.sendLegacyMessage("IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE");
user.sendLegacyMessage("ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE");
user.sendLegacyMessage("LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR");
user.sendLegacyMessage("CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF");
user.sendLegacyMessage("SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS");
user.sendLegacyMessage("INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN");
user.sendLegacyMessage("CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)");
user.sendLegacyMessage("ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE");
user.sendLegacyMessage("POSSIBILITY OF SUCH DAMAGE. ");
return false;
}

View File

@ -118,13 +118,13 @@ public class IslandTeamInviteCommand extends AbstractIslandTeamCommandArgument {
inviteList.put(invitedPlayerUUID, playerUUID);
user.sendMessage("invite.inviteSentTo", "[name]", args[0]);
// Send message to online player
invitedPlayer.sendMessage(ChatColor.GOLD + "invite.nameHasInvitedYou", "[name]", user.getName());
invitedPlayer.sendMessage(ChatColor.GOLD + "invite.toAcceptOrReject", "[label]", getLabel());
invitedPlayer.sendMessage("invite.nameHasInvitedYou", "[name]", user.getName());
invitedPlayer.sendMessage("invite.toAcceptOrReject", "[label]", getLabel());
if (getPlayers().hasIsland(invitedPlayer.getUniqueId())) {
Bukkit.getPlayer(invitedPlayerUUID).sendMessage(ChatColor.RED + getLocale(invitedPlayerUUID).get("invite.warningYouWillLoseIsland"));
invitedPlayer.sendMessage("invite.warningYouWillLoseIsland");
}
} else {
user.sendMessage(ChatColor.RED + "invite.error.YourIslandIsFull");
user.sendMessage("invite.error.YourIslandIsFull");
}
}
return false;

View File

@ -71,7 +71,7 @@ public class IslandTeamPromoteCommand extends AbstractIslandTeamCommandArgument
} else {
// Online
plugin.getServer().getPlayer(targetUUID).sendMessage(ChatColor.GREEN + getLocale(targetUUID).get("makeleader.youAreNowTheOwner"));
target.sendMessage("makeleader.youAreNowTheOwner");
// Check if new leader has a lower range permission than the island size
boolean hasARangePerm = false;
int range = Settings.islandProtectionRange;

View File

@ -45,11 +45,11 @@ public class IslandTeamUninviteCommand extends AbstractIslandTeamCommandArgument
User invitee = User.getInstance(inviteList.inverse().get(playerUUID));
if (invitee != null) {
inviteList.inverse().remove(playerUUID);
invitee.sendMessage(ChatColor.RED + getLocale(invitee.getUniqueId()).get("invite.nameHasUninvitedYou").replace("[name]", user.getName()));
user.sendMessage(ChatColor.GREEN + "general.success");
invitee.sendMessage("invite.nameHasUninvitedYou", "[name]", user.getName());
user.sendMessage("general.success");
}
} else {
user.sendMessage(ChatColor.YELLOW + "help.island.invite");
user.sendMessage("help.island.invite");
}
return false;
}