Performance Improvements (#340)

* Performance Improvements

* More Optimizations

* Even More Optimizations & Cleanups

* Almost a recode I guess
This commit is contained in:
Crypto Morin
2020-07-16 09:32:22 -07:00
committed by GitHub
parent f9f59f1f96
commit 54d5757d0a
52 changed files with 876 additions and 875 deletions

View File

@@ -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";
}

View File

@@ -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<Class<?>> gather(URL jar, List<Class<?>> 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;

View File

@@ -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) {

View File

@@ -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 <http://www.gnu.org/licenses/>.
*
*
*/
package me.clip.placeholderapi.util;
public enum TimeFormat {
DAYS,
HOURS,
MINUTES,
SECONDS
}

View File

@@ -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;

View File

@@ -0,0 +1,110 @@
/*
*
* 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 <http://www.gnu.org/licenses/>.
*
*
*/
package me.clip.placeholderapi.util;
import me.clip.placeholderapi.PlaceholderAPIPlugin;
import org.apache.commons.lang.StringUtils;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
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 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;
private boolean updateAvailable;
public UpdateChecker(PlaceholderAPIPlugin i) {
plugin = i;
pluginVersion = i.getDescription().getVersion();
}
public boolean hasUpdateAvailable() {
return updateAvailable;
}
public String getSpigotVersion() {
return spigotVersion;
}
public void fetch() {
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> {
try {
HttpsURLConnection con = (HttpsURLConnection) new URL(SPIGOT_API).openConnection();
// Prevents the server from freezing with bad internet connection.
con.setRequestMethod("GET");
con.setConnectTimeout(2000);
con.setReadTimeout(2000);
spigotVersion = new BufferedReader(new InputStreamReader(con.getInputStream(), StandardCharsets.UTF_8)).readLine();
} catch (Exception ex) {
plugin.getLogger().warning("Failed to check for updates on spigot.");
return;
}
if (spigotVersion == null || spigotVersion.isEmpty()) return;
updateAvailable = spigotIsNewer();
if (!updateAvailable) return;
Bukkit.getScheduler().runTask(plugin, () -> {
plugin.getLogger()
.info("An update for PlaceholderAPI (v" + spigotVersion + ") is available at:");
plugin.getLogger()
.info("https://www.spigotmc.org/resources/" + RESOURCE_ID + '/');
Bukkit.getPluginManager().registerEvents(this, plugin);
});
});
}
private boolean spigotIsNewer() {
if (spigotVersion == null || spigotVersion.isEmpty()) return false;
String plV = toReadable(pluginVersion);
String spV = toReadable(spigotVersion);
return plV.compareTo(spV) < 0;
}
private String toReadable(String version) {
if (version.contains("-DEV-")) version = StringUtils.split(version, "-DEV-")[0];
return StringUtils.remove(version, '.');
}
@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 + '/');
}
}
}