Need Help? Variable Randomly Doubling?

Discussion in 'Spigot Plugin Development' started by ThisIsNotAPerson, May 23, 2015.

  1. Hey guys,
    I decided to try to start to code my first plugin. It was going really well, and I seem to have hit a wall. For WHATEVER reason, every minute, instead of subtracting a number from a variable, it is doubling the number, then subtracting it somewhere. The issue is, the code is 1.3k lines, and I cannot, for the life of me, find out the issue. I checked, and the place where it is subtracting one from the variable is working fine, but somewhere in between the time that it subtracts one, and the time that it subtracts one again, the time doubles. I have no multiplication that I could find, and I have NO idea what could be causing this. Anyone might know? (I don't know if I should post the whole 1.3k lines of code in a code box/spoiler... like I said, there is nothing wrong with the section subtracting and then re-writing the variable to the file that I can tell)... Here is the code... It is probably HORRIBLY done, and using WAY more lines then it had to, but this is my first try at a Plugin ^.^ (Don't care about the source being posted. Just a fun little project for me that I might try to post at some point haha)

    (Too long to post here, had to put in pastebin)
  2. Which variables are doing what incorrectly? o.o

    I'm so confused D;
  3. Oh yeah, might help to explain WHAT variable haha, sorry, it's 2am and I'm tired :p Anyway, it's the time that the player is muted or banned. Example is on line 588 (Or around there)
  4. You are writing entries for item meta over, and over, and over. You could easily organize your code into an OOP tree to handle severity levels, and example text for the different 'responses' (ban/mute/kick, etc)

    You can use the Java dates API to handle date formatting. You do not need to format it yourself, and then paste the code several times.

    This (an except from your code that parses arguments):
    Code (Text):
                    else if (i == 8)
                            message = (args[1] + " " + args[2] + " " + args[3] + " " + args[4] + " " + args[5] + " " + args[6] + " " + args[7] + " " + args[8]);
                    else if (i == 9)
                            message = (args[1] + " " + args[2] + " " + args[3] + " " + args[4] + " " + args[5] + " " + args[6] + " " + args[7] + " " + args[8] + " " + args[9]);
                    else if (i == 10)
                            message = (args[1] + " " + args[2] + " " + args[3] + " " + args[4] + " " + args[5] + " " + args[6] + " " + args[7] + " " + args[8] + " " + args[9] + " " + args[10]);
    Is atrocious. Use a loop and a StringBuilder, Java 8's String.join() method, the Google commons Joiner class (built into Bukkit), or just combine your freaking strings in a loop and call trim() if you're really lazy.

    Organize your code first, represent some of your data and processing with objects in another class, and then come back to figuring out your issue with your variable.

    When you're done that, if you still haven't found out the issue with your variable, then I suggest printing debug information to help you find the issue, or using a debugger (that is likely built into your IDE!).

    I seldom have classes that are over 1,000 lines of code (I've had projects over 50,000 lines of code in total, where most classes do not get larger than 500 lines). If you have a class, especially as a beginner, that is 1.4k lines of code, you need to organize!
    #4 Jarcode, May 23, 2015
    Last edited: May 23, 2015
  5. Well... issue... I'm a noob. I don't understand ANYTHING you just said there, and you are basically telling me to rewrite the whole code 0.o

    As far as the organizing thing goes... yeah, I agree there, but again I am a noob and know very little of the language TBH. I just sort of was bored and wanted to start working on something.
  6. You don't have to rewrite any of your code. You have to re-organize it. I couldn't debug an issue with that code, regardless of how simple the error; it's far too difficult to read and navigate.

    Do you understand how objects and classes work in Java? Having a good understanding how to store your functionality in a single object, and then knowing how to instantiate it with the data you need it to operate on, is really important.

    In particular, the twenty if-statements that are being used to combine an array of strings is ridiculous. Never write code like that, ever - use this (you will need to import

    Joiner.on(" ").join(args);
  7. l
    Okay, I will start to look into trying to organize it. I literally asked 3 or 4 people if there was any way to shorten the Ifs and they all said no, just leave it, so it is good to know that there is a way :p

    Thanks for the help ^.^

  8. Thats not even the only way. There are many tutorials that you could have just searched for out there that recommend something like this:

    Code (Text):
    String message = StringUtils.join(args, ' ', 1, args.length);
    1 being the starting point (remembering that arrays start with 0)
  9. Gotta love how the other responses aren't related to the thread question. Sure, the code could be better, but the stuff that's getting pointed out is working and not the focus of this thread.

    I really do hope all of that arg[] appending gets shortened though xD

    Back on topic:
    I've noticed in your time tasks that you set i equal to their old time, then you decremented i, and then when you go to update the new time, you call setMuteTime() or setTime() and pass it i.

    i equals their new time in its entirety, but when you set the new i you are adding it to their oldtime and oldbantime. By doing so, you're effectively doubling the time, but still subtracting the 1.
    #9 iPyronic, May 23, 2015
    Last edited: May 23, 2015
  10. Props for finding the issue. I'm honestly more interested in helping with removing redundant and messy code than sifting through code that has more problems than the issue I'm trying to find.
    • Friendly Friendly x 1