[CLOSED]I need a few tips for a Java beginner

Discussion in 'Plugin Development' started by nggmc, Jul 24, 2012.

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

    nggmc

    [CLOSED]
    Alright. I was wondering how to create a random event example.

    /hi would randomly give you an item based on the items listed in config.yml which would have to be completely out of random. I also would like to know how to create that config.yml file :3. If theres any tutorial that would be just as good!

    Thanks!
     
  2. You should read Plugin Tutorial from the wiki
    It also contains how to use configs.

    And for your specific plugin, you'll need to create a List<ItemStack> and load the items from the config in onEnable() then in the command you just use new Random().nextInt(list.size()) and use that number in list.get(num) and voilla, it gets you one random entry from that list :)

    EDIT: fixed :}
     
  3. Two corrections:
    1. Don't use a new random generator every time. Make one and store it in a global vriable, then use it every time -> faster code execution
    2. Don't use -1 or it will never access the last list entry:
    (Source: http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Random.html#nextInt(int) )
     
    MuisYa likes this.
  4. Offline

    nggmc

    I dont quite understand. I got everything to work.. config and all but apparently list.size cannot be resolved.. :(. Is it suppossed to be List and if it is where should I import it from?
     
  5. list.size()

    Code:
    List<ItemStack> list = new ArrayList<ItemStack>();
    But if you want to list items (just names) you can use Material instead of ItemStack and use getConfig().getStringList(), loop through it, use Material.getMaterial(string) and add it to the list... then just use that random, and when getting the material you can use new ItemStack(material, 1) to create the item.


    Import would be from java.util I belive, I dunno, I never manually import stuff, I only look in the quick-fix list to see what import should it use if there are more classes of the same name.
     
  6. Offline

    russjr08

    I just use Ctrl+Shift+O when in Eclipse :D
     
  7. Offline

    nggmc

    Like this?
    Code:
     
            List<ItemStack> list = new ArrayList<ItemStack>();
    ItemStack random = new Random().nextInt(list.size());
     
  8. nggmc
    You seem to not understand what those codes do...

    new Random().nextInt() returns an integer... the list.size() is the amount of elements there are in the list, using that in nextInt() will return a random number between 0 and list.size() - 1 (the exclusive part V10lator was talking about)... and you can use that random number in list.get(number) to get the element from that index from the list...

    But like I said, if you just want to use item names (no data values and no amounts), you can just use Material instead of ItemStack and then create the ItemStack when you need to drop it... like I explained in my previous post :}
     
  9. Offline

    nggmc

    I get the type List is not generic?
    EDIT: Kay, im soo confused lol. I guess im just wasting your time :3 I will just give up on Java :(..
    Thanks!
     
  10. You need to learn basic Java before you get on writing plugins for Bukkit... expecially if you don't have any other programming experience with other languages.

    There are alot of basic Java tutorials, search them on google.
     
  11. Offline

    nggmc

    I fiddled around with my plugin a bit. And I was able to get the random feature to work. Thanks So much! :)
     
Thread Status:
Not open for further replies.

Share This Page