Adding a poison effect of a certain item.

Discussion in 'Plugin Development' started by HiDPiD, Feb 7, 2016.

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

    HiDPiD

    I´m not very good with making plugins but I really want to learn it. What i have right now: /dagger and you will get a Flint in your hand called AssassinDagger with nice colors and multiple lines. But I need it so that when I hit a mob or player, they will get poisoned for a few seconds. How do I do this?
     
  2. Offline

    pie_flavor

    @HiDPiD Listen for a EntityDamageByEntityEvent, check if the damager is a player and if so are they holding the assassin's dagger, and then apply the poison effect to the damaged entity.
     
  3. Offline

    HiDPiD

    Okay, but how do I do that? Like I said, im not that good ^^
     
  4. Offline

    pie_flavor

    @HiDPiD Apologies. I thought you were referring to skill level, not experience.
    Code:
    public class ExamplePlugin extends JavaPlugin implements Listener {
      @Override
      public void onEnable() {
        getServer().getPluginManager().registerEvents(this, this);
      }
      @EventHandler
      public void onDamage(EntityDamageByEntityEvent e) {
        if (e.getDamager().getType().equals(EntityType.PLAYER)) {
          Player p = (Player) e.getDamager();
          ItemStack stack = p.getItemInHand();
          if (stack != null && stack.getType().equals(Material.FLINT)) {
            ItemMeta meta = stack.getItemMeta();
            if (meta.hasDisplayName() && meta.getDisplayName().equals(/*insert name of your item here*/)) {
              LivingEntity entity = (LivingEntity) e.getEntity();
              entity.addPotionEffect(new PotionEffect(PotionEffectType.POISON, 60, 2));
            }
          }
        }
      }
    }
     
  5. Offline

    HiDPiD

    Thanks for the reply, but the poison effect doesn't work. What have I done wrong?




    package me.HiDPiD.Dagger2;

    import org.bukkit.entity.EntityType;
    import org.bukkit.entity.LivingEntity;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.EntityDamageByEntityEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;

    public class Core extends JavaPlugin implements Listener {
    @Override
    public void onEnable() {
    getServer().getPluginManager().registerEvents(this, this);
    getLogger().info(">> PozorDagger v" + getDescription().getVersion() +" is enabled! YEHIYEH! <<");
    getCommand("dagger").setExecutor(new PozorDagger());
    }

    @Override
    public void onDisable (){
    getLogger().info(">> PozorDagger v" + getDescription().getVersion() +" is disabled! i cri evritiem :'( <<");
    }

    @EventHandler
    public void onDamage(EntityDamageByEntityEvent e) {
    if (e.getDamager().getType().equals(EntityType.PLAYER)) {
    Player p = (Player) e.getDamager();
    ItemStack stack = p.getItemInHand();
    if (stack != null) {
    ItemMeta meta = stack.getItemMeta();
    if (meta.hasDisplayName() && meta.getDisplayName().equals("PozorDagger")) {
    LivingEntity entity = (LivingEntity) e.getEntity();
    entity.addPotionEffect(new PotionEffect(PotionEffectType.POISON, 3, 1));
    }
    }
    }
    }
    }
     
  6. Offline

    JoaoBM

    @pie_flavor Please don't SpoonFeed. He won't learn anything from this by copying the code.
     
  7. Offline

    pie_flavor

    @HiDPiD You have replaced the '60' with '3'. I too meant 3 seconds, but 60 is the number of ticks, which is what the int measures.
    @JoaoBM Funny, it failed because it wasn't copied enough.
     
  8. Offline

    Xerox262

    @pie_flavor No, it failed because you did not explain what you were doing so he didn't understand it and made it wrong.
     
    JoaoBM likes this.
  9. Offline

    mcdorli

    No one learns anything by spoonfeeding them code. That's why I miss the voting from stackoverflow. Some comments cpuld be easily voted down, like spoonfeeding.
     
  10. Offline

    HiDPiD

    Even when I replaced it back to 60, it doesn't work :(

    Oh, nevermind, it does work. Only not on hostile mobs! Thanks!

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 10, 2016
Thread Status:
Not open for further replies.

Share This Page