Solved Java Question

Discussion in 'Plugin Development' started by BurnerDiamond, Dec 23, 2014.

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

    BurnerDiamond

    How do I make one chest work with all classes. I know java and I know how to code bukkit but this is always something that slips my mind! How do I make an Itemstack in the main class, work in all the other classes

    So if I would have an item itemstack in my main. I would like for that itemstack to also work in my other class
     
  2. Offline

    Skionz

    @BurnerDiamond Pass your instance to the classes using constructors and use an accessor method to get the chest.
     
  3. Offline

    BurnerDiamond

    Okay... may I have a more detailed explanation. I know I have to use a constructor

    I thought it was simply

    public static itemstack
     
  4. Offline

    Skionz

  5. Offline

    BurnerDiamond

    Okay I am making a class where an Item has a lore on it and when it gets clicked it opens an inventory

    How do I make sure they are the same item... can I just copy the same Itemstack on each class?

    @Skionz
     
  6. Offline

    SuperOriginal

    @BurnerDiamond

    1. Get instance of main class
    2. Use an accessor method such as getItem()

    If you know Java and Bukkit, you should be able to do that just fine.
     
  7. Offline

    Skionz

    I told you twice... Are you sure you
     
  8. Offline

    ZodiacTheories

    @BurnerDiamond

    The answer is simple if you know Java. It should almost be second nature to you if you know it.
     
    Skionz likes this.
  9. Offline

    BurnerDiamond

    I don't think I made my question clear... No matter I just changed my code a bit and it worked! Now I need to figure out why my lore isn't working!
     
  10. Offline

    SuperOriginal

    @BurnerDiamond If you made it static i'm going to cry. Anways make sure you set the lore and the itemmeta after you're done editing them.
     
  11. Offline

    Skionz

    SuperOriginal likes this.
  12. Offline

    BurnerDiamond

    Nom you don't understand. It was not correct question, you answered correctly but the question wasn't phrased well enough. I did not make it a static I just decided to repeat the same thing in two different classes.
     
  13. Offline

    SuperOriginal

    @BurnerDiamond You wanted to access an itemstack across all classes... We understood.
     
  14. Offline

    Skionz

    @BurnerDiamond This is a good thing to learn, I suggest listening. I will show you an example of how to pass information through a constructor and grab more info with a mutuator.
    Here is my main class which creates a new instance of MyOtherClass and creates a new instance of the Main class for the arguments
    Code:
    public class Main() {
        public static void main(String[] args) {
            new MyOtherClass(new Main());
        }
    
        private String NAME = "My Cool Program";
    
        public String getName() {
            return this.NAME;
        }
    }
    MyOtherClass:
    Code:
    pubic class MyOtherClass() {
        public MyOtherClass(Main main) {
            System.out.println("The name of the program is: " + main.getName());
        }
    }
    The constructor defines how we create a new instance of the class. In this case it takes an instance of the Main class as it parameters and runs our Main classes mutator method which returns the name of the program.
     
Thread Status:
Not open for further replies.

Share This Page