Redstone Lamp State

Discussion in 'Plugin Development' started by Doooogle, Jan 14, 2014.

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

    Doooogle

    I've been trying to get it so if the block below you is an off redstone lamp, it turns it on, then off when you walk off it. I've already tried this, and it works:
    Code:java
    1. if(player.getLocation().getBlock().getRelative(BlockFace.DOWN).getType().equals(Material.REDSTONE_LAMP_OFF)){
    2.  
    3. Block block = player.getLocation().getBlock().getRelative(BlockFace.DOWN);
    4. block.setType(Material.REDSTONE_LAMP_ON);
    5.  
    6. }

    When using this on the server, it creates super lag, especially when running on it with speed, which is my intention. I was wondering if there was a way to simply change its state instead of completely replacing the block? Would be wonderful, thanks!
     
  2. Your code is not efficient enough.

    Block b = player.getLocation().getBlock().getRelative(BlockFace.DOWN);
    if(b.getType()==Material.REDSTONE_LAMP_OFF){
    b.setType(Material.REDSTONE_LAMP_ON);
    }
     
  3. Offline

    MHF_Wither

    his code looks as efficient as it's gonna get, you're just checking if the lamp is off a second time, kinda pointless.
     
  4. The code i posted checks the lamp 1 time...
     
  5. Offline

    MHF_Wither

    Sorry, I was assuming you where using his first if statement.
     
  6. Offline

    metalhedd

    The efficiency of this is going to be far more drastically affected by how you call this from PlayerMoveEvent than by chosing either of those 2 very similar variations. @Louis1234567890987654321's caches the results of the getRelative call which is a bit more efficient.
     
    desht likes this.
  7. Offline

    Doooogle

    Thank you very much Louis1234567890987654321 and others :)

    I just got time to give it a spin on my server; reduced lag by a tiny bit, but it's still there and annoying :/ is there anyway at all just to change the state of the lamp?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 6, 2016
  8. Offline

    metalhedd

    How are you calling this code? something like this should reduce the lag as much as possible:

    Code:java
    1.  
    2. public void onPlayerMove(PlayerMoveEvent event) {
    3. Location to = event.getTo().getBlock().getLocation();
    4. Location from = event.getFrom().getBlock().getLocation();
    5. if (!to.equals(from)) {
    6. // check for a lamp and turn it on if necessary
    7. }
    8. }
    9.  
     
  9. Offline

    Garris0n

    The reason this is causing lag is because of lighting updates from the lamp, not the move event or getRelative checks. Test it with different blocks to confirm, but I'm fairly sure that's the issue.

    Don't check the .equals, check the x, y, and z, as that will always be false (yaw/pitch changes call the move event). Checking the x, y, and z as integers would be even better because you'd only check when they moved blocks. Read my post above, though, I don't think that's the issue.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 6, 2016
  10. Offline

    metalhedd


    Read closer, I get the BLOCK locations, they WILL be equal.
     
  11. Offline

    Garris0n

    Ah, I didn't read that, okay. That's probably still not significant enough to matter, especially if you switched from .getRelative() to location.subtract(0, 1, 0).getBlock().
     
  12. Offline

    Doooogle

    Guys I asked the simplest question; is - it - possible - to - change - the - state - of - the - lamp? I already stated in my main post what the lag issue was; Garris0n was right, it is the block changes not the getRelative.
     
  13. Offline

    atesin

    take a look a plugin named lampcontrol

    do you want to make a "billy jean" minecraft style music video ? :D
     
Thread Status:
Not open for further replies.

Share This Page