What's wrong?

Discussion in 'Plugin Development' started by AttilaTheHunni, Apr 12, 2018.

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


    As you will undoubtedly be able to tell from my code, I'm absolutely new to coding and would really appreciate a bit of help developing my Dice Roller Plugin.

    The plugin is meant to allow players to roll various dice of their choosing using the format (number of dice)d(dice value)+ or -(modifier), with each of the parenthesis instead being their respective number.
    Ie: 1d20+5 , 5d30-3 , 2d400 , etc.

    So far the code:

    1) Works flawlessly when performing a single simple d20 roll, which is the default if no other parameters are specified (ie: /roll ).

    2) Works flawlessly when performing a single specific roll without a modifier (ie: 1d6 , 1d50 , 1d32)

    3)Works to an extent when performing a single specific roll with a modifier.

    The problem occurs, for whatever reason, when the user attempts to take a negative modifier (ie: 1d5-2). Instead of sending the player a single message containing the result of the roll, it sends the result seven times. I have no idea why this is the case, but the plugin seems to handle positive modifiers just fine (ie: 1d6+3).

    Aside from that, I can't seem to figure out how to send multiple results depending on the chosen number of dice (ie: 5d20 sending 5 1d20s to the player). How would I go about doing this?

    Here's a link to my source: https://pastebin.com/ezZW8Kq2

    Thanks in advance for both your time and potential suggestions, they're all greatly appreciated.
  2. Offline


    I think you are overthinking this. The code is far larger and has far more variables than needed.

    Try doing the following:
    1. onCommand
    2. Create a List<String>. This will contain the three expressions for the amount, the die value, and the modifer.
    3. Create a StringBuilder. This will help you build the expression.
    4. Create a for loop to loop through all the characters in the message.
    5. For each character, if the char is a '+', add "Stringbuilder#toString()" to the list and clear the stringbuilder.
    6. Else, if the char is a '-', do the same thing, but after clearing the stringbuilder, add '-'. This will make sure the next value is negative
    7. Else, if the char is 'd', we don't need it, so just continue.
    8. Else, if it is neither + nor -, add the character to the Stringbuilder.
    9. Now, outside of the loop, create a for loop representing the amount of dice being rolled. Let it to be equal to the Integer version of the value for the first String in the list.
    10. Then, roll a dice with the same amount of sides as the second entry for the list plus the modifier from the third entry.
  3. Also, you should be casting the sender to a Player AFTER you check instanceof. And if blocks don't need an else after them, you can remove the empty ones and hopefully cleanup your code a bit.

    Sent from my SM-G903F using Tapatalk
Thread Status:
Not open for further replies.

Share This Page