Skip to content

Commit

Permalink
1.1.11: Adjusted messages and dependencies
Browse files Browse the repository at this point in the history
I accidentally changed version in 'build.gradle.kts' in previous commit, oops, heh

- Moved 'AnnoyingUtility#getString(AnnoyingPlugin, String)' to 'AnnoyingPlugin' (now 'AnnoyingUtility#getMessagesString(String)')
- Renamed 'AnnoyingPlugin#unloadPlugin()' to 'AnnoyingPlugin#disablePlugin()'
- If required dependencies are missing it'll tell the user ALL of the missing required dependencies
- Added '/test disable' command to 'TestPlugin' that will disable/unregister the '/test' command
- Modified 'TestPlugin' 'msgs.yml'
  • Loading branch information
srnyx committed Dec 18, 2022
1 parent c3505f7 commit 0053792
Show file tree
Hide file tree
Showing 7 changed files with 162 additions and 51 deletions.
2 changes: 1 addition & 1 deletion src/main/java/xyz/srnyx/annoyingapi/AnnoyingMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ public List<JSONComponent> getComponents(@Nullable AnnoyingSender annoyingSender

final ConfigurationSection section = messages.getConfigurationSection(key);
if (section == null) {
final String[] split = AnnoyingUtility.getString(plugin, key).split(plugin.options.splitterJson);
final String[] split = messages.getString(key, key).split(plugin.options.splitterJson);
String display = split[0];

// Replacements
Expand Down
67 changes: 42 additions & 25 deletions src/main/java/xyz/srnyx/annoyingapi/AnnoyingPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import java.util.*;
import java.util.logging.Level;
import java.util.stream.Collectors;


/**
Expand Down Expand Up @@ -76,9 +77,9 @@ public AnnoyingPlugin() {
@Override
public final void onLoad() {
messages = new AnnoyingResource(this, options.messagesFileName);
options.prefix = AnnoyingUtility.getString(this, options.prefix);
options.splitterJson = AnnoyingUtility.getString(this, options.splitterJson);
options.splitterPlaceholder = AnnoyingUtility.getString(this, options.splitterPlaceholder);
options.prefix = getMessagesString(options.prefix);
options.splitterJson = getMessagesString(options.splitterJson);
options.splitterPlaceholder = getMessagesString(options.splitterPlaceholder);

// Custom onLoad
load();
Expand All @@ -103,8 +104,8 @@ public final void onEnable() {

// Download missing dependencies then enable the plugin
if (!missingDependencies.isEmpty()) {
log(Level.WARNING, "&6&lMissing dependencies! &eAnnoyingAPI will attempt to automatically download them...");
new AnnoyingDownload(AnnoyingPlugin.this, missingDependencies).downloadPlugins(this::enablePlugin);
log(Level.WARNING, "&6&lMissing dependencies! &eAnnoyingAPI will attempt to download/install them...");
new AnnoyingDownload(this, missingDependencies).downloadPlugins(this::enablePlugin);
return;
}

Expand All @@ -119,12 +120,13 @@ public final void onEnable() {
*/
private void enablePlugin() {
// Check if required dependencies are installed
for (final AnnoyingDependency dependency : options.dependencies) {
if (dependency.required && dependency.isNotInstalled()) {
log(Level.SEVERE, "&cMissing dependency, &4" + dependency.name + "&c is required! Unloading plugin...");
if (!getName().equals("AnnoyingAPI")) unloadPlugin();
return;
}
final Set<AnnoyingDependency> missing = options.dependencies.stream()
.filter(dependency -> dependency.required && dependency.isNotInstalled())
.collect(Collectors.toSet());
if (!missing.isEmpty()) {
log(Level.SEVERE, "&cDisabling " + getName() + "because it's missing required dependencies: &4" + missing);
if (!getName().equals("AnnoyingAPI")) disablePlugin();
return;
}

// Start messages
Expand Down Expand Up @@ -180,21 +182,11 @@ public void disable() {
}

/**
* Logs a message to the console
*
* @param level the level of the message
* @param message the message to log
*/
public void log(@Nullable Level level, @NotNull String message) {
if (level == null) level = Level.INFO;
getLogger().log(level, AnnoyingUtility.color(message));
}

/**
* Unloads the plugin (not the API)
* Disables the plugin (not the API)
* <p><i>This is not meant to be overriden, only override if you know what you're doing!</i>
*/
public final void unloadPlugin() {
// Unregister commands listeners, cancel tasks, and disable the plugin
public void disablePlugin() {
// Unregister commands & listeners, cancel tasks, and disable the plugin
options.commands.forEach(AnnoyingCommand::unregister);
options.listeners.forEach(AnnoyingListener::unregister);
Bukkit.getScheduler().cancelTasks(this);
Expand All @@ -210,4 +202,29 @@ public void reloadPlugin() {
manager.disablePlugin(this);
manager.enablePlugin(this);
}

/**
* Logs a message to the console
*
* @param level the level of the message
* @param message the message to log
*/
public void log(@Nullable Level level, @NotNull String message) {
if (level == null) level = Level.INFO;
getLogger().log(level, AnnoyingUtility.color(message));
}

/**
* Gets a string from {@link AnnoyingOptions#messagesFileName} with the specified key
* <p>If the string is not found, it will return the key
*
* @param key the key of the string
*
* @return the string
*/
@NotNull
public String getMessagesString(@NotNull String key) {
if (messages == null) return key;
return messages.getString(key, key);
}
}
16 changes: 0 additions & 16 deletions src/main/java/xyz/srnyx/annoyingapi/AnnoyingUtility.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,6 @@
* General utility methods for AnnoyingAPI
*/
public class AnnoyingUtility {
/**
* Gets a string from {@link AnnoyingOptions#messagesFileName} with the specified key
* <p>If the string is not found, it will return the key
*
* @param plugin the plugin to get the string from
* @param key the key of the string
*
* @return the string
*/
@NotNull
public static String getString(@NotNull AnnoyingPlugin plugin, @NotNull String key) {
final AnnoyingResource messages = plugin.messages;
if (messages == null) return key;
return messages.getString(key, key);
}

/**
* Gets an {@link OfflinePlayer} from the specified name
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ default List<String> onTabComplete(@NotNull CommandSender cmdSender, @NotNull Co
default void register() {
final PluginCommand command = getPlugin().getCommand(getName());
if (command == null) {
getPlugin().log(Level.WARNING, "&cCommand &4" + getName() + "&c not found!");
getPlugin().log(Level.WARNING, "&cCommand &4" + getName() + "&c not found in plugin.yml!");
return;
}
command.setExecutor(this);
Expand Down
13 changes: 11 additions & 2 deletions src/test/java/xyz/srnyx/testplugin/TestCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,21 +56,30 @@ public void onCommand(@NotNull AnnoyingSender sender) {
.send(sender);
return;
}
cooldown.start();

// test
if (sender.argEquals(0, "test")) {
new AnnoyingMessage(plugin, "test").send(sender);
} else if (sender.argEquals(0, "reload")) {
return;
}

// reload
if (sender.argEquals(0, "reload")) {
plugin.reloadPlugin();
return;
}

cooldown.start();
// disable
if (sender.argEquals(0, "disable")) unregister();
}

@Override
public List<String> onTabComplete(@NotNull AnnoyingSender sender) {
final List<String> suggestions = new ArrayList<>();
suggestions.add("test");
suggestions.add("reload");
suggestions.add("disable");
return suggestions;
}
}
1 change: 1 addition & 0 deletions src/test/java/xyz/srnyx/testplugin/TestPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public TestPlugin() {
options.noPermission = "no-permission";
options.playerOnly = "player-only";
options.invalidArguments = "invalid-arguments";
options.disabledCommand = "disabled-command";
options.commands.add(new TestCommand(this));
options.listeners.add(new TestListener(this));

Expand Down
112 changes: 106 additions & 6 deletions src/test/resources/msgs.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,110 @@
prefix: "&7lTEST &8&l| &7"
# --- JSON COMPONENTS --- #
# There are 6 different types of JSON components that can be used to send messages (each one can have hover text):
# - "text" A string of text that when hovered over, shows text.
# - "prompt" A string of text that when clicked, will put text into the player's chat box.
# - "clipboard" A string of text that when clicked, will copy the text to the player's clipboard.
# - "chat" A string of text that when clicked, will force the player to send a chat message.
# - "command" A string of text that when clicked, will force the player to run a command.
# - "web" A string of text that when clicked, will open a web page.
#
# The characters you put in plugin.splitter are used to split the message into the component arguments.
# By default, the splitter is "@@", which is what will be used in the examples below.
#
# This is the format of a single message, all single messages are "prompt" components:
key1: "display1@@hover1@@prompt1"
# Sent to player: "display1"
# Shown on hover: "hover1"
# Put in player's chat bar when clicked: "prompt1"
#
# "display1" cannot be empty. If it is, the key name will be sent.
# "hover1" can be empty. If it is, a normal (non-JSON) message will be sent.
# "prompt1" can be empty. If it is, a "text" message will be sent.
#
# These are the formats of an advanced message, all advanced message components depend on what they're sub-key start with:
key2:
text: "display2@@hover2"
# Sent to player: "display3"
# Shown on hover: "hover3"
prompt: "display3@@hover3@@prompt2"
# Sent to player: "display4"
# Shown on hover: "hover4"
# Put in player's chat bar when clicked: "prompt2"
clipboard: "display4@@hover4@@clipboard1"
# Sent to player: "display5"
# Shown on hover: "hover5"
# Copied to player's clipboard when clicked: "clipboard1"
chat: "display5@@hover5@@chat1"
# Sent to player: "display6"
# Shown on hover: "hover6"
# Chat message sent by player when clicked: "chat1"
command: "display6@@hover6@@command1"
# Sent to player: "display7"
# Shown on hover: "hover7"
# Command sent by player when clicked: "command1"
web: "display7@@hover7@@web1"
# Sent to player: "display8"
# Shown on hover: "hover8"
# Website prompted to open when clicked: "web1"
#
# "display" cannot be empty. If it is, the message will not be sent.
# "hover" can be empty. If it is, a normal (non-JSON) message will be sent.
# "prompt", "clipboard", "chat", "command", and "web" can be empty. If they are, a "text" message will be sent.
#
# All the messages in the above example will be strung together into a single message, "display1display2display3display4display5display6display7"
# This way you can create something like this: "[Accept][Decline]". When clicking on one of the options, it can run different functions/commands.
#
# To have multiple of the same components in a message, you simply have to put any text after the component's name:
key3:
command-accept: "[Accept]@@Accept the request@@accept"
command-decline: "[Decline]@@Decline the request@@deny"
# The name of a "text" sub-key doesn't matter since it's the default component (just don't have it start with another component's name).
# --- END JSON COMPONENTS --- #
#
# --- PLACEHOLDER PARAMETERS --- #
# *Some* placeholders will allow you to input a parameter. These parameters are used to change the output of the placeholder
# To input a parameter, you simply put it after the placeholder name (inside the %%), separated by the "plugin.splitters.placeholder" (default: "==")
# Example: "%placeholder==parameter%"
# Below are the different types of parameters that can be used...
# The examples will use "==" as the splitter, but you can change it by modifying "plugin.splitters.placeholder"
#
# - time: A time will be outputed in milliseconds by the plugin. The parameter will determine the format of the time.
# Docs: https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/time/DurationFormatUtils.html
# Examples: "%cooldown==m':'s%" -> "1:30", "%cooldown==H':'m':'s%" -> "1:30:0", "%cooldown==d'd' H'h' m'm' s's'%" -> "1h 30m 0s"
#
# - decimal: A number will be outputed in milliseconds by the plugin. The parameter will determine the format of the number.
# Docs: https://docs.oracle.com/javase/7/docs/api/java/text/DecimalFormat.html
# Examples: "%money==#,###%" -> "8,637", "%money==#,###.##%" -> "8,637.2", "%money==#,###.00%" -> "8,637.20"
#
# Placeholders that support parameters will be displayed as "%name==type%" in the message's placeholder list.
# Examples: "%cooldown=time%, %money=decimal%"
key4: "KDR: %kdr==#.##%" # %kdr==decimal%
# --- END PLACEHOLDER PARAMETERS --- #


# This message can be used to test practically anything!
test: "%prefix%This is a test message!"

# This will be what %prefix% is in all the other messages
# %prefix% can be used in any message in this file as many times as you want
prefix: "&7&lTEST &8&l| &7"

# These are the different splitters for messages/placeholders
splitter:
# This is the splitter for the JSON components (see above). Default: "@@"
json: "@@"
# This is the splitter for the placeholders. Default: "=="
placeholder: "=="
no-permission: "&cYou don't have permission to do that!"
player-only: "&cYou must be a player to do that!"
invalid-arguments: "&cInvalid arguments!"

test: "&aThis is a test!@@&2%command%@@%command%"
cooldown: "&cYou must wait %cooldown==s's'% before using this command again!@@&4%cooldown==s's'%@@%command%"
# Player doesn't have permission to use a command
no-permission: "%prefix%&cYou must have &4%permission%&c to use this!@@&c%command%@@%command%" # %permission%
# Console tries to use a command that can only be used by players
player-only: "%prefix%&cYou must be a player to run this command!@@&c%command%@@%command%"
# Command is used with invalid/incorrect arguments
invalid-arguments: "%prefix%&cInvalid arguments!@@&c%command%@@%command%"
# Command is used when it's disabled
disabled-command: "%prefix%&cThat command is disabled!"

# Message sent when using /annoying version
version: "%prefix%You are running AnnoyingAPI &3v%version%@@&b%command%@@%command%" # %version%

cooldown: "%prefix%&cYou must wait %cooldown==s's'% before using this command again!@@&4%cooldown==s's'%@@%command%"

0 comments on commit 0053792

Please sign in to comment.