fix: Add items to a player's inventory on the main thread (#1977)

- Fixes #1954
This commit is contained in:
Jordan 2022-10-12 19:48:56 +01:00 committed by GitHub
parent 33be942176
commit 878509406b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 47 additions and 38 deletions

View File

@ -1,5 +1,6 @@
package com.fastasyncworldedit.bukkit.util.image; 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.Drawable;
import com.fastasyncworldedit.core.util.image.ImageUtil; import com.fastasyncworldedit.core.util.image.ImageUtil;
import com.fastasyncworldedit.core.util.image.ImageViewer; import com.fastasyncworldedit.core.util.image.ImageViewer;
@ -163,15 +164,18 @@ public class BukkitImageViewer implements ImageViewer {
controller.showInFrames(player, frames, true); controller.showInFrames(player, frames, true);
} else { } else {
int slot = getMapSlot(player); int slot = getMapSlot(player);
if (slot == -1) { TaskManager.taskManager().sync(() -> {
if (initializing) { if (slot == -1) {
player.getInventory().setItemInMainHand(new ItemStack(Material.MAP)); if (initializing) {
} else { player.getInventory().setItemInMainHand(new ItemStack(Material.MAP));
return; } else {
return null;
}
} else if (player.getInventory().getHeldItemSlot() != slot) {
player.getInventory().setHeldItemSlot(slot);
} }
} else if (player.getInventory().getHeldItemSlot() != slot) { return null;
player.getInventory().setHeldItemSlot(slot); });
}
if (image == null && drawable != null) { if (image == null && drawable != null) {
image = drawable.draw(); image = drawable.draw();
} }

View File

@ -22,7 +22,6 @@ package com.sk89q.worldedit.bukkit;
import com.fastasyncworldedit.core.configuration.Caption; import com.fastasyncworldedit.core.configuration.Caption;
import com.fastasyncworldedit.core.configuration.Settings; import com.fastasyncworldedit.core.configuration.Settings;
import com.fastasyncworldedit.core.util.TaskManager; import com.fastasyncworldedit.core.util.TaskManager;
import com.fastasyncworldedit.core.util.task.RunnableVal;
import com.sk89q.util.StringUtil; import com.sk89q.util.StringUtil;
import com.sk89q.wepif.VaultResolver; import com.sk89q.wepif.VaultResolver;
import com.sk89q.worldedit.WorldEdit; import com.sk89q.worldedit.WorldEdit;
@ -162,31 +161,29 @@ public class BukkitPlayer extends AbstractPlayerActor {
public void giveItem(BaseItemStack itemStack) { public void giveItem(BaseItemStack itemStack) {
final PlayerInventory inv = player.getInventory(); final PlayerInventory inv = player.getInventory();
ItemStack newItem = BukkitAdapter.adapt(itemStack); ItemStack newItem = BukkitAdapter.adapt(itemStack);
if (itemStack.getType().getId().equalsIgnoreCase(WorldEdit.getInstance().getConfiguration().wandItem)) { TaskManager.taskManager().sync(() -> {
inv.remove(newItem); if (itemStack.getType().getId().equalsIgnoreCase(WorldEdit.getInstance().getConfiguration().wandItem)) {
} inv.remove(newItem);
final ItemStack item = player.getInventory().getItemInMainHand(); }
player.getInventory().setItemInMainHand(newItem); final ItemStack item = player.getInventory().getItemInMainHand();
HashMap<Integer, ItemStack> overflow = inv.addItem(item); player.getInventory().setItemInMainHand(newItem);
if (!overflow.isEmpty()) { HashMap<Integer, ItemStack> overflow = inv.addItem(item);
TaskManager.taskManager().sync(new RunnableVal<>() { if (!overflow.isEmpty()) {
@Override for (Map.Entry<Integer, ItemStack> entry : overflow.entrySet()) {
public void run(Object value) { ItemStack stack = entry.getValue();
for (Map.Entry<Integer, ItemStack> entry : overflow.entrySet()) { if (stack.getType() != Material.AIR && stack.getAmount() > 0) {
ItemStack stack = entry.getValue(); Item dropped = player.getWorld().dropItem(player.getLocation(), stack);
if (stack.getType() != Material.AIR && stack.getAmount() > 0) { PlayerDropItemEvent event = new PlayerDropItemEvent(player, dropped);
Item dropped = player.getWorld().dropItem(player.getLocation(), stack); Bukkit.getPluginManager().callEvent(event);
PlayerDropItemEvent event = new PlayerDropItemEvent(player, dropped); if (event.isCancelled()) {
Bukkit.getPluginManager().callEvent(event); dropped.remove();
if (event.isCancelled()) {
dropped.remove();
}
} }
} }
} }
}); }
} player.updateInventory();
player.updateInventory(); return null;
});
} }
//FAWE end //FAWE end

View File

@ -20,6 +20,7 @@
package com.sk89q.worldedit.bukkit; package com.sk89q.worldedit.bukkit;
import com.fastasyncworldedit.core.extent.inventory.SlottableBlockBag; import com.fastasyncworldedit.core.extent.inventory.SlottableBlockBag;
import com.fastasyncworldedit.core.util.TaskManager;
import com.sk89q.worldedit.blocks.BaseItem; import com.sk89q.worldedit.blocks.BaseItem;
import com.sk89q.worldedit.blocks.BaseItemStack; import com.sk89q.worldedit.blocks.BaseItemStack;
import com.sk89q.worldedit.extent.inventory.BlockBag; import com.sk89q.worldedit.extent.inventory.BlockBag;
@ -171,7 +172,10 @@ public class BukkitPlayerBlockBag extends BlockBag implements SlottableBlockBag
@Override @Override
public void flushChanges() { public void flushChanges() {
if (items != null) { if (items != null) {
player.getInventory().setContents(items); TaskManager.taskManager().sync(() -> {
player.getInventory().setContents(items);
return null;
});
items = null; items = null;
} }
} }

View File

@ -24,6 +24,7 @@ import com.fastasyncworldedit.core.Fawe;
import com.fastasyncworldedit.core.internal.exception.FaweException; import com.fastasyncworldedit.core.internal.exception.FaweException;
import com.fastasyncworldedit.core.queue.IChunkGet; import com.fastasyncworldedit.core.queue.IChunkGet;
import com.fastasyncworldedit.core.queue.implementation.packet.ChunkPacket; 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.ImmutableSet;
import com.google.common.collect.Sets; import com.google.common.collect.Sets;
import com.sk89q.jnbt.CompoundTag; import com.sk89q.jnbt.CompoundTag;
@ -279,12 +280,15 @@ public class BukkitWorld extends AbstractWorld {
return false; return false;
} }
InventoryHolder chest = (InventoryHolder) state; TaskManager.taskManager().sync(() -> {
Inventory inven = chest.getInventory(); InventoryHolder chest = (InventoryHolder) state;
if (chest instanceof Chest) { Inventory inven = chest.getInventory();
inven = ((Chest) chest).getBlockInventory(); if (chest instanceof Chest) {
} inven = ((Chest) chest).getBlockInventory();
inven.clear(); }
inven.clear();
return null;
});
return true; return true;
} }