Closed Azatik closed 9 years ago
A question: What do you define as a "private area"?
Also, the Listener annotation has:
Simply specify it as false and you'll get cancelled events.
Private area - Anti Grief plugin
How to use "ignoreCancelled()"?
@Listener(ignoreCancelled=true)
. Read up on annotations :)
I will write in a different way. The player died on the territory of the World Guard. A player can not gain access to the grave (to break grave).
I want execute an event when his cancels other plugin. In my case: Run ChangeBlockEvent.Break, when his cancels the AntiGrief-plugin.
So, just to decipher this:
You only want to handle this event if it was cancelled via the AntiGrief plugin?
Allow to break blocks on the territory of the World Guard (any Anti Grief Plugin).
@Azatik
Then what you want to do is adjust your Order to POST as well as ignoreCancelled = false
Then, if your criteria is met, setCancelled(false)
I created two events. EventBreakBlock:
public class EventBreakBlock {
@Listener
public void onEventBreakBlock(ChangeBlockEvent.Break event) {
if (event.getCause().first(Player.class).isPresent()) {
Player player = (Player) event.getCause().first(Player.class).get();
Text msg = Texts.of(TextColors.DARK_RED, "Action prohibited.");
player.sendMessage(msg);
event.setCancelled(true);
}
}
}
EventBreakBlockAllow:
public class EventBreakBlockAllow {
@Listener(ignoreCancelled=false, order = POST)
public void onEventBreakBlockAllow(ChangeBlockEvent.Break event) {
if (event.getCause().first(Player.class).isPresent()) {
Player player = (Player) event.getCause().first(Player.class).get();
Text msg = Texts.of(TextColors.DARK_GREEN, "Action allow.");
player.sendMessage(msg);
event.setCancelled(false);
}
}
}
Result:
@Azatik you don't need to cast with event.getCause().first(Player.class).get()
since passing the class will already make the return type a Player
. I'll test locally and see if I can reproduce.
@Azatik I ran locally and the event isn't cancelled, so the break is occurring. I'm not seeing the problem in this case.
@gabizou It works fine. Mini Problem: output message "Action prohibited." from event EventBreakBlock.
@Azatik yes, because the listener executed, and cancelled the event. Of course, the second listener will un-cancel it such that it's now not cancelled. This is why it's important for world protection plugins to not only have a listener for the early events to check protections, but a secondary post listener that checks if the event is still cancelled.
Your act of un cancelling the event in POST however is probably bad practice as at that point you're in a race condition of whichever listener gets the event first in the chain.
@gabizou There are better variants?
Could use BEFORE_POST
. POST
is intended for block monitoring plugins like Prism where they just log the activities etc.
Otherwise, this isn't an issue it seems.
I agree.
I'm writing a plugin to the graves. If a player dies in a private area, it will not break the grave. How to allow the player to break the grave in a private area?