Hi! I’m currently developing a plugin that uses Signs to do many things. I’m trying to trigger Redstone (or Dropper and Dispenser) but it seems kinda impossible.
The latest documentation says something about modifying BlockStates with Cause.source in the code, but Cause.source doesn’t exist anymore in SpongeAPI.
I’ve searched almost everywhere, and tried different “solutions”.
Ho do you guys trigger redstone from an event?
If its redstone wire (may work with others) then just do
state.offer(Keys.POWER, 6);
Apparently, not working either.
(The method offer(Key<MutableBoundedValue>, int) is undefined for the type BlockState)
I’m retrieving state from location.getBlock();
I’ve tried with location.offer(Keys.POWER, 6);
still not working (this doesn’t give error tho.)
Currently, the code is this:
public void activateRed(Location<World> location)
{
BlockState state = location.getBlock();
BlockType blocktype = location.getBlockType();
if (blocktype.equals(BlockTypes.DISPENSER)||
blocktype.equals(BlockTypes.DROPPER))
{
state.offer(Keys.POWERED, true);
} else if (blocktype.equals(BlockTypes.REDSTONE_WIRE)) {
state.offer(Keys.POWER, 15);
}
}
BlockState
s are immutable, so it’s not possible to offer
Key
s to them in the same way as you might an ItemStack
. The with(Key, value)
method returns a new BlockState
with the given data (provided that it supports that Key
, otherwise it’ll return Optional.empty()
). This new BlockState
can then replace the other one through Location#setBlock
.
However, I’m not sure if that would have the desired functionality for you because it likely just changes the state without running the code that performs the relative actions - you may end up powering one block for a tick, but it may not effect any other blocks. I’ve never worked with redstone through the API, but my understanding is that it’s a bit limited and due for some updates. Hopefully, someone familiar with this area of the API can take a look at this and point you in the right direction.
As a minor note, the BlockType
is available from the BlockState
and prevents the same lookup from being done twice.
Hey thanks for the advice! Changing it now, as for the “with” thing. Tried that too, doesn’t really work, it doesn’t return Optional.empty()
because i differentiated blocks that uses Keys.POWERED
and Keys.POWER
. Thanks for all the answers guys.
So, I applied again the new BlockState method but those BlockStates doesn’t seem to contain Keys.POWERED or Keys.POWER, which is odd because those blocks have those Keys.
Implying that toggle is true, this is the actual code
public void RedstoneEngine(Location<World> location, boolean toggle)
{
BlockState state = location.getBlock();
BlockType blocktype = state.getType();
if (blocktype.equals(BlockTypes.DISPENSER)||
blocktype.equals(BlockTypes.DROPPER))
{
BlockState newstate = state.with(Keys.POWERED, toggle).get();
location.setBlock(newstate);
} else if (blocktype.equals(BlockTypes.REDSTONE_WIRE)) {
if (toggle==true)
{
BlockState newstate = state.with(Keys.POWER, 15).get();
location.setBlock(newstate);
}
else if (toggle==false)
{
BlockState newstate = state.with(Keys.POWER, 0).get();
location.setBlock(newstate);
}
}
}