-1

I made a timer which checks if my twitch.tv stream is offline or not. the method get executed all 30 seconds.

If my stream is online the bot will automatically connect to my Stream.

If my Stream if offline there's an INT which will go +1 all 30 seconds and if it reaches 10 which are 5 minutes the bot should just part the IRC channel.

but somehow, the bot continues on counting. Did i made any faults with my if/else statements?

As soon my Stream is offline, bot starts counting all 30 seconds +1 as it should. but when it reaches 10 it just goes higher... its now at 30 already for example but i'm not sure why it doesn't part the channel.

Here is my function;

public void livemodetimer() {



    if(livemode == "off" && TwitchStatus.isstreamlive){
        livemode = "on";

    }else 

    if(livemode == "on" && TwitchStatus.isstreamlive == false){
        zaehler = zaehler+1;

         System.out.println("Stream Offline Counter. Disconnect bei (10).  Aktuell:"+zaehler);

    }else

        if(livemode == "on" && TwitchStatus.isstreamlive == true){
            zaehler = 0;


    if(zaehler >= 10){
        livemode = "off";
        zaehler = 0;
    }



    if (TwitchStatus.isstreamlive && livemode == "on" && multistartprepare == false){
    joinChannel("#"+YBot.MyBot.ownerchannel+"");
    multistartprepare = true;
    startup();
    }

    if(TwitchStatus.isstreamlive == false && livemode == "off" && multistartprepare == true){
          sendMessage("#"+YBot.MyBot.ownerchannel+"","Da der Stream Offline ist verzieh ich mich mal =) Bye!!");
    partChannel("#"+YBot.MyBot.ownerchannel+"");
    multistartprepare = false;
    zaehler = 0;

    TTmsg.cancel();
    TTmsg.purge();
    }
}

Does anyone have an idea why it doesn't call the partchannel stuff when it reaches 10.

Edited Version Below:

public void livemodetimer() {



    if(livemode == false && TwitchStatus.isstreamlive){
        livemode = true;

    }else 

    if(livemode && TwitchStatus.isstreamlive == false){
        zaehler = zaehler+1;

         System.out.println("Stream Offline Counter. Disconnect bei (10).  Aktuell:"+zaehler);

    }else


    if(livemode && TwitchStatus.isstreamlive == true){
        zaehler = 0;
    }


    if(zaehler >= 10){
        livemode = false;
        zaehler = 0;
    }



    if (TwitchStatus.isstreamlive && livemode && multistartprepare == false){
    joinChannel("#"+YBot.MyBot.ownerchannel+"");
    multistartprepare = true;
    startup();
    }

    if(TwitchStatus.isstreamlive == false && livemode == false && multistartprepare){
          sendMessage("#"+YBot.MyBot.ownerchannel+"","Da der Stream Offline ist verzieh ich mich mal =) Bye!!");
    partChannel("#"+YBot.MyBot.ownerchannel+"");
    multistartprepare = false;
    zaehler = 0;

    TTmsg.cancel();
    TTmsg.purge();
    }    
}
user3220962
  • 371
  • 1
  • 7
  • 19

3 Answers3

4

You have several problems.

You don't have an ending brace for your statement

if(livemode == "on" && TwitchStatus.isstreamlive == true){
            zaehler = 0;

It's hard to tell where you want the block closed, partly because your code uses tabs to indent, which doesn't get displayed very well here (also, don't mix tabs and spaces, your code editor should have a way to convert one to the other). Using consistent indentation helps me to keep from getting confused about nesting of braces.

Because the indentation is unclear I'm not sure what you want the else clauses to do, it would be better to use braces to explicitly show the blocks that you mean to be executed. Using a consistent formatting scheme like One True Brace style would be a big help here.

Basically pick a consistent way to format your code that is very explicit and stick to it, and you will have an easier time.

Using == for string comparison is bad (because it compares references, you could have 2 separate references both holding "on", and == would return false). You should use the equals method instead because it will compare the values.

Also using == true for boolean comparisons is redundant. It doesn't hurt anything, it's just awkward-looking.

Nathan Hughes
  • 94,330
  • 19
  • 181
  • 276
1

You are comparing strings with == when they need to be compared with equals

if(livemode == "on")

does not test if the value in livemode is equal to "on". Rather, it tests if these two objects are at the same physical memory location, which is most definitely what you do not want, since it is obviously possible for two strings in two different objects to contain the same value.

Change it to

if(livemode.equals("on")
aliteralmind
  • 19,847
  • 17
  • 77
  • 108
1

Your if-statement String comparison should use .equals()

String is an Object type in Java meaning it needs to call an .equals() function in order to properly compare it to other Strings. The == operator, if I'm not mistaken (I often am), will compare the memory locations of the Object and report false when the comparison fails.

if ( livemode.equals("on") && ...

should fix your issues.

Grambot
  • 4,370
  • 5
  • 28
  • 43