Developing a small plugin for learning purposes, what's wrong?

Discussion in 'Plugin Development' started by JayHerlth, Feb 3, 2014.

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

    JayHerlth

    Only assumption I can make is that a tool is not a proper PlayerItemBreakEvent, as a grass block BEING broken would be proper, rather a tool being broken by the use of the player. Is there a way to get around this or a way already put into place for things such as tools being broken?

    Code:java
    1. package me.jayherlth.splinters;
    2.  
    3. import org.bukkit.Material;
    4. import org.bukkit.entity.Player;
    5. import org.bukkit.event.EventHandler;
    6. import org.bukkit.event.player.PlayerItemBreakEvent;
    7. import org.bukkit.plugin.java.JavaPlugin;
    8.  
    9. public class Splinters extends JavaPlugin {
    10. @Override
    11. public void onEnable() {
    12. getLogger().info("Splinters is functioning properly.");
    13. }
    14.  
    15. @EventHandler
    16. public void toolBreak(PlayerItemBreakEvent e) {
    17. Player p = e.getPlayer();
    18. if(e.getBrokenItem().getType().equals(Material.WOOD_PICKAXE)) {
    19. p.sendMessage("This wood pick gave you a splinter!");
    20. }
    21. }
    22. }


    Eventually I plan on extending the plugin to deal damage to that player (1 heart for wood tools, 2 for stone, 3 iron, so on and so forth. The higher level the tool, the more risk you have of it breaking.) think about it, if you're using a wooden stick and it breaks while you're using it you'll most likely get a cut. If you're using aluminum or something else of the sort for a tool and it breaks you risk having a nice sized gash. It's more or less a plugin to say "Hey, we're going to be mean and when your nice tool breaks when you're in a life or death situation and you need to get out of that place or kill that zombie with an almost broken pick or sword you might just die from it. That's life."
     
  2. Offline

    Windy Day

    The event looks all good, you just need to register it.
     
  3. Offline

    JayHerlth

    What do you mean by registering it? I'm so confused right now.
    Right now, my expectations are the listener should trigger when the pick breaks, then tell the player when the pick broke that they had received a splinter.
     
  4. Offline

    shawnGreene()

    There are two things you must do. Where you declare you class you must also add "implements Listener". The other thing is that you must do is register the event you want to pass through.
    Code:java
    1. package me.jayherlth.splinters;
    2.  
    3.  
    4.  
    5. import org.bukkit.Material;
    6. import org.bukkit.entity.Player;
    7. import org.bukkit.event.EventHandler;
    8. import org.bukkit.event.player.PlayerItemBreakEvent;
    9. import org.bukkit.plugin.java.JavaPlugin;
    10.  
    11. public class Splinters extends JavaPlugin implements Listener{
    12. @Override
    13. public void onEnable() {
    14. getLogger().info("Splinters is functioning properly.");
    15. getServer().getPluginManager().registerEvents(new Splinters(),true);
    16. }
    17.  
    18. @EventHandler
    19. public void toolBreak(PlayerItemBreakEvent e) {
    20. Player p = e.getPlayer();
    21. if(e.getBrokenItem().getType().equals(Material.WOOD_PICKAXE)) {
    22. p.sendMessage("This wood pick gave you a splinter!");
    23. }
    24. }
    25.  
    26. }


    It may be easier to put each Listener event inside its own class however so you can manage it easier.
     
  5. Offline

    RebzO1

    for learning purposes this could be fun

    how would you change that to a diamond sword and when it breaks it damages/kills the layer with message saying fell on their sword

    or even more involved

    lets say a 5% chance that they fall on their sword.

    is that possible?

    this is the second time i barged in on some1's thread so i apologize but this could be turned into something fun
     
  6. Offline

    RawCode

    learning plugins looks a bit different -lots of debug messages everywhere!
     
  7. Offline

    JayHerlth

    That actually sounds pretty awesome! I'm currently writing it all down on paper, I think with a few cases I could get it to change material types. Build a random number generator and pull numbers from that to generate a true or false statement saying they had fallen from their sword. Generate numbers from 1-20 and only one number would result true then killing the player or just taking half their current health because they "fell" on their sword so not too many people get irritated.
     
    RebzO1 likes this.
  8. Offline

    Jake6177

    Register the event and make your class implement the Listener class.
     
  9. Offline

    JayHerlth

    Could you please explain by registering an event? I've looked around in some references and can't quite grasp a good hook on how exactly to go about registering an event..
     
  10. Offline

    The_Doctor_123

    Just to verify, you know Java?
     
  11. Offline

    Captain Dory

    Add this into your onEnable():
    Code:java
    1. Bukkit.getServer().getPluginManager.registerEvents(this,this);


    My god, you've really got to stop doing this. Lots of people sometimes fail, myself included a lot of times. Just because a developer forgets a small thing, it doesn't mean they are inexperienced with java.
    rant.over
     
  12. Offline

    The_Doctor_123

    Captain Dory
    I didn't even look at the code. The title of this topic just made me ask myself "Does he mean Java or the Bukkit API?" That's why I said "Just to verify."
     
  13. Offline

    Captain Dory

    Oh, that makes more sense. Could have been worded differently though. Sorry if you took what i said as disrespect :p
     
  14. Offline

    RebzO1

    JayHerlth liked and watching this thread, i am also going to attempt this you have given the groundwork for a great plugin idea, im sure its been done before but i dont care lol.

    being new at this and no prior coding experience before 9 days ago ill probably find this a tough nut to crack.
     
  15. Offline

    JayHerlth

    @RebzO1 I had started a plugin project for it if you'd like to learn and contribute alongside with me?

    You can check it out here, http://dev.bukkit.org/bukkit-plugins/splinters/ and once I get the foundation built for it I will start a git repository and start uploading versions and further developing it.

    EDIT/UPDATE: Plugin is on its knees crawling, successfully registered the event & time to build a damage system and event system for the different tools :) [​IMG]
     
  16. Offline

    RebzO1

    i am just starting out so i don't know how much help i would/could be, but ill have a stab at it
     
  17. Offline

    JayHerlth

    I've been working on it today, I'm getting a lot of the statements down right now. Later it will be learning how to better call methods and use classes for my plugin. Not sure I need it though.
     
  18. Offline

    RebzO1

    JayHerlth
    I just learn as i go and it is going slowly lol
     
Thread Status:
Not open for further replies.

Share This Page