{First Plugin} Doesn't work? Code help.

Discussion in 'Plugin Development' started by ZomBlade_Shadow, Dec 3, 2014.

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

    ZomBlade_Shadow

    Hi!
    So I really love coding and i'm watching tutorials and such but I want to make my first "Simple-ish" Plugin right.

    Functionality: When a Player throws another Player a snowball,they switch places (AKA Switcher)

    Code:


    package me.ZomBlade.Switcher;

    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.entity.Player;
    import org.bukkit.entity.Snowball;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.EntityDamageByEntityEvent;
    import org.bukkit.plugin.java.JavaPlugin;

    public class Main extends JavaPlugin implements Listener {


    public void onEnable() {

    getLogger().info("Switcher Enabled!");
    }


    public void onDisable() {

    getLogger().info("Switcher Disabled!");

    }



    @EventHandler


    public void onSwitcher(EntityDamageByEntityEvent e) {
    if ((e.getDamager() instanceof Snowball ) && (e.getEntity() instanceof Player)) {

    Snowball ball = (Snowball)e.getDamager();
    Player hit = (Player)e.getEntity();
    Player shooter = (Player)ball.getShooter();

    if (shooter instanceof Player) {
    Location hitloc = hit.getLocation();
    Location shooterloc = shooter.getLocation();
    shooter.teleport(hitloc);
    hit.teleport(shooterloc);
    hit.sendMessage(ChatColor.BLUE + "You have been switched!");
    }

    }

    }
    }
    There are no errors what so ever.Eclipse shows that everything isn't badly coded (ex: missing ; or },ect)
    My YML File:
    name: Switcher
    main: me.ZomBlade.Switcher.Main
    version: 1.0
    commands:
    Very basic.
    So what's the problem?
    when I do /pl there's the plugin.
    when I throw a snowball on another player,nothing happens at all.
    I'm a complete 3-day starter noob so bare with me please :)
    Thx.
     
  2. Offline

    DeadlyScone

    ZomBlade_Shadow
    you should also create a new class and implement your listener, then listen to the entitydamagebyentity event there.

    This just cleans things up a little :p
     
  3. Offline

    teej107

    DeadlyScone That doesn't matter when the code is executed at runtime. As Assist said, its a lack of registering events.
     
  4. Offline

    ZomBlade_Shadow

    Thanks guys!
    Great community,gonna see what I can do :D

    [EDIT]

    Can't manage to understand what/how to do.
    Can somebody just send me how to do it real quick?
    Just 1 example (for this plugin) will be enough so I understand.
    Thanks for still bearing with me :3

    [RENOOBYEDIT]

    OHMAGAAD!
    GOT IT!
    had to put
    getServer().getPluginManager().registerEvents(this, this);
    in public void onEnable();
    Thx to all for the information!
     
  5. Offline

    DeadlyScone

    teej107 seems you misunderstood what I said. I was not saying that was the issue. It was a optional suggestion.

    ZomBlade_Shadow
    It should be covered in any plugin tutorial. I would give you the code, but I am on my phone atm.
     
    teej107 likes this.
  6. Offline

    MajorSkillage

    DeadlyScone
    To register a listener use this onEnable()
    getServer().getPluginManager().registerEvents(this, this);

    don't get in the habit of using a listener in the same main class
     
  7. Offline

    Deleted user

    MajorSkillage

    It depends on what you're making. In OP's case I would never recommend making a new class for a listener. There's no point
     
  8. Offline

    coasterman10

    There is no point in making a new class for listeners unless you have a complex enough plugin that it makes sense to thematically split your code into multiple listener classes and encapsulate relevant data into that listener. In many cases, creating a new listener especially just for one event actually breaks down object-orientation by forcing the usage of getters and setters.
     
    Assist and teej107 like this.
  9. Offline

    ZomBlade_Shadow

    Whats the difference between making a new class for a listener and putting the listener in the same class as your code?
     
  10. Offline

    timtower Administrator Administrator Moderator

    ZomBlade_Shadow There isn't much.
    Maybe readability, but performance wise: nothing
     
    ZomBlade_Shadow likes this.
  11. Offline

    ZomBlade_Shadow

    timtower ohhh
    Getting the maximum information :)
    thx!
     
    es359 likes this.
  12. Offline

    mine-care

    Player shooter = (Player)ball.getShooter();

    if (shooter instanceof Player) {
    Location hitloc = hit.getLocation();
    This will throw you an error most likely. Read a bit about casting.
     
Thread Status:
Not open for further replies.

Share This Page