3
3
import com .codisimus .plugins .phatloots .PhatLoot ;
4
4
import com .codisimus .plugins .phatloots .PhatLoots ;
5
5
import com .codisimus .plugins .phatloots .PhatLootsCommandSender ;
6
+ import com .codisimus .plugins .phatloots .PhatLootsUtil ;
6
7
import java .util .ArrayList ;
7
8
import java .util .List ;
8
9
import java .util .Map ;
10
+ import java .util .Objects ;
9
11
import java .util .TreeMap ;
10
12
import org .bukkit .Bukkit ;
11
13
import org .bukkit .Material ;
14
16
import org .bukkit .event .inventory .ClickType ;
15
17
import org .bukkit .inventory .ItemStack ;
16
18
import org .bukkit .inventory .meta .ItemMeta ;
19
+ import org .bukkit .scheduler .BukkitRunnable ;
17
20
18
21
/**
19
22
* A CommandLoot is a Command which may be executed from the player or the console
24
27
public class CommandLoot extends Loot {
25
28
private static PhatLootsCommandSender cs = new PhatLootsCommandSender ();
26
29
public String command ;
30
+ public long delay = 0 ;
27
31
public boolean fromConsole ;
28
32
public boolean tempOP ;
29
33
@@ -59,6 +63,9 @@ public CommandLoot(Map<String, Object> map) {
59
63
try {
60
64
probability = (Double ) map .get (currentLine = "Probability" );
61
65
command = (String ) map .get (currentLine = "Command" );
66
+ if (map .containsKey (currentLine = "Delay" )) {
67
+ delay = ((Number ) map .get (currentLine )).longValue ();
68
+ }
62
69
fromConsole = (Boolean ) map .get (currentLine = "FromConsole" );
63
70
tempOP = (Boolean ) map .get (currentLine = "TempOP" );
64
71
} catch (Exception ex ) {
@@ -87,33 +94,38 @@ public void getLoot(LootBundle lootBundle, double lootingBonus) {
87
94
*
88
95
* @param player The Player looting or null if no Player is involved
89
96
*/
90
- public void execute (Player player ) {
91
- String cmd = command ;
92
- if (player == null ) {
93
- if (!fromConsole || command .contains ("<player>" ) || command .contains ("<killer>" )) {
94
- return ;
95
- }
96
- } else {
97
- cmd = cmd .replace ("<player>" , player .getName ());
98
- if (command .contains ("<killer>" )) {
99
- Player killer = player .getKiller ();
100
- if (killer == null ) {
101
- return ;
97
+ public void execute (final Player player ) {
98
+ new BukkitRunnable () {
99
+ @ Override
100
+ public void run () {
101
+ String cmd = command ;
102
+ if (player == null ) {
103
+ if (!fromConsole || command .contains ("<player>" ) || command .contains ("<killer>" )) {
104
+ return ;
105
+ }
102
106
} else {
103
- cmd = cmd .replace ("<killer>" , killer .getName ());
107
+ cmd = cmd .replace ("<player>" , player .getName ());
108
+ if (command .contains ("<killer>" )) {
109
+ Player killer = player .getKiller ();
110
+ if (killer == null ) {
111
+ return ;
112
+ } else {
113
+ cmd = cmd .replace ("<killer>" , killer .getName ());
114
+ }
115
+ }
116
+ }
117
+ if (fromConsole ) { //From console
118
+ Bukkit .dispatchCommand (cs , cmd );
119
+ } else if (tempOP ) { //From Player as OP
120
+ //Make the player OP for long enough to execute the command
121
+ player .setOp (true );
122
+ Bukkit .dispatchCommand (player , cmd );
123
+ player .setOp (false );
124
+ } else { //From Player
125
+ Bukkit .dispatchCommand (player , cmd );
104
126
}
105
127
}
106
- }
107
- if (fromConsole ) { //From console
108
- Bukkit .dispatchCommand (cs , cmd );
109
- } else if (tempOP ) { //From Player as OP
110
- //Make the player OP for long enough to execute the command
111
- player .setOp (true );
112
- Bukkit .dispatchCommand (player , cmd );
113
- player .setOp (false );
114
- } else { //From Player
115
- Bukkit .dispatchCommand (player , cmd );
116
- }
128
+ }.runTaskLater (PhatLoots .plugin , delay );
117
129
}
118
130
119
131
/**
@@ -135,6 +147,7 @@ public ItemStack getInfoStack() {
135
147
details .add ("§4Probability: §6" + probability );
136
148
details .add ("§4Command: §6" + command );
137
149
details .add (fromConsole ? "§6From Console" : "§6From Player" );
150
+ details .add ("§4Delayed: " + PhatLootsUtil .timeToString (delay ));
138
151
if (tempOP ) {
139
152
details .add ("§6Player is temporarily OPed" );
140
153
}
@@ -193,6 +206,11 @@ public String toString() {
193
206
//Only display the decimal values if the probability is not a whole number
194
207
sb .append (String .valueOf (Math .floor (probability ) == probability ? (int ) probability : probability ));
195
208
sb .append ("%" );
209
+ if (delay > 0 ) {
210
+ sb .append ("executed " );
211
+ sb .append (PhatLootsUtil .timeToString (delay ));
212
+ sb .append (" after looted" );
213
+ }
196
214
return sb .toString ();
197
215
}
198
216
@@ -201,6 +219,7 @@ public boolean equals(Object object) {
201
219
if (object instanceof CommandLoot ) {
202
220
CommandLoot loot = (CommandLoot ) object ;
203
221
return loot .fromConsole == fromConsole
222
+ && loot .delay == delay
204
223
&& loot .tempOP == tempOP
205
224
&& loot .command .equals (command );
206
225
} else {
@@ -211,7 +230,8 @@ public boolean equals(Object object) {
211
230
@ Override
212
231
public int hashCode () {
213
232
int hash = 7 ;
214
- hash = 37 * hash + (this .command != null ? this .command .hashCode () : 0 );
233
+ hash = 37 * hash + Objects .hashCode (this .command );
234
+ hash = 37 * hash + (int ) (this .delay ^ (this .delay >>> 32 ));
215
235
hash = 37 * hash + (this .fromConsole ? 1 : 0 );
216
236
hash = 37 * hash + (this .tempOP ? 1 : 0 );
217
237
return hash ;
@@ -222,6 +242,7 @@ public Map<String, Object> serialize() {
222
242
Map map = new TreeMap ();
223
243
map .put ("Probability" , probability );
224
244
map .put ("Command" , command );
245
+ map .put ("Delay" , delay );
225
246
map .put ("FromConsole" , fromConsole );
226
247
map .put ("TempOP" , tempOP );
227
248
return map ;
0 commit comments