Block Update/Lamp on/off

Discussion in 'Spigot Plugin Development' started by WolfLeader116, May 2, 2015.

  1. I am making a plugin that will keep lamps permanently lit if set, or turn on at night and turn off in the day. I have got the permanent on setting to work, but I can't get the light to turn on and off in day and night without a manual block update. I don't know how to trigger the block updates in the repeating task or to get the redstone lamp to turn on/off another way however. Can anyone show me how I can do this? Thanks!
    A bit of code or pseudo code would help me a lot more than just some message because I learn better with that.

    My current code is here: https://github.com/WolfLeader116/Lamps

    Thanks! :)
     
  2. I don't understand the point of this.
    Code (Text):
    try {
                                Location l = new Location(Bukkit.getServer().getWorld(data[0]), Double.valueOf(data[1]), Double.valueOf(data[2]), Double.valueOf(data[3]));
                                if (l.getBlock().getType() == Material.REDSTONE_LAMP_ON) {
                                    l.getBlock().setType(Material.REDSTONE_LAMP_ON);
                                } else if (l.getBlock().getType() == Material.REDSTONE_LAMP_OFF) {
                                    l.getBlock().setType(Material.REDSTONE_LAMP_OFF);
                                }
                            } catch (NullPointerException e) {
                                c.getConfig().set("night." + s, null);
                                c.save();
                            }
     
  3. I agree with you.

    You're basically checking if it's on and then making it on.
     
  4. That was my attemt at a block update. I was trying to just update the block so that the BlockRedstoneEvent would run, but it didn't work. Basically I want to try to get the BlockRedstoneEvent to run or anything else that will cause the block to update and turn off or on or stay the same depending on the time.
     
  5. Unfortunately my old plugin also broke with 1.8 :(

    I'm sure this is not the finest way but this is how i managed now to trigger a redstoneupdate (called from a TimerTask):
    Code (Text):
    public synchronized boolean switchLight(final Block block, boolean on) {
            if(block != null)
            {
                  if(on && block.getType() == Material.REDSTONE_LAMP_OFF)
                  {
                      Block torch = block.getRelative(BlockFace.NORTH);
                      Material mat = torch.getType();
                      torch.setType(Material.REDSTONE_TORCH_ON, true); //place a redstone torch next to it
                    torch.setType(mat, true); //and remove it
                    return true;
                  }
                  if(!on && block.getType() == Material.REDSTONE_LAMP_ON)
                  {
                    block.setType(Material.REDSTONE_LAMP_OFF, true);
                  }
                  return true;
            }
            return false;
        }
    To prevent active lights from turning off while block updates, i set a metadata to the block while loading the config and check in BlockRedstoneEvent.
    Code (Text):

        @EventHandler(priority = EventPriority.HIGHEST)
        public void onBlockRedstoneEvent(BlockRedstoneEvent ev) {
            if(ev.getBlock().getType() == Material.REDSTONE_LAMP_ON)
            {
                if(ev.getBlock().hasMetadata("automated"))
                {
                    ev.setNewCurrent(15);
                    return;
                }
            }
        }
     
    Maybe you should find another way but it works for me. Anyway, hope that helps.
     
    #5 mapstar87, May 21, 2015
    Last edited: May 22, 2015
  6. @mapstar87 why not just set the Material to REDSTONE_LAMP_ON instead of placing a redstone torch next to it?
     
  7. Because it worked only until i updated to 1.8 :/
    If you have better idea, let me know
    For some reason the lamp stays off if i just set the Type to REDSTONE_LAMP_ON
     
    • Useful Useful x 1
  8. NathanWolf

    Supporter

    Have you tried setting it without applying physics updates?

    Either way I think it'll be really fragile- the first time a block update happens it'll probably turn back off.
     
  9. Yes, this was how it didnt work anymore since 1.8
    That's what the BlockRedstoneEvent is for :)
     
    • Agree Agree x 1
  10. You should better use REDSTONE_BLOCK to trigger the the redstone update! Otherwise the torches may drop if there is no solid ground.