Fix chestplate duplication
This commit is contained in:
1 parent
dd9bd5f291
commit
6c629e3a5b
1 file changed
+22
-3
@@ -18,6 +18,7 @@ import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.entity.EntityShootBowEvent;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.PlayerInventory;
|
||||
import org.bukkit.potion.PotionData;
|
||||
import org.bukkit.potion.PotionType;
|
||||
|
||||
@@ -138,14 +139,32 @@ public class OnEntityShootBowEvent implements Listener {
|
||||
if (!(holdingFlyingBow || ridingFlyingBowArrow))
|
||||
return;
|
||||
|
||||
/* event is NOT cancellable in <1.20 Purpur builds, so revert the change */
|
||||
/*
|
||||
* Event is NOT cancellable in <1.20 Purpur builds, so revert by hand. The
|
||||
* vanilla equip is a SWAP: the elytra moves into the chest slot and the old
|
||||
* chestplate (oldItem) is pushed into the inventory slot the elytra came from.
|
||||
* Naively calling setChestplate(oldItem) would leave TWO copies of the old
|
||||
* chestplate -- the one we write into the chest slot plus the one vanilla
|
||||
* already moved into the inventory -- duping it (and any other hotbar item
|
||||
* swapped this way). Remove that displaced copy first.
|
||||
*/
|
||||
ItemStack previous = event.getOldItem();
|
||||
ItemStack elytra = newItem.clone();
|
||||
|
||||
Bukkit.getScheduler().runTask(FlyingBowOG.getPlugin(), () -> {
|
||||
|
||||
player.getInventory().setChestplate(previous);
|
||||
Map<Integer, ItemStack> leftovers = player.getInventory().addItem(elytra);
|
||||
PlayerInventory inventory = player.getInventory();
|
||||
|
||||
// Drop the displaced copy the vanilla swap left in the inventory (if any)
|
||||
// before restoring it to the chest slot, so we never end up with two.
|
||||
if (previous != null && previous.getType() != Material.AIR)
|
||||
inventory.removeItem(previous.clone());
|
||||
|
||||
// Restore the old chestplate to the chest slot (clears the elytra).
|
||||
inventory.setChestplate(previous);
|
||||
|
||||
// Hand the elytra back; drop whatever does not fit.
|
||||
Map<Integer, ItemStack> leftovers = inventory.addItem(elytra);
|
||||
leftovers.values().forEach(item -> player.getWorld().dropItemNaturally(player.getLocation(), item));
|
||||
|
||||
});
|
||||
|
||||
Reference in new issue
Block a user