Adding multiple enchantments to different items

Discussion in 'Spigot Help' started by SpyingBadge2143, Apr 3, 2021.

  1. I'm wondering how I could add enchantments to more then one item and wondering if it's possible to save your inventory into a kit and possibly a open source plugin I can look at to help with that.
    Code:

    Code (Java):
    package com.spyingbadge2143.kit_plugin;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.enchantments.Enchantment;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import org.bukkit.event.inventory.InventoryType;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemFlag;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.java.JavaPlugin;

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

    public class Kit_Plugin extends JavaPlugin implements Listener {

        public static Inventory kits;

        @Override
        public void onEnable() {
            System.out.println(ChatColor.AQUA + "Kit Plugin Running V 1.0 (:) SpyingBadge2143");
            this.getServer().getPluginManager().registerEvents(this, this);
            createInventory();


        }

        @Override
        public void onDisable() {
            System.out.println(ChatColor.AQUA + "Kit Plugin Shutting down V 1.0 (:) SpyingBadge2143");
        }


        private void createInventory() {
            Inventory inv = Bukkit.createInventory(null, 9, ChatColor.GREEN + "Kits");
            ItemStack item = new ItemStack(Material.NETHERITE_SWORD);
            ItemMeta meta = item.getItemMeta();
            meta.setDisplayName(ChatColor.GRAY + "Crystal+");
            List<String> lore = new ArrayList<>();
            lore.add("");
            lore.add(ChatColor.DARK_RED + "Click for this kit!");
            meta.addEnchant(Enchantment.DAMAGE_ALL, 1, true);
            meta.addItemFlags(ItemFlag.HIDE_ENCHANTS);
            item.setItemMeta(meta);
            inv.setItem(1, item);

            item.setType(Material.NETHERITE_SWORD);
            meta = item.getItemMeta();
            meta.setDisplayName(ChatColor.DARK_RED + "Totem+");
            item.setItemMeta(meta);
            inv.setItem(2, item);

            kits = inv;
        }


        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (label.equalsIgnoreCase("kits")) {
                if (!(sender instanceof Player)) {
                    sender.sendMessage("Joep you can't use kits in the console xD");
                    return true;
                }
                Player player = (Player) sender;
                player.openInventory(kits);
            }
            return false;
        }

        @EventHandler
        public void onClick(InventoryClickEvent event) {
            if (!event.getView().getTitle().contains("Kits"))
                return;
            if (event.getCurrentItem() == null)
                return;
            if (event.getCurrentItem().getItemMeta() == null)
                return;

            Player player = (Player) event.getWhoClicked();
            event.setCancelled(true);
            if (event.getClickedInventory().getType() == InventoryType.PLAYER)
                return;

            if (event.getSlot() == 1) {
                if (!player.hasPermission("Kits.Kits")) {
                    player.sendMessage(ChatColor.GRAY + "Kit Given!");
                    return;

                }
                player.closeInventory();
                player.updateInventory();
                return;

            }
            if (event.getSlot() == 2) {
                if (!player.hasPermission("Kits.Kits")) {
                    player.sendMessage(ChatColor.GRAY + "Kit Given!");
                    return;

                }
                this.give(player, this.getTotemKit());
                player.closeInventory();
                player.updateInventory();
                return;
            }
            private ItemStack[] getTotemKit()){
                player.getInventory().addItem(new ItemStack(Material.NETHERITE_SWORD, 1));
                player.getInventory().addItem(new ItemStack(Material.NETHERITE_AXE, 1));
                player.getInventory().addItem(new ItemStack(Material.NETHERITE_PICKAXE, 1));
                player.getInventory().addItem(new ItemStack(Material.ENCHANTED_GOLDEN_APPLE, 128));
            };
    return items;
        }
        private void getTotemKit(){
            ItemStack item = new ItemStack(Material.NETHERITE_SWORD);
            ItemMeta meta = item.getItemMeta();
            meta.addEnchant(Enchantment.DAMAGE_ALL, 5, true);
            meta.addEnchant(Enchantment.FIRE_ASPECT, 2, true);
            meta.addEnchant(Enchantment.SWEEPING_EDGE, 3, true);
            meta.addEnchant(Enchantment.VANISHING_CURSE, 1, true);
            meta.addEnchant(Enchantment.DURABILITY, 3, true);
            meta.addEnchant(Enchantment.MENDING, 1, true);
            ItemStack item1 = new ItemStack(Material.NETHERITE_AXE);
            ItemMeta meta = item1.getItemMeta();
            meta.addEnchant(Enchantment.DIG_SPEED, 5, true);
            meta.addEnchant(Enchantment.DAMAGE_ALL, 5, true);
            meta.addEnchant(Enchantment.VANISHING_CURSE, 1, true);
            meta.addEnchant(Enchantment.DURABILITY, 3, true);
            meta.addEnchant(Enchantment.MENDING, 1, true);
        }



        }




        }
     
  2. I don't understand what you're programming, but have you definitely learned the basics of java?
     
  3. I've watched some tutorials on java and I'm trying to script a kit plugin.
     
  4. Im tired at the moment so wont help you with your full problem but I can see that after you edit itemmeta you dont set the meta to the item.

    so basicly on getTotemKit() you are missing, item1.setitemMeta(meta);