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;
|
2013-05-06 05:40:22 +00:00
|
|
|
import com.earth2me.essentials.Trade.OverflowType;
|
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;
|
2013-05-05 03:13:17 +00:00
|
|
|
import java.math.BigDecimal;
|
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();
|
2013-05-04 17:22:55 +00:00
|
|
|
for (String kitItem : kits.getKeys(false))
|
2011-12-08 04:44:18 +00:00
|
|
|
{
|
2013-05-04 17:11:35 +00:00
|
|
|
if (user == null)
|
2011-12-08 04:44:18 +00:00
|
|
|
{
|
2013-05-04 17:22:55 +00:00
|
|
|
list.append(" ").append(capitalCase(kitItem));
|
2011-12-08 04:44:18 +00:00
|
|
|
}
|
2013-05-04 17:22:55 +00:00
|
|
|
else if (user.isAuthorized("essentials.kits." + kitItem.toLowerCase(Locale.ENGLISH)))
|
2013-05-04 17:11:35 +00:00
|
|
|
{
|
|
|
|
String cost = "";
|
2013-05-18 22:27:41 +00:00
|
|
|
String name = capitalCase(kitItem);
|
2013-05-05 03:13:17 +00:00
|
|
|
BigDecimal costPrice = new Trade("kit-" + kitItem.toLowerCase(Locale.ENGLISH), ess).getCommandCost(user);
|
2013-05-05 09:41:19 +00:00
|
|
|
if (costPrice.signum() > 0)
|
2013-05-04 17:11:35 +00:00
|
|
|
{
|
|
|
|
cost = _("kitCost", Util.displayCurrency(costPrice, ess));
|
|
|
|
}
|
2013-05-18 22:27:41 +00:00
|
|
|
final Map<String, Object> kit = ess.getSettings().getKit(kitItem);
|
2013-05-04 17:11:35 +00:00
|
|
|
|
2013-05-18 22:27:41 +00:00
|
|
|
if (Kit.getNextUse(user, kitItem, kit) != 0)
|
|
|
|
{
|
|
|
|
name = _("kitDelay", name);
|
|
|
|
}
|
|
|
|
|
|
|
|
list.append(" ").append(name).append(cost);
|
2013-05-04 17:11:35 +00:00
|
|
|
}
|
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
|
2013-05-18 22:27:41 +00:00
|
|
|
{
|
|
|
|
final Calendar time = new GregorianCalendar();
|
|
|
|
long nextUse = getNextUse(user, kitName, els);
|
|
|
|
|
|
|
|
if (nextUse == 0L)
|
|
|
|
{
|
|
|
|
user.setKitTimestamp(kitName, time.getTimeInMillis());
|
|
|
|
}
|
|
|
|
else if (nextUse < 0L)
|
|
|
|
{
|
|
|
|
user.sendMessage(_("kitOnce"));
|
|
|
|
throw new NoChargeException();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
user.sendMessage(_("kitTimed", Util.formatDateDiff(nextUse)));
|
|
|
|
throw new NoChargeException();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static long getNextUse(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"))
|
|
|
|
{
|
2013-05-18 22:27:41 +00:00
|
|
|
return 0L;
|
2012-08-21 23:27:33 +00:00
|
|
|
}
|
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
|
|
|
|
2013-04-30 17:03:54 +00:00
|
|
|
double delay = 0;
|
|
|
|
try
|
2013-05-18 22:27:41 +00:00
|
|
|
{
|
|
|
|
// Make sure delay is valid
|
2013-04-30 17:03:54 +00:00
|
|
|
delay = els.containsKey("delay") ? ((Number)els.get("delay")).doubleValue() : 0.0d;
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
throw new Exception(_("kitError2"));
|
|
|
|
}
|
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
|
|
|
|
2013-05-18 22:27:41 +00:00
|
|
|
// When can be use the kit again?
|
|
|
|
final Calendar delayTime = new GregorianCalendar();
|
|
|
|
delayTime.setTimeInMillis(lastTime);
|
|
|
|
delayTime.add(Calendar.SECOND, (int)delay);
|
|
|
|
delayTime.add(Calendar.MILLISECOND, (int)((delay * 1000.0) % 1000.0));
|
|
|
|
|
|
|
|
if (lastTime == 0L || lastTime > time.getTimeInMillis())
|
2012-06-10 20:55:48 +00:00
|
|
|
{
|
2013-05-18 22:27:41 +00:00
|
|
|
// If we have no record of kit use, or its corrupted, give them benifit of the doubt.
|
|
|
|
return 0L;
|
2012-06-10 20:55:48 +00:00
|
|
|
}
|
2013-05-18 22:27:41 +00:00
|
|
|
else if (delay < 0d)
|
2011-12-08 04:44:18 +00:00
|
|
|
{
|
2013-05-18 22:27:41 +00:00
|
|
|
// If the kit has a negative kit time, it can only be used once.
|
|
|
|
return -1;
|
2011-12-08 04:44:18 +00:00
|
|
|
}
|
2013-05-18 22:27:41 +00:00
|
|
|
else if (delayTime.before(time))
|
2012-11-27 18:25:24 +00:00
|
|
|
{
|
2013-05-18 22:27:41 +00:00
|
|
|
// If the kit was used in the past, but outside the delay time, it can be used.
|
|
|
|
return 0L;
|
2012-11-27 18:25:24 +00:00
|
|
|
}
|
2011-12-08 04:44:18 +00:00
|
|
|
else
|
|
|
|
{
|
2013-05-18 22:27:41 +00:00
|
|
|
// If the kit has been used recently, return the next time it can be used.
|
|
|
|
return delayTime.getTimeInMillis();
|
2011-12-08 04:44:18 +00:00
|
|
|
}
|
|
|
|
}
|
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);
|
2013-04-29 19:04:59 +00:00
|
|
|
|
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())
|
2013-04-29 19:04:59 +00:00
|
|
|
{
|
2013-04-29 11:31:08 +00:00
|
|
|
if (kitItem.startsWith(ess.getSettings().getCurrencySymbol()))
|
2012-11-04 21:25:09 +00:00
|
|
|
{
|
2013-05-05 03:13:17 +00:00
|
|
|
BigDecimal value = new BigDecimal(kitItem.substring(ess.getSettings().getCurrencySymbol().length()).trim());
|
2012-11-04 21:25:09 +00:00
|
|
|
Trade t = new Trade(value, ess);
|
2013-05-06 05:40:22 +00:00
|
|
|
t.pay(user, OverflowType.DROP);
|
2012-11-04 21:25:09 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|