Solved How to add potion affect to a player while he holds and item in his hand?

Discussion in 'Plugin Development' started by xexex77, Apr 20, 2018.

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

    xexex77

    How to add potion affect to a player while he holds and item in his hand?
    Code is below and its not working on my Minecraft server

    code:

    Code:
    public class EventsClass implements Listener {
    @EventHandler
        public void onPlayerInteractEvent(PlayerItemHeldEvent e){
            Player p = e.getPlayer();
            ItemStack item = p.getItemInHand();
            String name = p.getItemInHand().getItemMeta().getDisplayName();
            List<String> lore = item.getItemMeta().getLore();
            if(item != null) {
                if (name == "Axe") {
                    if (item.getItemMeta().getLore() != null) {
                        if (lore.contains("Daemon's Axe")) {
    
                            p.addPotionEffect(new PotionEffect(PotionEffectType.FIRE_RESISTANCE, 999999999, 5, false));
                            p.addPotionEffect(new PotionEffect(PotionEffectType.WITHER, 999999999, 1, false));
                            p.addPotionEffect(new PotionEffect(PotionEffectType.INCREASE_DAMAGE, 999999999, 2, false));
                        }
                    }
                }
            }
        }
    }
    Main class code:

    Code:
     @Override
        public void onEnable() {
            getServer().getConsoleSender().sendMessage(ChatColor.GREEN + "plugin is enabled");
            Bukkit.getPluginManager().registerEvents(new EventsClass(),this);
    
            Axe();
        }
    Whats wrong? please help.
     
    Last edited: Apr 22, 2018
  2. Offline

    MightyOne

    @xexex77 one who ask like this appears to not have a lot knowledge about what he is trying to do. So tell us, what have you coded so far? At which point exactly do you need help?
     
  3. Mmmh. That sounds familiar.

     
  4. Offline

    xexex77

    i want particles to appear around the player if he holds a specific item. (for example sword, bow, stick, block or anything else) or particles around an item that player holds in the hand(sword, bow, armor, block, stick e.t.c).

    like in the video, but on the item or on the player(programmatically using java).
     
    Last edited: Apr 21, 2018
  5. Offline

    xexex77

    New question above
     
    Last edited: Apr 22, 2018
  6. First: change: 'name.equals("Axe")'
    And add a hasItemMeta() -> return function before you call the getItemMeta because it can return null, which causes an error.

    Second: PlayerItemHeldEvent will only appear when you (RIGHT/LEFT/SHIFT) Click at a player, such as hitting it.
    You said that 'if the player is holding'. The onPlayerInteractEvent is not the right event, there is actually no event that runs without doing anything, as result you need to create an schedule.

    //EDIT

    Okey @xexex77, Here is my idea.

    You must create a ScheduleTimer at the onEnable().
    Then every time it calls the method you iterate over all the players.
    After that you can call the method to display particles after a couple conditions.
     
  7. Offline

    timtower Administrator Administrator Moderator

    Don't edit away the first post please, makes the first part of the thread useless.
     
  8. Offline

    xexex77

    there wasn't anything important i thnik, but the first part was about how to put particle affect on the player when he holds a weapon for example


    new code:

    Code:
    @EventHandler
        public void onPlayerInteractEvent(PlayerItemHeldEvent e){
            Player p = e.getPlayer();
            ItemStack item = p.getItemInHand();
            String name = p.getItemInHand().getItemMeta().getDisplayName();
            List<String> lore = item.getItemMeta().getLore();
            if(item != null) {
                if (name.equals("Axe")) {
                    if (item.hasItemMeta()) {
                        if (lore.contains("Daemon's Axe")) {
    
                            p.addPotionEffect(new PotionEffect(PotionEffectType.FIRE_RESISTANCE, 999999999, 5, false));
                            p.addPotionEffect(new PotionEffect(PotionEffectType.WITHER, 999999999, 1, false));
                            p.addPotionEffect(new PotionEffect(PotionEffectType.INCREASE_DAMAGE, 999999999, 2, false));
                        }
                    }
                }
            }
        }
    DanielTheDev you said that it will work if i'll hit someone one or ... But this pice of code is not working, maybe i missed something? Even if i'am hitting someone right clicking nothing
     
    Last edited: Apr 22, 2018
  9. Offline

    timtower Administrator Administrator Moderator

    Doesn't change the fact that it made the begin useless.
    I would go with the way from DanielTheDev
     
  10. Offline

    xexex77

    final working code no timer need
    code:
    Code:
    EventHandler
        public void onPlayerInteractEvent(PlayerItemHeldEvent e) {
            Player p = e.getPlayer();
            ItemStack item = p.getItemInHand();
            String name = p.getItemInHand().getItemMeta().getDisplayName();
            List<String> lore = item.getItemMeta().getLore();
                if (item != null) {
                    if (name.equals(ChatColor.RED+""+ChatColor.BOLD+"Axe of Blood")) {
                        if (item.hasItemMeta()) {
                            if (lore.contains(ChatColor.RED+"Daemon Master Knight's Axe")) {
    
                                p.addPotionEffect(new PotionEffect(PotionEffectType.FIRE_RESISTANCE, 999999999, 5, false));
                                p.addPotionEffect(new PotionEffect(PotionEffectType.WITHER, 999999999, 1, false));
                                p.addPotionEffect(new PotionEffect(PotionEffectType.INCREASE_DAMAGE, 999999999, 2, false));
                            }
                        }
                    }
                }
        }
     
Thread Status:
Not open for further replies.

Share This Page