Why I cant prevent place water in InteractBlockEvent.Secondary?

sponge 8.0

How can I prevent place water ?

Please help me.

Just doubled checked your work, i found that the event is cancelled yet water still spawns

    @Listener
    public void delMe(InteractBlockEvent.Secondary event, @First ServerPlayer player) {
        HandType hand = event.context().get(EventContextKeys.USED_HAND).get();
        ItemStack stack = player.itemInHand(hand);
        if (stack.type().equals(ItemTypes.WATER_BUCKET.get())) {
            event.setCancelled(true);
        }
    }

I could cancel it with this though

    @Listener
    public void delMeSecond(InteractItemEvent.Secondary event, @First ServerPlayer player) {
        if (event.itemStack().type().equals(ItemTypes.WATER_BUCKET.get())) {
            event.setCancelled(true);
        }
    }
1 Like

Thank you so much. but How can I check the target block in InteractItemEvent.Secondary that player has interacted ?

Could probably get it out of the context with EventContextKeys.BLOCK_HIT

https://jd.spongepowered.org/spongeapi/8.1.0/org/spongepowered/api/event/EventContextKeys.html#BLOCK_HIT

1 Like

Thank for you care!

1 Like

not working.
e.context().get(EventContextKeys.BLOCK_HIT).orElse(null) is always null. not exists.

image

Ill take a look when i get a sec

If it’s ergent you could get the block the player is looking at using RayTrace

1 Like

Please send me an example code to get the block the player is looking at using RayTrace

Something like

ServerPlayer player;
Optional<RayTraceResult<LocatableBlock>> opTrace = RayTrace
  .block()
  .continueWhileBlock(RayTrace.nonAir())
  .sourceEyePosition(player)
  .direction(player)
  .limit(7)
  .execute();
if(opTrace.isEmpty()){
    //player is not looking at a block
    return;
}
LocatableBlock lookingAt = opTrace.get().selectedObject();

1 Like

image

error in this line.
.sourceEyePosition(player)

Cannot resolve method ‘sourceEyePosition’ in ‘Predicate’

Is it api 8.0 or 8.1? its in 8.0 too

Here is the javadocs for it

https://jd.spongepowered.org/spongeapi/8.0.0/org/spongepowered/api/util/blockray/RayTrace.html#sourceEyePosition(org.spongepowered.api.entity.living.Living)

Just reread the docs

Its meant to be

RayTrace.block().continueWhileBlock(RayTrace.nonAir())

My bad

edit:
i have updated my code example

1 Like

Thank for you care!