How to Make a Random Event Bukkit Plugin!

Discussion in 'Resources' started by Bench3, Feb 5, 2012.

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

    Bench3

    Please Comment, Rate and Subscribe if this helped you!

    CLICK HERE!
     
  2. Offline

    CRAZYxMUNK3Y

    This should be in Plugin Development (Resources).

    c0mp
    Could you move it?
     
  3. Offline

    c0mp

    Done, thanks!
     
  4. Offline

    nisovin

    Two things:

    1. Why did you add a for loop that only ever has one iteration? It's completely unnecessary.
    2. The way you threw on that extra curly brace to the end of the file makes me cringe. You should always keep your code indented properly, otherwise you'll make stupid mistakes.
     
  5. Offline

    Coryf88

    A couple of things to add to nisovin's list.
    • No need to cast sender to a Player, just use sender.sendMessage. If you want the command to only be usable by a player, do an instanceof check on the sender and send a message to the sender if they aren't a player. Yours will result in an exception if the command is used from the server console or something else.
    • Should return true if the command is a valid command.
    • Why add 1 to the return of Random.nextInt? Just use 0 and 1 instead of 1 and 2.
    • Should create the Random instance as a class instance instead of a method instance.
    • Could use a switch or ternary operator instead of an if/elseif.
    Code:JAVA
    1. private Random random = new Random();
    2.  
    3. public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
    4. if (label.equalsIgnoreCase("flip")) {
    5. if (sender instanceof Player) {
    6. String output = null;
    7. switch (this.random.nextInt(2)) {
    8. case 0:
    9. output = ChatColor.RED + "HEADS!";
    10. break;
    11. case 1:
    12. output = ChatColor.GOLD + "TAILS!";
    13. break;
    14. }
    15. if (output != null) { // Just in case
    16. sender.sendMessage(output);
    17. }
    18. } else {
    19. sender.sendMessage("This command can only be used by a player.");
    20. }
    21. return true;
    22. }
    23. return false;
    24. }
     
  6. Offline

    xenox_ghost


    Hey man just wanted to say thank you for contributing this, it helped me out with my project greatly (sorta unrelated). Thanks
     
Thread Status:
Not open for further replies.

Share This Page