I added Folia support without breaking previous versions for Spigot.

I removed the Bukkit Scheduler implementation to use the Concurrent Executors implementation which is much more maintainable than using an implementation that requires to use another package for Folia like here https://github.com/MilkBowl/Vault/pull/915
This commit is contained in:
Bierque Jason 2023-04-15 16:18:45 +02:00
parent 460cb21c62
commit 2b189bf34e
7 changed files with 56 additions and 36 deletions

3
.idea/.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
# Default ignored files
/shelf/
/workspace.xml

7
.idea/discord.xml Normal file
View File

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="DiscordProjectSettings">
<option name="show" value="ASK" />
<option name="description" value="" />
</component>
</project>

12
.idea/misc.xml Normal file
View File

@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="MavenProjectsManager">
<option name="originalFiles">
<list>
<option value="$PROJECT_DIR$/pom.xml" />
</list>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" default="true" project-jdk-name="corretto-17" project-jdk-type="JavaSDK" />
</project>

View File

View File

@ -4,6 +4,7 @@ description: ${project.description}
authors: [cereal, Sleaker, mung3r]
website: ${project.url}
api-version: 1.13
folia-supported: true
main: ${mainClass}
load: startup

View File

@ -21,6 +21,8 @@ import java.net.URL;
import java.net.URLConnection;
import java.util.Collection;
import java.util.concurrent.Callable;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;
import net.milkbowl.vault.chat.Chat;
@ -76,6 +78,8 @@ import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;
import java.util.concurrent.Executors;
import net.milkbowl.vault.chat.plugins.Chat_TotalPermissions;
public class Vault extends JavaPlugin {
@ -89,12 +93,15 @@ public class Vault extends JavaPlugin {
private String currentVersionTitle = "";
private ServicesManager sm;
private Vault plugin;
private ScheduledExecutorService asyncTaskTimer;
@Override
public void onDisable() {
// Remove all Service Registrations
getServer().getServicesManager().unregisterAll(this);
Bukkit.getScheduler().cancelTasks(this);
if (asyncTaskTimer != null) {
asyncTaskTimer.shutdownNow();
}
}
@Override
@ -115,44 +122,34 @@ public class Vault extends JavaPlugin {
getCommand("vault-info").setExecutor(this);
getCommand("vault-convert").setExecutor(this);
getServer().getPluginManager().registerEvents(new VaultListener(), this);
asyncTaskTimer = Executors.newScheduledThreadPool(1);
// Schedule to check the version every 30 minutes for an update. This is to update the most recent
// version so if an admin reconnects they will be warned about newer versions.
this.getServer().getScheduler().runTask(this, new Runnable() {
@Override
public void run() {
// Programmatically set the default permission value cause Bukkit doesn't handle plugin.yml properly for Load order STARTUP plugins
org.bukkit.permissions.Permission perm = getServer().getPluginManager().getPermission("vault.update");
if (perm == null)
{
perm = new org.bukkit.permissions.Permission("vault.update");
perm.setDefault(PermissionDefault.OP);
plugin.getServer().getPluginManager().addPermission(perm);
}
perm.setDescription("Allows a user or the console to check for vault updates");
getServer().getScheduler().runTaskTimerAsynchronously(plugin, new Runnable() {
@Override
public void run() {
if (getServer().getConsoleSender().hasPermission("vault.update") && getConfig().getBoolean("update-check", true)) {
try {
log.info("Checking for Updates ... ");
newVersion = updateCheck(currentVersion);
if (newVersion > currentVersion) {
log.warning("Stable Version: " + newVersionTitle + " is out!" + " You are still running version: " + currentVersionTitle);
log.warning("Update at: https://dev.bukkit.org/projects/vault");
} else if (currentVersion > newVersion) {
log.info("Stable Version: " + newVersionTitle + " | Current Version: " + currentVersionTitle);
}
} catch (Exception e) {
// ignore exceptions
}
}
}
}, 0, 432000);
Executors.newSingleThreadExecutor().execute(() -> {
org.bukkit.permissions.Permission perm = getServer().getPluginManager().getPermission("vault.update");
if (perm == null)
{
perm = new org.bukkit.permissions.Permission("vault.update");
perm.setDefault(PermissionDefault.OP);
plugin.getServer().getPluginManager().addPermission(perm);
}
perm.setDescription("Allows a user or the console to check for vault updates");
asyncTaskTimer.scheduleAtFixedRate(() -> {
if (getServer().getConsoleSender().hasPermission("vault.update") && getConfig().getBoolean("update-check", true)) {
try {
log.info("Checking for Updates ... ");
newVersion = updateCheck(currentVersion);
if (newVersion > currentVersion) {
log.warning("Stable Version: " + newVersionTitle + " is out!" + " You are still running version: " + currentVersionTitle);
log.warning("Update at: https://dev.bukkit.org/projects/vault");
} else if (currentVersion > newVersion) {
log.info("Stable Version: " + newVersionTitle + " | Current Version: " + currentVersionTitle);
}
} catch (Exception e) {
// ignore exceptions
}
}
}, 0, 21600, TimeUnit.SECONDS);
});
// Load up the Plugin metrics