From 878509406b4f076db494a9e5ba70a73f1ff6b588 Mon Sep 17 00:00:00 2001 From: Jordan Date: Wed, 12 Oct 2022 19:48:56 +0100 Subject: [PATCH] fix: Add items to a player's inventory on the main thread (#1977) - Fixes #1954 --- .../bukkit/util/image/BukkitImageViewer.java | 20 +++++---- .../sk89q/worldedit/bukkit/BukkitPlayer.java | 43 +++++++++---------- .../bukkit/BukkitPlayerBlockBag.java | 6 ++- .../sk89q/worldedit/bukkit/BukkitWorld.java | 16 ++++--- 4 files changed, 47 insertions(+), 38 deletions(-) diff --git a/worldedit-bukkit/src/main/java/com/fastasyncworldedit/bukkit/util/image/BukkitImageViewer.java b/worldedit-bukkit/src/main/java/com/fastasyncworldedit/bukkit/util/image/BukkitImageViewer.java index 550b96641..2a61f3083 100644 --- a/worldedit-bukkit/src/main/java/com/fastasyncworldedit/bukkit/util/image/BukkitImageViewer.java +++ b/worldedit-bukkit/src/main/java/com/fastasyncworldedit/bukkit/util/image/BukkitImageViewer.java @@ -1,5 +1,6 @@ package com.fastasyncworldedit.bukkit.util.image; +import com.fastasyncworldedit.core.util.TaskManager; import com.fastasyncworldedit.core.util.image.Drawable; import com.fastasyncworldedit.core.util.image.ImageUtil; import com.fastasyncworldedit.core.util.image.ImageViewer; @@ -163,15 +164,18 @@ public class BukkitImageViewer implements ImageViewer { controller.showInFrames(player, frames, true); } else { int slot = getMapSlot(player); - if (slot == -1) { - if (initializing) { - player.getInventory().setItemInMainHand(new ItemStack(Material.MAP)); - } else { - return; + TaskManager.taskManager().sync(() -> { + if (slot == -1) { + if (initializing) { + player.getInventory().setItemInMainHand(new ItemStack(Material.MAP)); + } else { + return null; + } + } else if (player.getInventory().getHeldItemSlot() != slot) { + player.getInventory().setHeldItemSlot(slot); } - } else if (player.getInventory().getHeldItemSlot() != slot) { - player.getInventory().setHeldItemSlot(slot); - } + return null; + }); if (image == null && drawable != null) { image = drawable.draw(); } diff --git a/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/BukkitPlayer.java b/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/BukkitPlayer.java index acfd5cacd..9c5196e91 100644 --- a/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/BukkitPlayer.java +++ b/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/BukkitPlayer.java @@ -22,7 +22,6 @@ package com.sk89q.worldedit.bukkit; import com.fastasyncworldedit.core.configuration.Caption; import com.fastasyncworldedit.core.configuration.Settings; import com.fastasyncworldedit.core.util.TaskManager; -import com.fastasyncworldedit.core.util.task.RunnableVal; import com.sk89q.util.StringUtil; import com.sk89q.wepif.VaultResolver; import com.sk89q.worldedit.WorldEdit; @@ -162,31 +161,29 @@ public class BukkitPlayer extends AbstractPlayerActor { public void giveItem(BaseItemStack itemStack) { final PlayerInventory inv = player.getInventory(); ItemStack newItem = BukkitAdapter.adapt(itemStack); - if (itemStack.getType().getId().equalsIgnoreCase(WorldEdit.getInstance().getConfiguration().wandItem)) { - inv.remove(newItem); - } - final ItemStack item = player.getInventory().getItemInMainHand(); - player.getInventory().setItemInMainHand(newItem); - HashMap overflow = inv.addItem(item); - if (!overflow.isEmpty()) { - TaskManager.taskManager().sync(new RunnableVal<>() { - @Override - public void run(Object value) { - for (Map.Entry entry : overflow.entrySet()) { - ItemStack stack = entry.getValue(); - if (stack.getType() != Material.AIR && stack.getAmount() > 0) { - Item dropped = player.getWorld().dropItem(player.getLocation(), stack); - PlayerDropItemEvent event = new PlayerDropItemEvent(player, dropped); - Bukkit.getPluginManager().callEvent(event); - if (event.isCancelled()) { - dropped.remove(); - } + TaskManager.taskManager().sync(() -> { + if (itemStack.getType().getId().equalsIgnoreCase(WorldEdit.getInstance().getConfiguration().wandItem)) { + inv.remove(newItem); + } + final ItemStack item = player.getInventory().getItemInMainHand(); + player.getInventory().setItemInMainHand(newItem); + HashMap overflow = inv.addItem(item); + if (!overflow.isEmpty()) { + for (Map.Entry entry : overflow.entrySet()) { + ItemStack stack = entry.getValue(); + if (stack.getType() != Material.AIR && stack.getAmount() > 0) { + Item dropped = player.getWorld().dropItem(player.getLocation(), stack); + PlayerDropItemEvent event = new PlayerDropItemEvent(player, dropped); + Bukkit.getPluginManager().callEvent(event); + if (event.isCancelled()) { + dropped.remove(); } } } - }); - } - player.updateInventory(); + } + player.updateInventory(); + return null; + }); } //FAWE end diff --git a/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/BukkitPlayerBlockBag.java b/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/BukkitPlayerBlockBag.java index a36a20222..b060d78f8 100644 --- a/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/BukkitPlayerBlockBag.java +++ b/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/BukkitPlayerBlockBag.java @@ -20,6 +20,7 @@ package com.sk89q.worldedit.bukkit; import com.fastasyncworldedit.core.extent.inventory.SlottableBlockBag; +import com.fastasyncworldedit.core.util.TaskManager; import com.sk89q.worldedit.blocks.BaseItem; import com.sk89q.worldedit.blocks.BaseItemStack; import com.sk89q.worldedit.extent.inventory.BlockBag; @@ -171,7 +172,10 @@ public class BukkitPlayerBlockBag extends BlockBag implements SlottableBlockBag @Override public void flushChanges() { if (items != null) { - player.getInventory().setContents(items); + TaskManager.taskManager().sync(() -> { + player.getInventory().setContents(items); + return null; + }); items = null; } } diff --git a/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/BukkitWorld.java b/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/BukkitWorld.java index 66bf6648f..c4316584e 100644 --- a/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/BukkitWorld.java +++ b/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/BukkitWorld.java @@ -24,6 +24,7 @@ import com.fastasyncworldedit.core.Fawe; import com.fastasyncworldedit.core.internal.exception.FaweException; import com.fastasyncworldedit.core.queue.IChunkGet; import com.fastasyncworldedit.core.queue.implementation.packet.ChunkPacket; +import com.fastasyncworldedit.core.util.TaskManager; import com.google.common.collect.ImmutableSet; import com.google.common.collect.Sets; import com.sk89q.jnbt.CompoundTag; @@ -279,12 +280,15 @@ public class BukkitWorld extends AbstractWorld { return false; } - InventoryHolder chest = (InventoryHolder) state; - Inventory inven = chest.getInventory(); - if (chest instanceof Chest) { - inven = ((Chest) chest).getBlockInventory(); - } - inven.clear(); + TaskManager.taskManager().sync(() -> { + InventoryHolder chest = (InventoryHolder) state; + Inventory inven = chest.getInventory(); + if (chest instanceof Chest) { + inven = ((Chest) chest).getBlockInventory(); + } + inven.clear(); + return null; + }); return true; }