1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
|
package com.earth2me.essentials.commands;
import com.earth2me.essentials.Trade;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.bukkit.Server;
import com.earth2me.essentials.User;
import com.earth2me.essentials.Util;
import java.util.GregorianCalendar;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
public class Commandkit extends EssentialsCommand
{
public Commandkit()
{
super("kit");
}
@Override
public void run(final Server server, final User user, final String commandLabel, final String[] args) throws Exception
{
if (args.length < 1)
{
try
{
final Map<String, Object> kits = ess.getSettings().getKits();
final StringBuilder list = new StringBuilder();
for (String k : kits.keySet())
{
if (user.isAuthorized("essentials.kit." + k.toLowerCase()))
{
list.append(" ").append(k);
}
}
if (list.length() > 0)
{
user.sendMessage(Util.format("kits", list.toString()));
}
else
{
user.sendMessage(Util.i18n("noKits"));
}
}
catch (Exception ex)
{
user.sendMessage(Util.i18n("kitError"));
}
}
else
{
try
{
final String kitName = args[0].toLowerCase();
final Object kit = ess.getSettings().getKit(kitName);
List<String> items;
if (!user.isAuthorized("essentials.kit." + kitName))
{
user.sendMessage(Util.format("noKitPermission", "essentials.kit." + kitName));
return;
}
try
{
//System.out.println("Kit is timed");
final Map<String, Object> els = (Map<String, Object>)kit;
items = (List<String>)els.get("items");
final double delay = els.containsKey("delay") ? ((Number)els.get("delay")).doubleValue() : 0L;
final Calendar c = new GregorianCalendar();
c.add(Calendar.SECOND, -(int)delay);
c.add(Calendar.MILLISECOND, -(int)((delay*1000.0)%1000.0));
final long mintime = c.getTimeInMillis();
final Long lastTime = user.getKitTimestamp(kitName);
if (lastTime == null || lastTime < mintime) {
final Calendar now = new GregorianCalendar();
user.setKitTimestamp(kitName, now.getTimeInMillis());
} else {
final Calendar future = new GregorianCalendar();
future.setTimeInMillis(lastTime);
future.add(Calendar.SECOND, (int)delay);
future.add(Calendar.MILLISECOND, (int)((delay*1000.0)%1000.0));
user.sendMessage(Util.format("kitTimed", Util.formatDateDiff(future.getTimeInMillis())));
return;
}
}
catch (Exception ex)
{
items = (List<String>)kit;
}
final Trade charge = new Trade("kit-" + kitName, ess);
try
{
charge.isAffordableFor(user);
}
catch (Exception ex)
{
user.sendMessage(ex.getMessage());
return;
}
boolean spew = false;
for (String d : items)
{
final String[] parts = d.split("[^0-9]+", 3);
final int id = Material.getMaterial(Integer.parseInt(parts[0])).getId();
final int amount = parts.length > 1 ? Integer.parseInt(parts[parts.length > 2 ? 2 : 1]) : 1;
final short data = parts.length > 2 ? Short.parseShort(parts[1]) : 0;
final HashMap<Integer,ItemStack> overfilled = user.getInventory().addItem(new ItemStack(id, amount, data));
for (ItemStack itemStack : overfilled.values())
{
user.getWorld().dropItemNaturally(user.getLocation(), itemStack);
spew = true;
}
}
if (spew)
{
user.sendMessage(Util.i18n("kitInvFull"));
}
try
{
charge.charge(user);
}
catch (Exception ex)
{
user.sendMessage(ex.getMessage());
}
user.sendMessage(Util.format("kitGive", kitName));
}
catch (Exception ex)
{
user.sendMessage(Util.i18n("kitError2"));
user.sendMessage(Util.i18n("kitErrorHelp"));
}
}
}
}
|