Skip to content

Commit

Permalink
1.1.5: Improved options handling
Browse files Browse the repository at this point in the history
- Renamed 'AnnoyingOptions#onlyPlayer' to 'AnnoyingOptions#playerOnly'
- Moved 'AnnoyingDownloadFinish' into 'AnnoyingDownload' and renamed to 'FinishTask' (so it's now 'AnnoyingDownload.FinishTask')
- Moved 'AnnoyingDownload' and 'AnnoyingDependency' out of 'download' folder
- Moved 'AnnoyingMessage#getString(String)' into `AnnoyingUtility'
- Removed 'download' folder
- Improved 'AnnoyingOptions'
  - Added '#dependencies'
  - Added '#dependencyFinishTask'
  - Renamed '#messages' to '#messagesFileName' and it's now a 'String' instead of 'AnnoyingResource'
- Improved 'AnnoyingPlugin'
  - Added '#messages'
  - Made '#options' final so you can only modify it's variables
  - Initialized '#messages' in constructor
  - Translated 'AnnoyingOptions#prefix', 'AnnoyingOptions#splitterJson', and 'AnnoyingOptions#splitterPlaceholder' into their messages from '#messages'
  - Removed '#getOptions' (instead, edit '#options' in your own constructor [just make sure to call 'super()'])
  - Removed '#getDependencies' (now in 'AnnoyingOptions')
  - Removed '#getDependencyFinish' (now in 'AnnoyingOptions')
  • Loading branch information
srnyx committed Dec 7, 2022
1 parent 69605fc commit 7bde095
Show file tree
Hide file tree
Showing 9 changed files with 102 additions and 126 deletions.
3 changes: 1 addition & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
description = "AnnoyingAPI"
version = "1.1.4"
version = "1.1.5"
group = "xyz.srnyx"

plugins {
Expand Down Expand Up @@ -30,7 +30,6 @@ configure<PublishingExtension> {
publications {
create<MavenPublication>("maven") {
groupId = "com.github.srnyx"
artifactId = "annoyingapi"
from(components["java"])
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/xyz/srnyx/annoyingapi/AnnoyingCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ default boolean onCommand(@NotNull CommandSender cmdSender, @NotNull Command cmd

// Player check
if (isPlayerOnly() && !(cmdSender instanceof Player)) {
new AnnoyingMessage(plugin, plugin.options.onlyPlayer).send(sender);
new AnnoyingMessage(plugin, plugin.options.playerOnly).send(sender);
return true;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package xyz.srnyx.annoyingapi.download;
package xyz.srnyx.annoyingapi;

import org.bukkit.Bukkit;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package xyz.srnyx.annoyingapi.download;
package xyz.srnyx.annoyingapi;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
Expand All @@ -10,8 +10,6 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import xyz.srnyx.annoyingapi.AnnoyingPlugin;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
Expand All @@ -28,7 +26,7 @@
public class AnnoyingDownload {
@NotNull private final AnnoyingPlugin plugin;
@NotNull private final Set<AnnoyingDependency> dependencies;
private AnnoyingDownloadFinish finish;
private FinishTask finishTask;
private int remaining = 0;

/**
Expand Down Expand Up @@ -56,10 +54,10 @@ public AnnoyingDownload(@NotNull AnnoyingPlugin plugin, @NotNull AnnoyingDepende
/**
* Downloads the plugins
*
* @param finish the callback to run when all plugins have been processed
* @param finishTask the callback to run when all plugins have been processed
*/
public void downloadPlugins(@Nullable AnnoyingDownloadFinish finish) {
this.finish = finish;
public void downloadPlugins(@Nullable FinishTask finishTask) {
this.finishTask = finishTask;
remaining = dependencies.size();
dependencies.forEach(dependency -> new Thread(() -> attemptDownload(dependency)).start());
}
Expand Down Expand Up @@ -235,7 +233,7 @@ private void finish() {
remaining--;
if (remaining == 0) {
plugin.log(Level.INFO, "\n&a&lAll &2&l" + dependencies.size() + "&a&l plugins have been processed!\n&aPlease resolve any errors and then restart the server.");
if (finish != null) finish.onFinish(dependencies);
if (finishTask != null) finishTask.onFinish(dependencies);
}
}

Expand Down Expand Up @@ -269,4 +267,16 @@ public enum Platform {
*/
MANUAL
}

/**
* Used to handle the download finishTask event
*/
public interface FinishTask {
/**
* This is called when all dependencies have been downloaded
*
* @param plugins the {@link Set} of {@link AnnoyingDependency}s that were processed
*/
void onFinish(@NotNull Set<AnnoyingDependency> plugins);
}
}
52 changes: 13 additions & 39 deletions src/main/java/xyz/srnyx/annoyingapi/AnnoyingMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,13 @@
import org.apache.commons.lang.time.DurationFormatUtils;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.ConfigurationSection;

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

import xyz.srnyx.annoyingapi.file.AnnoyingResource;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand All @@ -30,14 +27,11 @@


/**
* Represents a message from the {@link AnnoyingOptions#messages} file
* Represents a message from the {@link AnnoyingOptions#messagesFileName} file
*/
public class AnnoyingMessage {
@Nullable private final AnnoyingResource messages;
@NotNull private final AnnoyingPlugin plugin;
@NotNull private final String key;
@NotNull private final String prefix;
@NotNull private final String splitterJson;
@NotNull private final String splitterPlaceholder;
@NotNull private final Map<String, String> replacements = new HashMap<>();

/**
Expand All @@ -47,12 +41,9 @@ public class AnnoyingMessage {
* @param key the key of the message
*/
public AnnoyingMessage(@NotNull AnnoyingPlugin plugin, @NotNull String key) {
this.messages = plugin.options.messages;
this.plugin = plugin;
this.key = key;
this.prefix = getString(plugin.options.prefix);
this.splitterJson = getString(plugin.options.splitterJson);
this.splitterPlaceholder = getString(plugin.options.splitterPlaceholder);
replace("%prefix%", prefix);
replace("%prefix%", plugin.options.prefix);
}

/**
Expand Down Expand Up @@ -83,12 +74,13 @@ public AnnoyingMessage replace(@NotNull String before, @Nullable Object after) {
* @see ReplaceType
*/
public AnnoyingMessage replace(@NotNull String before, @Nullable Object after, @NotNull ReplaceType type) {
final Matcher matcher = Pattern.compile("%" + Pattern.quote(before.replace("%", "") + splitterPlaceholder) + ".*?%").matcher(getMessage());
final String regex = "%" + Pattern.quote(before.replace("%", "") + plugin.options.splitterPlaceholder) + ".*?%";
final Matcher matcher = Pattern.compile(regex).matcher(getMessage());
final String match;
final String input;
if (matcher.find()) { // find the placeholder (%<placeholder><splitter><input>%) in the message
match = matcher.group(); // get the placeholder
final String split = match.split(splitterPlaceholder)[1]; // get the input part of the placeholder
final String split = match.split(plugin.options.splitterPlaceholder)[1]; // get the input part of the placeholder
input = split.substring(0, split.length() - 1); // remove the closing % from the input part
} else {
match = before; // use the original placeholder
Expand Down Expand Up @@ -153,11 +145,7 @@ public List<JSONComponent> getComponents() {
*/
@NotNull
public List<JSONComponent> getComponents(@Nullable AnnoyingSender annoyingSender) {
final ArrayList<JSONComponent> components = new ArrayList<>();
if (messages == null) {
components.add(new JTextComponent(key, ChatColor.RED + "No messages file found!"));
return components;
}
final List<JSONComponent> components = new ArrayList<>();

// Replace %command%
final StringBuilder command = new StringBuilder();
Expand All @@ -169,9 +157,9 @@ public List<JSONComponent> getComponents(@Nullable AnnoyingSender annoyingSender
}
replace("%command%", command.toString());

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

// Replacements
Expand All @@ -197,7 +185,7 @@ public List<JSONComponent> getComponents(@Nullable AnnoyingSender annoyingSender
for (final String subKey : section.getKeys(false)) {
String subMessage = section.getString(subKey);
if (subMessage == null) {
components.add(new JTextComponent(key + "." + subKey, AnnoyingUtility.color("&cCheck &4" + messages.getName() + ".yml&c!")));
components.add(new JTextComponent(key + "." + subKey, AnnoyingUtility.color("&cCheck &4" + plugin.options.messagesFileName + "&c!")));
continue;
}

Expand All @@ -207,9 +195,9 @@ public List<JSONComponent> getComponents(@Nullable AnnoyingSender annoyingSender

// Get component parts
final String[] split = subMessage
.replace("%prefix%", prefix)
.replace("%prefix%", plugin.options.prefix)
.replace("%command%", command)
.split(splitterJson);
.split(plugin.options.splitterJson);
final String display = split[0];
String hover = null;
String function = null;
Expand Down Expand Up @@ -398,20 +386,6 @@ public void send(@NotNull AnnoyingSender sender) {
JSON.send(sender.getCmdSender(), new ArrayList<>(getComponents(sender)));
}

/**
* Gets a string from {@link AnnoyingOptions#messages} with the specified key
* <p>If the string is not found, it will return the key
*
* @param msgKey the key of the string
*
* @return the string
*/
@NotNull
private String getString(@NotNull String msgKey) {
if (messages == null) return msgKey;
return messages.getString(msgKey, msgKey);
}

/**
* Different replace types for {@link #replace(String, Object, ReplaceType)}
*/
Expand Down
53 changes: 37 additions & 16 deletions src/main/java/xyz/srnyx/annoyingapi/AnnoyingOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@

import com.olliez4.interface4.util.json.JSON;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.event.Listener;

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

import xyz.srnyx.annoyingapi.file.AnnoyingResource;

import java.util.HashSet;
import java.util.Set;
Expand All @@ -22,45 +20,68 @@ public class AnnoyingOptions {
* <i>{@code OPTIONAL}</i> The primary color used by the API (e.g. for start messages)
*/
@NotNull public ChatColor colorLight = ChatColor.AQUA;

/**
* <i>{@code OPTIONAL}</i> The secondary color used by the API (e.g. for start messages)
*/
@NotNull public ChatColor colorDark = ChatColor.DARK_AQUA;

/**
* <i>{@code RECOMMENDED}</i> The {@link AnnoyingResource} containing the plugin's messages <i>(usually just {@code messages.yml})</i>
* <i>{@code RECOMMENDED}</i> The file name of the messages file <i>(usually {@code messages.yml})</i>
* <p>If not specified, no messages will be loaded (plugin will still enable)
*/
@Nullable public AnnoyingResource messages;
@NotNull public String messagesFileName = "messages.yml";

/**
* <i>{@code OPTIONAL}</i> The {@link #messages} key for the plugin's prefix
* <i>{@code OPTIONAL}</i> The {@link AnnoyingPlugin#messages} key for the plugin's prefix
* <p>AnnoyingAPI will turn this into the message from the resource file
*/
@NotNull public String prefix = "plugin.prefix";

/**
* <i>{@code OPTIONAL}</i> The {@link #messages} key for the plugin's {@link JSON} component splitter
* <i>{@code OPTIONAL}</i> The {@link AnnoyingPlugin#messages} key for the plugin's {@link JSON} component splitter
* <p>AnnoyingAPI will turn this into the message from the resource file
*/
@NotNull public String splitterJson = "plugin.splitters.json";

/**
* <i>{@code OPTIONAL}</i> The {@link #messages} key for the plugin's placeholder component splitter
* <i>{@code OPTIONAL}</i> The {@link AnnoyingPlugin#messages} key for the plugin's placeholder component splitter
* <p>AnnoyingAPI will turn this into the message from the resource file
*/
@NotNull public String splitterPlaceholder = "plugin.splitters.placeholder";

/**
* <i>{@code OPTIONAL}</i> The {@link #messages} key for the plugin's "no permission" message
* <i>{@code OPTIONAL}</i> The {@link AnnoyingPlugin#messages} key for the plugin's "no permission" message
*/
@NotNull public String noPermission = "error.no-permission";

/**
* <i>{@code OPTIONAL}</i> The {@link #messages} key for the plugin's "player-only" message
* <i>{@code OPTIONAL}</i> The {@link AnnoyingPlugin#messages} key for the plugin's "player-only" message
*/
@NotNull public String onlyPlayer = "error.only-player";
@NotNull public String playerOnly = "error.player-only";

/**
* <i>{@code OPTIONAL}</i> The {@link #messages} key for the plugin's "invalid arguments" message
* <i>{@code OPTIONAL}</i> The {@link AnnoyingPlugin#messages} key for the plugin's "invalid arguments" message
*/
@NotNull public String invalidArguments = "error.invalid-arguments";

/**
* <i>{@code OPTIONAL}</i> The {@link AnnoyingCommand}s to register (add to this {@link Set})
*/
@NotNull public final Set<AnnoyingCommand> commands = new HashSet<>();

/**
* <i>{@code OPTIONAL}</i> The {@link AnnoyingCommand}s to register
* <i>{@code OPTIONAL}</i> The {@link Listener}s to register (add to this {@link Set})
*/
@NotNull public Set<AnnoyingCommand> commands = new HashSet<>();
@NotNull public final Set<AnnoyingListener> listeners = new HashSet<>();

/**
* <i>{@code OPTIONAL}</i> Dependencies of the API and the plugin (add to this {@link Set})
*/
@NotNull public final Set<AnnoyingDependency> dependencies = new HashSet<>();

/**
* <i>{@code OPTIONAL}</i> The {@link Listener}s to register
* <i>{@code OPTIONAL}</i> The task to run when all dependencies are downloaded
*/
@NotNull public Set<AnnoyingListener> listeners = new HashSet<>();
@NotNull public AnnoyingDownload.FinishTask dependencyFinishTask = plugins -> Bukkit.getServer().shutdown();
}
Loading

0 comments on commit 7bde095

Please sign in to comment.