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

- Fixes #1954
Dieser Commit ist enthalten in:
Jordan 2022-10-12 19:48:56 +01:00 committet von GitHub
Ursprung 33be942176
Commit 878509406b
Es konnte kein GPG-Schlüssel zu dieser Signatur gefunden werden
GPG-Schlüssel-ID: 4AEE18F83AFDEB23
4 geänderte Dateien mit 47 neuen und 38 gelöschten Zeilen

Datei anzeigen

@ -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);
TaskManager.taskManager().sync(() -> {
if (slot == -1) { if (slot == -1) {
if (initializing) { if (initializing) {
player.getInventory().setItemInMainHand(new ItemStack(Material.MAP)); player.getInventory().setItemInMainHand(new ItemStack(Material.MAP));
} else { } else {
return; return null;
} }
} else if (player.getInventory().getHeldItemSlot() != slot) { } else if (player.getInventory().getHeldItemSlot() != slot) {
player.getInventory().setHeldItemSlot(slot); player.getInventory().setHeldItemSlot(slot);
} }
return null;
});
if (image == null && drawable != null) { if (image == null && drawable != null) {
image = drawable.draw(); image = drawable.draw();
} }

Datei anzeigen

@ -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,6 +161,7 @@ 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);
TaskManager.taskManager().sync(() -> {
if (itemStack.getType().getId().equalsIgnoreCase(WorldEdit.getInstance().getConfiguration().wandItem)) { if (itemStack.getType().getId().equalsIgnoreCase(WorldEdit.getInstance().getConfiguration().wandItem)) {
inv.remove(newItem); inv.remove(newItem);
} }
@ -169,9 +169,6 @@ public class BukkitPlayer extends AbstractPlayerActor {
player.getInventory().setItemInMainHand(newItem); player.getInventory().setItemInMainHand(newItem);
HashMap<Integer, ItemStack> overflow = inv.addItem(item); HashMap<Integer, ItemStack> overflow = inv.addItem(item);
if (!overflow.isEmpty()) { if (!overflow.isEmpty()) {
TaskManager.taskManager().sync(new RunnableVal<>() {
@Override
public void run(Object value) {
for (Map.Entry<Integer, ItemStack> entry : overflow.entrySet()) { for (Map.Entry<Integer, ItemStack> entry : overflow.entrySet()) {
ItemStack stack = entry.getValue(); ItemStack stack = entry.getValue();
if (stack.getType() != Material.AIR && stack.getAmount() > 0) { if (stack.getType() != Material.AIR && stack.getAmount() > 0) {
@ -184,9 +181,9 @@ public class BukkitPlayer extends AbstractPlayerActor {
} }
} }
} }
});
}
player.updateInventory(); player.updateInventory();
return null;
});
} }
//FAWE end //FAWE end

Datei anzeigen

@ -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) {
TaskManager.taskManager().sync(() -> {
player.getInventory().setContents(items); player.getInventory().setContents(items);
return null;
});
items = null; items = null;
} }
} }

Datei anzeigen

@ -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;
} }
TaskManager.taskManager().sync(() -> {
InventoryHolder chest = (InventoryHolder) state; InventoryHolder chest = (InventoryHolder) state;
Inventory inven = chest.getInventory(); Inventory inven = chest.getInventory();
if (chest instanceof Chest) { if (chest instanceof Chest) {
inven = ((Chest) chest).getBlockInventory(); inven = ((Chest) chest).getBlockInventory();
} }
inven.clear(); inven.clear();
return null;
});
return true; return true;
} }