Redstone for Non-Redstone Implementors

Discussion in 'Plugin Development' started by Eeketh, Jan 22, 2011.

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

    Eeketh

    If you want a generic material to react to redstone, how do you go about doing this?

    e.g. if you want a sign or stone to react to redstone

    It doesn't implement redstone, so won't trigger the onRedstoneChange.
    Could try using onRedstoneChange and looking for signs nearby, but how do you know if it's able to power the sign using the normal rules?

    e.g. redstone torch on a wall, or on a floor reacts differently, but there's no way to tell where the torch is. Similar issue with redstone wire - it only powers what it's pointing at, so if it goes to sign but then turns, it shouldn't transmit power, but again you can't see what the redstone looks like...

    Am I missing something, or does something need to be added to the API before this can be done?

    (ideally, would be awesome if everything was defined to hold a charge, but only some things transfer it to other blocks, so you can easily detect if a block should be powered or not... I assume tracks and doors will have to work something like this also as they only receive power?)
     
  2. Offline

    eisental

    Since you can't give redstone current to a sign or stone, the only way would be to listen to onRedstoneChange and check if the changed block is adjacent to the your block (block.getFace(BlockFace) comes to mind).

    I think it's not possible to know if a redstone wire is actually pointing to your block unless you manually write the code to check it, which should take quite some time to do.
     
  3. Offline

    Tsurara

    @eisental Could you give an example code? I'm not quite sure how it is done. (and right now I don't have the time to play with it).
    Thanks!
     
  4. Offline

    eisental

    It can be alot of work to cover all the possibilities but it should be something like this

    Code:
                public void onBlockRedstoneChange(BlockFromToEvent event) {
                    if (event.getBlock()==signBlock.getFace(BlockFace.EAST))
                             // redstone current in block next to the east face of your sign block
                    else if (event.getBlock()==signBlock.getFace(BlockFace.NORTH)
                             // and so on
                }
    
    You can check my plugin source code, i'm doing the same thing to read the inputs.
     
  5. Offline

    Afforess

    From Minecart Mania Core:

    Code:
    /**
    	 ** Returns true if the block at the given x, y, z coordinates is indirectly powered
    	 ** @param x coordinate
    	 ** @param y coordinate
    	 ** @param z coordinate
    	 **/
    	public static boolean isBlockIndirectlyPowered(int x, int y, int z) {
    		return isBlockPowered(x+1, y, z) || isBlockPowered(x-1, y, z) || isBlockPowered(x, y, z+1) || isBlockPowered(x, y, z-1) || isBlockPowered(x, y-1, z);
    	}
    	/**
    	 ** Returns true if the block at the given x, y, z coordinates is directly powered
    	 ** @param x coordinate
    	 ** @param y coordinate
    	 ** @param z coordinate
    	 **/
    	public static boolean isBlockPowered(int x, int y, int z) {
    		MaterialData md = getWorld().getBlockAt(x, y, z).getState().getData();
    		if (md instanceof Redstone) {
    			return ((Redstone) md).isPowered();
    		}
    		return false;
    	}
    
     
  6. Offline

    Tsurara

    Thanks you both very much :D
    I've tried your method, Afforess, but for some reason it's not working. be warned: stupid question ahead.
    I need to check if "isBlockPowered / isBlockIndirectlyPowered" is true? ><
     
  7. Offline

    Afforess

    First off, did it even compile? You need a method getWorld() for isBlockPowered to work.

    Second, if you want to check that a block that is not redstone dust or torch is powered, use indirectly powered. If you are checking a block that is redstone dust or a torch, use isBlockPowered. Yes, True == active power.
     
  8. Offline

    Tsurara

    Yes, I added the getWorld method.
    oh, I think I know what the problem was, I'll work on it tomorrow :3
    Thanks a bunch~
     
  9. Offline

    Eeketh

  10. Offline

    Tsurara

    Bah, I'm stumped. I'm clearly doing something really, really wrong, as it does not seems to work.
    can anyone kind enough download the source (Here) and see what I'm doing wrong? >:
    sorry ^^'
     
  11. Offline

    eisental

    Can you explain what you're trying to do and what happens instead? I looked at the source but couldn't understand what you're using the isBlockIndirectlyPowered for. Is the redstone supposed to activate the sponge?
     
  12. Offline

    Tsurara

    Yes, sorry for the not-clean code, I need to do it, I'm pretty sick so it's a mess, can't even think straight :p
    I'm trying to check if there is an active redstone near the sponge block. if there is, it activates. else, nothing happens.
    Nothing happens - in a way, nothing changed from the original code - the plugin is still working, sucking water.
    Thanks!~
     
Thread Status:
Not open for further replies.

Share This Page