expansions = PlaceholderAPI.getExpansions();
-
- if (expansions.isEmpty()) {
- return;
- }
-
- for (PlaceholderExpansion ex : expansions) {
- if (ex instanceof Cleanable) {
- ((Cleanable) ex).cleanup(e.getPlayer());
+ public void onQuit(PlayerQuitEvent event) {
+ for (PlaceholderHook hook : PlaceholderAPI.getPlaceholders().values()) {
+ if (hook instanceof Cleanable) {
+ ((Cleanable) hook).cleanup(event.getPlayer());
}
}
}
diff --git a/src/main/java/me/clip/placeholderapi/listeners/ServerLoadEventListener.java b/src/main/java/me/clip/placeholderapi/listeners/ServerLoadEventListener.java
index b1f38f7..e08da2b 100644
--- a/src/main/java/me/clip/placeholderapi/listeners/ServerLoadEventListener.java
+++ b/src/main/java/me/clip/placeholderapi/listeners/ServerLoadEventListener.java
@@ -31,7 +31,6 @@ import org.bukkit.event.server.ServerLoadEvent;
import java.util.Map;
public class ServerLoadEventListener implements Listener {
-
private final PlaceholderAPIPlugin plugin;
public ServerLoadEventListener(PlaceholderAPIPlugin instance) {
@@ -40,19 +39,19 @@ public class ServerLoadEventListener implements Listener {
}
/**
- * This method will be called when the server is first loaded
+ * This method will be called when the server is first loaded.
*
* The goal of the method is to register all the expansions as soon as possible
- * especially before players can join
+ * especially before players can join.
*
* This will ensure no issues with expanions and hooks.
*
- * @param e the server load event
+ * @param event the server load event.
*/
@EventHandler
- public void onServerLoad(ServerLoadEvent e) {
+ public void onServerLoad(ServerLoadEvent event) {
plugin.getLogger().info("Placeholder expansion registration initializing...");
- final Map alreadyRegistered = PlaceholderAPI.getPlaceholders();
+ Map alreadyRegistered = PlaceholderAPI.getPlaceholders();
plugin.getExpansionManager().registerAllExpansions();
if (alreadyRegistered != null && !alreadyRegistered.isEmpty()) {
diff --git a/src/main/java/me/clip/placeholderapi/util/Constants.java b/src/main/java/me/clip/placeholderapi/util/Constants.java
deleted file mode 100644
index d911793..0000000
--- a/src/main/java/me/clip/placeholderapi/util/Constants.java
+++ /dev/null
@@ -1,9 +0,0 @@
-package me.clip.placeholderapi.util;
-
-public class Constants {
- public static final String ADMIN_PERMISSION = "placeholderapi.admin";
- public static final String ECLOUD_PERMISSION = "placeholderapi.ecloud";
- public static final String INFO_PERMISSION = "placeholderapi.info";
- public static final String LIST_PERMISSION = "placeholderapi.list";
- public static final String RELOAD_PERMISSION = "placeholderapi.reload";
-}
diff --git a/src/main/java/me/clip/placeholderapi/util/FileUtil.java b/src/main/java/me/clip/placeholderapi/util/FileUtil.java
index 84eb613..9466e5b 100644
--- a/src/main/java/me/clip/placeholderapi/util/FileUtil.java
+++ b/src/main/java/me/clip/placeholderapi/util/FileUtil.java
@@ -42,65 +42,50 @@ public class FileUtil {
try {
File f = new File(PlaceholderAPIPlugin.getInstance().getDataFolder(), folder);
- if (!f.exists()) {
- return list;
- }
+ if (!f.exists()) return list;
FilenameFilter fileNameFilter = (dir, name) -> {
+ boolean isJar = name.endsWith(".jar");
if (fileName != null) {
- return name.endsWith(".jar") && name.replace(".jar", "")
- .equalsIgnoreCase(fileName.replace(".jar", ""));
+ return isJar && name.substring(0, name.length() - 4)
+ .equalsIgnoreCase(fileName.substring(0, fileName.length() - 4));
}
- return name.endsWith(".jar");
+ return isJar;
};
File[] jars = f.listFiles(fileNameFilter);
- if (jars == null) {
- return list;
- }
+ if (jars == null) return list;
for (File file : jars) {
list = gather(file.toURI().toURL(), list, type);
}
return list;
- } catch (Throwable t) {
+ } catch (Throwable ignored) {
}
return null;
}
private static List> gather(URL jar, List> list, Class> clazz) {
- if (list == null) {
- list = new ArrayList<>();
- }
-
+ // list cannot be null.
try (URLClassLoader cl = new URLClassLoader(new URL[]{jar}, clazz.getClassLoader());
JarInputStream jis = new JarInputStream(jar.openStream())) {
- while (true) {
- JarEntry j = jis.getNextJarEntry();
- if (j == null) {
- break;
- }
-
- String name = j.getName();
- if (name == null || name.isEmpty()) {
- continue;
- }
+ JarEntry entry;
+ while ((entry = jis.getNextJarEntry()) != null) {
+ String name = entry.getName();
+ if (name == null || name.isEmpty()) continue;
if (name.endsWith(".class")) {
- name = name.replace("/", ".");
- String cname = name.substring(0, name.lastIndexOf(".class"));
+ name = name.substring(0, name.length() - 6).replace('/', '.');
- Class> c = cl.loadClass(cname);
- if (clazz.isAssignableFrom(c)) {
- list.add(c);
- }
+ Class> loaded = cl.loadClass(name);
+ if (clazz.isAssignableFrom(loaded)) list.add(loaded);
}
}
- } catch (Throwable t) {
+ } catch (Throwable ignored) {
}
return list;
diff --git a/src/main/java/me/clip/placeholderapi/util/Msg.java b/src/main/java/me/clip/placeholderapi/util/Msg.java
index 3d526e4..e1cdd25 100644
--- a/src/main/java/me/clip/placeholderapi/util/Msg.java
+++ b/src/main/java/me/clip/placeholderapi/util/Msg.java
@@ -24,17 +24,20 @@ import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
-import java.util.Arrays;
-import java.util.Objects;
-import java.util.stream.Collectors;
-
-public final class Msg {
- public static void msg(CommandSender s, String... msg) {
- s.sendMessage(Arrays.stream(msg).filter(Objects::nonNull).map(Msg::color).collect(Collectors.joining("\n")));
+public class Msg {
+ public static void msg(CommandSender sender, String... messages) {
+ for (String message : messages) {
+ String msg = color(message);
+ sender.sendMessage(msg);
+ }
}
- public static void broadcast(String... msg) {
- Arrays.stream(msg).filter(Objects::nonNull).map(Msg::color).forEach(Bukkit::broadcastMessage);
+ public static void broadcast(String... messages) {
+ CommandSender sender = Bukkit.getConsoleSender();
+ for (String message : messages) {
+ String msg = color(message);
+ sender.sendMessage(msg);
+ }
}
public static String color(String text) {
diff --git a/src/main/java/me/clip/placeholderapi/util/TimeFormat.java b/src/main/java/me/clip/placeholderapi/util/TimeFormat.java
deleted file mode 100644
index a9c3190..0000000
--- a/src/main/java/me/clip/placeholderapi/util/TimeFormat.java
+++ /dev/null
@@ -1,28 +0,0 @@
-/*
- *
- * PlaceholderAPI
- * Copyright (C) 2019 Ryan McCarthy
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see .
- *
- *
- */
-package me.clip.placeholderapi.util;
-
-public enum TimeFormat {
- DAYS,
- HOURS,
- MINUTES,
- SECONDS
-}
diff --git a/src/main/java/me/clip/placeholderapi/util/TimeUtil.java b/src/main/java/me/clip/placeholderapi/util/TimeUtil.java
index b320ecf..8c834ff 100644
--- a/src/main/java/me/clip/placeholderapi/util/TimeUtil.java
+++ b/src/main/java/me/clip/placeholderapi/util/TimeUtil.java
@@ -22,10 +22,10 @@ package me.clip.placeholderapi.util;
import java.time.Duration;
import java.time.temporal.ChronoUnit;
+import java.util.concurrent.TimeUnit;
public class TimeUtil {
-
- public static String getRemaining(int seconds, TimeFormat type) {
+ public static String getRemaining(int seconds, TimeUnit type) {
if (seconds < 60) {
switch (type) {
case DAYS:
@@ -124,8 +124,8 @@ public class TimeUtil {
* @param duration {@link Duration} (eg, Duration.of(20, {@link ChronoUnit#SECONDS}) for 20 seconds)
* @return formatted time
*/
- public static String getTime(final Duration duration) {
- final StringBuilder builder = new StringBuilder();
+ public static String getTime(Duration duration) {
+ StringBuilder builder = new StringBuilder();
long seconds = duration.getSeconds();
long minutes = seconds / 60;
diff --git a/src/main/java/me/clip/placeholderapi/updatechecker/UpdateChecker.java b/src/main/java/me/clip/placeholderapi/util/UpdateChecker.java
similarity index 66%
rename from src/main/java/me/clip/placeholderapi/updatechecker/UpdateChecker.java
rename to src/main/java/me/clip/placeholderapi/util/UpdateChecker.java
index afafb0d..03d88e2 100644
--- a/src/main/java/me/clip/placeholderapi/updatechecker/UpdateChecker.java
+++ b/src/main/java/me/clip/placeholderapi/util/UpdateChecker.java
@@ -18,13 +18,13 @@
*
*
*/
-package me.clip.placeholderapi.updatechecker;
+package me.clip.placeholderapi.util;
import me.clip.placeholderapi.PlaceholderAPIPlugin;
-import me.clip.placeholderapi.util.Msg;
+import org.apache.commons.lang.StringUtils;
import org.bukkit.Bukkit;
+import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
-import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
@@ -32,10 +32,11 @@ import javax.net.ssl.HttpsURLConnection;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
+import java.nio.charset.StandardCharsets;
public class UpdateChecker implements Listener {
-
- private final int RESOURCE_ID = 6245;
+ private static final int RESOURCE_ID = 6245;
+ private static final String SPIGOT_API = "https://api.spigotmc.org/legacy/update.php?resource=" + RESOURCE_ID;
private final PlaceholderAPIPlugin plugin;
private final String pluginVersion;
private String spigotVersion;
@@ -57,39 +58,35 @@ public class UpdateChecker implements Listener {
public void fetch() {
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> {
try {
- HttpsURLConnection con = (HttpsURLConnection) new URL(
- "https://api.spigotmc.org/legacy/update.php?resource=" + RESOURCE_ID).openConnection();
+ HttpsURLConnection con = (HttpsURLConnection) new URL(SPIGOT_API).openConnection();
+
+ // Prevents the server from freezing with bad internet connection.
con.setRequestMethod("GET");
- spigotVersion = new BufferedReader(new InputStreamReader(con.getInputStream())).readLine();
+ con.setConnectTimeout(2000);
+ con.setReadTimeout(2000);
+
+ spigotVersion = new BufferedReader(new InputStreamReader(con.getInputStream(), StandardCharsets.UTF_8)).readLine();
} catch (Exception ex) {
- plugin.getLogger().info("Failed to check for updates on spigot.");
- return;
- }
-
- if (spigotVersion == null || spigotVersion.isEmpty()) {
+ plugin.getLogger().warning("Failed to check for updates on spigot.");
return;
}
+ if (spigotVersion == null || spigotVersion.isEmpty()) return;
updateAvailable = spigotIsNewer();
-
- if (!updateAvailable) {
- return;
- }
+ if (!updateAvailable) return;
Bukkit.getScheduler().runTask(plugin, () -> {
plugin.getLogger()
- .info("An update for PlaceholderAPI (v" + getSpigotVersion() + ") is available at:");
+ .info("An update for PlaceholderAPI (v" + spigotVersion + ") is available at:");
plugin.getLogger()
- .info("https://www.spigotmc.org/resources/placeholderapi." + RESOURCE_ID + "/");
+ .info("https://www.spigotmc.org/resources/" + RESOURCE_ID + '/');
Bukkit.getPluginManager().registerEvents(this, plugin);
});
});
}
private boolean spigotIsNewer() {
- if (spigotVersion == null || spigotVersion.isEmpty()) {
- return false;
- }
+ if (spigotVersion == null || spigotVersion.isEmpty()) return false;
String plV = toReadable(pluginVersion);
String spV = toReadable(spigotVersion);
@@ -97,21 +94,17 @@ public class UpdateChecker implements Listener {
}
private String toReadable(String version) {
- if (version.contains("-DEV-")) {
- version = version.split("-DEV-")[0];
- }
-
- return version.replaceAll("\\.", "");
+ if (version.contains("-DEV-")) version = StringUtils.split(version, "-DEV-")[0];
+ return StringUtils.remove(version, '.');
}
- @EventHandler(priority = EventPriority.MONITOR)
- public void onJoin(PlayerJoinEvent e) {
- if (e.getPlayer().hasPermission("placeholderapi.updatenotify")) {
- Msg.msg(e.getPlayer(),
- "&bAn update for &fPlaceholder&7API &e(&fPlaceholder&7API &fv" + getSpigotVersion()
- + "&e)"
- , "&bis available at &ehttps://www.spigotmc.org/resources/placeholderapi." + RESOURCE_ID
- + "/");
+ @EventHandler
+ public void onJoin(PlayerJoinEvent event) {
+ Player player = event.getPlayer();
+ if (player.hasPermission("placeholderapi.updatenotify")) {
+ Msg.msg(player,
+ "&bAn update for &fPlaceholder&7API &e(&fPlaceholder&7API &fv" + getSpigotVersion() + "&e)",
+ "&bis available at &ehttps://www.spigotmc.org/resources/placeholderapi." + RESOURCE_ID + '/');
}
}
}
diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml
index d4031ef..8b3e5aa 100644
--- a/src/main/resources/plugin.yml
+++ b/src/main/resources/plugin.yml
@@ -4,39 +4,51 @@ version: ${project.version}
api-version: '1.13'
authors: [extended_clip, Glare]
description: ${project.description}
-permissions:
- placeholderapi.*:
- description: ability to use all commands
- children:
- placeholderapi.admin: true
- placeholderapi.admin:
- description: ability to use all commands
- children:
- placeholderapi.list: true
- placeholderapi.reload: true
- placeholderapi.ecloud: true
- placeholderapi.parse: true
- placeholderapi.register: true
- placeholderapi.updatenotify: true
- placeholderapi.list:
- description: ability to use the list command
- default: op
- placeholderapi.reload:
- description: ability to use the reload command
- default: op
- placeholderapi.parse:
- description: ability to use parse command
- default: op
- placeholderapi.register:
- description: ability to register or unregister placeholder expansions
- default: op
- placeholderapi.ecloud:
- description: allows the usage of ecloud commands
- default: op
- placeholderapi.updatenotify:
- description: notifies you when there is a PAPI update
- default: op
+
commands:
- placeholderapi:
- description: PlaceholderAPI command
- aliases: [papi]
+ placeholderapi:
+ description: PlaceholderAPI command
+ aliases: [papi]
+
+permissions:
+ placeholderapi.*:
+ description: ability to use all commands
+ children:
+ placeholderapi.admin: true
+ placeholderapi.admin:
+ description: ability to use all commands
+ children:
+ placeholderapi.list: true
+ placeholderapi.reload: true
+ placeholderapi.ecloud: true
+ placeholderapi.parse: true
+ placeholderapi.register: true
+ placeholderapi.updatenotify: true
+ placeholderapi.list:
+ description: ability to use the list command
+ default: op
+ placeholderapi.reload:
+ description: ability to use the reload command
+ default: op
+ placeholderapi.parse:
+ description: ability to use parse command
+ default: op
+ placeholderapi.register:
+ description: ability to register or unregister placeholder expansions
+ default: op
+ placeholderapi.ecloud:
+ description: allows the usage of ecloud commands
+ default: op
+ children:
+ placeholderapi.ecloud.enable: true
+ placeholderapi.ecloud.disable: true
+ placeholderapi.ecloud.list: true
+ placeholderapi.ecloud.info: true
+ placeholderapi.ecloud.clear: true
+ placeholderapi.ecloud.status: true
+ placeholderapi.ecloud.refresh: true
+ placeholderapi.ecloud.download: true
+ placeholderapi.ecloud.versioninfo: true
+ placeholderapi.updatenotify:
+ description: notifies you when there is a PAPI update
+ default: op
\ No newline at end of file