mirror of
https://github.com/PlaceholderAPI/PlaceholderAPI
synced 2025-09-08 00:46:15 +02:00
9
src/main/java/me/clip/placeholderapi/util/Constants.java
Normal file
9
src/main/java/me/clip/placeholderapi/util/Constants.java
Normal file
@@ -0,0 +1,9 @@
|
||||
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";
|
||||
}
|
@@ -42,50 +42,65 @@ 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 isJar && name.substring(0, name.length() - 4)
|
||||
.equalsIgnoreCase(fileName.substring(0, fileName.length() - 4));
|
||||
return name.endsWith(".jar") && name.replace(".jar", "")
|
||||
.equalsIgnoreCase(fileName.replace(".jar", ""));
|
||||
}
|
||||
|
||||
return isJar;
|
||||
return name.endsWith(".jar");
|
||||
};
|
||||
|
||||
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 ignored) {
|
||||
} catch (Throwable t) {
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private static List<Class<?>> gather(URL jar, List<Class<?>> list, Class<?> clazz) {
|
||||
// list cannot be null.
|
||||
if (list == null) {
|
||||
list = new ArrayList<>();
|
||||
}
|
||||
|
||||
try (URLClassLoader cl = new URLClassLoader(new URL[]{jar}, clazz.getClassLoader());
|
||||
JarInputStream jis = new JarInputStream(jar.openStream())) {
|
||||
|
||||
JarEntry entry;
|
||||
while ((entry = jis.getNextJarEntry()) != null) {
|
||||
String name = entry.getName();
|
||||
if (name == null || name.isEmpty()) continue;
|
||||
while (true) {
|
||||
JarEntry j = jis.getNextJarEntry();
|
||||
if (j == null) {
|
||||
break;
|
||||
}
|
||||
|
||||
String name = j.getName();
|
||||
if (name == null || name.isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (name.endsWith(".class")) {
|
||||
name = name.substring(0, name.length() - 6).replace('/', '.');
|
||||
name = name.replace("/", ".");
|
||||
String cname = name.substring(0, name.lastIndexOf(".class"));
|
||||
|
||||
Class<?> loaded = cl.loadClass(name);
|
||||
if (clazz.isAssignableFrom(loaded)) list.add(loaded);
|
||||
Class<?> c = cl.loadClass(cname);
|
||||
if (clazz.isAssignableFrom(c)) {
|
||||
list.add(c);
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Throwable ignored) {
|
||||
} catch (Throwable t) {
|
||||
}
|
||||
|
||||
return list;
|
||||
|
@@ -24,20 +24,17 @@ import org.bukkit.Bukkit;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
public class Msg {
|
||||
public static void msg(CommandSender sender, String... messages) {
|
||||
for (String message : messages) {
|
||||
String msg = color(message);
|
||||
sender.sendMessage(msg);
|
||||
}
|
||||
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 static void broadcast(String... messages) {
|
||||
CommandSender sender = Bukkit.getConsoleSender();
|
||||
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 String color(String text) {
|
||||
|
28
src/main/java/me/clip/placeholderapi/util/TimeFormat.java
Normal file
28
src/main/java/me/clip/placeholderapi/util/TimeFormat.java
Normal file
@@ -0,0 +1,28 @@
|
||||
/*
|
||||
*
|
||||
* 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
|
||||
}
|
@@ -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, TimeUnit type) {
|
||||
|
||||
public static String getRemaining(int seconds, TimeFormat 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(Duration duration) {
|
||||
StringBuilder builder = new StringBuilder();
|
||||
public static String getTime(final Duration duration) {
|
||||
final StringBuilder builder = new StringBuilder();
|
||||
|
||||
long seconds = duration.getSeconds();
|
||||
long minutes = seconds / 60;
|
||||
|
@@ -1,110 +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;
|
||||
|
||||
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 + '/');
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user