Item onJoin

Discussion in 'Plugin Development' started by EvilKittyCat123, Nov 23, 2013.

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

    EvilKittyCat123

    Hey guys,
    I have a little problem. I want players to receive a nether star every time they join my server. And if they already have a nether star, they wouldn't get one either. How do I do this? I tried starting out by giving players the item when they join, but its not working at all. Nothing happens when they join, no errors in console or eclipse. Please help me, here is my code:
    Code:
     @EventHandler
            public void onPlayerJoin(PlayerJoinEvent evt) {
                evt.getPlayer();
                evt.getPlayer().getItemInHand().setType(Material.NETHER_STAR);
                }
     
  2. 1. Did you register your Listener (probably not)?
    2. Don't do it the way you are doing it, first check if the inventory already contains a nether star (inventory.contains) and if it doesn't try to add the item to the inventory and maybe drop it on the floor if the inventory is full. The way you are doing it now the item the player originally had in his hand is going to be deleted.
    Also you might want to assign the return of evt.getPlayer() to a variable.
     
  3. Offline

    user_90854156

    Registering the event:
    Code:java
    1. public void onEnable() {
    2. Bukkit.getServer().getPluginManager().registerEvents(this, this);
    3. }

    The Event:
    Code:java
    1. @EventHandler
    2. public void onPlayerJoin(PlayerJoinEvent evt) {
    3. Player p = (Player) evt.getPlayer(); //Remember to declare the player
    4. if (p.getInventory().contains(Material.NETHER_STAR)) { //Check the inventory for the item
    5. return;
    6. } else {
    7. p.setItemInHand(new ItemStack(Material.NETHER_STAR)); //Gives item in your hand
    8. }
    9. }


    Full code: http://pastebin.com/H0c4hkLm
     
  4. Offline

    EvilKittyCat123

    It still doesn't work, I tried both of your ways, but none of them works. No errors in eclipse, and no errors in console. Here is my full code:

    Code:java
    1. package me.stripaminer.HippieGames;
    2.  
    3. import java.util.logging.Logger;
    4.  
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.Material;
    8. import org.bukkit.command.Command;
    9. import org.bukkit.command.CommandSender;
    10. import org.bukkit.entity.Player;
    11. import org.bukkit.event.EventHandler;
    12. import org.bukkit.event.Listener;
    13. import org.bukkit.event.player.PlayerJoinEvent;
    14. import org.bukkit.inventory.ItemStack;
    15. import org.bukkit.plugin.PluginDescriptionFile;
    16. import org.bukkit.plugin.java.JavaPlugin;
    17.  
    18. public class HippieGames extends JavaPlugin implements Listener {
    19. public final Logger logger = Logger.getLogger("Minecraft");
    20.  
    21. public static HippieGames plugin;
    22.  
    23. @Override
    24. public void onDisable() {
    25. PluginDescriptionFile pdfFile = this.getDescription();
    26. this.logger.info(pdfFile.getName() + ChatColor.GOLD + " Has been disabled! ");
    27. }
    28.  
    29. @Override
    30. public void onEnable() {
    31. PluginDescriptionFile pdfFile = this.getDescription();
    32. this.logger.info(pdfFile.getName() + " Version " + pdfFile.getVersion() + ChatColor.AQUA + " Has been enabled! ");
    33. }
    34.  
    35. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args)
    36.  
    37.  
    38.  
    39. {
    40.  
    41. Player player = (Player) sender;
    42. if(commandLabel.equalsIgnoreCase("helpme"))
    43. player.sendMessage(ChatColor.YELLOW + "Welcome to "
    44. + ChatColor.AQUA + "HippieCraft's MiniGames Server"
    45. + ChatColor.YELLOW + "! "
    46. + ChatColor.YELLOW + "To get started, type "
    47. + ChatColor.AQUA + "/GetStarted"
    48. + ChatColor.YELLOW + ". For a list of our other servers, type "
    49. + ChatColor.AQUA + "/Servers"
    50. + ChatColor.YELLOW + ". For a list of our hired staff, type "
    51. + ChatColor.AQUA + "/Staff"
    52. + ChatColor.YELLOW + "."); {
    53. }
    54.  
    55.  
    56. {
    57.  
    58. //Stuff
    59.  
    60. }
    61.  
    62. Player player1 = (Player) sender;
    63. if(commandLabel.equalsIgnoreCase("getstarted"))
    64. player1.sendMessage(ChatColor.YELLOW + "===================================================== "
    65. + ChatColor.LIGHT_PURPLE + " Getting started: "
    66. + ChatColor.AQUA + " To get started, right click while holding your compass, and choose a minigame you would like to play. "
    67. + ChatColor.AQUA + "Once you've teleported to the minigame you would like to play, read the signs infront of you. "
    68. + ChatColor.AQUA + "You will get 1 item each time you login. To remove them, type "
    69. + ChatColor.LIGHT_PURPLE + "/Clearme"
    70. + ChatColor.AQUA + ChatColor.ITALIC + ". Now you are ready to play some epic MinGames! "
    71. + ChatColor.RED + ChatColor.ITALIC + "If you find any bugs/glitches, please report them on the forum! "
    72. + ChatColor.AQUA + ChatColor.BOLD + "("
    73. + ChatColor.YELLOW + ChatColor.BOLD + " [url="http://www.HippieGaming.Enjin.com"]www.HippieGaming.Enjin.com[/url] "
    74. + ChatColor.AQUA + ChatColor.BOLD + ") "
    75. + ChatColor.YELLOW + "=====================================================");
    76.  
    77.  
    78.  
    79. {
    80.  
    81. //Stuff
    82.  
    83. }
    84. Player player2 = (Player) sender;
    85. if(commandLabel.equalsIgnoreCase("clearme"))
    86. player2.sendMessage(ChatColor.YELLOW + "You have successfully deleted all of your items! "
    87. + ChatColor.AQUA + ChatColor.BOLD + "Relogg to receive another helpful item.");
    88. if(commandLabel.equalsIgnoreCase("clearme"))
    89. player2.getInventory().clear();
    90.  
    91. {
    92.  
    93. //Stuff
    94.  
    95. }
    96.  
    97. Player player3 = (Player) sender;
    98. if(commandLabel.equalsIgnoreCase("staff"))
    99. player3.sendMessage(ChatColor.YELLOW + "====================================================="
    100. + ChatColor.DARK_AQUA + ChatColor.BOLD + "["
    101. + ChatColor.GOLD + "Owner"
    102. + ChatColor.DARK_AQUA + ChatColor.BOLD + "]"
    103. + ChatColor.AQUA + " Stripaminer "
    104. + ChatColor.GREEN + ChatColor.BOLD + "["
    105. + ChatColor.AQUA + "Admin"
    106. + ChatColor.GREEN + ChatColor.BOLD + "] "
    107. + ChatColor.RED + "Sss211 "
    108. + ChatColor.YELLOW + ChatColor.BOLD + "["
    109. + ChatColor.DARK_PURPLE + "Moderator"
    110. + ChatColor.YELLOW + ChatColor.BOLD + "] "
    111. + ChatColor.GOLD + "Marwan400 "
    112. + ChatColor.YELLOW + "=====================================================");
    113. {
    114.  
    115. //Stuff
    116.  
    117. }
    118.  
    119. Player player4 = (Player) sender;
    120. if(commandLabel.equalsIgnoreCase("servers"))
    121. player4.sendMessage(ChatColor.YELLOW + "===================================================== "
    122. + ChatColor.GOLD + " Check out our other servers: "
    123. + ChatColor.AQUA + "Survival Server: "
    124. + ChatColor.YELLOW + ChatColor.BOLD + "HippieCraft.sundboe.com "
    125. + ChatColor.GREEN + "Creative Server: "
    126. + ChatColor.YELLOW + ChatColor.BOLD + "HippieCraft.sundboe.com:32000 "
    127. + ChatColor.YELLOW + "=====================================================");
    128. return false;
    129.  
    130.  
    131.  
    132. }
    133.  
    134. {
    135.  
    136. }
    137. public void onEnable1() { //Registers the event.
    138. System.out.println("Swage test");
    139. Bukkit.getServer().getPluginManager().registerEvents(this, this);
    140. }
    141.  
    142. @EventHandler
    143. public void onPlayerJoin(PlayerJoinEvent evt) {
    144. Player p = (Player) evt.getPlayer(); //Remember to declare the player
    145. if (p.getInventory().contains(Material.NETHER_STAR)) { //Check the inventory for the item
    146. return;
    147. } else {
    148. p.setItemInHand(new ItemStack(Material.NETHER_STAR)); //Gives item in your hand
    149.  
    150.  
    151. }
    152. }
    153.  
    154.  
    155.  
    156.  
    157.  
    158.  
    159.  
    160.  
    161.  
    162.  
    163.  
    164.  
    165.  
    166.  
    167.  
    168.  
    169.  
    170.  
    171.  
    172.  
    173.  
    174.  
    175.  
    176.  
    177.  
    178.  
    179.  
    180.  
    181.  
    182. {
    183.  
    184.  
    185.  
    186.  
    187.  
    188.  
    189.  
    190.  
    191. }
    192. }
    193.  
    194.  
    195.  
    196.  


    And here is my plugin.yml:


    Code:
    name: HippieGames
    main: me.stripaminer.HippieGames.HippieGames
    version: 1.0
    description: >
                Manages minigames, and helps admins & normal users!
    commands:
      helpme:
        description: Shows information about HippieCraft!
      getstarted:
        description: Need help getting started? Try this command!
      clearme:
        description: Remove all of your compasses.
      staff:
        description: A list of our hired staff.
      servers:
        description: A list of our other servers.
     
  5. You still didn't register the listener. Add this to your onEnable:
    Code:java
    1. Bukkit.getServer().getPluginManager().registerEvents(this, this);
     
  6. Offline

    Aperx

    A more simpler way of adding the nether start only when they don't have one is
    Code:java
    1. @EventHandler
    2. public void onPlayerJoin(PlayerJoinEvent evt) {
    3. Player p = (Player) evt.getPlayer();
    4. if (!p.getInventory().contains(Material.NETHER_STAR)) { //If player Does not have nether star, then add one, if they do, do nothing
    5. p.setItemInHand(new ItemStack(Material.NETHER_STAR));
    6. }
    7. }


    EDIT: Why are you using two onEnables? :confused:
    Just use one
     
  7. Offline

    EvilKittyCat123


    Thank you so much! it works now!

    Now for my next step in my plugin development, how do I add custom name & lore on this item that they would get?

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

    hamzaxx

    EvilKittyCat123 Next time please please read the documentation... http://wiki.bukkit.org/Event_API_Reference
     
  9. Offline

    EvilKittyCat123

    Okay, so I tried to add the lores and names myself, I only have 1 error. "Item cannot be resolved"
    Heres my code for that one:

    Code:java
    1. }
    2.  
    3. {
    4.  
    5. }
    6.  
    7.  
    8. @EventHandler
    9. public void onPlayerJoin(PlayerJoinEvent evt) {
    10. Player p = (Player) evt.getPlayer(); //Remember to declare the player
    11. if (p.getInventory().contains(Material.NETHER_STAR)) { //Check the inventory for the item
    12. return;
    13. } else {
    14. p.setItemInHand(new ItemStack(Material.NETHER_STAR)); //Gives item in your hand
    15. ItemMeta im = item.getItemMeta();
    16. im.setDisplayName(ChatColor.YELLOW + "" + ChatColor.BOLD + "displayName");
    17. setLore(Arrays.asList(ChatColor.AQUA + "Right click to hide all players!"));
    18.  
    19.  
    20.  
    21. }
    22.  
    23. //Stuff
    24.  
    25. }
    26.  
    27.  
    28.  
    29. private void setLore(List<String> asList) {
    30. // TODO Auto-generated method stub
    31.  
    32. }
    33.  
    34. {
    35.  
    36. {
    37. }
    38. }
    39. }
    40.  
     
  10. Offline

    CreeepyFace

    @EvilKittyCat, you dont have item stack.

    Do
    ItemStack item = new ItemStack(Material.NETHER_STAR);

    Example:
    Code:java
    1. @EventHandler
    2. public void onPlayerJoin(PlayerJoinEvent evt) {
    3. Player p = (Player) evt.getPlayer(); //Remember to declare the player
    4. if (p.getInventory().contains(Material.NETHER_STAR)) { //Check the inventory for the item
    5. return;
    6. } else {
    7. ItemStack item = new ItemStack(Material.NETHER_STAR);
    8. ItemMeta im = item.getItemMeta();
    9. im.setDisplayName(ChatColor.YELLOW + "" + ChatColor.BOLD + "displayName");
    10. im.setLore(Arrays.asList(ChatColor.AQUA + "Right click to hide all players!"));
    11. item.setItemMeta(im);
    12. p.setItemInHand(item); //Gives item in your hand
    13.  
    14.  
    15.  
    16. }
    17.  
    18. //Stuff
    19.  
    20. }
    21.  


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
Thread Status:
Not open for further replies.

Share This Page