How do I reference my other class in main class while creating custom items?

Discussion in 'Spigot Help' started by kosher_pig, Jun 19, 2021.

  1. NamespacedKey key = new NamespacedKey(here, "Dragon_Set_ChestPlate");
    basically where I wrote here how do I reference it in my main class


    thanks in advance
     
  2. You can pass the plugin instance as a constructor parameter in your secondary class, and assign it to a field. Then you can use that field as an argument for the NamespacedKey constructor. This technique is called 'dependency injection' via the constructor.
    Alternatively, you could use JavaPlugin.getPlugin(MyPlugin.class)
     
  3. There are many ways to do this, I am pretty lazy so I reference it statically instead of a more object oriented approach (such as dependency injection). My main class would look something like this:

    Code (Text):
     public final class TestPlugin extends JavaPlugin {
        private static TestPlugin instance;

        @Override
        public void onEnable() {
            instance = this;
        }


        public static TestPlugin getInstance() {
            return instance;
        }
    }
    You can then use the getInstance() method to get the plugin object
     
    #3 BoBoBalloon, Jun 19, 2021
    Last edited: Jun 19, 2021
  4. so i need to do what u did abd just replace test plugin with the bame of the class tjat i have? and than I can write this in the other class?
     
    #4 kosher_pig, Jun 20, 2021
    Last edited: Jun 20, 2021
  5. what do you mean?
     
  6. he worte there " private static TestPlugin instance" so o kust change the yest plugin to what my class called like dont I?
     
  7. Yes. You need to change it to your Main class name.
     
  8. and than in the pther class i weite this or what ever my main class is called?
     
  9. I have no idea what you're talking about.
     
  10. after i put this in the main class the instence on the other class where i need to enter the key o write "this" or whatever is my main class
     
  11. Main.getInstance()
     
  12. WOWOWOWOW TYYYYYY UR THE BEST I SEARCHED A SOLUTION FOR HOURS TYYYY
     
  13. The code is running good now but when I put it on the server it doesn't work?
    any way that's the code:

    Main class:
    package me.kosherdun.main;

    import org.bukkit.NamespacedKey;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.NamespacedKey;
    import org.bukkit.plugin.java.JavaPlugin;

    public final class Main extends JavaPlugin {


    @Override
    public void onEnable() {
    Items.init();
    }



    @Override
    public void onDisable() {

    }
    }





    -------------------------------------------------------------------------------------------------------------------
    the Items code:




    package me.kosherdun.main;

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

    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 void init() { crateDRAGONSETCHESTPLATE(); crateDRAGONSETLEGS(); crateDRAGONSETBOOTS();}



    private static void crateDRAGONSETCHESTPLATE() {
    ItemStack item = new ItemStack(Material.LEATHER_CHESTPLATE, 1);
    LeatherArmorMeta lch = (LeatherArmorMeta) item.getItemMeta();
    lch.setColor(Color.fromBGR(0, 0, 0));
    ItemMeta meta = item.getItemMeta();
    meta.setDisplayName(ChatColor.LIGHT_PURPLE + "Dragon Set ChestPlate ");
    List<String> lore = new ArrayList<>();
    lore.add(ChatColor.GOLD + "This Armor 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);
    meta.addItemFlags(ItemFlag.HIDE_ENCHANTS);
    item.setItemMeta(lch);
    item.setItemMeta(meta);
    dragon_set_chestplate = item;


    ShapedRecipe sr = new ShapedRecipe(NamespacedKey.minecraft("dragon_set_chestplate"), item);

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

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

    Bukkit.getServer().addRecipe(sr);


    }


    private static void crateDRAGONSETLEGS() {
    ItemStack item = new ItemStack(Material.LEATHER_LEGGINGS, 1);
    LeatherArmorMeta lch = (LeatherArmorMeta) item.getItemMeta();
    lch.setColor(Color.fromBGR(0, 0, 0));
    ItemMeta meta = item.getItemMeta();
    meta.setDisplayName(ChatColor.LIGHT_PURPLE + "Dragon Set Leggings ");
    List<String> lore = new ArrayList<>();
    lore.add(ChatColor.GOLD + "This Armor 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);
    meta.addItemFlags(ItemFlag.HIDE_ENCHANTS);
    item.setItemMeta(lch);
    item.setItemMeta(meta);
    dragon_set_leggings = item;


    ShapedRecipe sr = new ShapedRecipe(NamespacedKey.minecraft("dragon_set_leggings"), item);

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

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

    Bukkit.getServer().addRecipe(sr);


    }

    private static void crateDRAGONSETBOOTS() {
    ItemStack item = new ItemStack(Material.LEATHER_BOOTS, 1);
    LeatherArmorMeta lch = (LeatherArmorMeta) item.getItemMeta();
    lch.setColor(Color.fromBGR(0, 0, 0));
    ItemMeta meta = item.getItemMeta();
    meta.setDisplayName(ChatColor.LIGHT_PURPLE + "Dragon Set Boots ");
    List<String> lore = new ArrayList<>();
    lore.add(ChatColor.GOLD + "This Armor 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);
    meta.addItemFlags(ItemFlag.HIDE_ENCHANTS);
    item.setItemMeta(lch);
    item.setItemMeta(meta);
    dragon_set_boots = item;


    ShapedRecipe sr = new ShapedRecipe(NamespacedKey.minecraft("dragon_set_boots"), item);

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

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

    Bukkit.getServer().addRecipe(sr);


    }
    }





    (I went to a different approach to the problem cuz I saw it working and suggestions as for what I did wrong?)
     
  14. Please use code block.
     
  15. Main:
    Code (Text):



    package me.kosherdun.main;

    import org.bukkit.NamespacedKey;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.NamespacedKey;
    import org.bukkit.plugin.java.JavaPlugin;

    public final class Main extends JavaPlugin {


    @Override
    public void onEnable() {
    Items.init();
    }



    @Override
    public void onDisable() {

    }
    }
    the Items class:
    Code (Text):

    package me.kosherdun.main;

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

    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 void init() { crateDRAGONSETCHESTPLATE(); crateDRAGONSETLEGS(); crateDRAGONSETBOOTS();}



    private static void crateDRAGONSETCHESTPLATE() {
    ItemStack item = new ItemStack(Material.LEATHER_CHESTPLATE, 1);
    LeatherArmorMeta lch = (LeatherArmorMeta) item.getItemMeta();
    lch.setColor(Color.fromBGR(0, 0, 0));
    ItemMeta meta = item.getItemMeta();
    meta.setDisplayName(ChatColor.LIGHT_PURPLE + "Dragon Set ChestPlate ");
    List<String> lore = new ArrayList<>();
    lore.add(ChatColor.GOLD + "This Armor 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);
    meta.addItemFlags(ItemFlag.HIDE_ENCHANTS);
    item.setItemMeta(lch);
    item.setItemMeta(meta);
    dragon_set_chestplate = item;


    ShapedRecipe sr = new ShapedRecipe(NamespacedKey.minecraft("dragon_set_chestplate"), item);

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

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

    Bukkit.getServer().addRecipe(sr);


    }


    private static void crateDRAGONSETLEGS() {
    ItemStack item = new ItemStack(Material.LEATHER_LEGGINGS, 1);
    LeatherArmorMeta lch = (LeatherArmorMeta) item.getItemMeta();
    lch.setColor(Color.fromBGR(0, 0, 0));
    ItemMeta meta = item.getItemMeta();
    meta.setDisplayName(ChatColor.LIGHT_PURPLE + "Dragon Set Leggings ");
    List<String> lore = new ArrayList<>();
    lore.add(ChatColor.GOLD + "This Armor 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);
    meta.addItemFlags(ItemFlag.HIDE_ENCHANTS);
    item.setItemMeta(lch);
    item.setItemMeta(meta);
    dragon_set_leggings = item;


    ShapedRecipe sr = new ShapedRecipe(NamespacedKey.minecraft("dragon_set_leggings"), item);

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

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

    Bukkit.getServer().addRecipe(sr);


    }

    private static void crateDRAGONSETBOOTS() {
    ItemStack item = new ItemStack(Material.LEATHER_BOOTS, 1);
    LeatherArmorMeta lch = (LeatherArmorMeta) item.getItemMeta();
    lch.setColor(Color.fromBGR(0, 0, 0));
    ItemMeta meta = item.getItemMeta();
    meta.setDisplayName(ChatColor.LIGHT_PURPLE + "Dragon Set Boots ");
    List<String> lore = new ArrayList<>();
    lore.add(ChatColor.GOLD + "This Armor 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);
    meta.addItemFlags(ItemFlag.HIDE_ENCHANTS);
    item.setItemMeta(lch);
    item.setItemMeta(meta);
    dragon_set_boots = item;


    ShapedRecipe sr = new ShapedRecipe(NamespacedKey.minecraft("dragon_set_boots"), item);

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

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

    Bukkit.getServer().addRecipe(sr);


    }
    }
    BTW tyvm for helping me sooo much <3
     
  16. I'd recommend that you first read https://www.spigotmc.org/wiki/recipe-example/
     
  17. I know that's is how I did it in the start then I changed to this cuz I thought this way would work but it does not work as well do u want me to send u what I tried with the method u sent now?