1.16.5 not sure about this

Discussion in 'Spigot Plugin Development' started by not_pog, Jun 25, 2021.

  1. Hey, Idk what to do I am making a custom enchant in my plugin and having an issue with applying it to my item

    Main:
    Code (Text):
    package me.kosherdun.main;


    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.persistence.PersistentDataContainer;
    import org.bukkit.persistence.PersistentDataType;
    import org.bukkit.plugin.java.JavaPlugin;


    public final class Main extends JavaPlugin implements Listener {
        private static Main instance;

        @Override
        public void onEnable() {
            System.out.println("hallo");
            instance = this;
            Items itm = new Items();
            itm.init();
            Customenchants.register();

            this.getServer().getPluginManager().registerEvents(this, this);


        }


        public static Main getInstance() {
            return instance;
        }


        @Override
        public void onDisable() {

        }

        @EventHandler
        public void onEntityDamage(EntityDamageByEntityEvent e)
        {
            if(!(e.getDamager() instanceof Player)) return;

            Player p = (Player) e.getDamager();

            for(ItemStack armor : p.getInventory().getArmorContents())
            {
                ItemMeta meta = armor.getItemMeta();
                if(meta.getPersistentDataContainer() == null) continue;
                PersistentDataContainer container = meta.getPersistentDataContainer();
                if(!container.has(Customenchants.ANTITHORNS.getKey(), PersistentDataType.STRING)) continue;
                if(container.get(Customenchants.ANTITHORNS.getKey(), PersistentDataType.STRING).equalsIgnoreCase("Anti Thorns"))
                    p.damage(20);
                    break;

            }
        }




        }






     

    Items class:
    Code (Text):
    package me.kosherdun.main;

    import org.bukkit.*;
    import org.bukkit.enchantments.Enchantment;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.ShapedRecipe;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.inventory.meta.LeatherArmorMeta;
    import org.bukkit.persistence.PersistentDataType;

    import java.security.Key;
    import java.util.ArrayList;
    import java.util.List;

    public class Items {

        public static ItemStack dragon_set_chestplate;
        public static ItemStack dragon_set_leggings;
        public static ItemStack dragon_set_boots;
        public static ItemStack dragon_set_helmet;
        public static ItemStack drip_set_helmet;
        public static ItemStack drip_set_chestplate;
        public static ItemStack drip_set_leggings;
        public static ItemStack drip_set_boots;
        public static ItemStack zombie_set_helmet;


        public static void init() {
            crateDRAGONSETCHESTPLATE();
            crateDRAGONSETLEGS();
            crateDRAGONSETBOOTS();
            crateDRAGONSETHELMET();
            createDRIPSETHELMET();
            createDRIPSETCHESTPLATE();
            createDRIPSETLEGGINGS();
            createDRIPSETBOOTS();
            createZOMBIESETHELMET();
        }

        private static void crateDRAGONSETHELMET() {
            ItemStack item = new ItemStack(Material.DRAGON_HEAD, 1);
            ItemMeta meta = item.getItemMeta();
            meta.setDisplayName(ChatColor.LIGHT_PURPLE + "Dragon Set Helmet ");
            List<String> lore = new ArrayList<>();
            lore.add(ChatColor.GOLD + "This Armor Was Forged By The Dragon Himself");
            lore.add(ChatColor.DARK_GRAY + "This Is Cursed By The Endermen");
            meta.setLore(lore);
            meta.addEnchant(Enchantment.DURABILITY, 100, true);
            meta.addEnchant(Enchantment.PROTECTION_ENVIRONMENTAL, 10, true);
            item.setItemMeta(meta);

            dragon_set_helmet = item;


            NamespacedKey key = new NamespacedKey(Main.getInstance(), "Dragon_Set_Helmet");

            ShapedRecipe recipe = new ShapedRecipe(key, item);

            recipe.shape(
                    "DDD",
                    "DHD",
                    "DDD"
            );

            recipe.setIngredient('D', Material.DIAMOND);
            recipe.setIngredient('H', Material.DRAGON_HEAD);

            Bukkit.addRecipe(recipe);


        }


        private static void crateDRAGONSETCHESTPLATE() {
            ItemStack item = new ItemStack(Material.LEATHER_CHESTPLATE, 1);
            LeatherArmorMeta lch = (LeatherArmorMeta) item.getItemMeta();
            lch.setColor(Color.fromBGR(0, 0, 0));
            item.setItemMeta(lch);
            ItemMeta meta = item.getItemMeta();
            meta.setDisplayName(ChatColor.LIGHT_PURPLE + "Dragon Set ChestPlate ");
            List<String> lore = new ArrayList<>();
            lore.add(ChatColor.GOLD + "This Armor Was Forged By The Dragon Himself");
            lore.add(ChatColor.DARK_GRAY + "This Is Cursed By The Endermen");
            meta.setLore(lore);
            meta.addEnchant(Enchantment.DURABILITY, 100, true);
            meta.addEnchant(Enchantment.PROTECTION_ENVIRONMENTAL, 10, true);
            item.setItemMeta(meta);


            dragon_set_chestplate = item;
            NamespacedKey key = new NamespacedKey(Main.getInstance(), "Dragon_Set_ChestPlate");

            ShapedRecipe recipe = new ShapedRecipe(key, item);

            recipe.shape(
                    "DDD",
                    "DLD",
                    "DDD"
            );

            recipe.setIngredient('D', Material.DIAMOND);
            recipe.setIngredient('L', Material.LEATHER_CHESTPLATE);

            Bukkit.addRecipe(recipe);


        }


        private static void crateDRAGONSETLEGS() {
            ItemStack item = new ItemStack(Material.LEATHER_LEGGINGS, 1);
            LeatherArmorMeta llg = (LeatherArmorMeta) item.getItemMeta();
            llg.setColor(Color.fromBGR(0, 0, 0));
            item.setItemMeta(llg);
            ItemMeta meta = item.getItemMeta();
            meta.setDisplayName(ChatColor.LIGHT_PURPLE + "Dragon Set Leggings ");
            List<String> lore = new ArrayList<>();
            lore.add(ChatColor.GOLD + "This Armor Was Forged By The Dragon Himself");
            lore.add(ChatColor.DARK_GRAY + "This Is Cursed By The Endermen");
            meta.setLore(lore);
            meta.addEnchant(Enchantment.DURABILITY, 100, true);
            meta.addEnchant(Enchantment.PROTECTION_ENVIRONMENTAL, 10, true);
            item.setItemMeta(meta);


            dragon_set_leggings = item;

            NamespacedKey key = new NamespacedKey(Main.getInstance(), "Dragon_Set_Leggings");

            ShapedRecipe recipe = new ShapedRecipe(key, item);

            recipe.shape(
                    "DDD",
                    "DLD",
                    "DDD"
            );

            recipe.setIngredient('D', Material.DIAMOND);
            recipe.setIngredient('L', Material.LEATHER_LEGGINGS);

            Bukkit.addRecipe(recipe);


        }

        private static void crateDRAGONSETBOOTS() {

            ItemStack item = new ItemStack(Material.LEATHER_BOOTS, 1);
            LeatherArmorMeta lbo = (LeatherArmorMeta) item.getItemMeta();
            lbo.setColor(Color.fromBGR(0, 0, 0));
            item.setItemMeta(lbo);

            ItemMeta meta = item.getItemMeta();
            meta.setDisplayName(ChatColor.LIGHT_PURPLE + "Dragon Set Boots ");
            List<String> lore = new ArrayList<>();
            lore.add(ChatColor.GOLD + "This Armor Was Forged By The Dragon Himself");
            lore.add(ChatColor.DARK_GRAY + "This Is Cursed By The Endermen");
            meta.setLore(lore);
            meta.addEnchant(Enchantment.DURABILITY, 100, true);
            meta.addEnchant(Enchantment.PROTECTION_ENVIRONMENTAL, 10, true);
            item.setItemMeta(meta);

            dragon_set_boots = item;


            NamespacedKey key = new NamespacedKey(Main.getInstance(), "Dragon_Set_Boots");

            ShapedRecipe recipe = new ShapedRecipe(key, item);

            recipe.shape(
                    "DDD",
                    "DLD",
                    "DDD"
            );

            recipe.setIngredient('D', Material.DIAMOND);
            recipe.setIngredient('L', Material.LEATHER_BOOTS);

            Bukkit.addRecipe(recipe);


        }

        private static void createDRIPSETHELMET() {
            ItemStack item = new ItemStack(Material.GOLDEN_HELMET, 1);
            ItemMeta meta = item.getItemMeta();
            meta.setDisplayName(ChatColor.GREEN + "Drip Set Helmet ");
            List<String> lore = new ArrayList<>();
            lore.add(ChatColor.GOLD + "This Armor Is DRIP!");
            lore.add(ChatColor.DARK_GRAY + "ur too drip fuck");
            meta.setLore(lore);
            meta.addEnchant(Enchantment.PROTECTION_ENVIRONMENTAL, 50, true);
            meta.addEnchant(Enchantment.THORNS, 500, true);
            item.setItemMeta(meta);

            drip_set_helmet = item;


            NamespacedKey key = new NamespacedKey(Main.getInstance(), "Drip_Set_Helmet");

            ShapedRecipe recipe = new ShapedRecipe(key, item);

            recipe.shape(
                    "GGG",
                    "GHG",
                    "GGG"
            );

            recipe.setIngredient('G', Material.GOLD_INGOT);
            recipe.setIngredient('H', Material.GOLDEN_HELMET);

            Bukkit.addRecipe(recipe);
        }

        private static void createDRIPSETCHESTPLATE() {
            ItemStack item = new ItemStack(Material.GOLDEN_CHESTPLATE, 1);
            ItemMeta meta = item.getItemMeta();
            meta.setDisplayName(ChatColor.GREEN + "Drip Set ChestPlate");
            List<String> lore = new ArrayList<>();
            lore.add(ChatColor.GOLD + "This Armor Is DRIP!");
            lore.add(ChatColor.DARK_GRAY + "ur too drip fuck");
            meta.setLore(lore);
            meta.addEnchant(Enchantment.PROTECTION_ENVIRONMENTAL, 50, true);
            meta.addEnchant(Enchantment.THORNS, 500, true);
            item.setItemMeta(meta);

            drip_set_chestplate = item;


            NamespacedKey key = new NamespacedKey(Main.getInstance(), "Drip_Set_ChestPlate");

            ShapedRecipe recipe = new ShapedRecipe(key, item);

            recipe.shape(
                    "GGG",
                    "GCG",
                    "GGG"
            );

            recipe.setIngredient('G', Material.GOLD_INGOT);
            recipe.setIngredient('C', Material.GOLDEN_CHESTPLATE);

            Bukkit.addRecipe(recipe);
        }

        private static void createDRIPSETLEGGINGS() {
            ItemStack item = new ItemStack(Material.GOLDEN_LEGGINGS, 1);
            ItemMeta meta = item.getItemMeta();
            meta.setDisplayName(ChatColor.GREEN + "Drip Set Leggings");
            List<String> lore = new ArrayList<>();
            lore.add(ChatColor.GOLD + "This Armor Is DRIP!");
            lore.add(ChatColor.DARK_GRAY + "ur too drip fuck");
            meta.setLore(lore);
            meta.addEnchant(Enchantment.PROTECTION_ENVIRONMENTAL, 50, true);
            meta.addEnchant(Enchantment.THORNS, 500, true);
            item.setItemMeta(meta);

            drip_set_leggings = item;


            NamespacedKey key = new NamespacedKey(Main.getInstance(), "Drip_Set_Leggings");

            ShapedRecipe recipe = new ShapedRecipe(key, item);

            recipe.shape(
                    "GGG",
                    "GLG",
                    "GGG"
            );

            recipe.setIngredient('G', Material.GOLD_INGOT);
            recipe.setIngredient('L', Material.GOLDEN_LEGGINGS);

            Bukkit.addRecipe(recipe);
        }

        private static void createDRIPSETBOOTS() {
            ItemStack item = new ItemStack(Material.GOLDEN_BOOTS, 1);
            ItemMeta meta = item.getItemMeta();
            meta.setDisplayName(ChatColor.GREEN + "Drip Set Boots");
            List<String> lore = new ArrayList<>();
            lore.add(ChatColor.GOLD + "This Armor Is DRIP!");
            lore.add(ChatColor.DARK_GRAY + "ur too drip fuck");
            meta.setLore(lore);
            meta.addEnchant(Enchantment.PROTECTION_ENVIRONMENTAL, 50, true);
            meta.addEnchant(Enchantment.THORNS, 500, true);
            item.setItemMeta(meta);

            drip_set_boots = item;


            NamespacedKey key = new NamespacedKey(Main.getInstance(), "Drip_Set_Boots");

            ShapedRecipe recipe = new ShapedRecipe(key, item);

            recipe.shape(
                    "GGG",
                    "GBG",
                    "GGG"
            );

            recipe.setIngredient('G', Material.GOLD_INGOT);
            recipe.setIngredient('B', Material.GOLDEN_BOOTS);

            Bukkit.addRecipe(recipe);
        }

        private static void createZOMBIESETHELMET() {
            ItemStack item = new ItemStack(Material.LEATHER_HELMET, 1);
            NamespacedKey key = new NamespacedKey(Main.getInstance(), "Zombie_Set_Helmet");

            ItemMeta meta = item.getItemMeta();
            meta.setDisplayName(ChatColor.DARK_GRAY + "Zombie Set Helmet ");
            List<String> lore = new ArrayList<>();
            lore.add(ChatColor.BOLD + "" + ChatColor.YELLOW + "YAIKS! THATS ROTTEN");
            lore.add(ChatColor.GRAY + "Anti Thorns I");
            meta.setLore(lore);
            meta.getPersistentDataContainer().set(key, PersistentDataType.STRING, "Anti Thorns");
            item.addEnchantment(Enchantment.DURABILITY, 1);
            item.addEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, 1);
            item.addUnsafeEnchantment(Customenchants.ANTITHORNS, 1);
            item.setItemMeta(meta);



            zombie_set_helmet = item;


            ShapedRecipe recipe = new ShapedRecipe(key, item);

            recipe.shape(
                    "RRR",
                    "RLR",
                    "RRR"
            );

            recipe.setIngredient('R', Material.ROTTEN_FLESH);
            recipe.setIngredient('L', Material.LEATHER_HELMET);

            Bukkit.addRecipe(recipe);
     
        }
    }



     
    Cenchants (enchant wrapper):
    Code (Text):
    package me.kosherdun.main;

    import org.bukkit.NamespacedKey;
    import org.bukkit.enchantments.Enchantment;
    import org.bukkit.enchantments.EnchantmentTarget;
    import org.bukkit.inventory.ItemStack;

    public class Cenchants extends Enchantment {

        private final String name;
        private final int maxLvl;



        public Cenchants(String namespace, String name,int lvl) {
            super(NamespacedKey.minecraft(namespace));
            this.name = name;
            this.maxLvl = lvl;
        }

        @Override
        public String getName() {
            return name;
        }

        @Override
        public int getMaxLevel() {
            return maxLvl;
        }

        @Override
        public int getStartLevel() {
            return 0;
        }

        @Override
        public EnchantmentTarget getItemTarget() {
            return null;
        }

        @Override
        public boolean isTreasure() {
            return false;
        }

        @Override
        public boolean isCursed() {
            return false;
        }

        @Override
        public boolean conflictsWith(Enchantment other) {
            return false;
        }

        @Override
        public boolean canEnchantItem(ItemStack item) {
            return false;
        }
    }
     
    Customenchants (the enchant):
    Code (Text):
    package me.kosherdun.main;

    import org.bukkit.enchantments.Enchantment;

    import java.lang.reflect.Array;
    import java.lang.reflect.Field;
    import java.util.Arrays;
    import java.util.stream.Collectors;

    public class Customenchants {
        public static final Enchantment ANTITHORNS = new Cenchants("anti thorns", "Anti Thorns", 1);

        public static void register(){
            boolean registered = Arrays.stream(Enchantment.values()).collect(Collectors.toList()).contains(ANTITHORNS);

            if(!registered)
                registerEnchantment(ANTITHORNS);
            }

        public static void registerEnchantment(Enchantment enchantment){
            boolean registered = true;
            try{
                Field f = Enchantment.class.getDeclaredField("acceptingNew");
                f.setAccessible(true);
                f.set(null, true);
                Enchantment.registerEnchantment(enchantment);
            }catch (Exception e){
                registered = false;
                e.printStackTrace();
            }
            if (registered){

            }
        }
    }
     
    note: in Item class ignore the dragon and drip sets take a look only at the zombie helmet
     
  2. Strahan

    Benefactor

    I didn't take the time to read the code; first define:
    "Having an issue" is not particularly helpful. What is the issue? It silently fails? It throws an exception in the console? The wrong thing happens? While it is possible there are glaringly obvious errors in the code, it's best to give us specifics.
     
  3. Maybe would be more easier to help you if you post a log of the error of something instead of a lot of code without explain exactly what's happening.
     
  4. there is no log of errors its just that when i put it on the server ot doesnt work
     
  5. Strahan

    Benefactor

    If you do /pl does the plugin show up in red, green or not at all?