9
0
mirror of https://github.com/WiIIiam278/HuskSync.git synced 2025-12-19 14:59:21 +00:00

Ensure attributes are accessed on main thread only (#578)

This commit is contained in:
mfnalex
2025-08-29 14:20:44 +02:00
committed by GitHub
parent a31c3c48f7
commit 96f34092f6

View File

@@ -45,6 +45,7 @@ import org.bukkit.inventory.EquipmentSlotGroup;
//#endif //#endif
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.persistence.PersistentDataContainer; import org.bukkit.persistence.PersistentDataContainer;
import org.bukkit.plugin.Plugin;
import org.bukkit.potion.PotionEffect; import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType; import org.bukkit.potion.PotionEffectType;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@@ -567,6 +568,14 @@ public abstract class BukkitData implements Data {
@NotNull @NotNull
public static BukkitData.Attributes adapt(@NotNull Player player, @NotNull HuskSync plugin) { public static BukkitData.Attributes adapt(@NotNull Player player, @NotNull HuskSync plugin) {
if (!Bukkit.isPrimaryThread()) {
try {
return Bukkit.getScheduler().callSyncMethod((Plugin) plugin, () -> adapt(player, plugin)).get();
} catch (Exception e) {
throw new IllegalStateException("Failed to adapt attributes on main thread", e);
}
}
final List<Attribute> attributes = Lists.newArrayList(); final List<Attribute> attributes = Lists.newArrayList();
final AttributeSettings settings = plugin.getSettings().getSynchronization().getAttributes(); final AttributeSettings settings = plugin.getSettings().getSynchronization().getAttributes();
Registry.ATTRIBUTE.forEach(id -> { Registry.ATTRIBUTE.forEach(id -> {
@@ -665,6 +674,15 @@ public abstract class BukkitData implements Data {
@Override @Override
public void apply(@NotNull BukkitUser user, @NotNull BukkitHuskSync plugin) throws IllegalStateException { public void apply(@NotNull BukkitUser user, @NotNull BukkitHuskSync plugin) throws IllegalStateException {
if (!Bukkit.isPrimaryThread()) {
try {
Bukkit.getScheduler().callSyncMethod(plugin, () -> { this.apply(user, plugin); return null; }).get();
return;
} catch (Exception e) {
throw new IllegalStateException("Failed to apply attributes on main thread", e);
}
}
final AttributeSettings settings = plugin.getSettings().getSynchronization().getAttributes(); final AttributeSettings settings = plugin.getSettings().getSynchronization().getAttributes();
Registry.ATTRIBUTE.forEach(id -> { Registry.ATTRIBUTE.forEach(id -> {
if (settings.isIgnoredAttribute(id.getKey().toString())) { if (settings.isIgnoredAttribute(id.getKey().toString())) {