Kits timeout are now saved in user data

This commit is contained in:
snowleo 2011-09-30 00:53:25 +02:00
parent 6224b07215
commit 4edf1ad3f5
2 changed files with 59 additions and 44 deletions

View File

@ -58,6 +58,7 @@ public abstract class UserData extends PlayerExtension implements IConf
isSocialSpyEnabled = _isSocialSpyEnabled();
isNPC = _isNPC();
arePowerToolsEnabled = _arePowerToolsEnabled();
kitTimestamps = _getKitTimestamps();
}
private double money;
@ -813,4 +814,33 @@ public abstract class UserData extends PlayerExtension implements IConf
return config.getBoolean("powertoolsenabled", true);
}
private Map<String, Object> kitTimestamps;
private Map<String, Object> _getKitTimestamps()
{
final Object map = config.getProperty("timestamps.kits");
if (map instanceof Map)
{
return (Map<String, Object>)map;
}
else
{
return new HashMap<String, Object>();
}
}
public Long getKitTimestamp(final String name)
{
final Number num = (Number)kitTimestamps.get(name.toLowerCase());
return num == null ? null : num.longValue();
}
public void setKitTimestamp(final String name, final long time)
{
kitTimestamps.put(name.toLowerCase(), time);
config.setProperty("timestamps.kits", kitTimestamps);
config.save();
}
}

View File

@ -15,22 +15,20 @@ import org.bukkit.inventory.ItemStack;
public class Commandkit extends EssentialsCommand
{
static private final Map<User, Map<String, Long>> kitPlayers = new HashMap<User, Map<String, Long>>();
public Commandkit()
{
super("kit");
}
@Override
public void run(Server server, User user, String commandLabel, String[] args) throws Exception
public void run(final Server server, final User user, final String commandLabel, final String[] args) throws Exception
{
if (args.length < 1)
{
try
{
Map<String, Object> kits = ess.getSettings().getKits();
StringBuilder list = new StringBuilder();
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()))
@ -56,8 +54,8 @@ public class Commandkit extends EssentialsCommand
{
try
{
String kitName = args[0].toLowerCase();
Object kit = ess.getSettings().getKit(kitName);
final String kitName = args[0].toLowerCase();
final Object kit = ess.getSettings().getKit(kitName);
List<String> items;
if (!user.isAuthorized("essentials.kit." + kitName))
@ -70,47 +68,34 @@ public class Commandkit extends EssentialsCommand
{
//System.out.println("Kit is timed");
Map<String, Object> els = (Map<String, Object>)kit;
final Map<String, Object> els = (Map<String, Object>)kit;
items = (List<String>)els.get("items");
double delay = els.containsKey("delay") ? ((Number)els.get("delay")).doubleValue() : 0L;
Calendar c = new GregorianCalendar();
c.add(Calendar.SECOND, (int)delay);
c.add(Calendar.MILLISECOND, (int)((delay*1000.0)%1000.0));
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));
long time = c.getTimeInMillis();
Calendar now = new GregorianCalendar();
final long mintime = c.getTimeInMillis();
Map<String, Long> kitTimes;
if (!kitPlayers.containsKey(user))
{
kitTimes = new HashMap<String, Long>();
kitTimes.put(kitName, time);
kitPlayers.put(user, kitTimes);
}
else
{
kitTimes = kitPlayers.get(user);
if (!kitTimes.containsKey(kitName))
{
kitTimes.put(kitName, time);
}
else if (kitTimes.get(kitName) < now.getTimeInMillis())
{
kitTimes.put(kitName, time);
}
else
{
user.sendMessage(Util.format("kitTimed", Util.formatDateDiff(kitTimes.get(kitName))));
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;
}
Trade charge = new Trade("kit-" + kitName, ess);
final Trade charge = new Trade("kit-" + kitName, ess);
try
{
charge.isAffordableFor(user);
@ -124,11 +109,11 @@ public class Commandkit extends EssentialsCommand
boolean spew = false;
for (String d : items)
{
String[] parts = d.split("[^0-9]+", 3);
int id = Material.getMaterial(Integer.parseInt(parts[0])).getId();
int amount = parts.length > 1 ? Integer.parseInt(parts[parts.length > 2 ? 2 : 1]) : 1;
short data = parts.length > 2 ? Short.parseShort(parts[1]) : 0;
HashMap<Integer,ItemStack> overfilled = user.getInventory().addItem(new ItemStack(id, amount, data));
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);