Need help with errors

Discussion in 'Plugin Development' started by seang96, Jul 13, 2013.

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

    seang96

    I am working on a shop plugin. The problem that I am having at the moment seems to be a infinite loop with the "if(data.getInt(i1 + ".Amount") <= amount) {" and statements and an ArrayIndexOutOfBoundsException exception at line 208 when confirm is not used as an argument which is right before the "if (args[3].equalsIgnoreCase("confirm")) {" statements.

    The point of this part is to find thecheapest price of item, buy that first until it is empty, and if buyer has more amount then go for the next cheapest on the market.

    Thanks for the help. Tell me if more code is needed.


    Code:java
    1. if(args[0].equalsIgnoreCase("Buy")) {
    2. Material mat = Material.matchMaterial(args[1]);
    3. int id = mat.getId();
    4. int amount = Integer.parseInt(args[2]);
    5. int totalprice = 0;
    6. int currentamount = 0;
    7. int totalamount = amount;
    8. int itemamount = 0;
    9. while(currentamount < totalamount) {
    10. int lowestPrice = 2147483647;
    11. for(int i = 1; i <= data.getInt("Total"); i++) {
    12. if((data.getInt(i + ".Item") == id) && (data.getString(i + ".Status") == "Selling") && (data.getInt(i + ".Price") <= lowestPrice)) {
    13. lowestPrice = data.getInt(i + ".Price");
    14. if(data.getInt(i + ".Amount") <= amount) {
    15. int price = data.getInt(i + ".Price") * data.getInt(i + ".Amount");
    16. totalprice = + price;
    17. itemamount = + data.getInt(i + ".Amount");
    18. currentamount = + itemamount;
    19. amount = - itemamount;
    20. if(args[3].equalsIgnoreCase("confirm")) {
    21. EconomyResponse r1 = econ.depositPlayer(data.getString(i + ".Player"), price);
    22. if(r1.transactionSuccess()) {
    23. data.set(i + ".Status", "Sold");
    24. saveYamls();
    25. }
    26. }
    27. }
    28. else if(data.getInt(i + ".Amount") > amount){
    29. int price = amount * data.getInt(i + ".Price");
    30. totalprice = + price;
    31. itemamount = + amount;
    32. currentamount = + itemamount;
    33. if(args[3].equalsIgnoreCase("confirm")) {
    34. EconomyResponse r1 = econ.depositPlayer(data.getString(i + ".Player"), price);
    35. if(r1.transactionSuccess()) {
    36. data.set(i + ".Amount", (data.getInt(i + ".Amount") - amount));
    37. saveYamls();
    38. }
    39. }
    40. }
    41. if(args[3].equalsIgnoreCase("confirm")) {
    42. if(currentamount == totalamount && currentamount == itemamount) {
    43. EconomyResponse r = econ.withdrawPlayer(p.getName(), totalprice);
    44. if(r.transactionSuccess()) {
    45. ItemStack is = new ItemStack (mat, itemamount);
    46. p.getInventory().addItem(is);
    47. sender.sendMessage(String.format("You have bought " + totalamount + " of " + mat + " for " + totalprice + "."));
    48. }
    49. }
    50. }
    51. if(!(args[3].equalsIgnoreCase("confirm"))){
    52. if(!(data.getString(i + ".Check") == "False")) {
    53. loadYamls();
    54. data.set(i + ".Check", "False");
    55. saveYamls();
    56. if(currentamount == totalamount) {
    57. sender.sendMessage(String.format("You will pay " + totalprice + " for " + amount + " of " + mat + "."));
    58. for(int i2 = 1; i2 <= data.getInt("Total"); i2++) {
    59. if((data.getString(i2 + ".Check") == "False") && (data.getInt(i2 + ".Item") == id)) {
    60. loadYamls();
    61. data.set(i2 + ".Check", null);
    62. saveYamls();
    63. }
    64. }
    65. }
    66. }
    67. }
    68. }
    69. }
    70. }
    71. }



    EDIT: After further testing the loop causes the error "[SEVERE] Reached end of stream for /127.0.0.1" An example of how the error occurs is when someone sells 2 stone for 5, then sells 1 stone for 2. Then if they buy 2 stone it should buy the 1 for $2 then 1 of the 2 for $5, but that is when it errors.
    EDIT2: Changed code a little bit, removed useless loop. Both errors are still existent.
     
  2. Offline

    savagesun

    The EOS thing isn't an error, bukkit just sends that when someone views the server list. Have you checked to make sure there are 4 different arguments after the command? The IndexOutOfBounds means that there is no args[3].
     
  3. Offline

    seang96

    Confirm is the 4th argument, but i want it to do something else when args[3] isn't used (that is when the error occurs). Line 51-62 is what i want it to do without it. Any way to stop the error from happening and actually do what I want it to do? Also do you have any insight on why the plugin doesn't loop in line 9? It disconnects users and leaves no errors.

    Bump, does anyone have solutions for these problems?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 3, 2016
Thread Status:
Not open for further replies.

Share This Page