(1.17) Can you adjust the shield stun time?

Discussion in 'Spigot Plugin Help' started by electcheese, Jul 16, 2021.

  1. I am trying to make a custom shield and can't figure out how to adjust it. I tried to find it on the java docs but didn't find it.
    Code (Java):
    package me.electcheese.Shield;

    import java.util.ArrayList;
    import java.util.List;

    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.NamespacedKey;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.ShapedRecipe;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.java.JavaPlugin;

    public class Main extends JavaPlugin{
        @Override
        public void onEnable() {

        }

        @Override
        public void onDisable() {

        }
        public ShapedRecipe getRecipe() {
           
            ItemStack item = new ItemStack(Material.SHIELD);
            ItemMeta meta = item.getItemMeta();
           
            meta.setDisplayName(ChatColor.DARK_PURPLE + "Stuningly nice shield!");
           
           
            List<String> lore = new ArrayList<String>();
            lore.add(ChatColor.YELLOW + "Reduced stun time");
            lore.add("");
            meta.setLore(lore);
            item.setItemMeta(meta);
           
           
            NamespacedKey key = new NamespacedKey(this, "name");
           
            ShapedRecipe recipe = new ShapedRecipe(key, item);
           
            recipe.shape("III", "ISI", "III");
           
            recipe.setIngredient('I', Material.IRON_INGOT);
            recipe.setIngredient('S', Material.SHIELD);
           
            return recipe;
        }
    }
     
     
  2. My idea: check if the player was hit with an axe while having that shield in their left hand, then setting the cooldown on Material.Shield to some other value you want.
     
  3. Ok, would you happen to know how to set the shields cooldown? Also, how would you check to see if the damage was caused by an axe? And, how would you check if they were blocking? Sorry for all the questions.
     
    #3 electcheese, Jul 16, 2021
    Last edited: Jul 16, 2021
  4. First, listen to EntityDamageByEntityEvent.
    Then check if the damager is holding an axe and the damaged entity is a player.
    You can check if the player has their hand raised with Player#isHandRaised.
    You can set the cooldown with Player#setCooldown(<Material>,<time_in_ticks>) 0 just use Material.Shield as the Material and set the time to whatever you want (in ticks!)
     
  5. I am having trouble making the damager a player so I can check there inventory. How would I do that? Or is there a way to check an entity's item in main hand?
     
  6. Figured it out. I just did Player player = (Player) event.getDamager();
     
  7. I think mobs can do that too, not just players, so here's what you do:
    Check if the damager is a LivingEntity, cast them to LivingEntity, then check their equipment.
    This works no matter what the damager is - mob or player.
     
  8. I just tested it. It won't work because if the damage is blocked then the defender is never damaged.
     
  9. Then try listening to PlayerItemDamageEvent for when the shield is damaged due to the hit. I'm not sure if this will work if the shield is unbreakable though...
     
  10. The shield is not unbreakable.
     
  11. but I can't find how to get the player who damaged the item so I can check if they are holding an axe.
     
  12. I tried, but it is not working.
    Code (Java):
    @EventHandler
        public void onClick(PlayerItemDamageEvent event) {
            if(event.getItem().getType().equals(Material.SHIELD))
                if(event.getItem().getItemMeta().hasLore())
                    if(event.getItem().getItemMeta().getDisplayName().contains(ChatColor.DARK_PURPLE + "Stuningly nice shield!"))
                        if(event.getDamage() ==  9) {
                            event.getPlayer().setCooldown(Material.SHIELD, 5);
                        }
        }
     
  13. by the way, I am trying to make it so when you hit is with an axe, it still gives a cooldown, but it is a lower cooldown.