Skip to content

Commit

Permalink
Started working on Folia support
Browse files Browse the repository at this point in the history
  • Loading branch information
darbyjack committed Mar 24, 2023
1 parent e862abe commit 95786e3
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 59 deletions.
6 changes: 3 additions & 3 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ dependencies {
implementation("org.bstats:bstats-bukkit:3.0.1")
implementation("net.kyori:adventure-platform-bukkit:4.3.0")

compileOnly("org.spigotmc:spigot-api:1.19-R0.1-SNAPSHOT")
compileOnlyApi("dev.folia:folia-api:1.19.4-R0.1-SNAPSHOT") // this is temp
compileOnlyApi("org.jetbrains:annotations:23.0.0")

testImplementation("org.openjdk.jmh:jmh-core:1.32")
Expand All @@ -38,8 +38,8 @@ dependencies {


java {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
sourceCompatibility = JavaVersion.VERSION_17 // this is temp
targetCompatibility = JavaVersion.VERSION_17 // this is temp

withJavadocJar()
withSourcesJar()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ public void onDisable() {

HandlerList.unregisterAll(this);

Bukkit.getScheduler().cancelTasks(this);
Bukkit.getAsyncScheduler().cancelTasks(this); // this is hopefully temp

adventure.close();
adventure = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,40 +203,33 @@ public void fetch(final boolean allowUnverified) {
plugin.getLogger().log(Level.WARNING, "Failed to download expansion information", e);
}

// loop thru what's left on the main thread
plugin
.getServer()
.getScheduler()
.runTask(
plugin,
() -> {
try {
for (Map.Entry<String, CloudExpansion> entry : values.entrySet()) {
String name = entry.getKey();
CloudExpansion expansion = entry.getValue();

expansion.setName(name);

Optional<PlaceholderExpansion> localOpt =
plugin.getLocalExpansionManager().findExpansionByName(name);
if (localOpt.isPresent()) {
PlaceholderExpansion local = localOpt.get();
if (local.isRegistered()) {
expansion.setHasExpansion(true);
expansion.setShouldUpdate(
!local.getVersion().equalsIgnoreCase(expansion.getLatestVersion()));
}
}

cache.put(toIndexName(expansion), expansion);
}
} catch (Throwable e) {
// ugly swallowing of every throwable, but we have to be defensive
plugin
.getLogger()
.log(Level.WARNING, "Failed to download expansion information", e);
}
});
//todo: Figure out why this was being scheduled back on the main thread
try {
for (Map.Entry<String, CloudExpansion> entry : values.entrySet()) {
String name = entry.getKey();
CloudExpansion expansion = entry.getValue();

expansion.setName(name);

Optional<PlaceholderExpansion> localOpt =
plugin.getLocalExpansionManager().findExpansionByName(name);
if (localOpt.isPresent()) {
PlaceholderExpansion local = localOpt.get();
if (local.isRegistered()) {
expansion.setHasExpansion(true);
expansion.setShouldUpdate(
!local.getVersion().equalsIgnoreCase(expansion.getLatestVersion()));
}
}

cache.put(toIndexName(expansion), expansion);
}
} catch (Throwable e) {
// ugly swallowing of every throwable, but we have to be defensive
plugin
.getLogger()
.log(Level.WARNING, "Failed to download expansion information", e);
}
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,12 +269,13 @@ public boolean register(@NotNull final PlaceholderExpansion expansion) {
return false;
}

final ExpansionRegisterEvent event = new ExpansionRegisterEvent(expansion);
Bukkit.getPluginManager().callEvent(event);

if (event.isCancelled()) {
return false;
}
// this is temp
// final ExpansionRegisterEvent event = new ExpansionRegisterEvent(expansion);
// Bukkit.getPluginManager().callEvent(event);
//
// if (event.isCancelled()) {
// return false;
// }

expansionsLock.lock();
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.io.InputStreamReader;
import java.net.URL;
import java.util.Arrays;
import java.util.concurrent.CompletableFuture;
import javax.net.ssl.HttpsURLConnection;
import me.clip.placeholderapi.PlaceholderAPIPlugin;
import me.clip.placeholderapi.util.Msg;
Expand Down Expand Up @@ -54,35 +55,43 @@ public String getSpigotVersion() {
return spigotVersion;
}

//todo: Figure out a better approach for this?
public void fetch() {
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> {
CompletableFuture<Boolean> update = new CompletableFuture<>();
CompletableFuture.supplyAsync(() -> {
try {
HttpsURLConnection con = (HttpsURLConnection) new URL(
"https://api.spigotmc.org/legacy/update.php?resource=" + RESOURCE_ID).openConnection();
"https://api.spigotmc.org/legacy/update.php?resource=" + RESOURCE_ID).openConnection();
con.setRequestMethod("GET");
spigotVersion = new BufferedReader(new InputStreamReader(con.getInputStream())).readLine();
} catch (Exception ex) {
plugin.getLogger().info("Failed to check for updates on spigot.");
return;
return false;
}

if (spigotVersion == null || spigotVersion.isEmpty()) {
return;
plugin.getLogger().info("Failed to check for updates on spigot.");
return false;
}

updateAvailable = spigotIsNewer();

if (!updateAvailable) {
return;
plugin.getLogger().info("PlaceholderAPI is up to date.");
return false;
}

Bukkit.getScheduler().runTask(plugin, () -> {
return true;
});

update.whenComplete((result, error) -> {
if (result) {
plugin.getLogger()
.info("An update for PlaceholderAPI (v" + getSpigotVersion() + ") is available at:");
.info("An update for PlaceholderAPI (v" + getSpigotVersion() + ") is available at:");
plugin.getLogger()
.info("https://www.spigotmc.org/resources/placeholderapi." + RESOURCE_ID + "/");
Bukkit.getPluginManager().registerEvents(this, plugin);
});
.info("https://www.spigotmc.org/resources/placeholderapi." + RESOURCE_ID + "/");
Bukkit.getPluginManager().registerEvents(UpdateChecker.this, plugin);
}
});
}

Expand Down
6 changes: 1 addition & 5 deletions src/main/java/me/clip/placeholderapi/util/Futures.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,7 @@ public static <T> void onMainThread(@NotNull final Plugin plugin,
@NotNull final CompletableFuture<T> future,
@NotNull final BiConsumer<T, Throwable> consumer) {
future.whenComplete((value, exception) -> {
if (Bukkit.isPrimaryThread()) {
consumer.accept(value, exception);
} else {
Bukkit.getScheduler().runTask(plugin, () -> consumer.accept(value, exception));
}
consumer.accept(value, exception);
});
}

Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ main: "me.clip.placeholderapi.PlaceholderAPIPlugin"
version: ${version}
author: HelpChat

folia-supported: true

api-version: "1.13"
description: "An awesome placeholder provider!"

Expand Down

0 comments on commit 95786e3

Please sign in to comment.