Solved Get the location of an egg landing

Discussion in 'Plugin Development' started by CraterHater, Jun 30, 2015.

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

    CraterHater

    I want to get the location where an egg lands that is summoned when a player right clicks an wooden sword. On the landing location I want to summon tnt. How to do that. I already made this for the wooden sword:
    Code:
    @EventHandler
                                            public void onPlayerUse6(PlayerInteractEvent event){
                                                Player p = event.getPlayer();         
                                                if(event.getAction().equals(Action.RIGHT_CLICK_AIR)){
                                                    if(p.getItemInHand().getType() == Material.WOOD_SWORD){  
                                                       int value2 = getConfig().getInt(p.getName() + ".Permission1");
                                                        if(value2 == 2) {
                                                        if (Cooldowns.tryCooldown(p, "x6", 12000)) {
                                                           Egg A = p.getPlayer().launchProjectile(Egg.class);
                                                           //This egg must be getted to the location  
                                                             p.playSound(p.getLocation(), Sound.NOTE_PLING, 5, 5);
                                                            p.sendMessage("§b§2[Spells]:§rYou casted the Bomb spell");
                                                           return;
                                                        }else{
                                                        p.sendMessage("§b§2[Spells]:§rYou have " + (Cooldowns.getCooldown(p, "x6") / 1000) + " seconds left!");
                                                        p.playSound(p.getLocation(), Sound.NOTE_PLING, 3, 3);
                                                        return;
                                                        }
                             
     
  2. Offline

    Ward1246

    You want to use a projectile hit event, and make sure the entity type is an egg. Then spawn primed TNT at the location of the egg.

    It should look like this:
    Code:
        @EventHandler
        public void onProjectileHit(ProjectileHitEvent event) {
            if (event.getEntity().getType() == EntityType.EGG) {
                Egg egg = (Egg) event.getEntity();
                egg.getLocation().getWorld().spawn(egg.getLocation(), TNTPrimed.class);
            }
        }
    Let me know if that works. I didn't test it. It may not work, but I can fix it if it does not. Now then, let me explain the code. First we get an projectile hit event, then we make sure the entity that was the projectile is an egg, then we get the location of the egg and spawn primed TNT at it's location. Hope this helps.
     
  3. Offline

    WesJD

    @CraterHater @Ward1246 To start, he wants the egg that was thrown to explode, not just any egg that hits the ground. 1. Check who threw the egg
    2. If you really want it to be only an egg that was from the sword, use metadata.
     
  4. Offline

    CraterHater

    @WesJD could I get a bit of examples with that itemmeta

    @WesJD @Ward1246
    how to resolve "p" to Player in this event? I mean the player that trows one. ow and In the code scroll to the right :D
    Code:
     @EventHandler
                                                                                                                                            public void onProjectileHit(ProjectileHitEvent event) {
                                                                                                                                                if (event.getEntity().getType() == EntityType.EGG) {
                                                                                                                                                  
                                                                                                                                                  
                                                                                                                                                    int value6 = getConfig().getInt(p.getName() + ".KitValue");
                                                                                                                                                    if(value6 == 1) {
                                                                                                                                                    Egg egg = (Egg) event.getEntity();
                                                                                                                                                    egg.getLocation().getWorld().spawn(egg.getLocation(), TNTPrimed.class);
                                                                                                                                                }
                                                                                                                                            }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 12, 2016
  5. Offline

    Ward1246

    That completely slipped my mind.... This next code should fix that issue:
    Code:
        @EventHandler
        public void onProjectileHit(ProjectileHitEvent event) {
            if (event.getEntity().getType() == EntityType.EGG) {
                Egg egg = (Egg) event.getEntity();
                @SuppressWarnings("deprecation")
                Player player = (Player) egg.getShooter();
                if (player.getItemInHand().getType() == Material.WOOD_SWORD) {
                egg.getLocation().getWorld().spawn(egg.getLocation(), TNTPrimed.class);
                }
            }
        }
    That should also fix your problem on getting the player from the post above this one. Let me explain what I did. Now, we get the player who shot the egg, and check if they are holding a wooden sword. If they are, it will spawn the primed TNT. That should be the fix to most things I hope. Let me know if you get an issue/error.
     
  6. Offline

    CraterHater

  7. Offline

    Ward1246

    Happy to help :D
     
    CraterHater likes this.
  8. Offline

    WesJD

    @Ward1246 @CraterHater Sorry to keep responding, but no, that wont work, because if I click the sword, then hold something after, that won't work.
     
  9. Offline

    Ward1246

    Yes, that is true, but that's one of the easy ways to narrow it down to make sure this was supposed to happen. Let me put it in an example. Say this is being used for something like pvp or a mini game, the objective would be to use it, so players would likely keep holding it throughout the entire time. If it is used for mining purposes or to break blocks, you still should just keep holding it. For most of the reasons for this, it would be best to just hold it. But you do have a valid point, that's true. I assumed that @CraterHater wouldn't need the user to select other items. I say that this was the easiest way because you could do other methods, for example store them in a list, or give it a custom name/UUID. This post is long, but I am trying to say is that I found this to be a good option. He could also warn them if worst comes to worst. I figured that's the best way since users *I wouldn't think* are not very likely to deselect this item. To look on the bright side, doing this disallows the player to hold other things during it's use, making the situation slightly harder.

    Tl;Dr yes that's true, but it is a quick and easy fix to narrow down the likely hood of this event firing when it should not.
     
  10. Offline

    CraterHater

    @Ward1246 @WesJD I added something to that problem it works as following: The egg gets launched from the sword and when someone uses the sword it gives the player a specific point and in the event it checks if the player has that point and sets it to zero, works perfect.
     
  11. Offline

    Ward1246

    That's a good idea! Now that that's settled, I will stop posting. Let me know if you need anything in the future.
     
Thread Status:
Not open for further replies.

Share This Page