18
18
*/
19
19
package com .plotsquared .core .command ;
20
20
21
+ import com .google .inject .Inject ;
21
22
import com .plotsquared .core .configuration .caption .TranslatableCaption ;
23
+ import com .plotsquared .core .events .PlotSwapEvent ;
24
+ import com .plotsquared .core .events .Result ;
22
25
import com .plotsquared .core .location .Location ;
23
26
import com .plotsquared .core .permissions .Permission ;
24
27
import com .plotsquared .core .player .PlotPlayer ;
25
28
import com .plotsquared .core .plot .Plot ;
29
+ import com .plotsquared .core .util .EventDispatcher ;
26
30
import com .plotsquared .core .util .task .RunnableVal2 ;
27
31
import com .plotsquared .core .util .task .RunnableVal3 ;
28
32
import net .kyori .adventure .text .Component ;
38
42
requiredType = RequiredType .PLAYER )
39
43
public class Swap extends SubCommand {
40
44
45
+ @ Inject
46
+ private EventDispatcher eventDispatcher ;
47
+
41
48
@ Override
42
49
public CompletableFuture <Boolean > execute (
43
50
PlotPlayer <?> player , String [] args ,
44
51
RunnableVal3 <Command , Runnable , Runnable > confirm ,
45
52
RunnableVal2 <Command , CommandResult > whenDone
46
53
) {
47
54
Location location = player .getLocation ();
48
- Plot plot1 = location .getPlotAbs ();
49
- if (plot1 == null ) {
55
+ Plot plot = location .getPlotAbs ();
56
+ if (plot == null ) {
50
57
player .sendMessage (TranslatableCaption .of ("errors.not_in_plot" ));
51
58
return CompletableFuture .completedFuture (false );
52
59
}
53
- if (!plot1 .isOwner (player .getUUID ()) && !player .hasPermission (Permission .PERMISSION_ADMIN )) {
54
- player .sendMessage (TranslatableCaption .of ("permission.no_plot_perms" ));
55
- return CompletableFuture .completedFuture (false );
56
- }
57
60
if (args .length != 1 ) {
58
61
sendUsage (player );
59
62
return CompletableFuture .completedFuture (false );
60
63
}
61
- Plot plot2 = Plot .getPlotFromString (player , args [0 ], true );
62
- if (plot2 == null ) {
64
+ final Plot plotArg = Plot .getPlotFromString (player , args [0 ], true );
65
+ if (plotArg == null ) {
66
+ return CompletableFuture .completedFuture (false );
67
+ }
68
+ final PlotSwapEvent event = this .eventDispatcher .callSwap (player , plot , plotArg );
69
+ if (event .getEventResult () == Result .DENY ) {
70
+ if (event .sendErrorMessage ()) {
71
+ player .sendMessage (TranslatableCaption .of ("swap.event_cancelled" ));
72
+ }
73
+ return CompletableFuture .completedFuture (false );
74
+ }
75
+ if (!plot .isOwner (player .getUUID ()) && !player .hasPermission (Permission .PERMISSION_ADMIN ) && event .getEventResult () != Result .FORCE ) {
76
+ player .sendMessage (TranslatableCaption .of ("permission.no_plot_perms" ));
63
77
return CompletableFuture .completedFuture (false );
64
78
}
65
- if (plot1 .equals (plot2 )) {
79
+ final Plot target = event .target ();
80
+ if (plot .equals (target )) {
66
81
player .sendMessage (TranslatableCaption .of ("invalid.origin_cant_be_target" ));
67
82
return CompletableFuture .completedFuture (false );
68
83
}
69
- if (!plot1 .getArea ().isCompatible (plot2 .getArea ())) {
84
+ if (!plot .getArea ().isCompatible (target .getArea ())) {
70
85
player .sendMessage (TranslatableCaption .of ("errors.plotworld_incompatible" ));
71
86
return CompletableFuture .completedFuture (false );
72
87
}
73
- if (plot1 .isMerged () || plot2 .isMerged ()) {
88
+ if (plot .isMerged () || target .isMerged ()) {
74
89
player .sendMessage (TranslatableCaption .of ("swap.swap_merged" ));
75
90
return CompletableFuture .completedFuture (false );
76
91
}
77
92
78
93
// Set strings here as the plot objects are mutable (the PlotID changes after being moved).
79
- String p1 = plot1 .toString ();
80
- String p2 = plot2 .toString ();
94
+ String p1 = plot .toString ();
95
+ String p2 = target .toString ();
81
96
82
- return plot1 .getPlotModificationManager ().move (plot2 , player , () -> {
97
+ return plot .getPlotModificationManager ().move (target , player , () -> {
83
98
}, true ).thenApply (result -> {
84
99
if (result ) {
85
100
player .sendMessage (
@@ -89,6 +104,7 @@ public CompletableFuture<Boolean> execute(
89
104
.tag ("target" , Tag .inserting (Component .text (p2 )))
90
105
.build ()
91
106
);
107
+ this .eventDispatcher .callPostSwap (player , plot , target );
92
108
return true ;
93
109
} else {
94
110
player .sendMessage (TranslatableCaption .of ("swap.swap_overlap" ));
0 commit comments