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;
|
2013-04-29 11:31:08 +00:00
|
|
|
import com.earth2me.essentials.textreader.IText;
|
|
|
|
import com.earth2me.essentials.textreader.KeywordReplacer;
|
|
|
|
import com.earth2me.essentials.textreader.SimpleTextInput;
|
2011-12-08 04:44:18 +00:00
|
|
|
import java.util.*;
|
2012-04-04 02:09:27 +00:00
|
|
|
import java.util.logging.Level;
|
2012-02-22 12:32:51 +00:00
|
|
|
import org.bukkit.configuration.ConfigurationSection;
|
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
|
|
|
}
|
2013-01-06 18:28:24 +00:00
|
|
|
|
2011-12-08 04:44:18 +00:00
|
|
|
}
|
2013-01-06 18:28:24 +00:00
|
|
|
|
2012-11-27 18:25:24 +00:00
|
|
|
public static void checkTime(final User user, final String kitName, final Map<String, Object> els) throws Exception
|
2011-12-08 04:44:18 +00:00
|
|
|
{
|
2012-12-29 08:35:52 +00:00
|
|
|
if (user.isAuthorized("essentials.kit.exemptdelay"))
|
|
|
|
{
|
2012-08-21 23:27:33 +00:00
|
|
|
return;
|
|
|
|
}
|
2013-01-06 18:28:24 +00:00
|
|
|
|
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.
|
2012-11-27 18:25:24 +00:00
|
|
|
final double delay = els.containsKey("delay") ? ((Number)els.get("delay")).doubleValue() : 0.0d;
|
2012-06-10 20:55:48 +00:00
|
|
|
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);
|
2013-01-06 18:28:24 +00:00
|
|
|
|
2012-11-27 18:25:24 +00:00
|
|
|
if (lastTime < earliestLong || lastTime == 0L)
|
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
|
|
|
}
|
2012-11-27 18:25:24 +00:00
|
|
|
else if (earliestLong < 0L)
|
|
|
|
{
|
|
|
|
user.sendMessage(_("kitOnce"));
|
|
|
|
throw new NoChargeException();
|
|
|
|
}
|
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();
|
|
|
|
}
|
|
|
|
}
|
2013-01-06 18:28:24 +00:00
|
|
|
|
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)
|
|
|
|
{
|
2013-04-27 11:17:57 +00:00
|
|
|
throw new Exception(_("kitNotFound"));
|
2012-03-03 07:03:54 +00:00
|
|
|
}
|
2013-01-06 18:28:24 +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
|
|
|
}
|
|
|
|
}
|
2013-01-06 18:28:24 +00:00
|
|
|
|
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
|
|
|
|
{
|
2013-04-29 11:31:08 +00:00
|
|
|
IText input = new SimpleTextInput(items);
|
|
|
|
IText output = new KeywordReplacer(input, user, ess);
|
|
|
|
|
2011-12-08 04:44:18 +00:00
|
|
|
boolean spew = false;
|
2013-01-06 18:28:24 +00:00
|
|
|
final boolean allowUnsafe = ess.getSettings().allowUnsafeEnchantments();
|
2013-04-29 11:31:08 +00:00
|
|
|
for (String kitItem : output.getLines())
|
|
|
|
{
|
|
|
|
if (kitItem.startsWith(ess.getSettings().getCurrencySymbol()))
|
2012-11-04 21:25:09 +00:00
|
|
|
{
|
2013-04-29 11:31:08 +00:00
|
|
|
Double value = Double.parseDouble(kitItem.substring(ess.getSettings().getCurrencySymbol().length()).trim());
|
2012-11-04 21:25:09 +00:00
|
|
|
Trade t = new Trade(value, ess);
|
|
|
|
t.pay(user);
|
|
|
|
continue;
|
|
|
|
}
|
2013-01-06 18:28:24 +00:00
|
|
|
|
2013-04-29 11:31:08 +00:00
|
|
|
final String[] parts = kitItem.split(" ");
|
2013-01-12 14:12:12 +00:00
|
|
|
final ItemStack parseStack = ess.getItemDb().get(parts[0], parts.length > 1 ? Integer.parseInt(parts[1]) : 1);
|
2013-01-12 17:05:05 +00:00
|
|
|
final MetaItemStack metaStack = new MetaItemStack(parseStack);
|
2013-01-06 18:28:24 +00:00
|
|
|
|
2012-03-22 23:47:35 +00:00
|
|
|
if (parts.length > 2)
|
|
|
|
{
|
2013-04-29 11:31:08 +00:00
|
|
|
// We pass a null sender here because kits should not do perm checks
|
2013-03-07 02:57:25 +00:00
|
|
|
metaStack.parseStringMeta(null, allowUnsafe, parts, 2, ess);
|
2012-03-22 23:47:35 +00:00
|
|
|
}
|
2013-01-06 18:28:24 +00:00
|
|
|
|
2011-12-08 04:44:18 +00:00
|
|
|
final Map<Integer, ItemStack> overfilled;
|
|
|
|
if (user.isAuthorized("essentials.oversizedstacks"))
|
|
|
|
{
|
2013-01-12 17:05:05 +00:00
|
|
|
overfilled = InventoryWorkaround.addOversizedItems(user.getInventory(), ess.getSettings().getOversizedStackSize(), metaStack.getItemStack());
|
2011-12-08 04:44:18 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-01-12 17:05:05 +00:00
|
|
|
overfilled = InventoryWorkaround.addItems(user.getInventory(), metaStack.getItemStack());
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|