Re-Added PluginDependent to HandledCommand

This commit is contained in:
Auxilor
2021-07-05 17:53:28 +02:00
parent eafae209a6
commit 726f4a44c4
5 changed files with 30 additions and 24 deletions

View File

@@ -20,14 +20,16 @@ public abstract class PluginCommand extends HandledCommand implements CommandExe
* <p>
* The name cannot be the same as an existing command as this will conflict.
*
* @param plugin The plugin.
* @param name The name used in execution.
* @param permission The permission required to execute the command.
* @param playersOnly If only players should be able to execute this command.
*/
protected PluginCommand(@NotNull final String name,
protected PluginCommand(@NotNull final EcoPlugin plugin,
@NotNull final String name,
@NotNull final String permission,
final boolean playersOnly) {
super(name, permission, playersOnly);
super(plugin, name, permission, playersOnly);
}
/**

View File

@@ -1,5 +1,6 @@
package com.willfp.eco.core.command.impl;
import com.willfp.eco.core.EcoPlugin;
import com.willfp.eco.core.command.CommandBase;
import com.willfp.eco.internal.commands.HandledCommand;
import org.jetbrains.annotations.NotNull;
@@ -8,24 +9,28 @@ public abstract class Subcommand extends HandledCommand {
/**
* Create subcommand.
*
* @param plugin The plugin.
* @param name The subcommand name.
* @param permission The subcommand permission.
* @param playersOnly If the subcommand only works on players.
*/
protected Subcommand(@NotNull final String name,
protected Subcommand(@NotNull final EcoPlugin plugin,
@NotNull final String name,
@NotNull final String permission,
final boolean playersOnly) {
super(name, permission, playersOnly);
super(plugin, name, permission, playersOnly);
}
/**
* Create subcommand.
*
* @param plugin The plugin.
* @param name The name of the subcommand.
* @param parent The parent command.
*/
protected Subcommand(@NotNull final String name,
protected Subcommand(@NotNull final EcoPlugin plugin,
@NotNull final String name,
@NotNull final CommandBase parent) {
super(name, parent.getPermission(), parent.isPlayersOnly());
super(plugin, name, parent.getPermission(), parent.isPlayersOnly());
}
}

View File

@@ -1,9 +1,11 @@
package com.willfp.eco.internal.commands;
import com.willfp.eco.core.EcoPlugin;
import com.willfp.eco.core.PluginDependent;
import com.willfp.eco.core.command.CommandBase;
import com.willfp.eco.core.command.CommandHandler;
import com.willfp.eco.core.command.TabCompleteHandler;
import com.willfp.eco.core.command.util.CommandUtils;
import com.willfp.eco.internal.commands.util.CommandUtils;
import lombok.AccessLevel;
import lombok.Getter;
import org.bukkit.command.CommandSender;
@@ -16,7 +18,7 @@ import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
public abstract class HandledCommand implements CommandBase {
public abstract class HandledCommand extends PluginDependent<EcoPlugin> implements CommandBase {
/**
* The name of the command.
*/
@@ -50,13 +52,16 @@ public abstract class HandledCommand implements CommandBase {
* <p>
* The name cannot be the same as an existing command as this will conflict.
*
* @param plugin Instance of a plugin.
* @param name The name used in execution.
* @param permission The permission required to execute the command.
* @param playersOnly If only players should be able to execute this command.
*/
protected HandledCommand(@NotNull final String name,
protected HandledCommand(@NotNull final EcoPlugin plugin,
@NotNull final String name,
@NotNull final String permission,
final boolean playersOnly) {
super(plugin);
this.name = name;
this.permission = permission;
this.playersOnly = playersOnly;
@@ -84,14 +89,14 @@ public abstract class HandledCommand implements CommandBase {
*/
protected final void handle(@NotNull final CommandSender sender,
@NotNull final String[] args) {
if (!CommandUtils.canExecute(sender, this)) {
if (!CommandUtils.canExecute(sender, this, this.getPlugin())) {
return;
}
if (args.length > 0) {
for (CommandBase subcommand : this.getSubcommands()) {
if (subcommand.getName().equalsIgnoreCase(args[0])) {
if (!CommandUtils.canExecute(sender, subcommand)) {
if (!CommandUtils.canExecute(sender, subcommand, this.getPlugin())) {
return;
}

View File

@@ -1,7 +1,7 @@
package com.willfp.eco.core.command.util;
package com.willfp.eco.internal.commands.util;
import com.willfp.eco.core.EcoPlugin;
import com.willfp.eco.core.command.CommandBase;
import com.willfp.eco.internal.Internals;
import lombok.experimental.UtilityClass;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
@@ -9,22 +9,16 @@ import org.jetbrains.annotations.NotNull;
@UtilityClass
public class CommandUtils {
/**
* Check if the sender can execute a command.
*
* @param sender The sender.
* @param command The command.
* @return If possible. Sends messages.
*/
public boolean canExecute(@NotNull final CommandSender sender,
@NotNull final CommandBase command) {
@NotNull final CommandBase command,
@NotNull final EcoPlugin plugin) {
if (command.isPlayersOnly() && !(sender instanceof Player)) {
sender.sendMessage(Internals.getInstance().getPlugin().getLangYml().getMessage("not-player"));
sender.sendMessage(plugin.getLangYml().getMessage("not-player"));
return false;
}
if (!sender.hasPermission(command.getPermission()) && sender instanceof Player) {
sender.sendMessage(Internals.getInstance().getPlugin().getLangYml().getNoPermission());
sender.sendMessage(plugin.getLangYml().getNoPermission());
return false;
}