Solved A glitch on my plugin

Discussion in 'Plugin Development' started by MeTim, Mar 4, 2013.

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

    MeTim

    Hi everybody I have made a plugin and i have some glitches (It's a Fishing Rod plugin and it basicly teleport the player to the hook.) but if 2 players throw there hooks then it glitches I hope there is a fix because i can't fix it :(.
     
  2. Offline

    stuntguy3000

  3. Offline

    minoneer

    Well, how about posting some code and error logs? At leas I can't come up with what your code and errors look like
     
  4. Offline

    MeTim

    I have no errors but when i throw the hook of my fishing rod and my friend does it then I teleport to the hook of my friend.

    Here is my code:
    Code:
        @EventHandler(priority = EventPriority.HIGH)
     
        public void onPlayerFishingEvent(PlayerFishEvent event){
            Player p = event.getPlayer();
        Material item = p.getItemInHand().getType();
        if(item == Material.FISHING_ROD){
     
     
     
            java.util.List<Entity> nearby = p.getNearbyEntities(50,50,50);
            Entity hook = null;
            for (Entity e : nearby) {
       
                if (e.getType() == EntityType.FISHING_HOOK) {
                    hook = e;
                    break;
                }
            }
            if (hook != null) {
                Location hookLocation = hook.getLocation();
                p.getLocation();
                p.teleport(hookLocation);
                }
            }
        }
    }
    }
    }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 31, 2016
  5. Offline

    JayzaSapphire

    Can't you get the fishing hook in a different way? It appears that you are teleporting to the closest fishing rod, which would then be your friends fishing rod.
     
  6. You should instead use ProjectileHitEvent and get if entity is instanceof Fish (represents the hook) then cast to Fish and use getShooter() to get the owner and teleport him to the entity's location.
     
  7. Offline

    MeTim

    Digi I now have this code but is doesn't work:
    Code:
    public void OnFish (ProjectileHitEvent event){
        Entity e = event.getEntity();
    if(e.getType() == EntityType.FISHING_HOOK){
    Entity l = event.getEntity().getShooter(); 
            Location hookLocation = e.getLocation();
            l.teleport(hookLocation);
            }
        }
    }
     
  8. Actually, you should debug it, print some messages... paste this before any condition:
    Code:
    System.out.print("ProjectileHitEvent :: type = " + event.getEntityType());
    You should also learn to indent your code properly, it's all over the place :p
     
  9. Offline

    MeTim

    Digi Wut? Does that mean it does work? I'm very confused now :p
     
  10. How does adding a message make it work ? We need information to see if the event triggers and if it gives you the expected entity type, otherwise you're just blindly trusting it to work, which you say it does not.
     
  11. Offline

    MeTim

    Digi OK I have the code but I'm not getting messages. (I'm sorry for the late response :(.)
     
  12. Offline

    Burnett1

    Have you registered your events?
     
  13. Offline

    MeTim

    Yes and this is my full code:
    Code:
    public class MainClass extends JavaPlugin implements Listener {
     
        public final Logger logger = Logger.getLogger("Minecraft");
     
        public void onEnable() {
            Bukkit.getPluginManager().registerEvents(this, this);
            PluginDescriptionFile pdfFile = this.getDescription();
            this.logger.info("[" + pdfFile.getName() + "]" + " V"
                    + pdfFile.getVersion() + " Has Been Enabled!");
     
        }
     
        public void onDisable() {
            PluginDescriptionFile pdfFile = this.getDescription();
            this.logger.info("[" + pdfFile.getName() + "]" + " V"
                    + pdfFile.getVersion() + " Has Been Disabled!");
        }
     
        public void OnFish(ProjectileHitEvent event) {
    System.out.print("ProjectileHitEvent :: type = " + event.getEntityType());
            Entity e = event.getEntity();
            if (e.getType() == EntityType.FISHING_HOOK) {
                Entity l = event.getEntity().getShooter();
                Location hookLocation = e.getLocation();
                    l.teleport(hookLocation);
                }
            }
    }
    
     
  14. Offline

    MeTim

    Digi Changed my code but it still doesn't do what I want it to do. (It doesn't react.)
     
  15. Offline

    Burnett1

    ok this might not matter but try changing
    Code:
    Bukkit.getPluginManager
    to this.

    Code:
    this.getServer().getPluginManager()
     
  16. Offline

    MeTim

    Nope didn't matter but thanks for the help :)
     
  17. Offline

    lycano

    MeTim dont be lazy and put the Listener into a seperate class :D

    See https://github.com/Bukkit/SamplePlu...om/dinnerbone/bukkit/sample/SamplePlugin.java

    and https://github.com/Bukkit/SamplePlu...rbone/bukkit/sample/SamplePlayerListener.java

    Also dont use == use equals!

    Furthermore you can compress your listener method onFish to this.
    As you dont use e.getShooter() multiple times you can safely compress it instead of storing the object.
    You already have one reference with "e" and getLocation is probably done in teleport event when passing entity only so you dont have to do it in this event.

    (Dont forget to add the EventHandler)

    Code:
        public void onProjectileHitEvent(ProjectileHitEvent event) {
            Entity e = event.getEntity();
            if (e.getType().equals(EntityType.FISHING_HOOK)) {
                e.getShooter().teleport(e);
            }
        }
    
     
  18. That's really not required, if he has such a small code it's really pointless to split it up into more classes... it's more about personal preference on structuring.

    Again, not required, == works properly with primitives and Enums.
     
  19. Offline

    MeTim

    I have used your code but I changed something because it was giving an error I used "event.getEntity.getShooter" instead of "e.getShooter" but it still doesn't teleport me to the location of the hook.
     
  20. MeTim
    Probably because you ditched the reason why I asked you to print that message in the first place :)
    It was to first make sure that the event triggers and second to make sure that the entity type was actually what you expect, otherwise you're just relying on luck which will take you a long time until it reveals itself by luck.
     
  21. Offline

    MeTim

    Digi I didn't ditch it I'm just not getting any messages :)
     
  22. MeTim
    Then your event is not triggered and you're wasting your time focusing on the inner code of that method.

    You should post your entire current code that you tested with recently.
     
  23. Offline

    lycano

    MeTim event.getEntity().getShooter() is the same as when you would store the Entity first with Entity e = event.getEntity() and then call e.getShooter() so i would really like to know what error would be thrown.

    Digi about your statement "Not using equals" and encouraging using a comperator ... This can lead to problems as it might work in some cases but not in cases where the reference is not the same. Equals will always only compare the content and not the object vs an object.

    Also in this use-case you want to know if the type equals the other enum content and you dont want to check if a is the same as b. You would only need to compare the actual content so you dont want to use == over equals where its not needed.

    Plus using equals it will tell other ppl what you really want to do instead of using == which might lead to the impression that you need to check if a is b.

    I use equals when i want to compare the content and comperators where i do know exactly that there is no problem with it like if (count > 10) or if (foo == null).

    About "For a simple plugin you wont need to split your classes"
    This is about coding style. And i call it lazyness as a plugin may grow and you would have to do it anyways later on to use the specific listener again. With doing it your way there would be overhead by passing the whole plugin instance twice plus its not clear what you want to do in that case.

    Also it improves coding style which i think is always a good thing to communicate.

    And finally why passing everything when you would only need a small part of it?
     
  24. Offline

    MeTim

    My entire code:
    Code:
    import java.util.logging.Logger;
     
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.EntityType;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.ProjectileHitEvent;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class MainClass extends JavaPlugin implements Listener {
       
        public final Logger logger = Logger.getLogger("Minecraft");
     
        public void onEnable() {
            this.getServer().getPluginManager().registerEvents(this, this);
            PluginDescriptionFile pdfFile = this.getDescription();
            this.logger.info("[" + pdfFile.getName() + "]" + " V"
                    + pdfFile.getVersion() + " Has Been Enabled!");
     
        }
     
        public void onDisable() {
            PluginDescriptionFile pdfFile = this.getDescription();
            this.logger.info("[" + pdfFile.getName() + "]" + " V"
                    + pdfFile.getVersion() + " Has Been Disabled!");
        }
        @EventHandler
        public void onProjectileHitEvent(ProjectileHitEvent event) {
            System.out.print("ProjectileHitEvent :: type = " + event.getEntityType());
            Entity e = event.getEntity();
            if (e.getType().equals(EntityType.FISHING_HOOK)) {
                event.getEntity().getShooter().teleport(e);
            }
        }
        }
     
  25. Offline

    lycano

    MeTim arg! Sry, forget what i said about "event.getEntity() should be the same" as e.getShooter() ... You stored it as Entity not as Projectile so it would not work cause Entity does not have the method getShooter().

    So you have three options:
    1) Leave it as it is
    2) replace event.getEntity().getShooter().teleport(e) with ((Projectile) e).getShooter().teleport(e)
    3) store the Projectile in another object like you did before and use that in your teleport line.

    Again sry, i did not check the apidocs what getEntity() would return.

    Anyways, did you checked the log if the event is actually fired?
     
  26. Offline

    MeTim

    lycano Nope the event doesn't start when i fired my hook.
     
  27. Offline

    lycano

    MeTim Now that i am at home i have quickly written the plugin. The problem is that the FISHING_HOOK is not a projectile thus does not fire the event.

    You would have to listen on EntityDamageEvent or on EntityDamageByEntityEvent but then you have problem, that the rod actually does damage which i think you dont want.

    As i dont know if you can refactor the package name easily please tell me your namespace you use for your plugin i would then upload the plugin to my github profile and you can grab it from there. Hopefully learning something from it ^^
     
  28. Offline

    MeTim

    lycano If that would be possible to teleport the player to the location of the hook it would be nice :) Did you mean that the rod Here I have some code to check if the block is not air.
    Code:
    if ("Hook location here".getBlock().getRelative(0, -1,0).getType() != Material.AIR){
    namespace: me.MinerSheeps.PAFishingrod
    Nah I don't care if an entity gets damage.
     
  29. Offline

    lycano

    MeTim ah so you dont want to teleport to the entity. You want to teleport to the location of the fishing hook. Okay thats something completely different and is in fact easier than fine tuning the previous case (which should teleport someone in front of the entity instead directly into it).
     
Thread Status:
Not open for further replies.

Share This Page