Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package de.blablubbabc.test2;
- import org.bukkit.Bukkit;
- import org.bukkit.Material;
- import org.bukkit.inventory.ItemStack;
- import org.bukkit.inventory.PlayerInventory;
- import org.bukkit.plugin.Plugin;
- /*
- * Some dummy reason for periodically modifying the player's inventory: The
- * plugin slowly replenishes arrow stacks. It runs very frequently to increase
- * the chance of running into this issue.
- * Essential here: It uses PlayerInventory.get/setStorageContents() to apply
- * inventory changes. setStorageContents copies the items (see
- * CraftInventory.setItem and CraftItemStack.asNMSCopy).
- *
- * The player might currently charge a trident item while the active trident
- * item is replaced with a new ItemStack instance. Minecraft remembers the
- * itemstack that was used at the beginning of the charge (see
- * EntityHuman.activeItem). When checking if the player is still holding the
- * currently charging item, minecraft compares the active item's material with
- * the currently held item. Since the material is still the same after the
- * inventory got updated, minecraft will not abort the charging.
- * However, once the trident is thrown minecraft tries to remove it from the
- * player's inventory by searching for the exact ItemStack instance. This
- * fails, since the ItemStack is no longer the same instance.
- * The consequence is that the trident is thrown (and can then be picked up by
- * the player again), while no trident item is removed from the player's
- * inventory. The result is that the player is able to duplicate the trident
- * item this way.
- */
- public class TridentDuplicationBug {
- public void onEnable(Plugin plugin) {
- Bukkit.getScheduler().runTaskTimer(plugin, () -> {
- Bukkit.getOnlinePlayers().forEach(player -> {
- PlayerInventory playerInventory = player.getInventory();
- ItemStack[] newPlayerContents = playerInventory.getStorageContents();
- int size = newPlayerContents.length;
- for (int slot = 0; slot < size; ++slot) {
- ItemStack slotItem = newPlayerContents[slot];
- if (slotItem != null && slotItem.getType() == Material.ARROW
- && slotItem.getAmount() < slotItem.getMaxStackSize()) {
- slotItem.setAmount(slotItem.getMaxStackSize());
- }
- }
- // apply inventory changes:
- playerInventory.setStorageContents(newPlayerContents);
- player.sendMessage("Arrows replenished!");
- });
- }, 20L, 5L);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment