Skip to content

Commit

Permalink
Add Annoyable
Browse files Browse the repository at this point in the history
  • Loading branch information
srnyx committed Jun 27, 2023
1 parent 70cd077 commit 118be78
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import xyz.srnyx.annoyingapi.AnnoyingMessage;
import xyz.srnyx.annoyingapi.AnnoyingPlugin;
import xyz.srnyx.annoyingapi.parents.Annoyable;
import xyz.srnyx.annoyingapi.utility.AnnoyingUtility;

import java.util.Collection;
Expand All @@ -23,15 +24,7 @@
/**
* Represents a command that can be executed by a player or the console
*/
public interface AnnoyingCommand extends TabExecutor {
/**
* The {@link AnnoyingPlugin} that this command belongs to
*
* @return the plugin instance
*/
@NotNull
AnnoyingPlugin getPlugin();

public interface AnnoyingCommand extends TabExecutor, Annoyable {
/**
* <i>{@code OPTIONAL}</i> This is the name of the command
* <p>If not specified, the lowercase class name will be used ({@code Command} will be removed)
Expand Down Expand Up @@ -97,16 +90,16 @@ default Collection<String> onTabComplete(@NotNull AnnoyingSender sender) {
}

/**
* Returns whether the command is registered to the {@link #getPlugin()}
* Returns whether the command is registered to the {@link #getAnnoyingPlugin()}
*
* @return whether the command is registered
*/
default boolean isRegistered() {
return getPlugin().registeredCommands.contains(this);
return getAnnoyingPlugin().registeredCommands.contains(this);
}

/**
* Toggles the registration of the command to the {@link #getPlugin()}
* Toggles the registration of the command to the {@link #getAnnoyingPlugin()}
*
* @param registered whether the command should be registered or unregistered
*/
Expand All @@ -119,27 +112,27 @@ default void setRegistered(boolean registered) {
}

/**
* Registers the command to the {@link #getPlugin()}
* Registers the command to the {@link #getAnnoyingPlugin()}
*/
default void register() {
if (isRegistered()) return;
final PluginCommand command = getPlugin().getCommand(getName());
final PluginCommand command = getAnnoyingPlugin().getCommand(getName());
if (command == null) {
AnnoyingPlugin.log(Level.WARNING, "&cCommand &4" + getName() + "&c not found in plugin.yml!");
return;
}
command.setExecutor(this);
getPlugin().registeredCommands.add(this);
getAnnoyingPlugin().registeredCommands.add(this);
}

/**
* Unregisters the command from the {@link #getPlugin()}
* Unregisters the command from the {@link #getAnnoyingPlugin()}
*/
default void unregister() {
if (!isRegistered()) return;
final PluginCommand command = getPlugin().getCommand(getName());
if (command != null) command.setExecutor(new DisabledCommand(getPlugin()));
getPlugin().registeredCommands.remove(this);
final PluginCommand command = getAnnoyingPlugin().getCommand(getName());
if (command != null) command.setExecutor(new DisabledCommand(getAnnoyingPlugin()));
getAnnoyingPlugin().registeredCommands.remove(this);
}

/**
Expand All @@ -156,15 +149,15 @@ default void unregister() {
*/
@Override
default boolean onCommand(@NotNull CommandSender cmdSender, @NotNull Command cmd, @NotNull String label, @NotNull String[] args) {
final AnnoyingSender sender = new AnnoyingSender(getPlugin(), cmdSender, cmd, label, args);
final AnnoyingSender sender = new AnnoyingSender(getAnnoyingPlugin(), cmdSender, cmd, label, args);

// Permission & player check
final String permission = getPermission();
if ((permission != null && !sender.checkPermission(permission)) || (isPlayerOnly() && !sender.checkPlayer())) return true;

// Argument check
if (!getArgsPredicate().test(args)) {
new AnnoyingMessage(getPlugin(), getPlugin().options.messageKeys.invalidArguments).send(sender);
new AnnoyingMessage(getAnnoyingPlugin(), getAnnoyingPlugin().options.messageKeys.invalidArguments).send(sender);
return true;
}

Expand All @@ -189,7 +182,7 @@ default List<String> onTabComplete(@NotNull CommandSender cmdSender, @NotNull Co
if (permission != null && !cmdSender.hasPermission(permission)) return Collections.emptyList();

// Get suggestions
final Collection<String> suggestions = onTabComplete(new AnnoyingSender(getPlugin(), cmdSender, cmd, label, args));
final Collection<String> suggestions = onTabComplete(new AnnoyingSender(getAnnoyingPlugin(), cmdSender, cmd, label, args));
if (suggestions == null) return Collections.emptyList();

// Filter suggestions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public DisabledCommand(@NotNull AnnoyingPlugin plugin) {
* @return the plugin instance
*/
@Override @NotNull
public AnnoyingPlugin getPlugin() {
public AnnoyingPlugin getAnnoyingPlugin() {
return plugin;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ public EventHandlers(@NotNull AnnoyingPlugin plugin) {
this.plugin = plugin;
}

@NotNull
public AnnoyingPlugin getPlugin() {
@Override @NotNull
public AnnoyingPlugin getAnnoyingPlugin() {
return plugin;
}

Expand Down
16 changes: 16 additions & 0 deletions api/src/main/java/xyz/srnyx/annoyingapi/parents/Annoyable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package xyz.srnyx.annoyingapi.parents;

import org.jetbrains.annotations.NotNull;

import xyz.srnyx.annoyingapi.AnnoyingPlugin;


public interface Annoyable {
/**
* The {@link AnnoyingPlugin} instance
*
* @return the plugin instance
*/
@NotNull
AnnoyingPlugin getAnnoyingPlugin();
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public ExampleCommand(@NotNull ExamplePlugin plugin) {
}

@Override @NotNull
public ExamplePlugin getPlugin() {
public ExamplePlugin getAnnoyingPlugin() {
return plugin;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import org.jetbrains.annotations.NotNull;

import xyz.srnyx.annoyingapi.AnnoyingListener;
import xyz.srnyx.annoyingapi.parents.AnnoyingListener;
import xyz.srnyx.annoyingapi.AnnoyingMessage;
import xyz.srnyx.annoyingapi.events.PlayerDamageByPlayerEvent;
import xyz.srnyx.annoyingapi.utility.ItemDataUtility;
Expand All @@ -33,7 +33,7 @@ public ExampleListener(@NotNull ExamplePlugin plugin) {
}

@Override @NotNull
public ExamplePlugin getPlugin() {
public ExamplePlugin getAnnoyingPlugin() {
return plugin;
}

Expand Down

0 comments on commit 118be78

Please sign in to comment.