how to use diffrent classes in one plugin ?

Discussion in 'Spigot Plugin Development' started by rickerd120, Jun 25, 2015.

  1. hi,

    i am making a plugin, and i have, when you are not op. you cant please a tnt block..
    but i want olso that you cant craft it i have made the codes in a new class soooo..

    I have to class(es) but, when i open the server you can still craft tnt.. how can i use diffrent classes in a plugin?

    tnt craft code;

    package me.rickerd120.norecipe;

    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.ShapedRecipe;
    import org.bukkit.plugin.java.JavaPlugin;

    public class recipes extends JavaPlugin{

    public void onEnable(){
    ShapedRecipe TNT = new ShapedRecipe(new ItemStack(Material.AIR));
    TNT.shape(new String[]{"£*£","*£*","£*£"}).setIngredient('£', Material.SULPHUR).setIngredient('*', Material.SAND);
    Bukkit.getServer().addRecipe(TNT);
    }

    public void onDisable(){
    Bukkit.getServer().clearRecipes();
    }

    }
     
    • Agree Agree x 1
  2. foncused

    Moderator Patron

    You should either pastebin your code or use:
    Code (Text):
    // code
    Class names should always be CamelCase. Change public class recipes to public class Recipes.

    What is it you're trying to do with multiple classes? You just create the new class and put the code in it that you want. People do this for organization among other reasons. You can have classes for listeners, runnables, utilities, or whatever else your heart/OCD desires (for me it's both).

    If you wanted to create a class called 'Utilities' and have it contain a method like 'getX', you could call it by doing:
    Code (Text):
    Utilities.getX();
     
  3. yea... but i cant make all the things in one class?

    soo do i have to open a new class in the package?
     
  4. Ok, so after I've transcripted your post, here are a few things for you to do:
    1) Get yourself a good tutorial on Java basics.
    2) Learn about the concept of object-oriented-programming.
    3) If you're not a native English speaker, I won't blame you, but if you are ... wow ... you should really work on your phrasing and grammar.

    For your solution:
    You can import a class at the top (before your class declaration) so that you can use its public methods and public variables inside another class. If it is already in the same package, you can already use it without importing it. If you have no idea what I'm talking about, refer to my 1st and 2nd step I gave you before.
     
    • Like Like x 1
  5. foncused

    Moderator Patron

    You can create a new class inside the package you already have or create another package to put the class in. The difference comes down to organizational preference and having to import classes outside of the package you're working in.
     
    • Like Like x 1
  6. yea .. i am not English, Dutch so my English it not so good, yea i can use google translater. But thats make it not better i think :')
    i watched some tutorials but, do you know a site or a better video to learn how i can make plugins?
     
  7. foncused

    Moderator Patron

    You should really pick up Java before you start writing plugins. Following tutorials on the Bukkit API without knowing the language the framework is written in will only get you so far.