Solved Switch "Repeated Lever" logic

Discussion in 'Plugin Development' started by ElCreeperHD, Aug 21, 2016.

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

    ElCreeperHD

    I don't know how to explain this logic, but I want to set something to true every 1 second then set it to false when other second passed.
    A good example of this logic is a simple light that swaps from red to blue every 1 second. I am trying to do this with tasks but I don't know how to make the task change to different color in every execution. I tried using a boolean, but that didn't work.

    Basically I want to create a logic with a repeater and a command block on every side of the repeater.

    Sorry! I didn't knew how to explain this :I I hope you can help me!
     
  2. Offline

    I Al Istannen

  3. Offline

    ElCreeperHD

    @I Al Istannen Using plugins. I did it with a command block but the idea of a plugin is replacing that command block.
     
  4. Offline

    Zombie_Striker

    @ElCreeperHD
    You would need to use something like the following:
    Code:
    boolean isRed = false;
    //Inside your runable
    public void run(){
    if(isRed){
      //Do stuff when it's red
    }else{
      //Do stuff when it's blue
    }
    isRed=!isRed;
    }
     
  5. Offline

    ElCreeperHD

    @Zombie_Striker I can't do that as there is no way to check the stuff that I want to do because it is a part of Bukkit without an API. I already tried that and doing that makes the color change in the same task...
     
  6. Offline

    I Al Istannen

    @ElCreeperHD
    Okay. What do you have? Could you show any relevant code?

    1. Make a BukkitRunnable (scheduler) and run it every x ticks (20 are one second)
    2. Inside the bukkit runnable have a boolean variable "toggle"
    3. In the run method, check if toggle is true ==> Set to one color
    4. If it false ==> Set to other color
    5. Set it to the opposit of what it was
    EDIT: And what should change? A lever? A block?
     
  7. Offline

    ElCreeperHD

    I actually have this executing every 20 ticks:

    Code:
    package tk.cuboftp.PoliceCar;
    
    import org.bukkit.Server;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.scheduler.BukkitRunnable;
    
    public class Task
      extends BukkitRunnable
    {
      boolean d = true;
      private final JavaPlugin plugin;
     
      public Task(JavaPlugin plugin)
      {
        this.plugin = plugin;
      }
     
      public void run()
      {
        if (!this.d)
        {
          this.plugin.getServer().dispatchCommand(this.plugin.getServer().getConsoleSender(), "scoreboard teams option red color red");
          this.d = true;
        }
        if (this.d)
        {
          this.plugin.getServer().dispatchCommand(this.plugin.getServer().getConsoleSender(), "scoreboard teams option red color blue");
          this.d = false;
        }
      }
    }
    
     
  8. Offline

    I Al Istannen

    @ElCreeperHD
    You don't need the two ifs, an if and one else is enough.

    You can say "d = !d" at the end. Saves you from making it true and false on their own line

    Name it better than "d".

    What doesn't work? Does the code run? What happens if you insert a System.out.println statement?
     
  9. Offline

    ElCreeperHD

    The code runs but it swaps the color in the same tick so the visual effect is not even seen.
     
  10. Offline

    I Al Istannen

    @ElCreeperHD
    Oh yea, I see. A logic error.

    Do what I said. Remove the second if and use an else. This way only on of them will be executed. And then remove the "this.d = true | false" and add a "this.d = !this.d" at the end, to flip it.
     
  11. Offline

    ElCreeperHD

    @I Al Istannen I tried with else before and it wasn't working. Well, I will try again and reply with the results.
     
  12. Offline

    I Al Istannen

    @ElCreeperHD
    Well. It really should

    Code:
    if(toggle) {
      // do something
    } else {
      // do something else
    }
    
    toggle = !toggle;
    
    EDIT: Whoops could have quoted you, @Zombie_Striker. Sorry :p
     
    Last edited: Aug 21, 2016
  13. Offline

    ElCreeperHD

  14. Offline

    I Al Istannen

Thread Status:
Not open for further replies.

Share This Page