Solved Invert getGameRuleValue

Discussion in 'Plugin Development' started by ZonalYewHD, May 23, 2014.

Thread Status:
Not open for further replies.
  1. I am making a command which allows the user to set eternal day/night to true/false. However, in the player.sendMessage, I need to invert the getGameRuleValue("doDaylightCycle"), therefore if it is true, I need to replace it to false, and if it is false, I need to replace it to true. The message below is getting the value of the gamerule doDaylightCycle

    The message now is
    Code:
    player.sendMessage(ChatColor.GOLD+ "Always day is " + ChatColor.RED + player.getWorld().getGameRuleValue("doDaylightCycle"));
    
     
  2. Offline

    L33m4n123

    ever heard of the '!'
     
  3. L33m4n123 I have, but every where I try and place it, I get an error. I just need to know where to place it. Sorry if I wasn't clear on that.
     
  4. Offline

    L33m4n123

    Well. Infront of the boolean that should get negated ofc.
     
  5. L33m4n123 For some reason, I don't think it is getting parsed as a boolean, because the error I have is saying that
    Code:
    The operator ! is undefined for the argument type(s) String
    
     
  6. Offline

    L33m4n123

    Well I don't know where you put it but

    Code:
    boolean someBool = false;
    System.out.println("This returns: " + !someBool);
    will return true. So you don't set it infront of a boolean.

    Edit: By looking into the JavaDocs of bukkit you will realise that
    • world#getGameRuleValue(String)
    does not return a boolean but a string. So my bad. I just assumed that it returns a boolean because you said it returns true so you could do for example

    Code:
    String myBoolean = "true";
    String returnValue = (myBoolean.equals("true")) ? "false" : "true";
    System.out.println(returnValue);
    and the code can be shortened I just like to assign it a variable before printing it
     
  7. just do
    Code:java
    1.  
    2. boolean value = true;
    3. String string = "";
    4. if(value==true){
    5. string = "false";
    6. } else {
    7. string = "true";
    8.  
    9. p.sendMessage("The Value is: "+string);
    10.  
     
  8. Offline

    L33m4n123


    then he can simply

    Code:
    boolean value = true;
    p.sendMessage("The value is: " + !value);
     
    FisheyLP likes this.
  9. Offline

    AronTheGamer

    I guess it still not works

    Try

    player.sendMessage(ChatColor.GOLD+ "Always day is " + ChatColor.RED + (? player.getWorld().getGameRuleValue("doDaylightCycle") == "true" ? "false" : "true"));
     
  10. Offline

    Ronbo

    I hate to be rude, and I also hate to bump old threads, but seriously? This thread is a perfect example of my belief in this: If you don't know Java that well, don't go trying to help others - it just spreads terrible code throughout the community.

    If you respond to a thread, others will think it's being dealt with and not hop on and correct your poor suggestions.

    I get that you're trying to help, but giving poor Java advice really doesn't help.

    Code:
    player.sendMessage(ChatColor.GOLD+ "Always day is " + ChatColor.RED + (? player.getWorld().getGameRuleValue("doDaylightCycle") == "true" ? "false" : "true"));
    wtf? == for Strings? random ? at the beginning of the ternary op?

    Code:
    String string = "";
    if(value==true){
    string = "false";
    } else {
    string = "true";
     
    p.sendMessage("The Value is: "+string);
    What? Doesn't even address OP's problem, since he didn't know how to convert String "true" to boolean true in the first place, and this solution just assumes 'value = true;'.

    Code:
    String myBoolean = "true";
    String returnValue = (myBoolean.equals("true")) ? "false" : "true";
    System.out.println(returnValue);
    So here we have a solution that isn't completely incorrect, but still demonstrates lack of Java knowledge.

    The cleanest fix would've been to simply replace
    Code:
    player.getWorld().getGameRuleValue("doDaylightCycle")
    with
    Code:
    !Boolean.parseBoolean(player.getWorld().getGameRuleValue("doDaylightCycle"))

    Rant over I've just been having a bad 24h...
     
    FisheyLP and nlthijs48 like this.
Thread Status:
Not open for further replies.

Share This Page