Double Jump With Armor (Requesting Help)

Discussion in 'Plugin Development' started by bcohen9685, Jul 4, 2015.

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

    bcohen9685

    Hi there, I've been testing out making some custom enchantments and I for some reason (probably simple) has seemed to stump me. I've created the enchantment and when I do /enchantdoublejump it sets the lore properly, but the event has me stumped.

    I've tried to seperate events but either have worked, here are both:
    Code:
    @EventHandler
        public void onFlightAttempt(PlayerToggleFlightEvent event) {
         
           if(!event.isFlying() && event.getPlayer().getGameMode() != GameMode.CREATIVE && ((Inventory) event.getPlayer().getInventory().getChestplate().getItemMeta()).equals(ChatColor.GRAY + "Jump I")) {
         
               event.getPlayer().setVelocity(event.getPlayer().getVelocity().add(new Vector(0,0.25,0)));
               event.setCancelled(true);
         
           }  
    
    And the second one:
    Code:
    public void onPlayerFly(PlayerToggleFlightEvent e)
          {
            Player p = e.getPlayer();
          
              if (p.getGameMode() != GameMode.CREATIVE && ((Inventory) p.getInventory().getChestplate().getItemMeta()).equals(ChatColor.GRAY + "Jump I"))
              {
                e.setCancelled(true);
                p.setAllowFlight(false);
                p.setFlying(false);
                p.setVelocity(p.getLocation().getDirection().multiply(2.0D)
                  .setY(0.9D));
                p.playEffect(p.getLocation(), Effect.BLAZE_SHOOT, 15);
              }
            }
    
    Any help is appreciated! Thank you!
     
  2. @bcohen9685
    Did you register your events?
    If you did, in the first one you're adding 0.25 to the Y, that might not be noticable. And in the second one you're casting from ItemMeta to Inventory, which should throw an error in console. If it doesn't you probably haven't registered your events.
     
  3. Offline

    bcohen9685

    @megamichiel I've registered them and other events work. Let me try adding more velocity to Y. I'll get back to you.

    EDIT: I simply removed the second one, and the first one I added more velocity to Y which didn't work.

    Any help?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 10, 2016
  4. You're meant to do: if (event.isFlying()
    Not: if (!event.isFlying()

    For the first event method you have. isFlying() returns whether the NEW state is flying, not the current/old state of the Player's flying mode. To get the current/old state of the flying mode, use event.getPlayer().isFlying(), but I don't know why you'd need this as this'd always be the opposite of event.isFlying().

    Also, PlayerToggleFlightEvent is only called if the player CAN fly (getAllowFlight()) so when they get the lore or w/e, you must make them able to fly. You also must handle this in PlayerToggleFlightEvent, making sure they're not flying without the chestplate on (unless in creative) or having /fly in Essentials (not sure if this is possible as Essentials may not have an API for this).

    I'll post the cleaned up, formatted version of your current version that's "safe" to use, implementing megamichiel's suggestions. This doesn't make them into fly mode, nor does it handle Essentials /fly or cancelling if they don't have the item.

    Code:
    if (event.isFlying() && event.getPlayer().getGameMode() != GameMode.CREATIVE) {
        ItemStack chestplate = event.getPlayer().getInventory().getChestplate();
        if (chestplate != null) {
            ItemMeta chestplateMeta = chestplate.getItemMeta();
            if (chestplateMeta != null && chestplateMeta.getDisplayName() != null  && chestplateMeta.getDisplayName().startsWith(ChatColor.GRAY + "Jump ")) { // This may not work due to the colours. You might want to double check the colour of the display name if this doesn't work.
                event.setCancelled(true);
                event.getPlayer().setVelocity(event.getPlayer().getVelocity().multiply(3F)); // Can change this value.
            }
        }
    }
    
     
    Last edited: Jul 5, 2015
  5. Offline

    bcohen9685

    @KingFaris11 I ended up just removing the color & yet this still didn't work.
     
  6. So a player is in fly mode (getAllowFlight() is true) and not in Creative mode, and this doesn't work? Are you sure they're in fly mode?
     
  7. Offline

    meguy26

    @bcohen9685
    Maybe this will help, you will have to add the armor checks though, double jump code from one of my plugins:
    Code:
        @EventHandler
        void fly(PlayerMoveEvent ev) {
            Player p = ev.getPlayer();
            if (p.getGameMode() != GameMode.CREATIVE
                    && p.getGameMode() != GameMode.SPECTATOR
                    && plugin.getConfig().getBoolean("Fun.JumpEnabled")) {
                if (p.getLocation().subtract(0, 1, 0).getBlock().getType() != Material.AIR
                        && !p.isFlying() && ev.getFrom().getY() < ev.getTo().getY()) {
                    p.setAllowFlight(true);
                }
            }
        }
    
        @EventHandler
        void fly2(PlayerToggleFlightEvent ev) {
            Player p = ev.getPlayer();
            if (p.getGameMode() != GameMode.CREATIVE
                    && p.getGameMode() != GameMode.SPECTATOR
                    && plugin.getConfig().getBoolean("Fun.JumpEnabled")) {
                ev.setCancelled(true);
                p.setAllowFlight(false);
                p.setFlying(false);
                p.setVelocity(p.getLocation().getDirection().multiply(1.3));
                p.addPotionEffect(new PotionEffect(
                        PotionEffectType.DAMAGE_RESISTANCE, 20 * 4, 500));
                p.playSound(p.getLocation(), Sound.FIREWORK_BLAST, 2, 1);
            }
        }
     
  8. Offline

    bcohen9685

    @meguy26 The problem is, to check for the armor I need to use the InventoryClickEvent and I'm having trouble merging both.
     
  9. Offline

    meguy26

    @bcohen9685
    Why in the world would you need to use InventorClickEvent?
     
  10. Just one information: When you try to get the ItemMeta of an item which has no ItemMeta it throws a NullPointerException. You should do if(item.hasItemMeta()) for example
     
  11. Offline

    bcohen9685

    @meguy26 To check when they put on the armor.


    @Mr01Luki Should've caught that, fixed!
     
  12. Offline

    meguy26

    @bcohen9685
    Do you want to make them double jump as soon as they put the armor on, or only double jump if they have the armor on.
     
  13. Offline

    Burn38

    @bcohen9685 just check if player.getInventory().getArmorContents() contains your itemstack when before applying the double jump.
     
  14. Offline

    bcohen9685

    I figured it out after some research :)! One more question though, I'm making a custom enchantment plugin (if it's not noticeable) and I was wondering, since I'm using lore, when you enchant an item, if it could find the NEXT available line of lore and edit it on that?

    So for example, if the first line of lore on a sword contains
    Sharpness I

    and someone enchanted their sword with a custom enchantment, could it automatically know to edit the lore on line 2? I'll probably figure it out by the time of response, but if anyone could help, that'd be awesome.
     
Thread Status:
Not open for further replies.

Share This Page