TF-EssentialsX/Essentials/src/com/earth2me/essentials/Kit.java

180 lines
5.3 KiB
Java
Raw Normal View History

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;
import com.earth2me.essentials.commands.NoChargeException;
import com.earth2me.essentials.craftbukkit.InventoryWorkaround;
import com.earth2me.essentials.textreader.IText;
import com.earth2me.essentials.textreader.KeywordReplacer;
import com.earth2me.essentials.textreader.SimpleTextInput;
import java.util.*;
import java.util.logging.Level;
2012-02-22 12:32:51 +00:00
import org.bukkit.configuration.ConfigurationSection;
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();
final StringBuilder list = new StringBuilder();
2013-05-04 17:22:55 +00:00
for (String kitItem : kits.getKeys(false))
{
if (user == null)
{
2013-05-04 17:22:55 +00:00
list.append(" ").append(capitalCase(kitItem));
}
2013-05-04 17:22:55 +00:00
else if (user.isAuthorized("essentials.kits." + kitItem.toLowerCase(Locale.ENGLISH)))
{
String cost = "";
2013-05-04 17:22:55 +00:00
Double costPrice = new Trade("kit-" + kitItem.toLowerCase(Locale.ENGLISH), ess).getCommandCost(user);
if (costPrice > 0d)
{
cost = _("kitCost", Util.displayCurrency(costPrice, ess));
}
2013-05-04 17:22:55 +00:00
list.append(" ").append(capitalCase(kitItem)).append(cost);
}
}
2012-02-22 12:32:51 +00:00
return list.toString().trim();
}
catch (Exception ex)
{
throw new Exception(_("kitError"), ex);
}
}
public static void checkTime(final User user, final String kitName, final Map<String, Object> els) throws Exception
{
if (user.isAuthorized("essentials.kit.exemptdelay"))
{
return;
}
final Calendar time = new GregorianCalendar();
// Take the current time, and remove the delay from it.
final double delay = els.containsKey("delay") ? ((Number)els.get("delay")).doubleValue() : 0.0d;
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();
// When was the last kit used?
final long lastTime = user.getKitTimestamp(kitName);
if (lastTime < earliestLong || lastTime == 0L)
{
user.setKitTimestamp(kitName, time.getTimeInMillis());
}
else if (lastTime > time.getTimeInMillis())
{
// 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());
}
else if (earliestLong < 0L)
{
user.sendMessage(_("kitOnce"));
throw new NoChargeException();
}
else
{
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())));
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
{
if (kit == null)
{
throw new Exception(_("kitNotFound"));
2012-03-03 07:03:54 +00:00
}
try
{
2012-03-03 07:03:54 +00:00
return (List<String>)kit.get("items");
}
catch (Exception e)
{
user.sendMessage(_("kitError2"));
throw new Exception(_("kitErrorHelp"), e);
}
}
public static void expandItems(final IEssentials ess, final User user, final List<String> items) throws Exception
{
try
{
IText input = new SimpleTextInput(items);
IText output = new KeywordReplacer(input, user, ess);
2013-04-29 19:04:59 +00:00
boolean spew = false;
final boolean allowUnsafe = ess.getSettings().allowUnsafeEnchantments();
for (String kitItem : output.getLines())
2013-04-29 19:04:59 +00:00
{
if (kitItem.startsWith(ess.getSettings().getCurrencySymbol()))
{
Double value = Double.parseDouble(kitItem.substring(ess.getSettings().getCurrencySymbol().length()).trim());
Trade t = new Trade(value, ess);
t.pay(user);
continue;
}
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);
if (parts.length > 2)
{
// We pass a null sender here because kits should not do perm checks
metaStack.parseStringMeta(null, allowUnsafe, parts, 2, ess);
}
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());
}
else
{
2013-01-12 17:05:05 +00:00
overfilled = InventoryWorkaround.addItems(user.getInventory(), metaStack.getItemStack());
}
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();
if (ess.getSettings().isDebug())
{
ess.getLogger().log(Level.WARNING, e.getMessage());
}
else
{
ess.getLogger().log(Level.WARNING, e.getMessage());
}
throw new Exception(_("kitError2"), e);
}
}
}