Solved Need help with randoms.

Discussion in 'Plugin Development' started by Rockindavies21, Jun 9, 2014.

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

    Rockindavies21

    Hey everyone, i have a problem. I'm making a plugin for random items/money from a chest and it has a few bugs i can't seem to fix. The first one is, when someone types /magicchest it doesn't give them the same amount of money as announced. Also, after around 100-200 uses the plugin says "An internal error has occured" in red and the plugin goes dead. Any help will be much appreciated :)!

    Code:
    Code:java
    1. package me.rockindavies21.tcrandomchest;
    2.  
    3. import java.util.Random;
    4. import java.util.logging.Logger;
    5.  
    6. import net.milkbowl.vault.economy.Economy;
    7.  
    8. import org.bukkit.Bukkit;
    9. import org.bukkit.Material;
    10. import org.bukkit.Sound;
    11. import org.bukkit.command.Command;
    12. import org.bukkit.command.CommandSender;
    13. import org.bukkit.entity.Player;
    14. import org.bukkit.event.Listener;
    15. import org.bukkit.inventory.ItemStack;
    16. import org.bukkit.inventory.PlayerInventory;
    17. import org.bukkit.plugin.RegisteredServiceProvider;
    18. import org.bukkit.plugin.java.JavaPlugin;
    19.  
    20. public class TCRandomChest extends JavaPlugin implements Listener{
    21. private static final Logger log = Logger.getLogger("Minecraft");
    22.  
    23. public static Economy econ = null;
    24.  
    25. @Override
    26. public void onEnable() {
    27. if (!setupEconomy() ) {
    28. log.severe(String.format("[%s] - Disabled due to no Vault dependency found!", getDescription().getName()));
    29. getServer().getPluginManager().disablePlugin(this);
    30. return;
    31. }
    32.  
    33. }
    34.  
    35.  
    36. @Override
    37. public void onDisable() {
    38.  
    39. }
    40.  
    41. private boolean setupEconomy() {
    42. if (getServer().getPluginManager().getPlugin("Vault") == null) {
    43. return false;
    44. }
    45. RegisteredServiceProvider<Economy> rsp = getServer().getServicesManager().getRegistration(Economy.class);
    46. if (rsp == null) {
    47. return false;
    48. }
    49. econ = rsp.getProvider();
    50. return econ != null;
    51. }
    52.  
    53. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    54. Player p = (Player) sender;
    55. if(!(sender instanceof Player)){
    56. sender.sendMessage("You must be ingame to do this!");
    57. return true;
    58. }
    59.  
    60. if(commandLabel.equalsIgnoreCase("magicchest")){
    61. if(p.hasPermission("tc.magic.chest")){
    62.  
    63. Random rdm = new Random();
    64. int Chance = rdm.nextInt(100);
    65.  
    66. if(Chance == 100) {
    67. p.playSound(p.getLocation(), Sound.LEVEL_UP, 1, 1);
    68. Bukkit.broadcastMessage("§e===========================================");
    69. Bukkit.broadcastMessage("§a§l " + p.getName() + " §fFound the §a§lWARS §fSword" );
    70. Bukkit.broadcastMessage(" §fin the magical chest!");
    71. Bukkit.broadcastMessage("§e===========================================");
    72. }
    73.  
    74. if(Chance == 95) {
    75. p.playSound(p.getLocation(), Sound.LEVEL_UP, 1, 1);
    76. Bukkit.broadcastMessage("§e===========================================");
    77. Bukkit.broadcastMessage(" §a§l " + p.getName() + " §fFound a §b§lBEACON §fin the" );
    78. Bukkit.broadcastMessage(" §fmagical chest!");
    79. Bukkit.broadcastMessage("§e===========================================");
    80.  
    81. ItemStack itemstack = new ItemStack(Material.BEACON);
    82.  
    83. PlayerInventory pi = p.getInventory();
    84.  
    85. pi.addItem(itemstack);
    86.  
    87. }
    88.  
    89.  
    90. if(Chance <= 90) {
    91. p.sendMessage("§e===========================================");
    92. p.sendMessage(" §fYou have found §a§l$" + rdm.nextInt(Chance) + " §fin the" );
    93. p.sendMessage(" §fchest, better luck next time!");
    94. p.sendMessage("§e===========================================");
    95.  
    96. econ.depositPlayer(p.getName(), rdm.nextInt(Chance));
    97. p.playSound(p.getLocation(), Sound.CHEST_OPEN, 1, 1);
    98. }
    99.  
    100.  
    101.  
    102.  
    103. }
    104. }
    105. return false;
    106. }
    107. }
     
  2. Offline

    MCCoding

    Rockindavies21

    It's because you are generating two random numbers that's why they are different, do this

    Code:java
    1. int randomNumber = rdm.nextInt(Chance);


    then in your broadcast do randomNumber instead of rdm.nextInt(Chance)
     
  3. Offline

    Rockindavies21

    MCCoding Still doesn't work :/.

    EDIT: It works i didn't change the broadcaster the first time. Thanks!
     
  4. Offline

    spy_1134

    You should replace where it displays the message about the amount earned and where you deposit money in the player's account with just the number Chance, not a randomly generated number between 0 and Chance.

    Make sure that you fix the amount you are depositing as well.
     
  5. Offline

    Wingzzz

    Mark the thread as solved if it is! :p
     
  6. Offline

    Hellgast

    Doing this, "Chance" will never be 100. Only 0 to 99.
    1. int Chance = rdm.nextInt(100);
    2. if(Chance == 100) {
     
  7. Offline

    Rockindavies21

    spy_1134 Fixed.

    Wingzzz I will :p i still have another problem.

    Hellgast Changed to 99.

    The other error i have is about after 100 uses of the chest (works fine) i get an internal error and the plugin goes dead.
     
  8. Offline

    Necrodoom

  9. Offline

    Rockindavies21

    Necrodoom
    Current code:
    Code:java
    1. package me.rockindavies21.tcrandomchest;
    2.  
    3. import java.util.Random;
    4. import java.util.logging.Logger;
    5.  
    6. import net.milkbowl.vault.economy.Economy;
    7.  
    8. import org.bukkit.Bukkit;
    9. import org.bukkit.Material;
    10. import org.bukkit.Sound;
    11. import org.bukkit.command.Command;
    12. import org.bukkit.command.CommandSender;
    13. import org.bukkit.entity.Player;
    14. import org.bukkit.event.Listener;
    15. import org.bukkit.inventory.ItemStack;
    16. import org.bukkit.inventory.PlayerInventory;
    17. import org.bukkit.plugin.RegisteredServiceProvider;
    18. import org.bukkit.plugin.java.JavaPlugin;
    19.  
    20. public class TCRandomChest extends JavaPlugin implements Listener{
    21. private static final Logger log = Logger.getLogger("Minecraft");
    22.  
    23. public static Economy econ = null;
    24.  
    25. @Override
    26. public void onEnable() {
    27. if (!setupEconomy() ) {
    28. log.severe(String.format("[%s] - Disabled due to no Vault dependency found!", getDescription().getName()));
    29. getServer().getPluginManager().disablePlugin(this);
    30. return;
    31. }
    32.  
    33. }
    34.  
    35.  
    36. @Override
    37. public void onDisable() {
    38.  
    39. }
    40.  
    41. private boolean setupEconomy() {
    42. if (getServer().getPluginManager().getPlugin("Vault") == null) {
    43. return false;
    44. }
    45. RegisteredServiceProvider<Economy> rsp = getServer().getServicesManager().getRegistration(Economy.class);
    46. if (rsp == null) {
    47. return false;
    48. }
    49. econ = rsp.getProvider();
    50. return econ != null;
    51. }
    52.  
    53. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    54. Player p = (Player) sender;
    55. if(!(sender instanceof Player)){
    56. sender.sendMessage("You must be ingame to do this!");
    57. return true;
    58. }
    59.  
    60. if(commandLabel.equalsIgnoreCase("magicchest")){
    61. if(p.hasPermission("tc.magic.chest")){
    62.  
    63. Random rdm = new Random();
    64. int Chance = rdm.nextInt(100);
    65. int randomNumber = rdm.nextInt(Chance);
    66.  
    67. if(Chance == 99) {
    68. p.playSound(p.getLocation(), Sound.LEVEL_UP, 1, 1);
    69. Bukkit.broadcastMessage("§e===========================================");
    70. Bukkit.broadcastMessage("§a§l " + p.getName() + " §fFound the §a§lWARS §fSword" );
    71. Bukkit.broadcastMessage(" §fin the magical chest!");
    72. Bukkit.broadcastMessage("§e===========================================");
    73. }
    74.  
    75. if(Chance == 95) {
    76. p.playSound(p.getLocation(), Sound.LEVEL_UP, 1, 1);
    77. Bukkit.broadcastMessage("§e===========================================");
    78. Bukkit.broadcastMessage(" §a§l " + p.getName() + " §fFound a §b§lBEACON §fin the" );
    79. Bukkit.broadcastMessage(" §fmagical chest!");
    80. Bukkit.broadcastMessage("§e===========================================");
    81.  
    82. ItemStack itemstack = new ItemStack(Material.BEACON);
    83.  
    84. PlayerInventory pi = p.getInventory();
    85.  
    86. pi.addItem(itemstack);
    87.  
    88. }
    89.  
    90.  
    91. if(Chance <= 90) {
    92. p.sendMessage("§e===========================================");
    93. p.sendMessage(" §fYou have found §a§l$" + randomNumber + " §fin the" );
    94. p.sendMessage(" §fchest, better luck next time!");
    95. p.sendMessage("§e===========================================");
    96.  
    97. econ.depositPlayer(p.getName(), randomNumber);
    98. p.playSound(p.getLocation(), Sound.CHEST_OPEN, 1, 1);
    99. }
    100.  
    101.  
    102.  
    103.  
    104. }
    105. }
    106. return false;
    107. }
    108. }
     
  10. Offline

    Necrodoom

  11. Offline

    Rockindavies21

  12. Offline

    Necrodoom

    Internal error means you got an error, and thus there would be a stacktrace for it.
     
    xTigerRebornx likes this.
  13. Offline

    Rockindavies21

    Necrodoom I know, but nothing shows up in the console at all when i get the error.
     
  14. Offline

    Necrodoom

    Paste console from when you execute command then.
     
  15. Offline

    Rockindavies21

    Necrodoom Wowwww, it doesn't show up in the console but it's in the logs -.-. Must be something with multicraft idk. But, it's a serversigns error. The plugin i use to bind /magicchest to a chest. Gonna go ask them. Thanks.

    Marked as solved! :)
     
  16. Offline

    Wingzzz

    Rockindavies21
    Mutlicraft limits lines to something like 120/s by default (not sure if configurable). Always look for the message or prefer referencing the log files.
     
Thread Status:
Not open for further replies.

Share This Page