Skip to content
Closed
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
14 changes: 14 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@
<pattern>org.bstats</pattern>
<shadedPattern>dev.espi.protectionstones</shadedPattern>
</relocation>
<relocation>
<pattern>com.github.Anon8281.universalScheduler</pattern>
<shadedPattern>dev.espi.protectionstones.universalScheduler</shadedPattern>
</relocation>
</relocations>
</configuration>
<executions>
Expand Down Expand Up @@ -195,6 +199,10 @@
<id>papermc</id>
<url>https://papermc.io/repo/repository/maven-public/</url>
</repository>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>

<dependencies>
Expand All @@ -204,6 +212,12 @@
<version>3.0.2</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.github.Anon8281</groupId>
<artifactId>UniversalScheduler</artifactId>
<version>0.1.7</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
Expand Down
18 changes: 16 additions & 2 deletions src/main/java/dev/espi/protectionstones/BlockHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@
import com.sk89q.worldguard.protection.flags.Flags;
import com.sk89q.worldguard.protection.flags.StateFlag;
import com.sk89q.worldguard.protection.managers.RegionManager;
import com.sk89q.worldguard.protection.managers.storage.StorageException;
import com.sk89q.worldguard.protection.regions.ProtectedCuboidRegion;
import com.sk89q.worldguard.protection.regions.ProtectedRegion;
import dev.espi.protectionstones.commands.ArgMerge;
import dev.espi.protectionstones.event.PSCreateEvent;
import dev.espi.protectionstones.utils.BackupUtil;
import dev.espi.protectionstones.utils.LimitUtil;
import dev.espi.protectionstones.utils.MiscUtil;
import dev.espi.protectionstones.utils.WGMerge;
Expand Down Expand Up @@ -275,7 +277,7 @@ public static boolean createActualRegion(Player p, Location l, PSProtectBlock bl
if (blockOptions.autoHide) {
PSL.msg(p, PSL.REGION_HIDDEN.msg());
// run on next tick so placing tile entities don't complain
Bukkit.getScheduler().runTask(ProtectionStones.getInstance(), () -> l.getBlock().setType(Material.AIR));
ProtectionStones.getScheduler().runTask(l, () -> l.getBlock().setType(Material.AIR));
}

if (blockOptions.startWithTaxAutopay) {
Expand All @@ -289,6 +291,18 @@ public static boolean createActualRegion(Player p, Location l, PSProtectBlock bl
if (r != null) playerMergeTask(p, r);
}

// async backup: flush WG to disk then copy regions file — no main thread I/O
final RegionManager backupRm = rm;
final World backupWorld = l.getWorld();
ProtectionStones.getScheduler().runTaskAsynchronously(() -> {
try {
backupRm.saveChanges();
} catch (StorageException e) {
e.printStackTrace();
}
BackupUtil.backupWorldRegions(backupWorld);
});

return true;
}

Expand All @@ -312,7 +326,7 @@ private static void playerMergeTask(Player p, PSRegion r) {
// actually do auto merge
if (!showGUI) {
PSRegion finalMergeTo = mergeTo;
Bukkit.getScheduler().runTaskAsynchronously(ProtectionStones.getInstance(), () -> {
ProtectionStones.getScheduler().runTaskAsynchronously(() -> {
try {
WGMerge.mergeRealRegions(p.getWorld(), r.getWGRegionManager(), finalMergeTo, Arrays.asList(finalMergeTo, r));
PSL.msg(p, PSL.MERGE_AUTO_MERGED.msg().replace("%region%", finalMergeTo.getId()));
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/dev/espi/protectionstones/ListenerClass.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public void onPlayerJoin(PlayerJoinEvent e) {
UUIDCache.storeUUIDNamePair(p.getUniqueId(), p.getName());

// allow worldguard to resolve all UUIDs to names
Bukkit.getScheduler().runTaskAsynchronously(ProtectionStones.getInstance(), () -> UUIDCache.storeWGProfile(p.getUniqueId(), p.getName()));
ProtectionStones.getScheduler().runTaskAsynchronously(() -> UUIDCache.storeWGProfile(p.getUniqueId(), p.getName()));

// add recipes to player's recipe book
p.discoverRecipes(RecipeUtil.getRecipeKeys());
Expand All @@ -81,7 +81,7 @@ public void onPlayerJoin(PlayerJoinEvent e) {

// tax join message
if (ProtectionStones.getInstance().getConfigOptions().taxEnabled && ProtectionStones.getInstance().getConfigOptions().taxMessageOnJoin) {
Bukkit.getScheduler().runTaskAsynchronously(ProtectionStones.getInstance(), () -> {
ProtectionStones.getScheduler().runTaskAsynchronously(() -> {
int amount = 0;
for (PSRegion psr : psp.getTaxEligibleRegions()) {
for (PSRegion.TaxPayment tp : psr.getTaxPaymentsDue()) {
Expand Down Expand Up @@ -576,7 +576,7 @@ public void onPSCreate(PSCreateEvent event) {
if (!event.getRegion().getTypeOptions().eventsEnabled) return;

// run on next tick (after the region is created to allow for edits to the region)
Bukkit.getServer().getScheduler().runTask(ProtectionStones.getInstance(), () -> {
ProtectionStones.getScheduler().runTask(event.getRegion().getProtectBlock().getLocation(), () -> {
// run custom commands (in config)
for (String action : event.getRegion().getTypeOptions().regionCreateCommands) {
execEvent(action, event.getPlayer(), event.getPlayer().getName(), event.getRegion());
Expand Down
25 changes: 13 additions & 12 deletions src/main/java/dev/espi/protectionstones/PSEconomy.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

package dev.espi.protectionstones;

import com.github.Anon8281.universalScheduler.scheduling.tasks.MyScheduledTask;
import com.sk89q.worldguard.protection.managers.RegionManager;
import com.sk89q.worldguard.protection.managers.storage.StorageException;
import com.sk89q.worldguard.protection.regions.ProtectedRegion;
Expand All @@ -38,7 +39,7 @@

public class PSEconomy {
private List<PSRegion> rentedList = new CopyOnWriteArrayList<>();
private static int rentRunner = -1, taxRunner = -1;
private static MyScheduledTask rentRunner, taxRunner;

public PSEconomy() {
if (!ProtectionStones.getInstance().isVaultSupportEnabled()) {
Expand All @@ -49,11 +50,11 @@ public PSEconomy() {
loadRentList();

// start rent
rentRunner = Bukkit.getScheduler().runTaskTimerAsynchronously(ProtectionStones.getInstance(), this::updateRents, 0, 200).getTaskId();
rentRunner = ProtectionStones.getScheduler().runTaskTimerAsynchronously(this::updateRents, 0, 200);

// start taxes
if (ProtectionStones.getInstance().getConfigOptions().taxEnabled)
taxRunner = Bukkit.getScheduler().runTaskTimerAsynchronously(ProtectionStones.getInstance(), this::updateTaxes, 0, 200).getTaskId();
taxRunner = ProtectionStones.getScheduler().runTaskTimerAsynchronously(this::updateTaxes, 0, 200);
}

private synchronized void updateRents() {
Expand Down Expand Up @@ -89,13 +90,13 @@ private void updateTaxes() {
* Stops the economy cycle. Used for reloads when creating a new PSEconomy.
*/
public void stop() {
if (rentRunner != -1) {
Bukkit.getScheduler().cancelTask(rentRunner);
rentRunner = -1;
if (rentRunner != null) {
rentRunner.cancel();
rentRunner = null;
}
if (taxRunner != -1) {
Bukkit.getScheduler().cancelTask(taxRunner);
taxRunner = -1;
if (taxRunner != null) {
taxRunner.cancel();
taxRunner = null;
}
}

Expand Down Expand Up @@ -126,7 +127,7 @@ public void loadRentList() {
public static void processTaxes(PSRegion r) {
// if taxes are enabled for this regions
if (r.getTypeOptions() != null && r.getTypeOptions().taxPeriod != -1) {
Bukkit.getScheduler().runTask(ProtectionStones.getInstance(), () -> {
ProtectionStones.getScheduler().runTask(r.getProtectBlock().getLocation(), () -> {
// update tax payments due
r.updateTaxPayments();

Expand Down Expand Up @@ -191,7 +192,7 @@ public static void doRentPayment(PSRegion r) {
}

// update money must be run in main thread
Bukkit.getScheduler().runTask(ProtectionStones.getInstance(), () -> tenant.pay(landlord, r.getPrice()));
ProtectionStones.getScheduler().runTask(() -> tenant.pay(landlord, r.getPrice()));
r.setRentLastPaid(Instant.now().getEpochSecond());
try { // must save region to persist last paid
r.getWGRegionManager().saveChanges();
Expand All @@ -208,4 +209,4 @@ public static void doRentPayment(PSRegion r) {
public List<PSRegion> getRentedList() {
return rentedList;
}
}
}
17 changes: 15 additions & 2 deletions src/main/java/dev/espi/protectionstones/ProtectionStones.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import com.electronwill.nightconfig.core.Config;
import com.electronwill.nightconfig.core.file.CommentedFileConfig;
import com.electronwill.nightconfig.toml.TomlFormat;
import com.github.Anon8281.universalScheduler.UniversalScheduler;
import com.github.Anon8281.universalScheduler.scheduling.schedulers.TaskScheduler;
import com.sk89q.worldguard.protection.managers.RegionManager;
import com.sk89q.worldguard.protection.regions.ProtectedRegion;
import dev.espi.protectionstones.commands.ArgHelp;
Expand Down Expand Up @@ -69,6 +71,7 @@ public class ProtectionStones extends JavaPlugin {

private static List<PSCommandArg> commandArgs = new ArrayList<>();
private static ProtectionStones plugin;
private static TaskScheduler scheduler;

private PSEconomy economy;

Expand Down Expand Up @@ -209,6 +212,10 @@ public static ProtectionStones getInstance() {
return plugin;
}

public static TaskScheduler getScheduler() {
return scheduler;
}

/**
* @return the plugin's logger
*/
Expand Down Expand Up @@ -555,6 +562,7 @@ public void onEnable() {
Config.setInsertionOrderPreserved(true); // make sure that config upgrades aren't a complete mess

plugin = this;
scheduler = UniversalScheduler.getScheduler(this);
configLocation = new File(this.getDataFolder() + "/config.toml");
blockDataFolder = new File(this.getDataFolder() + "/blocks");

Expand Down Expand Up @@ -637,7 +645,7 @@ public void onEnable() {
// uuid cache
getLogger().info("Building UUID cache... (if slow change async-load-uuid-cache in the config to true)");
if (configOptions.asyncLoadUUIDCache) { // async load
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> {
getScheduler().runTaskAsynchronously(() -> {
for (OfflinePlayer op : Bukkit.getOfflinePlayers()) {
UUIDCache.storeUUIDNamePair(op.getUniqueId(), op.getName());
}
Expand All @@ -661,4 +669,9 @@ public void onEnable() {
getLogger().info(ChatColor.WHITE + "ProtectionStones has successfully started!");
}

}
@Override
public void onDisable() {
if (scheduler != null) scheduler.cancelTasks();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public boolean executeArgument(CommandSender s, String[] args, HashMap<String, S
String addPlayerName = UUIDCache.getNameFromUUID(addPlayerUuid);

// getting player regions is slow, so run it async
Bukkit.getServer().getScheduler().runTaskAsynchronously(ProtectionStones.getInstance(), () -> {
ProtectionStones.getScheduler().runTaskAsynchronously(() -> {
List<PSRegion> regions;

// obtain region list that player is being added to or removed from
Expand Down Expand Up @@ -129,7 +129,7 @@ public boolean executeArgument(CommandSender s, String[] args, HashMap<String, S
}

// add to WorldGuard profile cache
Bukkit.getScheduler().runTaskAsynchronously(ProtectionStones.getInstance(), () -> UUIDCache.storeWGProfile(addPlayerUuid, addPlayerName));
ProtectionStones.getScheduler().runTaskAsynchronously(() -> UUIDCache.storeWGProfile(addPlayerUuid, addPlayerName));

} else if ((operationType.equals("remove") && r.isMember(addPlayerUuid))
|| (operationType.equals("removeowner") && r.isOwner(addPlayerUuid))) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ static boolean argumentAdminCleanup(CommandSender p, String[] preParseArgs) {

// async cleanup task
String finalAlias = alias;
Bukkit.getScheduler().runTaskAsynchronously(ProtectionStones.getInstance(), () -> {
ProtectionStones.getScheduler().runTaskAsynchronously(() -> {
int days = (args.size() > 0) ? Integer.parseInt(args.get(0)) : 30; // 30 days is default if days aren't specified

PSL.msg(p, PSL.ADMIN_CLEANUP_HEADER.msg()
Expand Down Expand Up @@ -146,8 +146,9 @@ static boolean argumentAdminCleanup(CommandSender p, String[] preParseArgs) {

static private void regionLoop(Iterator<PSRegion> deleteRegionsIterator, CommandSender p, boolean isRemoveOperation) {
if (deleteRegionsIterator.hasNext()) {
Bukkit.getScheduler().runTaskLater(ProtectionStones.getInstance(), () ->
processRegion(deleteRegionsIterator, p, isRemoveOperation), 1);
PSRegion region = deleteRegionsIterator.next();
ProtectionStones.getScheduler().runTaskLater(region.getProtectBlock().getLocation(), () ->
processRegion(region, deleteRegionsIterator, p, isRemoveOperation), 1);
} else { // finished region iteration
PSL.msg(p, PSL.ADMIN_CLEANUP_FOOTER.msg()
.replace("%arg%", isRemoveOperation ? "remove" : "preview"));
Expand All @@ -168,8 +169,7 @@ static private void regionLoop(Iterator<PSRegion> deleteRegionsIterator, Command
// Process a region, and then iterate to the next region on the next tick.
// This is to prevent the server from pausing for the entire duration of the cleanup.
// (lag from loading chunks to remove protection blocks)
static private void processRegion(Iterator<PSRegion> deleteRegionsIterator, CommandSender p, boolean isRemoveOperation) {
PSRegion r = deleteRegionsIterator.next();
static private void processRegion(PSRegion r, Iterator<PSRegion> deleteRegionsIterator, CommandSender p, boolean isRemoveOperation) {

if (isRemoveOperation) { // delete

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,15 @@ static boolean argumentAdminHide(CommandSender p, String[] args) {
mgr = WGUtils.getRegionManagerWithWorld(w);
}

Bukkit.getScheduler().runTaskAsynchronously(ProtectionStones.getInstance(), () -> {
ProtectionStones.getScheduler().runTaskAsynchronously(() -> {
// loop through regions that are protection stones and hide or unhide the block
for (ProtectedRegion r : mgr.getRegions().values()) {
if (ProtectionStones.isPSRegion(r)) {
PSRegion region = PSRegion.fromWGRegion(w, r);
if (args[1].equalsIgnoreCase("hide")) {
Bukkit.getScheduler().runTask(ProtectionStones.getInstance(), region::hide);
ProtectionStones.getScheduler().runTask(region.getProtectBlock().getLocation(), region::hide);
} else if (args[1].equalsIgnoreCase("unhide")){
Bukkit.getScheduler().runTask(ProtectionStones.getInstance(), region::unhide);
ProtectionStones.getScheduler().runTask(region.getProtectBlock().getLocation(), region::unhide);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import dev.espi.protectionstones.PSRegion;
import dev.espi.protectionstones.ProtectionStones;
import dev.espi.protectionstones.utils.WGUtils;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;

Expand All @@ -34,7 +33,7 @@ static boolean argumentAdminSetTaxAutopayers(CommandSender s, String[] args) {

PSL.msg(s, ChatColor.GRAY + "Scanning through regions, and setting tax autopayers for regions that don't have one...");

Bukkit.getScheduler().runTaskAsynchronously(ProtectionStones.getInstance(), () -> {
ProtectionStones.getScheduler().runTaskAsynchronously(() -> {
WGUtils.getAllRegionManagers().forEach((w, rgm) -> {
for (ProtectedRegion r : rgm.getRegions().values()) {
PSRegion psr = PSRegion.fromWGRegion(w, r);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import dev.espi.protectionstones.PSPlayer;
import dev.espi.protectionstones.ProtectionStones;
import dev.espi.protectionstones.utils.UUIDCache;
import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
Expand Down Expand Up @@ -68,7 +67,7 @@ public HashMap<String, Boolean> getRegisteredFlags() {
@Override
public boolean executeArgument(CommandSender s, String[] args, HashMap<String, String> flags) {
Player p = (Player) s;
Bukkit.getScheduler().runTaskAsynchronously(ProtectionStones.getInstance(), () -> {
ProtectionStones.getScheduler().runTaskAsynchronously(() -> {
int[] count;

if (args.length == 1) {
Expand Down
Loading
Loading