Some different questions

Discussion in 'Plugin Development' started by bigbeno37, Jul 8, 2012.

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

    bigbeno37

    Hey there!

    I have been writing up some code and I need to get some questions I have come across answered.

    1) I want to make a command that gives the user a specified amount of sticks (EG /givemesticks 64). In my onCommand method, I have defined args[0] as an int via parseInt(args[0]), but however I would like a way to determine if that executed successfully (to determine if it was actually an int instead of a String) to then say to the user '/givemesticks (number of sticks)'.

    2) How do I go about removing a single item from a users inventory via getPlayer().getInventory() and not a stack?

    3) Can someone please explain to me why later on when I make advanced plugins that I will need several classes (and even packages) to get my plugin working? An example of a multi-packaged plugin is Factions: https://github.com/MassiveCraft/Factions

    4) How would I go about making a /broadcast command that broadcasts the message defined after the /broadcast part?

    Thanks to anyone who replies, it really helps me out.

    Thanks,
    bigbeno37
     
  2. Offline

    spy_1134

    #3
    It helps you keep your code clean and organized and lets you specify your own types of objects/data for use in your plugins.
     
  3. Offline

    jamietech

    1) Surround the parseInt with a try catch and catch NumberFormatException (this is thrown when it's not an integer).
    2) Set the stack size to one less than it currently is.
    3) You don't need extra packages or classes, they are simply used to allow the developer to easily read their code and find things when they need to be changed.
    4) Create a method that takes an integer and a string array, then loop over each part of the array list, and if the part you're on is higher than the integer, add it to the stringbuilder and append a seperator, then return the string version of the stringbuilder.
    EDIT: Whoops! String array not array list, my bad >.<
     
  4. Offline

    bigbeno37

    So how would I set the stack size to one less?

    Why would someone create multiple packages for their plugins? For as far as I know, outside classes cannot access ANYTHING inside other packages.

    So you're saying that I create a for loop, get it to pass through each of the array elements, and then how exactly do I use a string separator? Also, what is a string builder? Is it an actual part of Java or is it just an expression?
     
  5. Offline

    CorrieKay

    To clarify on number three, Technically you dont need seperate declared classes within a plugin, you could, in theory, use nested classes to create everything you need... But its a HUGE bitch, and invalidates the point of using different classes (not to mention its a technically incorrect usage of nested classes)

    Its for code organization, and instantiation of code.
     
  6. Offline

    bigbeno37

    So essentially, I could have a class just for declaring different objects such as Strings, booleans, and the like?
     
  7. Offline

    CorrieKay

    Aye, but remember to use them XP
     
  8. Offline

    bigbeno37

    Well that's good to know. Time to make <pluginname>...uh...<pluginname>Objects. Actually, that's not a bad name.
     
  9. Offline

    sayaad

    Or you can do as I do, create one API class and a VAR class then dump static variables in the VAR class and static voids in the API class xD
     
  10. Offline

    bigbeno37

    Actually that's not such a bad idea. Just store all of that 'useless' (not really) info and methods in that class and call 'em when needed.

    So back onto question 2, how do I make the stack size one less than it is? What code would I use?
     
  11. Offline

    sayaad

    Code:java
    1. Inventory inven = player.getInventory();
    2. ItemStack[] is = inven.getContents();
    3. for(int i = 0; i < is.length; i++){
    4. ItemStack stack = inven.getItem(i);
    5. if(stack.equals(ItemStackToRemove)){
    6. stack.setAmount(stack.getAmount() - 1);
    7. return;
    8. }
    9. }
    10.  
     
  12. Offline

    bigbeno37

    So why would I need to minus 1 from the stack?
     
  13. Offline

    russjr08

    To remove an item from the stack
     
  14. Cause it's way easier to maintain code that has tons of lines (speaking of hundreds to thousands) if the code is modular and if you need to change something you can find it easily by following logic package and class names. Just have a look at this and think about how that would look with just one class:
    [​IMG]
    That are around 3700 lines of code btw and yes, it is a bukkit plugin.
    Sure you can, the bukkit API would not work if you couldn't: http://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html
     
  15. Offline

    bigbeno37

    Oh thanks for that! I genuinely thought that packages couldn't access others. Look how wrong I was. So when should I start using just multiple packages? When I reach near 1000+ lines of code, or before?

    EDIT:
    What do I replace 'ItemStackToRemove' with?
     
  16. Offline

    jamietech

    The item stack to remove :p
     
  17. Offline

    bigbeno37

    So just say I wanted to remove a diamond. What would I put in?
     
  18. Offline

    CorrieKay

    You dont use packages to seperate quantities of code. I use it to categorize my classes. for instance, my plugin system has a bunch of different packages, all based on what the classes inside do.

    Chat, Handlers, Commands, utils, warp, etc.

    I typically keep my main class file in the parent package, while putting everything else in sub packages.
     
  19. Offline

    one4me

    new ItemStack(Material.DIAMOND, 1)
     
  20. Offline

    bigbeno37

    So how exactly would one create multiple 'listeners'? Also, what does @Override mean? More specifically, what does the '@' do?
     
  21. Offline

    desht

    '@' specifies an annotation - http://docs.oracle.com/javase/1.5.0/docs/guide/language/annotations.html

    In brief, annotations let you specify various "meta" information about your code. @Override tells the compiler that the next method overrides an existing definition in a superclass. This is useful for two reasons: 1) when you or someone else reads your code, you know immediately that it's an overridden method, and 2) if your override doesn't match the original definition, the compiler (or your IDE) can tell you immediately.

    Multiple listener: you put your listener methods in a class (which can be your main plugin class, as long as it implements the Listener interface), and add the @EventHandler annotation above each listener method. @EventHandler is a Bukkit-defined annotation - yes, you can define your own annotations. Then you use the registerEvents() call to tell Bukkit which class(es) in your plugin contain listener methods. http://wiki.bukkit.org/Introduction_to_the_New_Event_System has more on this.

    Finally, why would you use multiple classes in your plugin? Lots of people have already provided some good answers to this, but consider: any complex machine (like a car or a computer) is built from a set of simpler components. The same applies when you're writing code - any non-trivial project really needs to be composed of a set of components (in Java and most OO languages, that means classes). By keeping individual components small, you can easily define their behaviour (data and methods) and how they interact with other components of your project.
     
    CorrieKay likes this.
  22. Offline

    bigbeno37

    Thanks so much for that in-depth reply. Okay, so basically, on the register events command, I can add more than one listener? Or do I need to write that out for as many listeners as I have?
     
  23. Offline

    desht

    The linked document explains it all in detail, but:
    • You must call registerEvents() once for an instance of each class that contains event-handler methods. That's usually done in your onEnable(), but doesn't have to be.
    • One class can contain multiple event-handler methods. Each of those methods has an @EventHandler annotation, and takes a single parameter which is a subclass of the Bukkit Event class.
    • That same class may also contain non-event-handler methods.
    The concept of "listener" here may be a little confusing - it could be used to refer to either the class, or the individual methods. Best to be unambiguous in your terminology.
     
  24. Offline

    bigbeno37

    That there is what I needed to know. What I mean by 'listener' is a class/es that contains one or more @EventHandlers. I know that all classes can contain EventHandlers and just regular methods as well.
     
Thread Status:
Not open for further replies.

Share This Page