Skip to content

Commit

Permalink
1.1.8: More utility methods
Browse files Browse the repository at this point in the history
- Added '%permission%' placeholder for 'erorr.no-permission' message
- Added 'AnnoyingPlugin#reload()'
- Added '/test reload' to test 'AnnoyingPlugin#reload()' in test JAR/plugin
- 'onTabComplete(AnnoyingSender)' now returns a 'Collection<String>' to support both 'List's and 'Set's
- Moved 'AnnoyingCommandRegister', 'AnnoyingDependency', and 'AnnoyingDownload' into 'dependency' folder
- Removed 'AnnoyingPlugin#commandRegister'
- Added more 'AnnoyingSender' methods
  - '#getPlayer()'
  - '#argEquals(int, String)
- Added more 'AnnoyingUtility' methods
  - '#getPlayer(String)'
  - '#getOnlinePlayerNames()'
  - '#getOfflinePlayerNames()'
  - '#getAllPlayerNames()'
  - '#getWorldNames()'
  • Loading branch information
srnyx committed Dec 10, 2022
1 parent ab5791c commit bf454f8
Show file tree
Hide file tree
Showing 12 changed files with 175 additions and 99 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
description = "AnnoyingAPI"
version = "1.1.7"
version = "1.1.8"
group = "xyz.srnyx"

repositories {
Expand Down
16 changes: 10 additions & 6 deletions src/main/java/xyz/srnyx/annoyingapi/AnnoyingCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.function.Predicate;
Expand Down Expand Up @@ -40,7 +41,9 @@ default boolean onCommand(@NotNull CommandSender cmdSender, @NotNull Command cmd
// Permission check
final String permission = getPermission();
if (permission != null && !cmdSender.hasPermission(getPermission())) {
new AnnoyingMessage(plugin, plugin.options.noPermission).send(sender);
new AnnoyingMessage(plugin, plugin.options.noPermission)
.replace("%permission%", permission)
.send(sender);
return true;
}

Expand Down Expand Up @@ -74,10 +77,10 @@ default boolean onCommand(@NotNull CommandSender cmdSender, @NotNull Command cmd
default List<String> onTabComplete(@NotNull CommandSender cmdSender, @NotNull Command cmd, @NotNull String label, @NotNull String[] args) {
// Permission check
final String permission = getPermission();
if (permission != null && !cmdSender.hasPermission(getPermission())) return Collections.emptyList();
if (permission != null && !cmdSender.hasPermission(permission)) return Collections.emptyList();

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

// Filter suggestions
Expand Down Expand Up @@ -168,14 +171,15 @@ default Predicate<String[]> getArgsPredicate() {
void onCommand(@NotNull AnnoyingSender sender);

/**
* <i>{@code REQUIRED}</i> This is the tab completion for the command
* <i>{@code OPTIONAL}</i> This is the tab completion for the command
* <p><i>{@link AnnoyingUtility} will come in handy</i>
*
* @param sender the sender of the command
*
* @return a list of suggestions for the command
* @return a {@link Collection} of suggestions
*/
@Nullable
default List<String> onTabComplete(@NotNull AnnoyingSender sender) {
default Collection<String> onTabComplete(@NotNull AnnoyingSender sender) {
return null;
}
}
2 changes: 2 additions & 0 deletions src/main/java/xyz/srnyx/annoyingapi/AnnoyingOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

import org.jetbrains.annotations.NotNull;

import xyz.srnyx.annoyingapi.dependency.AnnoyingDependency;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
Expand Down
37 changes: 19 additions & 18 deletions src/main/java/xyz/srnyx/annoyingapi/AnnoyingPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@

import org.apache.commons.lang.StringUtils;

import org.bukkit.Bukkit;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitRunnable;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import xyz.srnyx.annoyingapi.dependency.AnnoyingDependency;
import xyz.srnyx.annoyingapi.dependency.AnnoyingDownload;
import xyz.srnyx.annoyingapi.file.AnnoyingResource;
import xyz.srnyx.annoyingapi.plugin.ApiCommand;

Expand All @@ -22,12 +27,7 @@ public class AnnoyingPlugin extends JavaPlugin {
/**
* A {@link List} containing missing dependencies from <b>ALL</b> plugins using AnnoyingAPI
*/
@NotNull private static final List<AnnoyingDependency> missingDependencies = new ArrayList<>();

/**
* Instance of {@link AnnoyingCommandRegister} to register commands
*/
@Nullable public static AnnoyingCommandRegister commandRegister = null;
@NotNull private static final List<AnnoyingDependency> MISSING_DEPENDENCIES = new ArrayList<>();

/**
* The API options for the plugin
Expand All @@ -50,15 +50,7 @@ public class AnnoyingPlugin extends JavaPlugin {
public AnnoyingPlugin() {
super();
if (getName().equals("AnnoyingAPI")) {
// Command register
try {
Class.forName("com.mojang.brigadier.CommandDispatcher");
commandRegister = new AnnoyingCommandRegister();
} catch (final ClassNotFoundException | NoClassDefFoundError ignored) {
// Ignored
}

// Get API dependencies
// API dependencies
final Map<AnnoyingDownload.Platform, String> interface4 = new EnumMap<>(AnnoyingDownload.Platform.class);
interface4.put(AnnoyingDownload.Platform.SPIGOT, "102119");
options.dependencies.add(new AnnoyingDependency("Interface4", interface4, true));
Expand All @@ -83,16 +75,16 @@ public final void onEnable() {

// Get missing dependencies
for (final AnnoyingDependency dependency : options.dependencies) {
if (dependency.isNotInstalled() && missingDependencies.stream().noneMatch(d -> d.name.equals(dependency.name))) missingDependencies.add(dependency);
if (dependency.isNotInstalled() && MISSING_DEPENDENCIES.stream().noneMatch(d -> d.name.equals(dependency.name))) MISSING_DEPENDENCIES.add(dependency);
}

// Download missing dependencies using API
if (getName().equals("AnnoyingAPI")) {
new BukkitRunnable() {
public void run() {
if (!missingDependencies.isEmpty()) {
if (!MISSING_DEPENDENCIES.isEmpty()) {
log(Level.WARNING, "&6&lMissing dependencies! &eAnnoyingAPI will attempt to automatically download them...");
new AnnoyingDownload(AnnoyingPlugin.this, missingDependencies).downloadPlugins();
new AnnoyingDownload(AnnoyingPlugin.this, MISSING_DEPENDENCIES).downloadPlugins();
}
}
}.runTaskLater(this, 1L);
Expand Down Expand Up @@ -152,4 +144,13 @@ public void log(@Nullable Level level, @NotNull String message) {
if (level == null) level = Level.INFO;
getLogger().log(level, AnnoyingUtility.color(message));
}

/**
* Reloads the plugin (calls {@link PluginManager#disablePlugin(Plugin)} and then {@link PluginManager#enablePlugin(Plugin)})
*/
public void reload() {
final PluginManager manager = Bukkit.getPluginManager();
manager.disablePlugin(this);
manager.enablePlugin(this);
}
}
23 changes: 23 additions & 0 deletions src/main/java/xyz/srnyx/annoyingapi/AnnoyingSender.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -82,4 +83,26 @@ public String getLabel() {
public String[] getArgs() {
return args;
}

/**
* Casts the {@link CommandSender} to a {@link Player}
*
* @return the {@link Player} that was used
*/
@NotNull
public Player getPlayer() {
return (Player) cmdSender;
}

/**
* Returns if the specified argument index is equal to the specified string (case-insensitive)
*
* @param index the argument index
* @param string the string to compare to
*
* @return if the specified argument index is equal to the specified string (case-insensitive)
*/
public boolean argEquals(int index, @Nullable String string) {
return args[index].equalsIgnoreCase(string);
}
}
71 changes: 71 additions & 0 deletions src/main/java/xyz/srnyx/annoyingapi/AnnoyingUtility.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@

import org.apache.commons.lang.time.DurationFormatUtils;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;

import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
Expand All @@ -18,6 +21,9 @@
import xyz.srnyx.annoyingapi.file.AnnoyingResource;

import java.text.DecimalFormat;
import java.util.Arrays;
import java.util.Set;
import java.util.stream.Collectors;


/**
Expand All @@ -40,6 +46,71 @@ public static String getString(@NotNull AnnoyingPlugin plugin, @NotNull String k
return messages.getString(key, key);
}

/**
* Gets an {@link OfflinePlayer} from the specified name
*
* @param name the name of the player
*
* @return the {@link OfflinePlayer}, or null if not found
*/
@Nullable
public static OfflinePlayer getPlayer(@NotNull String name) {
for (final OfflinePlayer offline : Bukkit.getOfflinePlayers()) {
final String opName = offline.getName();
if (opName != null && opName.equalsIgnoreCase(name)) return offline;
}
return null;
}

/**
* Gets a {@link Set} of all online player names
*
* @return the {@link Set} of player names
*/
@NotNull
public static Set<String> getOnlinePlayerNames() {
return Bukkit.getOnlinePlayers().stream()
.map(Player::getName)
.collect(Collectors.toSet());
}

/**
* Gets a {@link Set} of all offline player names
*
* @return the {@link Set} of player names
*/
@NotNull
public static Set<String> getOfflinePlayerNames() {
return Arrays.stream(Bukkit.getOfflinePlayers())
.filter(player -> !player.isOnline())
.map(OfflinePlayer::getName)
.collect(Collectors.toSet());
}

/**
* Gets a {@link Set} of all player names
*
* @return the {@link Set} of player names
*/
@NotNull
public static Set<String> getAllPlayerNames() {
return Arrays.stream(Bukkit.getOfflinePlayers())
.map(OfflinePlayer::getName)
.collect(Collectors.toSet());
}

/**
* Gets a {@link Set} of all world names
*
* @return the {@link Set} of world names
*/
@NotNull
public static Set<String> getWorldNames() {
return Bukkit.getWorlds().stream()
.map(org.bukkit.World::getName)
.collect(Collectors.toSet());
}

/**
* Translates {@code &} color codes to {@link ChatColor}
*
Expand Down
Loading

0 comments on commit bf454f8

Please sign in to comment.