Detecting Redstone Power

Discussion in 'Plugin Development' started by Edward Hand, Feb 21, 2011.

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

    Edward Hand

    I want to call an event when some random block starts being powered by redstone but REDSTONE_CHANGE events are only called when redstone dust/torches/switches and the like change power state. Does anyone have any good way of doing this?
     
  2. Offline

    fullwall

    Well, for RainbowWool I just bruteforced - every REDSTONE_CHANGE event, I checked all the blocks around if they were a wool type. You're probably best off doing that and seeing if it's a specific material, or you could do random checks every so often on set blocks, and use block.isPowered().
     
  3. Offline

    Edward Hand

    Can you tell me what the difference is between block.isBlockPowered() and block.isBlockIndirectlyPowered()?
     
  4. Offline

    fullwall

    Ummmm... I'm not exactly sure, to be honest. Check the commit logs/javadocs, I guess.
     
  5. Offline

    Edward Hand

    The javadocs are useless :S

    I've worked it out I think. isBlockPowered() is what you'd expect. isBlockIndirectlyPowered() is like asking "if I replaced this block with redstone dust, would that dust be powered"

    Thanks for your help btw.
     
  6. Offline

    fullwall

    That's OK. Maybe you could help me with hooking into minecraft.server classes? I'm trying to make a workbench plugin, but I can't find out how to tell when it's open, or what it has...
     
  7. Offline

    Edward Hand

    Found it.

    You would tell when its open simply by detecting when they right clicked on a work bench.

    You can tell what is in it with the following:
    Code:
    ((CraftPlayer)somePlayer).getHandle().activeContainer.a.a
    That variable holds an array of 9 ItemStacks.

    But note: these are of type net.minecraft.server.ItemStack and are NOT the same as the bukkit ones. To get data from them, access the public variables type, amount, data and durability.

    For detecting closing of the crafting bench use:
    Code:
    if( ((CraftPlayer)somePlayer).getHandle().activeContainer == null)
    Hope that answers your question.
     
  8. Offline

    fullwall

    How do I get the result (What comes out)? You're so good at finding the code that does stuff :p.
     
  9. Offline

    Edward Hand

    Thanks ;)

    Code:
    ((CraftPlayer)somePlayer).getHandle().activeContainer.b.a[0]
    --- merged: Feb 21, 2011 1:43 PM ---
    (And if you want to thank me in your plugin thread I wont complain ;))
     
  10. Offline

    fullwall

    Of course :). Just remind me if I forget (I probably won't). One last question - how would I check when it is changed? Would I just have to keep checking every so often till its closed?
     
  11. Offline

    Edward Hand

    Yeah. There doesn't seem to be an event being called for that. Just make a separate thread and loop inside of there with 50ms sleeps until they close it and there shouldn't be any problems.
     
  12. Offline

    fullwall

    What if a whole heap of players are opening workbenches. Lag?
     
  13. Offline

    Edward Hand

    So long as you make sure to do the looping within a separate thread and so long as you remember to Sleep(50) every time you go round the loop and so long as you remember to make the thread stop when they close the workbench, there is unlikely to be any effect. It's not like whatever you're doing is going to be that processor intensive, right?
     
  14. Offline

    fullwall

    Not really, but I'm a cautious guy :). I haven't done much threading/concurrency in Java yet - just learnt to start coding for bukkit. What would I use to accomplish threading? A timer/scheduled executor service/bukkit schedule?
     
  15. Offline

    Edward Hand

  16. Offline

    fullwall

    Thanks for the link and your help :). I'm assuming the threaded plugin is InfiniteLava?
     
  17. Offline

    Edward Hand

    No. It's an unreleased plugin I made for playing midi files.
    http://www.youtube.com/watch?v=ha2MBUGiFU0
    (The playing of the notes is done within a separate thread)
     
  18. Offline

    fullwall

    That's an awesome plugin. I'd love to see the source, if it's not too much trouble. Definitely getting credit for your time :).
     
  19. Offline

    Edward Hand

    I'll send you a message with a link so it doesn't get nicked [​IMG]
     
  20. Offline

    fullwall

    Don't worry, I won't do anything evil :). Are you still working on it?
     
  21. Offline

    Edward Hand

    Well, I just made it as a proof of concept and its not especially usable just yet. I might finish and release it. I think the author of craftbook has something similar planned for craftbook though, so I don't want to release it and then it immediately be overshadowed by craftbooks version if you know what I mean.
     
  22. Offline

    fullwall

    I see. You could release it targeted for those who don't want all of craftbook's features, just the MIDI bit? Yours looks pretty easy to set up too.
     
  23. Offline

    Edward Hand

    I have so many ideas for plugins I want to make. So many!
     
  24. Offline

    fullwall

    The activecontainer.a.a field is not visible :(.
     
  25. Offline

    Edward Hand

    Ah. Try activecontainer.a.getContents()
     
  26. Offline

    fullwall

    activecontainer.a itself is not visible, sorry if that wasn't clear.
     
  27. Offline

    Edward Hand

    Ah. Right. You need to cast it as a ContainerWorkbench like this:

    Code:
    ((ContainerWorkbench)((CraftPlayer)somePlayer).getHandle().activeContainer).a.getContents()
     
  28. Offline

    fullwall

    ItemStack result = ((ContainerWorkbench)ep.activeContainer).b
    .getContents()[0]; - This would get the result? Thank you SO MUCH for going through the code. I can never find anything in that mess :p.
     
  29. Offline

    Edward Hand

    It should do, yes.
    --- merged: Feb 21, 2011 7:34 PM ---
    Sorry. I just looked into this more closely and to detect when they are no longer using the bench you need to check for:

    Code:
    playerStuff.activeContainer == playerStuff.defaultContainer
    rather than null as I said previously.
     
Thread Status:
Not open for further replies.

Share This Page