Solved Can't figure out how to inflict potions effects

Discussion in 'Plugin Development' started by RainingTNT123, Apr 28, 2020.

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

    RainingTNT123

    Can anyone help me with this code I have been looking around and testing for about 3 days now and cannot figure it out. It would mean so much to me if you could help!

    Here is my code:

    Code:
    package me.RainingTNT123.Ultimates.Events;
    
    
    
    import org.bukkit.ChatColor;
    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.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
    
    
    
    public class EventsClass implements Listener {
    
    
        @EventHandler
        public void onHit(EntityDamageByEntityEvent event) {
            if(event.getDamager() instanceof Player) {
                Player player = (Player) event.getDamager();
         
            ItemStack hand = player.getInventory().getItemInHand();
            if(hand.getItemMeta().getDisplayName().contains(ChatColor.DARK_PURPLE + "The Withered Sword")){
                player.addPotionEffect(new PotionEffect(PotionEffectType.WITHER, 200, 2));
            }
    
        }
    } 
    }
    My other class (You may not need this)

    Code:
    package me.RainingTNT123.Ultimates.items;
    
    import java.util.ArrayList;
    
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.enchantments.Enchantment;
    import org.bukkit.entity.Entity;
    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.ItemFlag;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.ShapedRecipe;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.Plugin;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
    
    import me.RainingTNT123.Ultimates.Main;
    
    public class UltimateItems implements Listener{
    
        private Plugin plugin = Main.getPlugin(Main.class);
    
       
        @EventHandler
        public void onHit(EntityDamageByEntityEvent event) {
            Entity e = event.getEntity();
            Entity damager = event.getDamager();
           
            if(e instanceof Player) {
                Player player = (Player) e;
                player.addPotionEffect(new PotionEffect(PotionEffectType.WITHER, 100, 1));
                    //    if(event.getDamager() instanceof Player) {
            //    Player player = (Player) event.getDamager();
        //        Player damaged = (Player) event.getEntity();
           
               
               
            //ItemStack hand = player.getInventory().getItemInHand();
            //if(hand.getItemMeta().getDisplayName().contains(ChatColor.DARK_PURPLE + "The Withered Sword")){
                // damaged.addPotionEffect(new PotionEffect(PotionEffectType.WITHER, 100, 1));
               
            }
        }
       
        public void customRecipe() {
            ItemStack item = new ItemStack(Material.STONE_SWORD,1);
            ItemMeta meta = item.getItemMeta();
           
                meta.setDisplayName(ChatColor.DARK_PURPLE + "The Withered Sword");
                ArrayList<String> lore = new ArrayList<String>();
                lore.add(ChatColor.GRAY + "This type of sword was used by the withered");
                lore.add(ChatColor.GRAY + "but this is only a replica of the sword.");
                lore.add(ChatColor.GRAY + " ");
                lore.add(ChatColor.BLUE + "Wither II for 3 seconds");
                lore.add(ChatColor.BLUE + "Blindness for 3 seconds");
                lore.add(ChatColor.BLUE + "+12 Attack Damage");
                lore.add(ChatColor.DARK_GRAY + ChatColor.ITALIC.toString() + "Ultimate item");
                meta.setLore(lore);
                meta.addEnchant(Enchantment.DURABILITY, 10000, true);
                meta.addItemFlags(ItemFlag.HIDE_ATTRIBUTES);
                meta.addItemFlags(ItemFlag.HIDE_ENCHANTS);
                item.setItemMeta(meta);
               
            ShapedRecipe witheredsword =new ShapedRecipe(item);
           
            witheredsword.shape("%^%","%#%","%^%");
            witheredsword.setIngredient('#', Material.DIAMOND_SWORD);
            witheredsword.setIngredient('%', Material.SKULL_ITEM, 1);
            witheredsword.setIngredient('^', Material.NETHER_STAR);
           
            plugin.getServer().addRecipe(witheredsword);
           
        }
    }
    
     
    Last edited: Apr 30, 2020
  2. Offline

    KarimAKL

    @RainingTNT123 Listen to the EntityDamageByEntityEvent, then check if the damager's hand contains the specified item, then you can give the victim the effect using the LivingEntity#addPotionEffect(PotionEffect) method.
     
  3. Offline

    RainingTNT123

    Welp I just needed to change Player to Living Entity...
    We'll that was easy.

    Thanks for the help @KarimAKL
     
    Last edited: May 1, 2020
Thread Status:
Not open for further replies.

Share This Page