Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Folia support #329

Draft
wants to merge 8 commits into
base: master
Choose a base branch
from
2 changes: 2 additions & 0 deletions bukkit/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ dependencies {
implementation("org.bstats:bstats-bukkit:3.0.2")
implementation("net.kyori:adventure-text-minimessage:4.16.0")
implementation("net.kyori:adventure-platform-bukkit:4.3.3")
implementation("com.github.technicallycoded:FoliaLib:main-SNAPSHOT")
compileOnly("org.jetbrains:annotations:24.1.0")
compileOnly("org.spigotmc:spigot-api:1.21.3-R0.1-SNAPSHOT")
compileOnly("me.clip:placeholderapi:2.11.6")
Expand Down Expand Up @@ -81,6 +82,7 @@ tasks {
relocate("net.querz", "dev.aurelium.auraskills.querz")
relocate("com.archyx.polyglot", "dev.aurelium.auraskills.polyglot")
relocate("org.atteo.evo.inflector", "dev.aurelium.auraskills.inflector")
relocate("com.tcoded.folialib", "dev.aurelium.auraskills.folialib")

exclude("acf-*.properties")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@
import dev.aurelium.auraskills.common.message.type.CommandMessage;
import dev.aurelium.auraskills.common.migration.MigrationManager;
import dev.aurelium.auraskills.common.reward.RewardManager;
import dev.aurelium.auraskills.common.scheduler.Scheduler;
import dev.aurelium.auraskills.common.skill.SkillLoader;
import dev.aurelium.auraskills.common.skill.SkillManager;
import dev.aurelium.auraskills.common.skill.SkillRegistry;
Expand All @@ -97,6 +96,7 @@
import dev.aurelium.auraskills.common.util.file.FileUtil;
import dev.aurelium.slate.Slate;
import dev.aurelium.slate.inv.InventoryManager;
import dev.aurelium.slate.scheduler.Scheduler;
import net.kyori.adventure.platform.bukkit.BukkitAudiences;
import org.bstats.bukkit.Metrics;
import org.bukkit.Bukkit;
Expand Down Expand Up @@ -140,7 +140,7 @@ public class AuraSkills extends JavaPlugin implements AuraSkillsPlugin {
private LeaderboardManager leaderboardManager;
private BukkitUiProvider uiProvider;
private RewardManager rewardManager;
private Scheduler scheduler;
private BukkitScheduler scheduler;
private StorageProvider storageProvider;
private Slate slate;
private MenuFileManager menuFileManager;
Expand Down Expand Up @@ -223,7 +223,7 @@ public void onEnable() {
leaderboardManager = new LeaderboardManager(this);
uiProvider = new BukkitUiProvider(this);
modifierManager = new BukkitModifierManager(this);
inventoryManager = new InventoryManager(this, dev.aurelium.slate.scheduler.Scheduler.createScheduler(this));
inventoryManager = new InventoryManager(this, Scheduler.createScheduler(this));
inventoryManager.init();
rewardManager = new BukkitRewardManager(this); // Loaded later
lootTableManager = new LootTableManager(this); // Loaded later
Expand Down Expand Up @@ -578,7 +578,7 @@ public RewardManager getRewardManager() {
}

@Override
public Scheduler getScheduler() {
public BukkitScheduler getScheduler() {
return scheduler;
}

Expand Down Expand Up @@ -647,14 +647,16 @@ public String getPrefix(Locale locale) {

@Override
public void runConsoleCommand(String command) {
getServer().dispatchCommand(getServer().getConsoleSender(), command);
// Console commands need to be run on the global region scheduler
scheduler.executeSync(() -> getServer().dispatchCommand(getServer().getConsoleSender(), command));
}

@Override
public void runPlayerCommand(User user, String command) {
Player player = ((BukkitUser) user).getPlayer();
if (player != null) {
getServer().dispatchCommand(player, command);
// Use performCommand and redirect it to the proper thread
scheduler.executeAtEntity(player, (t) -> player.performCommand(command));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,8 +247,7 @@ public void onItemRequirementRemove(@Flags("itemheld") Player player, Skill skil
player.getInventory().setItemInMainHand(item);
player.sendMessage(plugin.getPrefix(locale) + TextUtil.replace(plugin.getMsg(CommandMessage.ITEM_REQUIREMENT_REMOVE_REMOVED, locale),
"{skill}", skill.getDisplayName(locale)));
}
else {
} else {
player.sendMessage(plugin.getPrefix(locale) + TextUtil.replace(plugin.getMsg(CommandMessage.ITEM_REQUIREMENT_REMOVE_DOES_NOT_EXIST, locale),
"{skill}", skill.getDisplayName(locale)));
}
Expand Down Expand Up @@ -325,31 +324,33 @@ public void onItemGive(CommandSender sender, @Flags("other") Player player, Stri
if (amount != -1) {
item.setAmount(amount);
}
ItemStack leftoverItem = ItemUtils.addItemToInventory(player, item);
plugin.getScheduler().executeAtEntity(player, (t) -> {
ItemStack leftoverItem = ItemUtils.addItemToInventory(player, item);

String senderMsg = TextUtil.replace(plugin.getMsg(CommandMessage.ITEM_GIVE_SENDER, locale),
"{amount}", String.valueOf(item.getAmount()), "{key}", key, "{player}", player.getName());
if (!senderMsg.isEmpty()) {
sender.sendMessage(plugin.getPrefix(locale) + senderMsg);
}
String senderMsg = TextUtil.replace(plugin.getMsg(CommandMessage.ITEM_GIVE_SENDER, locale),
"{amount}", String.valueOf(item.getAmount()), "{key}", key, "{player}", player.getName());
if (!senderMsg.isEmpty()) {
sender.sendMessage(plugin.getPrefix(locale) + senderMsg);
}

if (!sender.equals(player)) {
String message = TextUtil.replace(plugin.getMsg(CommandMessage.ITEM_GIVE_RECEIVER, locale),
"{amount}", String.valueOf(item.getAmount()), "{key}", key);
if (!message.isEmpty()) {
player.sendMessage(plugin.getPrefix(locale) + message);
if (!sender.equals(player)) {
String message = TextUtil.replace(plugin.getMsg(CommandMessage.ITEM_GIVE_RECEIVER, locale),
"{amount}", String.valueOf(item.getAmount()), "{key}", key);
if (!message.isEmpty()) {
player.sendMessage(plugin.getPrefix(locale) + message);
}
}
}
// Add to unclaimed items if leftover
if (leftoverItem != null) {
User user = plugin.getUser(player);
user.getUnclaimedItems().add(new KeyIntPair(key, leftoverItem.getAmount()));

String message = plugin.getMsg(LevelerMessage.UNCLAIMED_ITEM, locale);
if (!message.isEmpty()) {
player.sendMessage(plugin.getPrefix(locale) + message);
// Add to unclaimed items if leftover
if (leftoverItem != null) {
User user = plugin.getUser(player);
user.getUnclaimedItems().add(new KeyIntPair(key, leftoverItem.getAmount()));

String message = plugin.getMsg(LevelerMessage.UNCLAIMED_ITEM, locale);
if (!message.isEmpty()) {
player.sendMessage(plugin.getPrefix(locale) + message);
}
}
}
});
} else {
sender.sendMessage(plugin.getPrefix(locale) + TextUtil.replace(plugin.getMsg(CommandMessage.ITEM_UNREGISTER_NOT_REGISTERED, locale), "{key}", key));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,15 @@
import dev.aurelium.auraskills.api.skill.Skill;
import dev.aurelium.auraskills.api.stat.Stat;
import dev.aurelium.auraskills.api.stat.StatModifier;
import dev.aurelium.auraskills.api.util.NumberUtil;
import dev.aurelium.auraskills.bukkit.AuraSkills;
import dev.aurelium.auraskills.common.message.type.CommandMessage;
import dev.aurelium.auraskills.common.user.User;
import dev.aurelium.auraskills.common.user.UserState;
import dev.aurelium.auraskills.api.util.NumberUtil;
import dev.aurelium.auraskills.common.util.text.TextUtil;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.command.CommandSender;
import org.bukkit.scheduler.BukkitRunnable;

import java.util.*;

Expand Down Expand Up @@ -53,12 +52,8 @@ public void onSkills(CommandSender sender, String player) {
plugin.getScheduler().executeAsync(() -> {
try {
UserState userState = plugin.getStorageProvider().loadState(uuid);
new BukkitRunnable() {
@Override
public void run() {
sendSkillsMessage(sender, player, uuid, userState.skillLevels(), userState.skillXp());
}
}.runTask(plugin);
plugin.getScheduler().executeSync(() ->
sendSkillsMessage(sender, player, uuid, userState.skillLevels(), userState.skillXp()));
} catch (Exception ignored) {
sender.sendMessage(manager.formatMessage(manager.getCommandIssuer(sender), MessageType.ERROR, MinecraftMessageKeys.NO_PLAYER_FOUND, "{search}", player));
}
Expand All @@ -85,12 +80,8 @@ public void onStats(CommandSender sender, String player) {
plugin.getScheduler().executeAsync(() -> {
try {
UserState userState = plugin.getStorageProvider().loadState(uuid);
new BukkitRunnable() {
@Override
public void run() {
sendStatsMessage(sender, player, uuid, userState.skillLevels(), userState.statModifiers());
}
}.runTask(plugin);
plugin.getScheduler().executeSync(() ->
sendStatsMessage(sender, player, uuid, userState.skillLevels(), userState.statModifiers()));
} catch (Exception ignored) {
sender.sendMessage(manager.formatMessage(manager.getCommandIssuer(sender), MessageType.ERROR, MinecraftMessageKeys.NO_PLAYER_FOUND, "{search}", player));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.command.RemoteConsoleCommandSender;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable;

import java.util.*;
import java.util.concurrent.CompletableFuture;
Expand Down Expand Up @@ -311,7 +310,7 @@ public void onClaimItems(Player player) {
player.sendMessage(plugin.getPrefix(locale) + plugin.getMsg(CommandMessage.CLAIMITEMS_NO_ITEMS, locale));
return;
}
UnclaimedItemsMenu.getInventory(plugin, user).open(player);
plugin.getScheduler().executeAtEntity(player, (t) -> UnclaimedItemsMenu.getInventory(plugin, user).open(player));
}

@Subcommand("version")
Expand Down Expand Up @@ -393,17 +392,14 @@ private CompletableFuture<UserState> getPlayerDataState(OfflinePlayer player) {
future.complete(null);
}
} else {
new BukkitRunnable() {
@Override
public void run() {
try {
future.complete(plugin.getStorageProvider().loadState(player.getUniqueId()));
} catch (Exception e) {
future.complete(null);
e.printStackTrace();
}
plugin.getScheduler().executeAsync(() -> {
try {
future.complete(plugin.getStorageProvider().loadState(player.getUniqueId()));
} catch (Exception e) {
future.complete(null);
e.printStackTrace();
}
}.runTaskAsynchronously(plugin);
});
}
return future;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ public StatsCommand(AuraSkills plugin) {
public void onStats(Player player) {
plugin.getSlate().openMenu(player, "stats");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public void createHologram(Location location, String text) {
}

public void deleteHologram(Hologram dh) {
plugin.getScheduler().scheduleSync(dh::delete, 30L * 50L, TimeUnit.MILLISECONDS);
plugin.getScheduler().scheduleAtLocation(dh.getLocation(), dh::delete, 30L * 50L, TimeUnit.MILLISECONDS);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public void createHologram(Location location, String text) {
}

public void deleteHologram(Hologram hd) {
plugin.getScheduler().scheduleSync(hd::delete, 30L * 50L, TimeUnit.MILLISECONDS);
plugin.getScheduler().scheduleAtLocation(hd.getLocation(), hd::delete, 30L * 50L, TimeUnit.MILLISECONDS);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ public void onMythicSkillDamage(MythicDamageEvent event) {
if (target.hasMetadata("NPC")) return;
}

// This is unsafe here since it will read attributes from the player, but since MythicMobs doesn't support
// Folia, we can ignore this. If MythicMobs ever supports Folia, they should fire this event on the proper region
DamageResult result = damageHandler.handleDamage(
BukkitAdapter.adapt(attacker), BukkitAdapter.adapt(target), getDamageType(attacker),
event.getDamageMetadata().getDamageCause(), event.getDamage(), "mythicmobs");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,17 @@ public void giveItem(User user, NamespacedId key, int amount) {
return;
}

ItemStack leftoverItem = ItemUtils.addItemToInventory(player, item); // Attempt item give
// Handle items that could not fit in the inventory
if (leftoverItem != null) {
// Add unclaimed item key and amount to player data
user.getUnclaimedItems().add(new KeyIntPair(key.toString(), leftoverItem.getAmount()));
// Notify player
plugin.getServer().getScheduler().runTaskLater(plugin, () ->
player.sendMessage(plugin.getPrefix(user.getLocale()) + plugin.getMsg(LevelerMessage.UNCLAIMED_ITEM, user.getLocale())), 1);
}
plugin.getScheduler().executeAtEntity(player, (t) -> {
ItemStack leftoverItem = ItemUtils.addItemToInventory(player, item); // Attempt item give
// Handle items that could not fit in the inventory
if (leftoverItem != null) {
// Add unclaimed item key and amount to player data
user.getUnclaimedItems().add(new KeyIntPair(key.toString(), leftoverItem.getAmount()));
// Notify player
plugin.getScheduler().executeSync(() ->
player.sendMessage(plugin.getPrefix(user.getLocale()) + plugin.getMsg(LevelerMessage.UNCLAIMED_ITEM, user.getLocale())));
}
});
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public void init(Player player, InventoryContents contents) {
item.setAmount(amount);
contents.set(row, column, ClickableItem.from(getDisplayItem(item), data -> {
// Give item on click
// This should be running already on the thread that owns the player entity
ItemStack leftoverItem = ItemUtils.addItemToInventory(player, item);
if (leftoverItem == null) { // All items were added
user.getUnclaimedItems().remove(keyIntPair);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,10 @@ public void playLevelUpSound(@NotNull User user) {
@Override
public void reloadModifiers(User user) {
Player player = ((BukkitUser) user).getPlayer();
if (player != null) {
plugin.getModifierManager().reloadPlayer(player);
}
plugin.getScheduler().executeAtEntity(player, t -> {
if (player != null) {
plugin.getModifierManager().reloadPlayer(player);
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ private DamageModifier getCrit(Player player, User user) {
}
// Set metadata for holograms to detect
player.setMetadata("skillsCritical", new FixedMetadataValue(plugin, true));
plugin.getScheduler().scheduleSync(() -> player.removeMetadata("skillsCritical", plugin), 50, TimeUnit.MILLISECONDS);
plugin.getScheduler().scheduleAtEntity(player, () -> player.removeMetadata("skillsCritical", plugin), 50, TimeUnit.MILLISECONDS);

double value = user.getEffectiveTraitLevel(Traits.CRIT_DAMAGE) / 100;
return new DamageModifier(value, DamageModifier.Operation.ADD_COMBINED);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ private void detectUserLanguage(User user, Player player) {

private void sendUpdateMessage(Player player) {
// Use a delayed task to give time for permission plugins to load data
plugin.getServer().getScheduler().runTaskLater(plugin, () -> {
plugin.getScheduler().scheduleSync(() -> {
if (!plugin.configBoolean(Option.CHECK_FOR_UPDATES)) {
return;
}
Expand All @@ -111,7 +111,7 @@ private void sendUpdateMessage(Player player) {
player.sendMessage(plugin.getPrefix(plugin.getDefaultLanguage()) + ChatColor.WHITE + "Download it on Spigot: " + ChatColor.YELLOW + "" + ChatColor.UNDERLINE + "https://spigotmc.org/resources/" + plugin.getResourceId());
}
});
}, 40L);
}, 2L, TimeUnit.SECONDS);
}

}
Loading