customrealms / core

Core library for the CustomRealms runtime
https://customrealms.io/core
MIT License
37 stars 14 forks source link

Calling addItem on a player's inventory causes an error #22

Open FalcoG opened 2 years ago

FalcoG commented 2 years ago

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.

AL1L commented 2 years ago

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

Maybe line 30 needs to be spread?