core icon indicating copy to clipboard operation
core copied to clipboard

Calling addItem on a player's inventory causes an error

Open FalcoG opened this issue 4 years ago • 1 comments

When attempting to give a player an item by using addItem, it causes a Java error.

[11:18:21 ERROR]: [...] NO METHOD FOUND:
[11:18:21 ERROR]: [...]     org.bukkit.craftbukkit.v1_17_R1.inventory.CraftInventoryPlayer
[11:18:21 ERROR]: [...]     addItem
[11:18:21 ERROR]: [...]      - java.lang.Object[]
[11:18:21 ERROR]: [...] 

Example code to replicate it:

import { PlayerChatEvent, ServerCommands, ServerEvents, Players, ItemStack, Material } from '@customrealms/core'

ServerEvents.register(PlayerChatEvent, event => {
  const player = event.getPlayer()

  const inventory = player.getInventory()
  const invSlot = inventory.firstEmpty()
  if (invSlot === -1) return

  const voteItemMaterial = Material.withName('DIRT')
  if (voteItemMaterial == null) return

  const voteItem = ItemStack.create(voteItemMaterial, 1)
  console.log('give item', voteItemMaterial.getName(), invSlot)
  inventory.addItem(voteItem)
})

edit: replacing inventory.addItem(voteItem) with inventory.setItem(invSlot, voteItem) doesn't work either (no error) but replacing it with inventory.setItemInMainHand(voteItem) does work.

FalcoG avatar Jan 16 '22 10:01 FalcoG

https://github.com/customrealms/core/blob/c66d3e4ee0997461df3cbb3cf43e062470a0de57/src/inventory/Inventory.ts#L28-L32

Maybe line 30 needs to be spread?

AL1L avatar Jan 26 '22 17:01 AL1L