1
- /**
2
- * This file is part of Skript.
3
- *
4
- * Skript is free software: you can redistribute it and/or modify
5
- * it under the terms of the GNU General Public License as published by
6
- * the Free Software Foundation, either version 3 of the License, or
7
- * (at your option) any later version.
8
- *
9
- * Skript is distributed in the hope that it will be useful,
10
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
- * GNU General Public License for more details.
13
- *
14
- * You should have received a copy of the GNU General Public License
15
- * along with Skript. If not, see <http://www.gnu.org/licenses/>.
16
- *
17
- * Copyright Peter Güttinger, SkriptLang team and contributors
18
- */
19
1
package ch .njol .skript .effects ;
20
2
21
- import org .bukkit .entity .Player ;
22
- import org .bukkit .event .Cancellable ;
23
- import org .bukkit .event .Event ;
24
- import org .bukkit .event .Event .Result ;
25
- import org .bukkit .event .block .BlockCanBuildEvent ;
26
- import org .bukkit .event .inventory .InventoryInteractEvent ;
27
- import org .bukkit .event .player .PlayerDropItemEvent ;
28
- import org .bukkit .event .player .PlayerInteractEvent ;
29
- import org .bukkit .event .player .PlayerLoginEvent ;
30
- import org .jetbrains .annotations .Nullable ;
31
-
32
3
import ch .njol .skript .Skript ;
33
4
import ch .njol .skript .bukkitutil .PlayerUtils ;
34
5
import ch .njol .skript .doc .Description ;
42
13
import ch .njol .skript .log .ErrorQuality ;
43
14
import ch .njol .skript .util .Utils ;
44
15
import ch .njol .util .Kleenean ;
16
+ import org .bukkit .entity .Player ;
17
+ import org .bukkit .event .Cancellable ;
18
+ import org .bukkit .event .Event ;
19
+ import org .bukkit .event .block .BlockCanBuildEvent ;
20
+ import org .bukkit .event .entity .EntityToggleSwimEvent ;
21
+ import org .bukkit .event .inventory .InventoryInteractEvent ;
22
+ import org .bukkit .event .player .PlayerDropItemEvent ;
23
+ import org .bukkit .event .player .PlayerInteractEvent ;
24
+ import org .bukkit .event .player .PlayerLoginEvent ;
25
+ import org .jetbrains .annotations .Nullable ;
45
26
46
- /**
47
- * @author Peter Güttinger
48
- */
49
27
@ Name ("Cancel Event" )
50
28
@ Description ("Cancels the event (e.g. prevent blocks from being placed, or damage being taken)." )
51
- @ Examples ({"on damage:" ,
52
- " victim is a player" ,
53
- " victim has the permission \" skript.god\" " ,
54
- " cancel the event" })
29
+ @ Examples ({
30
+ "on damage:" ,
31
+ "\t victim is a player" ,
32
+ "\t victim has the permission \" skript.god\" " ,
33
+ "\t cancel the event"
34
+ })
55
35
@ Since ("1.0" )
56
36
public class EffCancelEvent extends Effect {
37
+
57
38
static {
58
39
Skript .registerEffect (EffCancelEvent .class , "cancel [the] event" , "uncancel [the] event" );
59
40
}
60
41
61
42
private boolean cancel ;
62
-
63
- @ SuppressWarnings ("null" )
43
+
64
44
@ Override
65
- public boolean init (final Expression <?>[] vars , final int matchedPattern , final Kleenean isDelayed , final ParseResult parser ) {
45
+ public boolean init (Expression <?>[] expressions , int matchedPattern ,
46
+ Kleenean isDelayed , ParseResult parseResult ) {
66
47
if (isDelayed == Kleenean .TRUE ) {
67
- Skript .error ("Can't cancel an event anymore after it has already passed" , ErrorQuality .SEMANTIC_ERROR );
48
+ Skript .error ("An event cannot be cancelled after it has already passed" , ErrorQuality .SEMANTIC_ERROR );
68
49
return false ;
69
50
}
51
+
70
52
cancel = matchedPattern == 0 ;
71
- final Class <? extends Event >[] es = getParser ().getCurrentEvents ();
72
- if (es == null )
53
+ Class <? extends Event >[] currentEvents = getParser ().getCurrentEvents ();
54
+
55
+ if (currentEvents == null )
56
+ return false ;
57
+
58
+ if (cancel && getParser ().isCurrentEvent (EntityToggleSwimEvent .class )) {
59
+ Skript .error ("Cancelling a toggle swim event has no effect" );
73
60
return false ;
74
- for (final Class <? extends Event > e : es ) {
75
- if (Cancellable .class .isAssignableFrom (e ) || BlockCanBuildEvent .class .isAssignableFrom (e ))
61
+ }
62
+
63
+ for (Class <? extends Event > event : currentEvents ) {
64
+ if (Cancellable .class .isAssignableFrom (event ) || BlockCanBuildEvent .class .isAssignableFrom (event ))
76
65
return true ; // TODO warning if some event(s) cannot be cancelled even though some can (needs a way to be suppressed)
77
66
}
67
+
78
68
if (getParser ().isCurrentEvent (PlayerLoginEvent .class ))
79
69
Skript .error ("A connect event cannot be cancelled, but the player may be kicked ('kick player by reason of \" ...\" ')" , ErrorQuality .SEMANTIC_ERROR );
80
70
else
@@ -83,24 +73,24 @@ public boolean init(final Expression<?>[] vars, final int matchedPattern, final
83
73
}
84
74
85
75
@ Override
86
- public void execute (final Event e ) {
87
- if (e instanceof Cancellable )
88
- ((Cancellable ) e ).setCancelled (cancel );
89
- if (e instanceof PlayerInteractEvent ) {
90
- EvtClick .interactTracker .eventModified ((Cancellable ) e );
91
- ((PlayerInteractEvent ) e ).setUseItemInHand (cancel ? Result .DENY : Result .DEFAULT );
92
- ((PlayerInteractEvent ) e ).setUseInteractedBlock (cancel ? Result .DENY : Result .DEFAULT );
93
- } else if (e instanceof BlockCanBuildEvent ) {
94
- ((BlockCanBuildEvent ) e ).setBuildable (!cancel );
95
- } else if (e instanceof PlayerDropItemEvent ) {
96
- PlayerUtils .updateInventory (((PlayerDropItemEvent ) e ).getPlayer ());
97
- } else if (e instanceof InventoryInteractEvent ) {
98
- PlayerUtils .updateInventory (((Player ) ((InventoryInteractEvent ) e ).getWhoClicked ()));
76
+ public void execute (Event event ) {
77
+ if (event instanceof Cancellable )
78
+ ((Cancellable ) event ).setCancelled (cancel );
79
+ if (event instanceof PlayerInteractEvent ) {
80
+ EvtClick .interactTracker .eventModified ((Cancellable ) event );
81
+ ((PlayerInteractEvent ) event ).setUseItemInHand (cancel ? Event . Result .DENY : Event . Result .DEFAULT );
82
+ ((PlayerInteractEvent ) event ).setUseInteractedBlock (cancel ? Event . Result .DENY : Event . Result .DEFAULT );
83
+ } else if (event instanceof BlockCanBuildEvent ) {
84
+ ((BlockCanBuildEvent ) event ).setBuildable (!cancel );
85
+ } else if (event instanceof PlayerDropItemEvent ) {
86
+ PlayerUtils .updateInventory (((PlayerDropItemEvent ) event ).getPlayer ());
87
+ } else if (event instanceof InventoryInteractEvent ) {
88
+ PlayerUtils .updateInventory (((Player ) ((InventoryInteractEvent ) event ).getWhoClicked ()));
99
89
}
100
90
}
101
91
102
92
@ Override
103
- public String toString (final @ Nullable Event e , final boolean debug ) {
93
+ public String toString (@ Nullable Event event , boolean debug ) {
104
94
return (cancel ? "" : "un" ) + "cancel event" ;
105
95
}
106
96
0 commit comments