Solved Problem adding a static ItemStack to Inventory

Discussion in 'Plugin Development' started by Hexxed_, Jul 11, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    Hexxed_

    So first off, I'm pretty new to Java and I'd highly appreciate it if you could tell me why it didn't work :).
    So what i wanted to do is create an ItemStack in an a Function and simply add it to the Players Inventory but it does for some reason not work. Instead it throws giant error messages at me. Here is the code:
    Code:java
    1. public static ItemStack getClassSelector() {
    2. ItemStack cs = new ItemStack(Material.NETHER_STAR, 1);
    3. ItemMeta csmeta = cs.getItemMeta();
    4. List<String> cslore = csmeta.getLore();
    5. csmeta.setDisplayName(ChatColor.GREEN + "Class Selector " + ChatColor.GRAY + "(Right Click to open)");
    6. cslore.add("§eUse this to choose your class to play!");
    7. csmeta.setLore(cslore);
    8. cs.setItemMeta(csmeta);
    9. return cs;
    10. }
     
  2. Offline

    Zaaryus

    Could you please post error log and show the rest of code where you give the player this item?
     
  3. Offline

    Hexxed_

    Im not sure how I would copy stuff out of cmd.exe but i can give you the Listener I am currently using this function in
    Code:
    Code:java
    1. @EventHandler
    2. public void onJoin(PlayerJoinEvent e) {
    3. Player p = e.getPlayer();
    4. if (p.getGameMode() == GameMode.SURVIVAL) {
    5. e.setJoinMessage("§9"+p.getDisplayName()+" §ejoined the game§a (INGAME)");
    6. API.ingameplayers.add(p);
    7. p.sendMessage("§eWelcome back§5 "+p.getDisplayName()+" §e. Have fun playing!");
    8. p.setScoreboard(API.board);
    9. API.ingameteam.addPlayer(p);
    10. }
    11. if (p.getGameMode() == GameMode.ADVENTURE) {
    12. p.teleport(API.getCastleLocation);
    13. e.setJoinMessage("§9"+p.getDisplayName()+" §ejoined the game§c (LOBBY)");
    14. p.sendMessage("§eWelcome back§5 "+p.getDisplayName()+" §e. Choose a class and walk through the Portal!");
    15. p.setScoreboard(API.board);
    16. API.jointeam.addPlayer(p);
    17. p.addPotionEffect(new PotionEffect(PotionEffectType.INVISIBILITY, Integer.MAX_VALUE, 0));
    18. p.getInventory().addItem(Items.getClassSelector());
    19. }
    20. if (p.getGameMode() == GameMode.CREATIVE) {
    21. p.setScoreboard(API.board);
    22. e.setJoinMessage("§9"+p.getDisplayName()+" §ejoined the game§6 (ADMIN)");
    23. }
    24. }
     
  4. Offline

    izarooni

    Right-click cmd select mark, highlight what you want and press enter to copy. Not sure about enter but I think so
     
  5. Offline

    Zaaryus

    Code:java
    1. public static ItemStack getClassSelector() {
    2. ItemStack cs = new ItemStack(Material.NETHER_STAR);
    3. ItemMeta csmeta = cs.getItemMeta();
    4. List<String> cslore = new ArrayList<String>();
    5. csmeta.setDisplayName(ChatColor.GREEN + "Class Selector " + ChatColor.GRAY + "(Right Click to open)");
    6. cslore.add("§eUse this to choose your class to play!");
    7. csmeta.setLore(cslore);
    8. cs.setItemMeta(csmeta);
    9. return cs;
    10. }

    I changed
    Code:java
    1. List<String> cslore = csmeta.getLore();

    Into
    Code:java
    1. List<String> cslore = new ArrayList<String>();

    (I'm pretty sure this removes current lore, which is something you probably don't need)
     
  6. Offline

    Hexxed_

    Thanks so far :). Here is the Error Log in the Console:
    Code:java
    1. [22:56:42 ERROR]: Could not pass event PlayerJoinEvent to WoK v0.0.1
    2. org.bukkit.event.EventException
    3. at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    4. va:427) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
    5. at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.jav
    6. a:62) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
    7. at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.j
    8. ava:481) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
    9. at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.j
    10. ava:466) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
    11. at net.minecraft.server.v1_7_R1.PlayerList.c(PlayerList.java:225) [craft
    12. bukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
    13. at net.minecraft.server.v1_7_R1.PlayerList.a(PlayerList.java:116) [craft
    14. bukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
    15. at net.minecraft.server.v1_7_R1.LoginListener.c(LoginListener.java:78) [
    16. craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
    17. at net.minecraft.server.v1_7_R1.LoginListener.a(LoginListener.java:42) [
    18. craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
    19. at net.minecraft.server.v1_7_R1.NetworkManager.a(NetworkManager.java:149
    20. ) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
    21. at net.minecraft.server.v1_7_R1.ServerConnection.c(SourceFile:134) [craf
    22. tbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
    23. at net.minecraft.server.v1_7_R1.MinecraftServer.u(MinecraftServer.java:6
    24. 55) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
    25. at net.minecraft.server.v1_7_R1.DedicatedServer.u(DedicatedServer.java:2
    26. 50) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
    27. at net.minecraft.server.v1_7_R1.MinecraftServer.t(MinecraftServer.java:5
    28. 45) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
    29. at net.minecraft.server.v1_7_R1.MinecraftServer.run(MinecraftServer.java
    30. :457) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
    31. at net.minecraft.server.v1_7_R1.ThreadServerApplication.run(SourceFile:6
    32. 17) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
    33. Caused by: java.lang.NullPointerException
    34. at me.tada89.mc.WoKItemList.getClassSelector(WoKItemList.java:21) ~[?:?]
    35.  
    36. at me.tada89.mc.JoinListener.onJoin(JoinListener.java:37) ~[?:?]
    37. at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.7.0
    38. _45]
    39. at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.7.0
    40. _45]
    41. at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1
    42. .7.0_45]
    43. at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.7.0_45]
    44. at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    45. va:425) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
    46. ... 14 more
     
  7. Offline

    Zaaryus

    That seems to be something else, could you show me the full class? I don't know where either line 21 in WoKItemList or line 37 in your JoinListener is.
     
  8. Offline

    Hexxed_

    Ok then.
    WoKItemList:
    Code:java
    1. package me.tada89.mc;
    2.  
    3. import java.util.List;
    4.  
    5. import org.bukkit.ChatColor;
    6. import org.bukkit.Material;
    7. import org.bukkit.event.Listener;
    8. import org.bukkit.inventory.ItemStack;
    9. import org.bukkit.inventory.meta.ItemMeta;
    10.  
    11. public class WoKItemList implements Listener {
    12.  
    13. //ItemStacks
    14. //Lobby
    15. //Class Selector (CS)
    16. public static ItemStack getClassSelector() {
    17. ItemStack cs = new ItemStack(Material.NETHER_STAR, 1);
    18. ItemMeta csmeta = cs.getItemMeta();
    19. List<String> cslore = csmeta.getLore();
    20. csmeta.setDisplayName(ChatColor.GREEN + "Class Selector " + ChatColor.GRAY + "(Right Click to open)");
    21. cslore.add("§eUse this to choose your class to play!");
    22. csmeta.setLore(cslore);
    23. cs.setItemMeta(csmeta);
    24. return cs;
    25. }
    26.  
    27. }


    JoinListener:
    Code:java
    1. package me.tada89.mc;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.GameMode;
    5. import org.bukkit.entity.Player;
    6. import org.bukkit.event.EventHandler;
    7. import org.bukkit.event.Listener;
    8. import org.bukkit.event.player.PlayerJoinEvent;
    9. import org.bukkit.inventory.ItemStack;
    10. import org.bukkit.potion.PotionEffect;
    11. import org.bukkit.potion.PotionEffectType;
    12. import org.bukkit.scoreboard.Scoreboard;
    13. import org.bukkit.scoreboard.ScoreboardManager;
    14. import org.bukkit.scoreboard.Team;
    15.  
    16. public class JoinListener implements Listener{
    17.  
    18.  
    19. //Listener
    20. @EventHandler
    21. public void onJoin(PlayerJoinEvent e) {
    22. Player p = e.getPlayer();
    23. if (p.getGameMode() == GameMode.SURVIVAL) {
    24. e.setJoinMessage("§9"+p.getDisplayName()+" §ejoined the game§a (INGAME)");
    25. API.ingameplayers.add(p);
    26. p.sendMessage("§eWelcome back§5 "+p.getDisplayName()+" §e. Have fun playing!");
    27. p.setScoreboard(API.board);
    28. API.ingameteam.addPlayer(p);
    29. }
    30. if (p.getGameMode() == GameMode.ADVENTURE) {
    31. p.teleport(API.getCastleLocation);
    32. e.setJoinMessage("§9"+p.getDisplayName()+" §ejoined the game§c (LOBBY)");
    33. p.sendMessage("§eWelcome back§5 "+p.getDisplayName()+" §e. Choose a class and walk through the Portal!");
    34. p.setScoreboard(API.board);
    35. API.jointeam.addPlayer(p);
    36. p.addPotionEffect(new PotionEffect(PotionEffectType.INVISIBILITY, Integer.MAX_VALUE, 0));
    37. p.getInventory().addItem(WoKItemList.getClassSelector());
    38. }
    39. if (p.getGameMode() == GameMode.CREATIVE) {
    40. p.setScoreboard(API.board);
    41. e.setJoinMessage("§9"+p.getDisplayName()+" §ejoined the game§6 (ADMIN)");
    42. }
    43. }
    44.  
    45. }

    There is really not that much more to it :/.

    Nevermind Zaaryus fixed it already I just oversaw the reply with the Lore thingy. I guess you can't use existing Lore because using a new ArrayList instead of getting the existing lore fixed it for me. Thanks for helping :).

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  9. Offline

    Zaaryus

    Okay! Good that you fixed it, good luck with your plugin! :)
    BTW: Remember to mark as solved when it's solved! ;)
     
Thread Status:
Not open for further replies.

Share This Page