Skip to content

Commit e157cf2

Browse files
committed
Added optional delay to CommandLoot
Closes #41
1 parent f11b9eb commit e157cf2

File tree

2 files changed

+72
-25
lines changed

2 files changed

+72
-25
lines changed

src/com/codisimus/plugins/phatloots/PhatLootsUtil.java

+26
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import java.util.LinkedList;
77
import java.util.Random;
88
import org.apache.commons.lang.WordUtils;
9+
import org.apache.commons.lang.time.DateUtils;
910
import org.bukkit.Location;
1011
import org.bukkit.Material;
1112
import org.bukkit.block.Block;
@@ -205,4 +206,29 @@ public static String concatArgs(String[] args) {
205206
}
206207
return sb.substring(1);
207208
}
209+
210+
/**
211+
* Returns a human friendly String of the remaining time until the PhatLootChest resets
212+
*
213+
* @param time The given time
214+
* @return the remaining time until the PhatLootChest resets
215+
*/
216+
public static String timeToString(long time) {
217+
if (time < 0) {
218+
return "forever";
219+
}
220+
221+
//Find the appropriate unit of time and return that amount
222+
if (time > DateUtils.MILLIS_PER_DAY) {
223+
return time / DateUtils.MILLIS_PER_DAY + " day(s)";
224+
} else if (time > DateUtils.MILLIS_PER_HOUR) {
225+
return time / DateUtils.MILLIS_PER_HOUR + " hour(s)";
226+
} else if (time > DateUtils.MILLIS_PER_MINUTE) {
227+
return time / DateUtils.MILLIS_PER_MINUTE + " minute(s)";
228+
} else if (time > DateUtils.MILLIS_PER_SECOND) {
229+
return time / DateUtils.MILLIS_PER_SECOND + " second(s)";
230+
} else {
231+
return time + " millisecond(s)";
232+
}
233+
}
208234
}

src/com/codisimus/plugins/phatloots/loot/CommandLoot.java

+46-25
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
import com.codisimus.plugins.phatloots.PhatLoot;
44
import com.codisimus.plugins.phatloots.PhatLoots;
55
import com.codisimus.plugins.phatloots.PhatLootsCommandSender;
6+
import com.codisimus.plugins.phatloots.PhatLootsUtil;
67
import java.util.ArrayList;
78
import java.util.List;
89
import java.util.Map;
10+
import java.util.Objects;
911
import java.util.TreeMap;
1012
import org.bukkit.Bukkit;
1113
import org.bukkit.Material;
@@ -14,6 +16,7 @@
1416
import org.bukkit.event.inventory.ClickType;
1517
import org.bukkit.inventory.ItemStack;
1618
import org.bukkit.inventory.meta.ItemMeta;
19+
import org.bukkit.scheduler.BukkitRunnable;
1720

1821
/**
1922
* A CommandLoot is a Command which may be executed from the player or the console
@@ -24,6 +27,7 @@
2427
public class CommandLoot extends Loot {
2528
private static PhatLootsCommandSender cs = new PhatLootsCommandSender();
2629
public String command;
30+
public long delay = 0;
2731
public boolean fromConsole;
2832
public boolean tempOP;
2933

@@ -59,6 +63,9 @@ public CommandLoot(Map<String, Object> map) {
5963
try {
6064
probability = (Double) map.get(currentLine = "Probability");
6165
command = (String) map.get(currentLine = "Command");
66+
if (map.containsKey(currentLine = "Delay")) {
67+
delay = ((Number) map.get(currentLine)).longValue();
68+
}
6269
fromConsole = (Boolean) map.get(currentLine = "FromConsole");
6370
tempOP = (Boolean) map.get(currentLine = "TempOP");
6471
} catch (Exception ex) {
@@ -87,33 +94,38 @@ public void getLoot(LootBundle lootBundle, double lootingBonus) {
8794
*
8895
* @param player The Player looting or null if no Player is involved
8996
*/
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+
}
102106
} 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);
104126
}
105127
}
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);
117129
}
118130

119131
/**
@@ -135,6 +147,7 @@ public ItemStack getInfoStack() {
135147
details.add("§4Probability: §6" + probability);
136148
details.add("§4Command: §6" + command);
137149
details.add(fromConsole ? "§6From Console" : "§6From Player");
150+
details.add("§4Delayed: " + PhatLootsUtil.timeToString(delay));
138151
if (tempOP) {
139152
details.add("§6Player is temporarily OPed");
140153
}
@@ -193,6 +206,11 @@ public String toString() {
193206
//Only display the decimal values if the probability is not a whole number
194207
sb.append(String.valueOf(Math.floor(probability) == probability ? (int) probability : probability));
195208
sb.append("%");
209+
if (delay > 0) {
210+
sb.append("executed ");
211+
sb.append(PhatLootsUtil.timeToString(delay));
212+
sb.append(" after looted");
213+
}
196214
return sb.toString();
197215
}
198216

@@ -201,6 +219,7 @@ public boolean equals(Object object) {
201219
if (object instanceof CommandLoot) {
202220
CommandLoot loot = (CommandLoot) object;
203221
return loot.fromConsole == fromConsole
222+
&& loot.delay == delay
204223
&& loot.tempOP == tempOP
205224
&& loot.command.equals(command);
206225
} else {
@@ -211,7 +230,8 @@ public boolean equals(Object object) {
211230
@Override
212231
public int hashCode() {
213232
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));
215235
hash = 37 * hash + (this.fromConsole ? 1 : 0);
216236
hash = 37 * hash + (this.tempOP ? 1 : 0);
217237
return hash;
@@ -222,6 +242,7 @@ public Map<String, Object> serialize() {
222242
Map map = new TreeMap();
223243
map.put("Probability", probability);
224244
map.put("Command", command);
245+
map.put("Delay", delay);
225246
map.put("FromConsole", fromConsole);
226247
map.put("TempOP", tempOP);
227248
return map;

0 commit comments

Comments
 (0)