Guest User

Untitled

a guest
Mar 4th, 2020
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.39 KB | None | 0 0
  1. package de.blablubbabc.test2;
  2.  
  3. import org.bukkit.Bukkit;
  4. import org.bukkit.Material;
  5. import org.bukkit.inventory.ItemStack;
  6. import org.bukkit.inventory.PlayerInventory;
  7. import org.bukkit.plugin.Plugin;
  8.  
  9. /*
  10.  * Some dummy reason for periodically modifying the player's inventory: The
  11.  * plugin slowly replenishes arrow stacks. It runs very frequently to increase
  12.  * the chance of running into this issue.
  13.  * Essential here: It uses PlayerInventory.get/setStorageContents() to apply
  14.  * inventory changes. setStorageContents copies the items (see
  15.  * CraftInventory.setItem and CraftItemStack.asNMSCopy).
  16.  *
  17.  * The player might currently charge a trident item while the active trident
  18.  * item is replaced with a new ItemStack instance. Minecraft remembers the
  19.  * itemstack that was used at the beginning of the charge (see
  20.  * EntityHuman.activeItem). When checking if the player is still holding the
  21.  * currently charging item, minecraft compares the active item's material with
  22.  * the currently held item. Since the material is still the same after the
  23.  * inventory got updated, minecraft will not abort the charging.
  24.  * However, once the trident is thrown minecraft tries to remove it from the
  25.  * player's inventory by searching for the exact ItemStack instance. This
  26.  * fails, since the ItemStack is no longer the same instance.
  27.  * The consequence is that the trident is thrown (and can then be picked up by
  28.  * the player again), while no trident item is removed from the player's
  29.  * inventory. The result is that the player is able to duplicate the trident
  30.  * item this way.
  31.  */
  32. public class TridentDuplicationBug {
  33.  
  34.     public void onEnable(Plugin plugin) {
  35.         Bukkit.getScheduler().runTaskTimer(plugin, () -> {
  36.             Bukkit.getOnlinePlayers().forEach(player -> {
  37.                 PlayerInventory playerInventory = player.getInventory();
  38.                 ItemStack[] newPlayerContents = playerInventory.getStorageContents();
  39.                 int size = newPlayerContents.length;
  40.                 for (int slot = 0; slot < size; ++slot) {
  41.                     ItemStack slotItem = newPlayerContents[slot];
  42.                     if (slotItem != null && slotItem.getType() == Material.ARROW
  43.                             && slotItem.getAmount() < slotItem.getMaxStackSize()) {
  44.                         slotItem.setAmount(slotItem.getMaxStackSize());
  45.                     }
  46.                 }
  47.                 // apply inventory changes:
  48.                 playerInventory.setStorageContents(newPlayerContents);
  49.                 player.sendMessage("Arrows replenished!");
  50.             });
  51.         }, 20L, 5L);
  52.     }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment