-
Hello, I get this weird exception when using following code. I've spent a lot of time trying to figure out what's wrong and I have no clue. Am I doing something wrong or did I discover some sort of LuaLink - or more likely LuaJ / LuaKT - bug?
local Material = import "org.bukkit.Material"
local ItemStack = import "org.bukkit.inventory.ItemStack"
script.onLoad(function()
-- Listening to PlayerJoinEvent in attempt to give player items when they join for the first time.
script.hook("org.bukkit.event.player.PlayerJoinEvent", function(event)
-- NOTE: Priting player name for debug purposes, as to make sure that event:getPlayer() works as expected.
print(event:getPlayer():getName())
-- NOTE: Condition is currently inverted as I don't feel like deleting player data each time I want to test the code.
if (event:getPlayer():hasPlayedBefore() == true) then
-- Getting player's inventory.
local inventory = event:getPlayer():getInventory()
-- Adding starting items to player's inventory.
-- NOTE: There's also Inventory#addItem(ItemStack...) but I couldn't get this to work.
-- Code below works as expected, assuming the condition above is not throwing.
inventory:setItem(inventory:firstEmpty(), ItemStack:of(Material.WOODEN_SWORD))
inventory:setItem(inventory:firstEmpty(), ItemStack:of(Material.WOODEN_AXE))
inventory:setItem(inventory:firstEmpty(), ItemStack:of(Material.WOODEN_PICKAXE))
inventory:setItem(inventory:firstEmpty(), ItemStack:of(Material.WOODEN_SHOVEL))
inventory:setItem(inventory:firstEmpty(), ItemStack:of(Material.BREAD))
end
end)
end) |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
I've ran into this very same issue before (for the same event in fact) and if I remember correctly its due to there being a (private?) property in the Player class also named I'm unavailable to test myself but try to simply access it like a property like this: Also your comment about not being able to get |
Beta Was this translation helpful? Give feedback.
-
Yep, it works. Thank you very much! :)
Yes I did, throws NPE:
inventory:addItem({ItemStack:of(Material.WOODEN_SWORD), ItemStack:of(Material.WOODEN_AXE)}) |
Beta Was this translation helpful? Give feedback.
I've ran into this very same issue before (for the same event in fact) and if I remember correctly its due to there being a (private?) property in the Player class also named
hasPlayedBefore
and for some reason it's coercing that and due to how Lua works you can't have two named the same thing.I'm unavailable to test myself but try to simply access it like a property like this:
event:getPlayer().hasPlayedBefore
Also your comment about not being able to get
Inventory#addItem(ItemStack...)
to work. Have you tried passing the items stacks as a table?