2011-12-08 04:44:18 +00:00
|
|
|
package com.earth2me.essentials;
|
|
|
|
|
|
|
|
import static com.earth2me.essentials.I18n._;
|
2012-03-03 07:09:41 +00:00
|
|
|
import static com.earth2me.essentials.I18n.capitalCase;
|
2011-12-08 04:44:18 +00:00
|
|
|
import com.earth2me.essentials.commands.NoChargeException;
|
|
|
|
import com.earth2me.essentials.craftbukkit.InventoryWorkaround;
|
|
|
|
import java.util.*;
|
2012-04-04 02:09:27 +00:00
|
|
|
import java.util.logging.Level;
|
2011-12-08 04:44:18 +00:00
|
|
|
import org.bukkit.Material;
|
2012-02-22 12:32:51 +00:00
|
|
|
import org.bukkit.configuration.ConfigurationSection;
|
2012-03-22 23:47:35 +00:00
|
|
|
import org.bukkit.enchantments.Enchantment;
|
2011-12-08 04:44:18 +00:00
|
|
|
import org.bukkit.inventory.ItemStack;
|
|
|
|
|
|
|
|
|
|
|
|
public class Kit
|
|
|
|
{
|
|
|
|
//TODO: Convert this to use one of the new text classes?
|
|
|
|
public static String listKits(final IEssentials ess, final User user) throws Exception
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2012-02-22 12:32:51 +00:00
|
|
|
final ConfigurationSection kits = ess.getSettings().getKits();
|
2011-12-08 04:44:18 +00:00
|
|
|
final StringBuilder list = new StringBuilder();
|
2012-02-22 12:32:51 +00:00
|
|
|
for (String kiteItem : kits.getKeys(false))
|
2011-12-08 04:44:18 +00:00
|
|
|
{
|
2012-09-08 19:32:48 +00:00
|
|
|
if (user == null || user.isAuthorized("essentials.kits." + kiteItem.toLowerCase(Locale.ENGLISH)))
|
2011-12-08 04:44:18 +00:00
|
|
|
{
|
2012-03-03 07:09:41 +00:00
|
|
|
list.append(" ").append(capitalCase(kiteItem));
|
2011-12-08 04:44:18 +00:00
|
|
|
}
|
|
|
|
}
|
2012-02-22 12:32:51 +00:00
|
|
|
return list.toString().trim();
|
2011-12-08 04:44:18 +00:00
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
2012-04-04 02:09:27 +00:00
|
|
|
throw new Exception(_("kitError"), ex);
|
2011-12-08 04:44:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void checkTime(final User user, final String kitName, final Map<String, Object> els) throws NoChargeException
|
|
|
|
{
|
2012-08-21 23:27:33 +00:00
|
|
|
if (user.isAuthorized("essentials.kit.exemptdelay")) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-06-10 20:55:48 +00:00
|
|
|
final Calendar time = new GregorianCalendar();
|
2011-12-08 04:44:18 +00:00
|
|
|
|
2012-06-10 20:55:48 +00:00
|
|
|
// Take the current time, and remove the delay from it.
|
|
|
|
final double delay = els.containsKey("delay") ? ((Number)els.get("delay")).doubleValue() : 0L;
|
|
|
|
final Calendar earliestTime = new GregorianCalendar();
|
|
|
|
earliestTime.add(Calendar.SECOND, -(int)delay);
|
|
|
|
earliestTime.add(Calendar.MILLISECOND, -(int)((delay * 1000.0) % 1000.0));
|
|
|
|
// This value contains the most recent time a kit could have been used that would allow another use.
|
|
|
|
final long earliestLong = earliestTime.getTimeInMillis();
|
2011-12-08 04:44:18 +00:00
|
|
|
|
2012-06-10 20:55:48 +00:00
|
|
|
// When was the last kit used?
|
2012-09-02 17:11:29 +00:00
|
|
|
final long lastTime = user.getKitTimestamp(kitName);
|
2012-06-10 20:55:48 +00:00
|
|
|
|
2012-09-02 17:11:29 +00:00
|
|
|
if (lastTime < earliestLong)
|
2012-06-10 20:55:48 +00:00
|
|
|
{
|
|
|
|
user.setKitTimestamp(kitName, time.getTimeInMillis());
|
|
|
|
}
|
|
|
|
else if (lastTime > time.getTimeInMillis())
|
2011-12-08 04:44:18 +00:00
|
|
|
{
|
2012-06-10 20:55:48 +00:00
|
|
|
// This is to make sure time didn't get messed up on last kit use.
|
|
|
|
// If this happens, let's give the user the benifit of the doubt.
|
|
|
|
user.setKitTimestamp(kitName, time.getTimeInMillis());
|
2011-12-08 04:44:18 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-06-10 20:55:48 +00:00
|
|
|
time.setTimeInMillis(lastTime);
|
|
|
|
time.add(Calendar.SECOND, (int)delay);
|
|
|
|
time.add(Calendar.MILLISECOND, (int)((delay * 1000.0) % 1000.0));
|
|
|
|
user.sendMessage(_("kitTimed", Util.formatDateDiff(time.getTimeInMillis())));
|
2011-12-08 04:44:18 +00:00
|
|
|
throw new NoChargeException();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-03 07:03:54 +00:00
|
|
|
public static List<String> getItems(final User user, final Map<String, Object> kit) throws Exception
|
2011-12-08 04:44:18 +00:00
|
|
|
{
|
2012-03-04 20:06:50 +00:00
|
|
|
if (kit == null)
|
|
|
|
{
|
2012-03-03 07:03:54 +00:00
|
|
|
throw new Exception(_("kitError2"));
|
|
|
|
}
|
2012-08-10 01:26:23 +00:00
|
|
|
|
2012-03-04 20:06:50 +00:00
|
|
|
|
2011-12-08 04:44:18 +00:00
|
|
|
try
|
|
|
|
{
|
2012-03-03 07:03:54 +00:00
|
|
|
return (List<String>)kit.get("items");
|
2011-12-08 04:44:18 +00:00
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
user.sendMessage(_("kitError2"));
|
2012-06-10 20:55:48 +00:00
|
|
|
throw new Exception(_("kitErrorHelp"), e);
|
2011-12-08 04:44:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void expandItems(final IEssentials ess, final User user, final List<String> items) throws Exception
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
boolean spew = false;
|
|
|
|
for (String d : items)
|
|
|
|
{
|
2012-11-04 21:25:09 +00:00
|
|
|
if (d.startsWith(ess.getSettings().getCurrencySymbol()))
|
|
|
|
{
|
|
|
|
Double value = Double.parseDouble(d.substring(ess.getSettings().getCurrencySymbol().length()).trim());
|
|
|
|
Trade t = new Trade(value, ess);
|
|
|
|
t.pay(user);
|
|
|
|
continue;
|
|
|
|
}
|
2012-03-22 23:47:35 +00:00
|
|
|
final String[] parts = d.split(" ");
|
|
|
|
final String[] item = parts[0].split("[:+',;.]", 2);
|
|
|
|
final int id = Material.getMaterial(Integer.parseInt(item[0])).getId();
|
|
|
|
final short data = item.length > 1 ? Short.parseShort(item[1]) : 0;
|
|
|
|
final int amount = parts.length > 1 ? Integer.parseInt(parts[1]) : 1;
|
|
|
|
|
|
|
|
final ItemStack stack = new ItemStack(id, amount, data);
|
|
|
|
if (parts.length > 2)
|
|
|
|
{
|
|
|
|
for (int i = 2; i < parts.length; i++)
|
|
|
|
{
|
|
|
|
final String[] split = parts[i].split("[:+',;.]", 2);
|
|
|
|
if (split.length < 1)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2012-03-26 12:28:32 +00:00
|
|
|
final Enchantment enchantment = Enchantments.getByName(split[0]);
|
2012-04-04 02:09:27 +00:00
|
|
|
if (enchantment == null)
|
|
|
|
{
|
|
|
|
throw new Exception("Enchantment not found: " + split[0]);
|
|
|
|
}
|
2012-03-22 23:47:35 +00:00
|
|
|
int level;
|
|
|
|
if (split.length > 1)
|
|
|
|
{
|
|
|
|
level = Integer.parseInt(split[1]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
level = enchantment.getMaxLevel();
|
|
|
|
}
|
2012-04-04 02:09:27 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
stack.addEnchantment(enchantment, level);
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
throw new Exception("Enchantment " + enchantment.getName() + ": " + ex.getMessage(), ex);
|
|
|
|
}
|
2012-03-22 23:47:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-08 04:44:18 +00:00
|
|
|
final Map<Integer, ItemStack> overfilled;
|
|
|
|
if (user.isAuthorized("essentials.oversizedstacks"))
|
|
|
|
{
|
2012-03-22 23:47:35 +00:00
|
|
|
overfilled = InventoryWorkaround.addItem(user.getInventory(), true, ess.getSettings().getOversizedStackSize(), stack);
|
2011-12-08 04:44:18 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-03-26 12:28:32 +00:00
|
|
|
overfilled = InventoryWorkaround.addItem(user.getInventory(), true, 0, stack);
|
2011-12-08 04:44:18 +00:00
|
|
|
}
|
|
|
|
for (ItemStack itemStack : overfilled.values())
|
|
|
|
{
|
|
|
|
user.getWorld().dropItemNaturally(user.getLocation(), itemStack);
|
|
|
|
spew = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
user.updateInventory();
|
|
|
|
if (spew)
|
|
|
|
{
|
|
|
|
user.sendMessage(_("kitInvFull"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
user.updateInventory();
|
2012-04-04 02:09:27 +00:00
|
|
|
if (ess.getSettings().isDebug())
|
|
|
|
{
|
|
|
|
ess.getLogger().log(Level.WARNING, e.getMessage());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ess.getLogger().log(Level.WARNING, e.getMessage());
|
|
|
|
}
|
|
|
|
throw new Exception(_("kitError2"), e);
|
2011-12-08 04:44:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|