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
57 changes: 37 additions & 20 deletions src/oneblock/CommandHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import oneblock.gui.GUI;
import oneblock.invitation.Guest;
import oneblock.invitation.Invitation;
import oneblock.utils.Scheduler;
import oneblock.utils.Utils;
import oneblock.worldguard.OBWorldGuard;

Expand Down Expand Up @@ -76,15 +77,19 @@ public boolean onCommand(CommandSender sender, Command cmd, String label, String
if (player == null) return false;
UUID uuid = player.getUniqueId();
int X_pl = 0, Z_pl = 0;
Runnable newIsland = null;
int plID = PlayerInfo.getId(uuid);
if (plID == -1) {
PlayerInfo inf = new PlayerInfo(uuid);
plID = PlayerInfo.getFreeId(useEmptyIslands);
int result[] = plugin.getIslandCoordinates(plID);
X_pl = result[0]; Z_pl = result[1];
if (plID != PlayerInfo.size())
Island.clear(getWorld(), X_pl, getY(), Z_pl, getOffset()/4);
Island.place(getWorld(), X_pl, getY(), Z_pl);
final boolean reused = plID != PlayerInfo.size();
final int X_isl = X_pl, Z_isl = Z_pl;
newIsland = () -> {
if (reused) Island.clear(getWorld(), X_isl, getY(), Z_isl, getOffset()/4);
Island.place(getWorld(), X_isl, getY(), Z_isl);
};
plugin.worldGuard.createRegion(uuid, X_pl, Z_pl, getOffset(), plID);
PlayerInfo.set(plID, inf);
if (!superlegacy)
Expand All @@ -96,7 +101,13 @@ public boolean onCommand(CommandSender sender, Command cmd, String label, String
}
if (!plugin.enabled) plugin.runMainTask();
if (progress_bar) PlayerInfo.get(plID).bar.setVisible(true);
player.teleport(new Location(getWorld(), X_pl + 0.5, getY() + 1.2013, Z_pl + 0.5));
final Location spawn = new Location(getWorld(), X_pl + 0.5, getY() + 1.2013, Z_pl + 0.5);
if (newIsland == null) Scheduler.teleport(player, spawn);
else {
// Build the island on its own region thread (Folia), then bring the player over.
final Runnable build = newIsland;
Scheduler.runAt(spawn, () -> { build.run(); Scheduler.teleport(player, spawn); });
}
if (OBWorldGuard.isEnabled()) plugin.worldGuard.addMember(uuid, plID);
return true;
}
Expand All @@ -108,7 +119,7 @@ public boolean onCommand(CommandSender sender, Command cmd, String label, String
sender.sendMessage(Messages.leave_not_set);
return true;
}
player.teleport(plugin.getLeave());
Scheduler.teleport(player, plugin.getLeave());
return true;
}
case ("v"):
Expand Down Expand Up @@ -141,7 +152,7 @@ public boolean onCommand(CommandSender sender, Command cmd, String label, String
final int X_pl = result[0], Z_pl = result[1];

if (protection) Guest.list.add(new Guest(uuid, player.getUniqueId()));
player.teleport(new Location(getWorld(), X_pl + 0.5, getY() + 1.2013, Z_pl + 0.5));
Scheduler.teleport(player, new Location(getWorld(), X_pl + 0.5, getY() + 1.2013, Z_pl + 0.5));
PlayerInfo.removeBarFor(player);
return true;
}
Expand Down Expand Up @@ -212,13 +223,15 @@ public boolean onCommand(CommandSender sender, Command cmd, String label, String
}
if (!(member instanceof Player)) return true;
Player member_ex = (Player) member;
int memberID = plugin.findNearestRegionId(member_ex.getLocation());
if (memberID == ownerID) {
if (!member_ex.hasPermission("oneblock.set"))
member_ex.performCommand("ob j");
info.removeBar(member_ex);
sender.sendMessage(member.getName() + Messages.kicked);
}
Scheduler.runFor(member_ex, () -> {
int memberID = plugin.findNearestRegionId(member_ex.getLocation());
if (memberID == ownerID) {
if (!member_ex.hasPermission("oneblock.set"))
member_ex.performCommand("ob j");
info.removeBar(member_ex);
sender.sendMessage(member_ex.getName() + Messages.kicked);
}
});
return true;
}
case ("accept"):{
Expand Down Expand Up @@ -266,7 +279,7 @@ public boolean onCommand(CommandSender sender, Command cmd, String label, String
if (requirePermission(sender, "oneblock.set"))
{
config = YamlConfiguration.loadConfiguration(LegacyConfigSaver.file); // Loading the config.yml file before making changes.
Bukkit.getScheduler().runTaskLater(plugin, () -> { LegacyConfigSaver.Save(config); }, 2L); // Saving the config.yml file after making changes.
Scheduler.runLater(() -> { LegacyConfigSaver.Save(config); }, 2L); // Saving the config.yml file after making changes.
switch (parametr) {
case ("set"): {
if (player == null && args.length < 6) {
Expand Down Expand Up @@ -312,7 +325,8 @@ public boolean onCommand(CommandSender sender, Command cmd, String label, String

if (!plugin.enabled) plugin.runMainTask();

getWorld().getBlockAt(getX(), getY(), getZ()).setType(GRASS_BLOCK.get());
Scheduler.runAt(new Location(getWorld(), getX(), getY(), getZ()),
() -> getWorld().getBlockAt(getX(), getY(), getZ()).setType(GRASS_BLOCK.get()));
plugin.worldGuard.recreateRegions();
LegacyConfigSaver.Save(config);

Expand Down Expand Up @@ -356,7 +370,7 @@ public boolean onCommand(CommandSender sender, Command cmd, String label, String
// ReloadBorders is scheduled 2 ticks later so the fall-through
// below can first persist `border=true/false` via the shared
// bool-toggle body; the ordering is load-bearing.
Bukkit.getScheduler().runTaskLater(plugin, () -> { plugin.ReloadBorders(); }, 2L);
Scheduler.runLater(() -> { plugin.ReloadBorders(); }, 2L);
// fall through to shared bool-toggle body:
case ("circlemode"):
case ("useemptyislands"):
Expand Down Expand Up @@ -435,7 +449,8 @@ public boolean onCommand(CommandSender sender, Command cmd, String label, String
if (progress_bar)
inf.bar.setVisible(false);
int result[] = plugin.getIslandCoordinates(id);
Island.clear(getWorld(), result[0], getY(), result[1], getOffset()/4);
Scheduler.runAt(new Location(getWorld(), result[0], getY(), result[1]),
() -> Island.clear(getWorld(), result[0], getY(), result[1], getOffset()/4));
sender.sendMessage(String.format("%splayer %s island is destroyed! :D", ChatColor.GREEN, args[1]));
return true;
}
Expand Down Expand Up @@ -590,9 +605,11 @@ public boolean onCommand(CommandSender sender, Command cmd, String label, String
UUID uuid = p.getUniqueId();
if (PlayerInfo.getId(uuid) != -1) {
int result[] = plugin.getIslandCoordinates(PlayerInfo.getId(uuid));
Island.scan(getWorld(), result[0], getY(), result[1]);
sender.sendMessage(ChatColor.GREEN + "A copy of your island has been successfully saved!");
config.set("custom_island", Island.map());
Scheduler.runAt(new Location(getWorld(), result[0], getY(), result[1]), () -> {
Island.scan(getWorld(), result[0], getY(), result[1]);
sender.sendMessage(ChatColor.GREEN + "A copy of your island has been successfully saved!");
config.set("custom_island", Island.map());
});
} else
sender.sendMessage(ChatColor.RED + "You don't have an island!");
return true;
Expand Down
19 changes: 10 additions & 9 deletions src/oneblock/OneBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ public static String getBarTitle(Player p, int lvl) {
@Override
public void onEnable() {
plugin = this;
Scheduler.init(this);
GUI.legacy = !Utils.findMethod(SkullMeta.class, "setOwningPlayer");
final Metrics metrics = new Metrics(this, 14477);
final PluginManager pluginManager = Bukkit.getPluginManager();
Expand Down Expand Up @@ -181,7 +182,7 @@ public void onEnable() {

if (getOffset() == 0) return;

Bukkit.getScheduler().runTaskTimerAsynchronously(this, new WorldInitTask(this), 32, 80);
Scheduler.runTimerAsync(new WorldInitTask(this), 32, 80);
}

private Place.Type determinePlaceType(PluginManager pluginManager) {
Expand All @@ -207,12 +208,12 @@ private void setupMetrics(Metrics metrics) {
}

public void runMainTask() {
Bukkit.getScheduler().cancelTasks(this);
Scheduler.cancelTasks();
if (getOffset() == 0) return;
Bukkit.getScheduler().runTaskTimerAsynchronously(this, new PlayerCacheRefreshTask(this), 0, 120);
Bukkit.getScheduler().runTaskTimerAsynchronously(this, new PlayerDataSaveTask(this), 200, 6000);
if (!superlegacy) Bukkit.getScheduler().runTaskTimerAsynchronously(this, new IslandParticleTask(this), 40, 40);
Bukkit.getScheduler().runTaskTimer(this, new IslandBlockGenTask(this), 40, 80);
Scheduler.runTimerAsync(new PlayerCacheRefreshTask(this), 0, 120);
Scheduler.runTimerAsync(new PlayerDataSaveTask(this), 200, 6000);
if (!superlegacy) Scheduler.runTimerAsync(new IslandParticleTask(this), 40, 40);
Scheduler.runTimer(new IslandBlockGenTask(this), 40, 80);
enabled = true;

if (OBWorldGuard.canUse && Bukkit.getPluginManager().isPluginEnabled("WorldGuard")) {
Expand Down Expand Up @@ -293,16 +294,16 @@ public void UpdateBorderLocation(Player pl, Location loc) {

public void UpdateBorder(final Player pl) {
WorldBorder border = pl.getWorldBorder();
Bukkit.getScheduler().runTaskLater(this,
Scheduler.runForLater(pl,
() -> { pl.setWorldBorder(border); }, 10L);
}

public void ReloadBorders() {
if (!isBorderSupported) return;
World w = getWor();
if (w == null) return;
if (border) w.getPlayers().forEach(pl -> plugin.UpdateBorderLocation(pl, pl.getLocation()));
else w.getPlayers().forEach(pl -> pl.setWorldBorder(null));
if (border) Scheduler.playersIn(w).forEach(pl -> Scheduler.runFor(pl, () -> plugin.UpdateBorderLocation(pl, pl.getLocation())));
else Scheduler.playersIn(w).forEach(pl -> Scheduler.runFor(pl, () -> pl.setWorldBorder(null)));
}

public boolean isWithinIslandBounds(Location loc, int centerX, int centerZ) {
Expand Down
6 changes: 4 additions & 2 deletions src/oneblock/RewardManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;

import oneblock.utils.Scheduler;
import oneblock.utils.Utils;

import java.io.File;
Expand Down Expand Up @@ -83,8 +84,9 @@ private void executeCommandList(Player player, List<String> commands, Map<String
finalCommand = finalCommand.replace(entry.getKey(), entry.getValue());
}

// Execute command as console
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), finalCommand);
// Execute command as console (Folia: on the global region thread)
final String dispatched = finalCommand;
Scheduler.run(() -> Bukkit.dispatchCommand(Bukkit.getConsoleSender(), dispatched));
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/oneblock/events/BlockEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.bukkit.util.Vector;

import oneblock.PlayerInfo;
import oneblock.utils.Scheduler;

public class BlockEvent implements Listener {
protected static final double DROP_TELEPORT_HEIGHT_OFFSET = 0.8;
Expand Down Expand Up @@ -56,6 +57,6 @@ public void BlockBreak(final BlockBreakEvent e) {
if (block.getX() != result[0]) return;
if (block.getZ() != result[1]) return;

Bukkit.getScheduler().runTaskLater(plugin, () -> {plugin.BlockGen(result[0], result[1], plID, ponl, block);}, 1L);
Scheduler.runAtLater(block.getLocation(), () -> {plugin.BlockGen(result[0], result[1], plID, ponl, block);}, 1L);
}
}
4 changes: 2 additions & 2 deletions src/oneblock/invitation/Invitation.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.bukkit.entity.Player;

import oneblock.CommandHandler;
import oneblock.utils.Scheduler;
import oneblock.OneBlock;
import oneblock.PlayerInfo;

Expand Down Expand Up @@ -35,8 +36,7 @@ public static void add(UUID name, UUID to) {
return;
Invitation inv_ = new Invitation(name, to);
Invitation.list.add(inv_);
Bukkit.getScheduler().runTaskLater(OneBlock.plugin,
() -> { Invitation.list.remove(inv_); }, 300L);
Scheduler.runLater(() -> { Invitation.list.remove(inv_); }, 300L);
}

public static Invitation check(UUID uuid) {
Expand Down
5 changes: 4 additions & 1 deletion src/oneblock/placement/Place.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import org.bukkit.Bukkit;
import org.bukkit.block.Block;

import oneblock.utils.Scheduler;

public abstract class Place {

public enum Type
Expand Down Expand Up @@ -49,7 +51,8 @@ public boolean executeCommand(Block block, String command, String playerName) {
if (!playerName.isEmpty()) //if (playerName != null)
dispatched = dispatched.replace("{player}", playerName);

Bukkit.dispatchCommand(Bukkit.getConsoleSender(), dispatched);
final String command_ = dispatched;
Scheduler.run(() -> Bukkit.dispatchCommand(Bukkit.getConsoleSender(), command_));
return true;
}
}
61 changes: 34 additions & 27 deletions src/oneblock/tasks/IslandBlockGenTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import oneblock.OneBlock;
import oneblock.PlayerInfo;
import oneblock.invitation.Guest;
import oneblock.utils.Scheduler;

/**
* Main-thread block-generation pulse: every four seconds (80 ticks) it
Expand All @@ -28,34 +29,40 @@ public final class IslandBlockGenTask implements Runnable {

@Override
public void run() { // SubBlockGen
for (Player player : plugin.cache.getPlayers()) {
final World wor = OneBlock.getWor();
if (player.getWorld() != wor) continue;
final UUID uuid = player.getUniqueId();
final int result[] = plugin.cache.getIslandCoordinates(player);
final int X_pl = result[0], Z_pl = result[1], plID = result[2];

if (OneBlock.protection && !player.hasPermission("Oneblock.ignoreBarrier")) {
boolean CheckGuest = false;
Location loc = player.getLocation();
PlayerInfo inf = Guest.getPlayerInfo(uuid);
if (inf != null) {
int crd[] = plugin.getIslandCoordinates(PlayerInfo.getId(inf.uuid));
CheckGuest = plugin.isWithinIslandBounds(loc, crd[0], crd[1]);
if (!CheckGuest) Guest.remove(uuid);
}
if (!plugin.isWithinIslandBounds(loc, X_pl, Z_pl) && !CheckGuest) {
player.performCommand("ob j");
player.sendMessage(Messages.protection);
continue;
}
for (Player player : plugin.cache.getPlayers())
Scheduler.runFor(player, () -> pulse(player));
}

private void pulse(Player player) {
final World wor = OneBlock.getWor();
if (player.getWorld() != wor) return;
final UUID uuid = player.getUniqueId();
final int result[] = plugin.cache.getIslandCoordinates(player);
final int X_pl = result[0], Z_pl = result[1], plID = result[2];

if (OneBlock.protection && !player.hasPermission("Oneblock.ignoreBarrier")) {
boolean CheckGuest = false;
Location loc = player.getLocation();
PlayerInfo inf = Guest.getPlayerInfo(uuid);
if (inf != null) {
int crd[] = plugin.getIslandCoordinates(PlayerInfo.getId(inf.uuid));
CheckGuest = plugin.isWithinIslandBounds(loc, crd[0], crd[1]);
if (!CheckGuest) Guest.remove(uuid);
}
if (!plugin.isWithinIslandBounds(loc, X_pl, Z_pl) && !CheckGuest) {
player.performCommand("ob j");
player.sendMessage(Messages.protection);
return;
}

final Block block = wor.getBlockAt(X_pl, OneBlock.getY(), Z_pl);
if (block.getType() != Material.AIR) continue;
if (PlayerInfo.getId(uuid) == -1) continue;

plugin.BlockGen(X_pl, Z_pl, plID, player, block);
}

// On Folia the island block may belong to another region than the player.
Scheduler.runAt(new Location(wor, X_pl, OneBlock.getY(), Z_pl), () -> {
final Block block = wor.getBlockAt(X_pl, OneBlock.getY(), Z_pl);
if (block.getType() != Material.AIR) return;
if (PlayerInfo.getId(uuid) == -1) return;

plugin.BlockGen(X_pl, Z_pl, plID, player, block);
});
}
}
12 changes: 7 additions & 5 deletions src/oneblock/tasks/IslandParticleTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.bukkit.entity.Player;

import oneblock.OneBlock;
import oneblock.utils.Scheduler;

public final class IslandParticleTask implements Runnable {
private static final double[][] PARTICLE_OFFSETS = {{0, 0}, {0, 1}, {1, 0}, {1, 1}};
Expand All @@ -25,12 +26,13 @@ public void run() {
if (result == null) continue;
int X_pl = result[0], Z_pl = result[1];
double baseY = OneBlock.getY() + 0.5;


for (double[] offset : PARTICLE_OFFSETS) {
Location loc = new Location(wor, X_pl + offset[0], baseY, Z_pl + offset[1]);
wor.spawnParticle(Particle.PORTAL, loc, PARTICLE_COUNT, 0, 0, 0, 0);
}
Scheduler.runAt(new Location(wor, X_pl, baseY, Z_pl), () -> {
for (double[] offset : PARTICLE_OFFSETS) {
Location loc = new Location(wor, X_pl + offset[0], baseY, Z_pl + offset[1]);
wor.spawnParticle(Particle.PORTAL, loc, PARTICLE_COUNT, 0, 0, 0, 0);
}
});
}
}
}
3 changes: 2 additions & 1 deletion src/oneblock/tasks/PlayerCacheRefreshTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.bukkit.World;

import oneblock.OneBlock;
import oneblock.utils.Scheduler;

/**
* Async-scheduled refresh of {@link oneblock.PlayerCache} contents from
Expand All @@ -18,6 +19,6 @@ public final class PlayerCacheRefreshTask implements Runnable {
@Override
public void run() {
World w = OneBlock.getWorld();
if (w != null) plugin.cache.updateCache(w.getPlayers());
if (w != null) plugin.cache.updateCache(Scheduler.playersIn(w));
}
}
Loading