On player join event?

Discussion in 'Plugin Development' started by mrpoopy345, Aug 6, 2014.

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

    mrpoopy345

    I am trying to give a player an un droppable snowball when he joins the server, and no matter what I do it doesn't work!

    Code:
    Code:java
    1. package me.mrpoopy;
    2.  
    3. import org.bukkit.Material;
    4. import org.bukkit.event.EventHandler;
    5. import org.bukkit.event.Listener;
    6. import org.bukkit.event.player.PlayerDropItemEvent;
    7. import org.bukkit.event.player.PlayerJoinEvent;
    8. import org.bukkit.inventory.ItemStack;
    9. import org.bukkit.plugin.java.JavaPlugin;
    10.  
    11. public class L1 extends JavaPlugin implements Listener{
    12. @EventHandler
    13. public void onPlayerDropItem(PlayerDropItemEvent event){
    14. if(event.getItemDrop().getItemStack().getType() == Material.SNOW_BALL);
    15. event.setCancelled(true);
    16. }
    17. public void onJoin(PlayerJoinEvent e){
    18. e.getPlayer().getInventory().addItem(new ItemStack(Material.SNOW_BALL, 1));
    19. }
    20. }


    Any help?
     
  2. Offline

    Fhbgsdhkfbl

    Well for one, you don't have your events registered in an onEnable
     
  3. Secondly, you haven't got an @EventHandler for onJoin, you only have one for EventHandler. By the way, I'm guessing that's not your main class? If so, it should not extend JavaPlugin. Only your Main class should extend JavaPlugin. In your onEnable() (if you don't have one - make one!) - register the event using either Condition 1 or Condition 2 depending on your condition.

    [1] L1 is not your main class:
    this.getServer().getPluginManager().registerEvents(new L1(), this);

    [2] L1 is your main class:
    this.getServer().getPluginManager().registerEvents(this, this);
     
  4. Offline

    Garris0n

    Thirdly, you don't seem to understand how if statements work.
     
    KingFaris11 likes this.
  5. Offline

    number1_Master

    Fourthly, you do not seem to follow general programming conventions of consistent variable names, spacing, or general neatness.

    Other than that, I don't think the 4 of us missed anything?
     
    KingFaris11 likes this.
  6. Offline

    Garris0n

    number1_Master Well, I don't personally like using == on enums, but it's not necessarily wrong.
     
  7. Fifthly, your package name is a bit weird. Name it something like a domain backwards. E.g. com.faris.<plugin>

    Would actually be: <plugin>.faris.com - which makes sense.

    You can also use .me, etc.
     
  8. Offline

    mrpoopy345

    My onEnable is now

    Code:java
    1. public void onEnable()
    2. {
    3. System.out.println("[ZyanGUI] Enabled!");
    4. this.getServer().getPluginManager().registerEvents(new L1(), this);
    5. Bukkit.getPluginManager().registerEvents(new Listener()
    6.  
    7. {


    And I get the error that the plugin is already initialized in the console. Any help with that?

    READ AHEAD ONLY IF YOU HAVE POSTED ON THIS THREAD BEFORE
    ---------------------------------------------------------------------------------------------------------------------------------
    Also, I think you guys are being a bit dickish. Yeah, I posted this thread for help, not for a few people to laugh at me for my lack of knowledge in bukkit. KingFaris actually helped me, and thank you for that, but the rest of you did pretty much nothing.


    Please elaborate a bit.
    number1_Master I guess you kind of helped me...
    but why couldn't you, again, elaborate a bit?
    And xAstraah, you, my friend, are a douchbag.

    You have to understand that some people aren't coding wizards like you, and not beat on them for that.
    Real self confidence booster here guys. :p Thanks.
    ---------------------------------------------------------------------------------------------------------------------------------
     
  9. Offline

    xAstraah

    mrpoopy345, In all fairness you should know the basics of Java before attempting to program plugins,
    Learn what Assignment is maybe xD.

    (Most people on Bukkit have at least a year's programming experience. And if you asked this question in the Bukkit IRC they would probably tell you the same thing i am.)
     
    KingFaris11 likes this.
  10. My suggestion, watch a video about this.
    Or visit the developpers site from bukkit!
    http://jd.bukkit.org/
     
  11. Offline

    Necrodoom

    mrpoopy345 people have told you what you did wrong, and that you need to correct them.
    You are complaining that people have pointed out your mistakes in the file, saying that you don't have Java knowledge, when in fact you must have Java knowledge before trying to code plugins.
     
  12. Offline

    number1_Master

    mrpoopy345 Writing need code is a large part of programming in many languages. I only commented as Faris linked me to this thread, and I did not know what else to add. Even though I might have not been terribly helpful, I personally take a lot of time (sometimes more than I have to) to write fairly legible code. Writing perfect score essay is the same as writing perfectly understandable code. Sure you might be doing something wrong, but that is due to the topic of the essay, not the written words themselves.
     
  13. Offline

    Garris0n

    Point taken, go read through all of this.
     
  14. Offline

    KaitouKidFTW

    mrpoopy345 What is your full code at the moment?
     
  15. Offline

    mrpoopy345

    Ok, full code, as it is.

    Main file:

    Code:java
    1. public void onEnable()
    2. {
    3. System.out.println("[ZyanGUI] Enabled!");
    4. this.getServer().getPluginManager().registerEvents(new L1(), this);
    5. Bukkit.getPluginManager().registerEvents(new Listener()
    6.  
    7. {
    8.  
    9. @SuppressWarnings("deprecation")
    10. @EventHandler
    11. public void GuiShortCut(PlayerInteractEvent eve)
    12. {
    13. Player player = eve.getPlayer();
    14. if (player.getItemInHand().getType() != Material.SNOW_BALL && (eve.getAction() == Action.RIGHT_CLICK_BLOCK))
    15. {
    16. Player p = eve.getPlayer();
    17. ItemStack slot0 = new ItemStack(Material.BOOK, 1);
    18. ItemStack slot1 = new ItemStack(Material.BOOK, 1);
    19. ItemStack slot2 = new ItemStack(Material.BOOK, 1);
    20. ItemStack slot3 = new ItemStack(Material.BOOK, 1);
    21.  
    22.  
    23.  
    24.  
    25. ItemMeta meta0 = slot0.getItemMeta();
    26.  
    27. ItemMeta meta1 = slot1.getItemMeta();
    28. ItemMeta meta2 = slot2.getItemMeta();
    29. ItemMeta meta3 = slot3.getItemMeta();
    30.  
    31.  
    32. meta0.setDisplayName(ChatColor.GOLD + "Go to Block A!");
    33. meta1.setDisplayName(ChatColor.GOLD + "Go to the shop!");
    34. meta2.setDisplayName(ChatColor.GOLD + "Go to plots");
    35. meta3.setDisplayName(ChatColor.GOLD + "See what perks you can get from donating!");
    36.  
    37.  
    38.  
    39.  
    40. slot0.setItemMeta(meta0);
    41. slot1.setItemMeta(meta1);
    42. slot2.setItemMeta(meta2);
    43. slot3.setItemMeta(meta3);
    44.  
    45.  
    46. Inventory x104 = Bukkit.createInventory(null, 9, "Custom Menu");
    47.  
    48.  
    49. x104.setItem(0, slot0);
    50. x104.setItem(1, slot1);
    51. x104.setItem(2, slot2);
    52. x104.setItem(3, slot3);
    53.  
    54.  
    55.  
    56.  
    57.  
    58.  
    59. p.openInventory(x104);
    60. eve.setCancelled(true);
    61. }
    62. if (eve.getItem().getTypeId() == 332);
    63. {
    64. Player p = eve.getPlayer();
    65.  
    66. ItemStack slot0 = new ItemStack(Material.DIAMOND_PICKAXE, 1);
    67. ItemStack slot1 = new ItemStack(Material.CHEST, 1);
    68. ItemStack slot2 = new ItemStack(Material.DIRT, 1);
    69. ItemStack slot3 = new ItemStack(Material.DIAMOND, 1);
    70.  
    71.  
    72.  
    73.  
    74. ItemMeta meta0 = slot0.getItemMeta();
    75.  
    76. ItemMeta meta1 = slot1.getItemMeta();
    77. ItemMeta meta2 = slot2.getItemMeta();
    78. ItemMeta meta3 = slot3.getItemMeta();
    79.  
    80.  
    81. meta0.setDisplayName(ChatColor.DARK_GREEN + "Go to Prison Block A!");
    82. meta1.setDisplayName(ChatColor.AQUA + "Go to the shop!");
    83. meta2.setDisplayName(ChatColor.DARK_RED + "Go to plots!");
    84. meta3.setDisplayName(ChatColor.BLUE + "See all the perks that donators get!");
    85.  
    86.  
    87.  
    88.  
    89. slot0.setItemMeta(meta0);
    90. slot1.setItemMeta(meta1);
    91. slot2.setItemMeta(meta2);
    92. slot3.setItemMeta(meta3);
    93.  
    94.  
    95. Inventory x104 = Bukkit.createInventory(null, 9, "Game Menu");
    96.  
    97.  
    98. x104.setItem(0, slot0);
    99. x104.setItem(1, slot1);
    100. x104.setItem(2, slot2);
    101. x104.setItem(3, slot3);
    102.  
    103.  
    104.  
    105.  
    106.  
    107.  
    108.  
    109.  
    110.  
    111. p.openInventory(x104);
    112. eve.setCancelled(true);
    113. }
    114. }
    115.  
    116. @EventHandler
    117. public void ClickListener(InventoryClickEvent cevent)
    118. {
    119. Player por = (Player)cevent.getWhoClicked();
    120. if (cevent.getInventory().getName() == "Custom Menu")
    121. {
    122. if (cevent.getSlot() == 0)
    123. {
    124. Player pl = (Player)cevent.getWhoClicked();
    125. pl.closeInventory();
    126. pl.chat("/warp a");
    127. }
    128. if (cevent.getSlot() == 1)
    129. {
    130. Player pl = (Player)cevent.getWhoClicked();
    131. pl.closeInventory();
    132. pl.chat("/warp shop");
    133. }
    134. if (cevent.getSlot() == 2)
    135. {
    136. Player pl = (Player)cevent.getWhoClicked();
    137. pl.closeInventory();
    138. pl.chat("/warp plots");
    139. }
    140. if (cevent.getSlot() == 3)
    141. {
    142. Player pl = (Player)cevent.getWhoClicked();
    143. pl.closeInventory();
    144. pl.chat("/warp donate");
    145. }
    146. }
    147. if (cevent.getInventory().getName() == "Game Menu")
    148. {
    149. if (cevent.getSlot() == 0)
    150. {
    151. Player pl = (Player)cevent.getWhoClicked();
    152. pl.closeInventory();
    153. pl.sendMessage(ChatColor.ITALIC + "You have been teleported to Block A!");
    154. pl.chat("/warp donate");
    155. System.out.println("[ZyanGUI]" + pl.getDisplayName() + " issued the BLOCK-A Command!");
    156. }
    157. if (cevent.getSlot() == 1)
    158. {
    159. Player pl = (Player)cevent.getWhoClicked();
    160. pl.closeInventory();
    161. pl.sendMessage(ChatColor.ITALIC + "You have been teleported to the shop!");
    162. pl.chat("/warp shop");
    163. System.out.println("[ZyanGUI]" + pl.getDisplayName() + " issued the SHOP Command!");
    164. }
    165. if (cevent.getSlot() == 2)
    166. {
    167. Player pl = (Player)cevent.getWhoClicked();
    168. pl.closeInventory();
    169. pl.sendMessage(ChatColor.ITALIC + "You have been teleported to plots!");
    170. pl.chat("/warp plots");
    171. System.out.println("[ZyanGUI]" + pl.getDisplayName() + " issued the PLOTS Command!");
    172. }
    173. if (cevent.getSlot() == 3)
    174. {
    175. Player pl = (Player)cevent.getWhoClicked();
    176. pl.closeInventory();
    177. pl.chat("/warp donate");
    178. System.out.println("[ZyanGUI]" + pl.getDisplayName() + " issued the DONATE Command!");
    179. }
    180. }
    181. }
    182.  
    183.  
    184. }, this);
    185. }
    186. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    187. Player player = (Player)sender;
    188. String name = sender.getName();
    189. String command = cmd.getName();
    190. if (command.equalsIgnoreCase("prefix")) {
    191. if (args.length==0) {
    192. player.sendMessage(ChatColor.RED + "Wrong usage: /prefix <player> prefix");
    193. return false;
    194. }
    195. if (args.length==1)
    196. Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "manuaddv " + name + " prefix " + args[0]);
    197. player.sendMessage("manuaddv " + name + " prefix " + args[0]);
    198. return true;
    199. }
    200. else {
    201. Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "manuaddv " + args[0] + " prefix " + args[1]);
    202. player.sendMessage("manuaddv " + args[0] + " prefix " + args[1]);
    203. return true;
    204. }
    205. }
    206.  
    207. public boolean onCommand1(CommandSender sender, Command cmd, String label, String[] args) {
    208. Player player = (Player)sender;
    209. String command = cmd.getName();
    210. if (command.equalsIgnoreCase("suffix")) {
    211. Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "manuaddv " + player + " suffix " + args);
    212. }
    213.  
    214. return false;
    215. }
    216.  
    217. public void onDisable()
    218. {
    219. System.out.println("[ZyanGUI]: Plugin Disabled...");
    220. }
    221. }
    222.  


    L1:

    Code:java
    1. package me.mrpoopy;
    2.  
    3. import org.bukkit.Material;
    4. import org.bukkit.event.EventHandler;
    5. import org.bukkit.event.Listener;
    6. import org.bukkit.event.player.PlayerDropItemEvent;
    7. import org.bukkit.event.player.PlayerJoinEvent;
    8. import org.bukkit.inventory.ItemStack;
    9. import org.bukkit.plugin.java.JavaPlugin;
    10.  
    11. public class L1 extends JavaPlugin implements Listener{
    12. @EventHandler
    13. public void onPlayerDropItem(PlayerDropItemEvent event){
    14. if(event.getItemDrop().getItemStack().getType() == Material.SNOW_BALL);
    15. event.setCancelled(true);
    16. }
    17. @EventHandler
    18. public void onJoin(PlayerJoinEvent event){
    19. event.getPlayer().getInventory().addItem(new ItemStack(Material.SNOW_BALL, 1));
    20. }
    21. }
     
  16. Offline

    KaitouKidFTW

    mrpoopy345 So, you are trying to stop them from dropping snowball correct?
     
  17. mrpoopy345

    Are you finding it hard to read?

    - Garrison2k14

    You use:
    Code:
    if (<boolean>) {
        event.setCancelled(true);
    }
    
     
  18. Offline

    KaitouKidFTW

    mrpoopy345
    Code:java
    1. if("Player drops a snowball"){ //Open curly bracket
    2.  
    3. // Cancel the event
    4. // Send a message if you want
    5.  
    6. } //Close curly bracket
     
  19. Offline

    mrpoopy345

    Can you give some code?
     
  20. Offline

    xAstraah

    mrpoopy345, Spoon feeding you will never help, this is also some of the basics.
     
  21. Offline

    KaitouKidFTW

    mrpoopy345 Your code is correct. All you have to do is have a curly bracket on your 'if' statement
     
  22. xAstraah
    I myself was unable to take any java classes, and I too started to learn java through bukkit.
    Yes, having java knowledge makes using java easier. But having something like bukkit to give it form, I think helped me and helps others to understand how java works, and how what they write effects the outcome.
    I applaud this guy. He had no idea how to do something, and he wanted to fix that. He jumped right in and fought to understand and fix it. This attitude may not always lead to success, but it certainly does lead to knowing more about everything than just sticking to what you know. And knowledge is always the main goal, if not the enjoyment of the process.
    Don't discourage someone for starting out with less knowledge than you. For all you know, he may progress to the point where he will be answering your questions as if they were obvious. And I believe he can get there.
    People always have problems. You do too. The fact that the problems he currently faces are understood by you means nothing. Nothing. Nothing.


    mrpoopy345
    Don't give up. Starting out is always tough. It doesn't matter what it is. Starting out is tough. But it doesn't last long. And pretty soon, the problems that confuse you now, you will have faced a thousand time over, and you will no longer look at them as a problem at all.
     
    Flamedek likes this.
  23. Offline

    LordVakar

    I have not replied to this thread before, but if you have never looked around in bukkit forums, you find in almost every thread in this development section, people act "dickish".
     
    xAstraah likes this.
  24. Offline

    xAstraah

    xYourFreindx, Yes diving into Bukkit head first can be helpful and i think alot of great developer's did it like that and when you get into coding stuff like NMS or MiniGames you really start to learn alot more about OOP. But for the basics for Events and Event Handlers etc he could have easily YouTube'd a plugin tutorial and learn't this in 5 minutes. This is not aimed to be hurtful but to encourage him to search around before making a post on a forum.

    EDIT: Most people on Bukkit IRC / Forums have really good Java knowlegdge and don't want to help them because it is like speaking to a brick wall. So if someone doesn't know the BareBones of Java and then someone is like Are you getting a NPE in your stack trace the other person will not understand and then making it 50% Harder to help them with their problems.


    mrpoopy345 that video should push you up half the hill to success for the plugin you are making.

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

Share This Page