Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ group 'sh.okx'
version '3.15.2'

java {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
sourceCompatibility = JavaVersion.VERSION_21
targetCompatibility = JavaVersion.VERSION_21
}

repositories {
mavenCentral()
maven {
url 'https://hub.spigotmc.org/nexus/content/repositories/snapshots/'
url 'https://repo.papermc.io/repository/maven-public/'
}
maven {
url 'https://repo.extendedclip.com/content/repositories/placeholderapi/'
Expand Down Expand Up @@ -51,7 +51,7 @@ dependencies {
implementation group: 'org.slf4j', name: 'slf4j-api', version: '1.7.30'

compileOnly 'org.jetbrains:annotations:22.0.0'
compileOnly 'org.spigotmc:spigot-api:1.20.1-R0.1-SNAPSHOT'
compileOnly 'io.papermc.paper:paper-api:1.21.4-R0.1-SNAPSHOT'
compileOnly('com.github.Realizedd:TokenManager:3.2.4') {
transitive = false
}
Expand Down
33 changes: 32 additions & 1 deletion src/main/java/sh/okx/rankup/AutoRankup.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,17 @@
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.scheduler.BukkitTask;
import sh.okx.rankup.util.folia.FoliaScheduler;
import sh.okx.rankup.util.folia.TaskWrapper;

@RequiredArgsConstructor
public class AutoRankup extends BukkitRunnable {
public class AutoRankup implements Runnable {
private final RankupPlugin rankup;

private TaskWrapper task;
private BukkitTask bukkitTask;

@Override
public void run() {
if (rankup.error()) {
Expand All @@ -26,4 +32,29 @@ public void run() {
}
}
}

public void runTaskTimer(RankupPlugin rankupPlugin, long delay, long period) {
if (FoliaScheduler.isFolia()) {
task = FoliaScheduler.getAsyncScheduler().runAtFixedRate(rankupPlugin, $ -> this.run(), delay, period);
} else {
bukkitTask = new BukkitRunnable() {
@Override
public void run() {
AutoRankup.this.run();
}
}.runTaskTimer(rankupPlugin, delay, period);
}
}

public boolean isCancelled() {
return task == null ? bukkitTask.isCancelled() : task.isCancelled();
}

public void cancel() {
if (task == null) {
bukkitTask.cancel();
} else {
task.cancel();
}
}
}
55 changes: 30 additions & 25 deletions src/main/java/sh/okx/rankup/Metrics.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import lombok.Getter;
import org.bukkit.Bukkit;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.plugin.ServicePriority;
import sh.okx.rankup.util.folia.FoliaScheduler;

import javax.net.ssl.HttpsURLConnection;
import java.io.*;
Expand Down Expand Up @@ -50,7 +52,8 @@ public class Metrics {
private static final String URL = "https://bStats.org/submitData/bukkit";

// Is bStats enabled on this server?
private boolean enabled;
@Getter
private final boolean enabled;

// Should failed requests be logged?
private static boolean logFailedRequests;
Expand Down Expand Up @@ -138,15 +141,6 @@ public Metrics(Plugin plugin) {
}
}

/**
* Checks if bStats is enabled.
*
* @return Whether bStats is enabled or not.
*/
public boolean isEnabled() {
return enabled;
}

/**
* Adds a custom chart.
*
Expand All @@ -163,19 +157,30 @@ public void addCustomChart(CustomChart chart) {
* Starts the Scheduler which submits our data every 30 minutes.
*/
private void startSubmitting() {
final Timer timer = new Timer(true); // We use a timer cause the Bukkit scheduler is affected by server lags
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
if (!plugin.isEnabled()) { // Plugin was disabled
timer.cancel();
return;
if (FoliaScheduler.isFolia()) {
FoliaScheduler.getGlobalRegionScheduler().runAtFixedRate(plugin,
(ignored) -> {
if (!plugin.isEnabled()) { // Plugin was disabled
return;
}
submitData();
}, 20L * 60 * 5, 20L * 60 * 30
);
} else {
final Timer timer = new Timer(true); // We use a timer cause the Bukkit scheduler is affected by server lags
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
if (!plugin.isEnabled()) { // Plugin was disabled
timer.cancel();
return;
}
// Nevertheless we want our code to run in the Bukkit main thread, so we have to use the Bukkit scheduler
// Don't be afraid! The connection to the bStats server is still async, only the stats collection is sync ;)
Bukkit.getScheduler().runTask(plugin, () -> submitData());
}
// Nevertheless we want our code to run in the Bukkit main thread, so we have to use the Bukkit scheduler
// Don't be afraid! The connection to the bStats server is still async, only the stats collection is sync ;)
Bukkit.getScheduler().runTask(plugin, () -> submitData());
}
}, 1000 * 60 * 5, 1000 * 60 * 30);
}, 1000 * 60 * 5, 1000 * 60 * 30);
}
// Submit the data every 30 minutes, first time after 5 minutes to give other plugins enough time to start
// WARNING: Changing the frequency has no effect but your plugin WILL be blocked/deleted!
// WARNING: Just don't do it!
Expand Down Expand Up @@ -257,7 +262,7 @@ private JsonObject getServerData() {
}

/**
* Collects the data and sends it afterwards.
* Collects the data and sends it afterward.
*/
private void submitData() {
final JsonObject data = getServerData();
Expand All @@ -280,7 +285,7 @@ private void submitData() {
Method jsonStringGetter = jsonObjectJsonSimple.getDeclaredMethod("toJSONString");
jsonStringGetter.setAccessible(true);
String jsonString = (String) jsonStringGetter.invoke(plugin);
JsonObject object = new JsonParser().parse(jsonString).getAsJsonObject();
JsonObject object = JsonParser.parseString(jsonString).getAsJsonObject();
pluginData.add(object);
}
} catch (ClassNotFoundException e) {
Expand Down Expand Up @@ -330,7 +335,7 @@ private static void sendData(Plugin plugin, JsonObject data) throws Exception {
throw new IllegalAccessException("This method must not be called from the main thread!");
}
if (logSentData) {
plugin.getLogger().info("Sending data to bStats: " + data.toString());
plugin.getLogger().info("Sending data to bStats: " + data);
}
HttpsURLConnection connection = (HttpsURLConnection) new URL(URL).openConnection();

Expand Down
25 changes: 14 additions & 11 deletions src/main/java/sh/okx/rankup/RankupPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
import sh.okx.rankup.serialization.YamlDeserializer;
import sh.okx.rankup.util.UpdateNotifier;
import sh.okx.rankup.util.VersionChecker;
import sh.okx.rankup.util.folia.FoliaScheduler;

import java.io.File;
import java.io.FileNotFoundException;
Expand Down Expand Up @@ -280,15 +281,18 @@ private void addAllRequirements(Map<String, Integer> map, RankList<? extends Ran
}

/**
* Closes all rankup inventories on disable so players cannot grab items from the inventory on a
* Closes all rankup inventories on "disable" so players cannot grab items from the inventory on a
* plugin reload.
*/
private void closeInventories() {
for (Player player : Bukkit.getOnlinePlayers()) {
InventoryView view = player.getOpenInventory();
if (view.getType() == InventoryType.CHEST
&& view.getTopInventory().getHolder() instanceof Gui) {
player.closeInventory();
try {
InventoryView view = player.getOpenInventory();
if (view.getType() == InventoryType.CHEST
&& view.getTopInventory().getHolder() instanceof Gui) {
player.closeInventory();
}
} catch (Throwable ignored) {
}
}
}
Expand All @@ -301,10 +305,13 @@ private void loadConfigs(boolean init) {
messages = YamlConfiguration.loadConfiguration(localeFile);

if (init) {
Bukkit.getScheduler().runTask(this, () -> {
final Runnable runnable = () -> {
refreshRanks();
error();
});
};

if (FoliaScheduler.isFolia()) FoliaScheduler.getGlobalRegionScheduler().execute(this, runnable);
else Bukkit.getScheduler().runTask(this, runnable);
} else {
refreshRanks();
}
Expand All @@ -317,16 +324,12 @@ public void refreshRanks() {

if (config.getBoolean("prestige")) {
prestiges = new Prestiges(this, loadConfig("prestiges.yml"));
// prestiges.getOrderedList();
} else {
prestiges = null;
}

rankups = new Rankups(this, loadRankupConfig("rankups"));
// check rankups are not in an infinite loop
// rankups.getOrderedList();


} catch (RuntimeException e) {
this.errorMessage = e.getClass().getName() + ": " + e.getMessage();
e.printStackTrace();
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/sh/okx/rankup/commands/InfoCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -319,18 +319,22 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
sender.sendMessage(
ChatColor.GREEN + "" + ChatColor.BOLD + description.getName() + " " + version +
ChatColor.YELLOW + " by " + ChatColor.BLUE + ChatColor.BOLD + String.join(", ", description.getAuthors()));

if (sender.hasPermission("rankup.reload")) {
sender.sendMessage(ChatColor.GREEN + "/" + label + " reload " + ChatColor.YELLOW + "Reloads configuration files.");
}

if (sender.hasPermission("rankup.force")) {
sender.sendMessage(ChatColor.GREEN + "/" + label + " forcerankup <player> " + ChatColor.YELLOW + "Force a player to rankup, bypassing requirements.");

if (plugin.getPrestiges() != null) {
sender.sendMessage(
ChatColor.GREEN + "/" + label + " forceprestige <player> " + ChatColor.YELLOW
+ "Force a player to prestige, bypassing requirements.");
}
sender.sendMessage(ChatColor.GREEN + "/" + label + " rankdown <player> " + ChatColor.YELLOW + "Force a player to move down one rank.");
}

if (sender.hasPermission("rankup.playtime")) {
sender.sendMessage(ChatColor.GREEN + "/" + label + " playtime " + ChatColor.YELLOW + "View your playtime");
}
Expand Down
10 changes: 7 additions & 3 deletions src/main/java/sh/okx/rankup/gui/GuiListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.Inventory;
import sh.okx.rankup.RankupPlugin;
import sh.okx.rankup.util.folia.FoliaScheduler;

@RequiredArgsConstructor
public class GuiListener implements Listener {
Expand All @@ -27,19 +28,22 @@ public void on(InventoryClickEvent e) {
Gui gui = (Gui) inventory.getHolder();

if (gui.getRankup().isSimilar(e.getCurrentItem())) {
Bukkit.getScheduler().runTask(plugin, player::closeInventory);
if (FoliaScheduler.isFolia()) FoliaScheduler.getEntityScheduler().run(player, plugin, $ -> player.closeInventory(), null);
else Bukkit.getScheduler().runTask(plugin, () -> player.closeInventory());
if (gui.isPrestige()) {
plugin.getHelper().prestige(player);
} else {
plugin.getHelper().rankup(player);
}
} else if (gui.getCancel().isSimilar(e.getCurrentItem())) {
Bukkit.getScheduler().runTask(plugin, () -> {
final Runnable runnable = () -> {
player.closeInventory();
if (gui.isReturnToRanksGui()) {
Bukkit.dispatchCommand(player, "ranks");
}
});
};
if (FoliaScheduler.isFolia()) FoliaScheduler.getEntityScheduler().run(player, plugin, $ -> runnable.run(), null);
else Bukkit.getScheduler().runTask(plugin, runnable);
}
}
}
14 changes: 10 additions & 4 deletions src/main/java/sh/okx/rankup/ranks/Rank.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import sh.okx.rankup.RankupPlugin;
import sh.okx.rankup.ranks.requirements.RankRequirements;
import sh.okx.rankup.requirements.Requirement;
import sh.okx.rankup.util.folia.FoliaScheduler;

@EqualsAndHashCode
@RequiredArgsConstructor(access = AccessLevel.PROTECTED)
Expand Down Expand Up @@ -46,10 +47,15 @@ public void applyRequirements(Player player) {
}

public void runCommands(Player player, Rank next) {
for (String command : commands) {
String string = plugin.newMessageBuilder(command).replacePlayer(player).replaceOldRank(this).replaceRank(next).toString(player);
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), string);
}
final Runnable runnable = () -> {
for (String command : commands) {
String string = plugin.newMessageBuilder(command).replacePlayer(player).replaceOldRank(this).replaceRank(next).toString(player);
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), string);
}
};

if (FoliaScheduler.isFolia()) FoliaScheduler.getGlobalRegionScheduler().run(plugin, $ -> runnable.run());
else runnable.run();
}

@Override
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/sh/okx/rankup/ranksgui/RanksGui.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import sh.okx.rankup.ranks.Rank;
import sh.okx.rankup.ranks.RankElement;
import sh.okx.rankup.util.Colour;
import sh.okx.rankup.util.folia.FoliaScheduler;

public class RanksGui {
private final RankupPlugin plugin;
Expand Down Expand Up @@ -111,10 +112,12 @@ public void click(InventoryClickEvent event) {
}
int slot = event.getRawSlot();
if (slot == rankupSlot) {
Bukkit.getScheduler().runTask(plugin, () -> {
final Runnable runnable = () -> {
player.closeInventory();
Bukkit.dispatchCommand(player, "rankup gui");
});
};
if (FoliaScheduler.isFolia()) FoliaScheduler.getEntityScheduler().run(player, plugin, $ -> runnable.run(), null);
else Bukkit.getScheduler().runTask(plugin, runnable);
}
}

Expand Down
Loading