Solved @Eventhandler Trying to create a wand problems

Discussion in 'Plugin Development' started by XINERA, Jun 26, 2015.

Thread Status:
Not open for further replies.
  1. So the problem is that when i hold over event.getEgg (Line 53 PlayerListener Class) it says i have to create a new variable onEggThrowEvent but i don't wan't that i wan't it to search for the impact location of the egg projectile i shot by right clicking air with a blaze rod. I appreciate all help i get because i wan't to know new stuff as i just started coding java plugins yesterday.

    This is the PlayerListener Code
    Code:
    package me.Xinera;
    
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.World;
    import org.bukkit.entity.Egg;
    import org.bukkit.entity.Fireball;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerBucketFillEvent;
    import org.bukkit.event.player.PlayerEggThrowEvent;
    import org.bukkit.event.player.PlayerInteractEvent;
    
    public class PlayerListener implements Listener {
    
        public PlayerListener(EventHandle plugin) {
            plugin.getServer().getPluginManager().registerEvents(this, plugin);
        }
    
        @EventHandler
        public void onThrow(PlayerEggThrowEvent e) {
     
            Player player = e.getPlayer();
     
            player.sendMessage(ChatColor.YELLOW + "EGGS EGGS!");
            player.setLevel(100);
     
        }
    
        @EventHandler
        public void onThrow1(PlayerBucketFillEvent e) {
     
            Player player = e.getPlayer();
     
            player.sendMessage(ChatColor.RED + "THAT'S MY LIQUID!");
            player.setFireTicks(100);
     
        }
    
        @EventHandler
        public void ballFiring(PlayerInteractEvent e){
            Player player = e.getPlayer();
            Action action = e.getAction();
            if (!(e.getAction() == Action.RIGHT_CLICK_AIR)) return;
     
            if (!(e.getItem().getType() == Material.BLAZE_ROD)) return;
     
            Egg egg = e.getPlayer().launchProjectile(Egg.class);
     
            Location impactLocation = event.getEgg().getLocation();
            World world = impactLocation.getWorld();
            world.createExplosion (impactLocation, 3); 
    
        }
    
    
    
    }
    

    This is the EventHandle (Main Class) Code
    Code:
    package me.Xinera;
    
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class EventHandle extends JavaPlugin {
    
        public void onEnable() {
            new PlayerListener(this);
        }
        public void onDisable() {
     
        }
    
    }
    
    Oh loool forgot that i used e. and not event. in the rest of the Event.
    But now the getEgg() has a red line under and it says: The method getEgg() is undefined for the type PlayerInteractEvent.

    <Edited by bwfcwalshy: Merged posts, please use the edit button rather than double posting.>
     
    Last edited by a moderator: Jun 26, 2015
  2. That's because there's no method with name getEgg() in PlayerInteractEvent. I see that you don't really know what you're doing. You need to listen for a ProjectileHitEvent and check if the projectile is an instance of Egg. After that you can do stuff with the entity.
     
  3. So is this right? If not, please help me to get started and explain what it does exc.
    Code:
    }
       
        @EventHandler
        public void ballFiring(PlayerInteractEvent e){
            Player player = e.getPlayer();
            Action action = e.getAction();
            if (!(e.getAction() == Action.RIGHT_CLICK_AIR)) return;
           
            if (!(e.getItem().getType() == Material.BLAZE_ROD)) return;
           
            Egg egg = e.getPlayer().launchProjectile(Egg.class);
           
            Location impactLocation = e.getEgg().getLocation();
            World world = impactLocation.getWorld();
            world.createExplosion (impactLocation, 3);       
    
        }
       
        @EventHandler
        public void onHit(ProjectileHitEvent e){
            Projectile p = e.getEntity();
        }
     
  4. @XINERA
    Yeah, that looks fine. all you need to do now is check if the projectile is an egg. You can do that using this check:
    Code:
    <object> instanceof <type>
    p instanceof Egg
    When that returns true you can cast from the projectile to the egg if you need anything from the egg. If you don't need to cast then you can immediately create an explosion at the egg's location. Tip: Add the egg to something like an arraylist to make sure the egg is that kind of egg, else when someone throws an egg it will create an explosion instead of spawn a chicken :p.
     
  5. Exactly where did you mean i should put the p instanceof Egg i have to change this line of code huh?
    Code:
            Location impactLocation = getEgg().getLocation();
        World world = impactLocation.getWorld();
        world.createExplosion (impactLocation, 3);        
    But if i do it there the Projectile p = e.getEntity isn't connecting to that one so in one way i think it has to be in this code i have been sitting here for like half an hour trying to figure out where it should be.
    Sorry but as i said i started coding Java yesterday and i don't know how it works but i'm starting to get the hang of some things.

    I think i've figured out how to do it i'm supposed to put it here huh?
    Code:
    @EventHandler
        public void onHit(ProjectileHitEvent e){
            Projectile p = e.getEntity();
            if (p instanceof Egg);
    
    (something about the explosion here)
    Because If the projectile is an egg it should look up the location of the where the Egg hit and then create an explosion.
    It feels like i'm starting to get the hang of it (I swear i'm not xD)

    <Edited by bwfcwalshy: Merged posts, please use the edit button rather than double posting.>
     
    Last edited by a moderator: Jun 26, 2015
  6. @XINERA
    Yeah, it's supposed to be in the ProjectileHitEvent method :p. But instead of the semicolon (;), you create a new block (a part between curly brackets/{}). and you do your stuff there
     
  7. You know that i said that it said : The method getEgg() is undefined for the type PlayerInteractEvent.
    It actually says : he method getEgg() is undefined for the type PlayerListener now which is the Class? Does this change anything.

    And i still don't know how to make the projectilehitevent explode. Because when i write this code there's a red line under get Egg.
    Code:
                Location impactLocation = getEgg().getLocation();
    And this code a red line under getLocation
    Code:
                Location impactLocation = getLocation();
    And in both cases it says that the method is undefined because there is no such method in ProjectileHitEvent.

    Like i dunno what to change here
    Code:
        @EventHandler
        public void onHit(ProjectileHitEvent e){
           
            Projectile p = e.getEntity();
           
            if (p instanceof Egg){
                       
                Location impactLocation = getEgg().getLocation();
                World world = impactLocation.getWorld();
                world.createExplosion (impactLocation, 3);
     
  8. @XINERA
    That is because getEgg() looks for a method within the same class, but not in the event. There isn't a getEgg() anywhere. You need to replace the getEgg() with the p to make it work. You should really consider learning java some more before asking stuff like this.
     
  9. Sorry :/ but it's done now i think i could've figured that out if i just thought logically

    But can you or someone else give me some tips should i just watch alot of youtube videos?
     
    Last edited: Jun 27, 2015
  10. Offline

    tytwining

    What I recommend is that you either look up tutorials on Google or YouTube, otherwise if you have the time you could do what I did and purely screw around in Eclipse and make random plugins, googling what you don't know and using the tools Eclipse gives you. I find that my way was more fun then reading pages and pages of things online and listening to a ton of videos.
     
  11. Offline

    _Filip

  12. Offline

    Rahat

    @XINERA Mark this thread as solved
     
Thread Status:
Not open for further replies.

Share This Page