Skip to content
Permalink
Browse files

Kit cleanup and refactor.

  • Loading branch information...
khobbits committed Jun 25, 2014
1 parent 84a0363 commit f6b2524a8c1151948d4a24b27dfae9872a449ec1
@@ -21,6 +21,24 @@

public class Kit
{
final IEssentials ess;
final String kitName;
final Map<String, Object> kit;
final Trade charge;

public Kit(final String kitName, final IEssentials ess) throws Exception
{
this.kitName = kitName;
this.ess = ess;
this.kit = ess.getSettings().getKit(kitName);
this.charge = new Trade("kit-" + kitName, new Trade("kit-kit", ess), ess);

if (kit == null)
{
throw new Exception(tl("kitNotFound"));
}
}

//TODO: Convert this to use one of the new text classes?
public static String listKits(final IEssentials ess, final User user) throws Exception
{
@@ -43,9 +61,9 @@ else if (user.isAuthorized("essentials.kits." + kitItem.toLowerCase(Locale.ENGLI
{
cost = tl("kitCost", NumberUtil.displayCurrency(costPrice, ess));
}
final Map<String, Object> kit = ess.getSettings().getKit(kitItem);

if (Kit.getNextUse(user, kitItem, kit) != 0)
Kit kit = new Kit(kitItem, ess);
if (kit.getNextUse(user) != 0)
{
name = tl("kitDelay", name);
}
@@ -62,14 +80,26 @@ else if (user.isAuthorized("essentials.kits." + kitItem.toLowerCase(Locale.ENGLI

}

public static void checkTime(final User user, final String kitName, final Map<String, Object> els) throws Exception
public String getName()
{
final Calendar time = new GregorianCalendar();
long nextUse = getNextUse(user, kitName, els);
return kitName;
}

public void checkPerms(final User user) throws Exception
{
if (!user.isAuthorized("essentials.kits." + kitName))
{
throw new Exception(tl("noKitPermission", "essentials.kits." + kitName));
}
}

public void checkDelay(final User user) throws Exception
{
long nextUse = getNextUse(user);

if (nextUse == 0L)
{
user.setKitTimestamp(kitName, time.getTimeInMillis());
return;
}
else if (nextUse < 0L)
{
@@ -83,7 +113,23 @@ else if (nextUse < 0L)
}
}

public static long getNextUse(final User user, final String kitName, final Map<String, Object> els) throws Exception
public void checkAffordable(final User user) throws Exception
{
charge.isAffordableFor(user);
}

public void setTime(final User user) throws Exception
{
final Calendar time = new GregorianCalendar();
user.setKitTimestamp(kitName, time.getTimeInMillis());
}

public void chargeUser(final User user) throws Exception
{
charge.charge(user);
}

public long getNextUse(final User user) throws Exception
{
if (user.isAuthorized("essentials.kit.exemptdelay"))
{
@@ -96,7 +142,7 @@ public static long getNextUse(final User user, final String kitName, final Map<S
try
{
// Make sure delay is valid
delay = els.containsKey("delay") ? ((Number)els.get("delay")).doubleValue() : 0.0d;
delay = kit.containsKey("delay") ? ((Number)kit.get("delay")).doubleValue() : 0.0d;
}
catch (Exception e)
{
@@ -134,7 +180,7 @@ else if (delayTime.before(time))
}
}

public static List<String> getItems(final IEssentials ess, final User user, final String kitName, final Map<String, Object> kit) throws Exception
public List<String> getItems(final User user) throws Exception
{
if (kit == null)
{
@@ -166,7 +212,12 @@ else if (delayTime.before(time))
}
}

public static void expandItems(final IEssentials ess, final User user, final List<String> items) throws Exception
public void expandItems(final User user) throws Exception
{
expandItems(user, getItems(user));
}

public void expandItems(final User user, final List<String> items) throws Exception
{
try
{
@@ -187,11 +238,12 @@ public static void expandItems(final IEssentials ess, final User user, final Lis

final String[] parts = kitItem.split(" +");
final ItemStack parseStack = ess.getItemDb().get(parts[0], parts.length > 1 ? Integer.parseInt(parts[1]) : 1);

if (parseStack.getType() == Material.AIR) {

if (parseStack.getType() == Material.AIR)
{
continue;
}

final MetaItemStack metaStack = new MetaItemStack(parseStack);

if (parts.length > 2)
@@ -213,10 +265,12 @@ public static void expandItems(final IEssentials ess, final User user, final Lis
for (ItemStack itemStack : overfilled.values())
{
int spillAmount = itemStack.getAmount();
if (!allowOversizedStacks) {
itemStack.setAmount(spillAmount < itemStack.getMaxStackSize() ? spillAmount : itemStack.getMaxStackSize());
if (!allowOversizedStacks)
{
itemStack.setAmount(spillAmount < itemStack.getMaxStackSize() ? spillAmount : itemStack.getMaxStackSize());
}
while (spillAmount > 0) {
while (spillAmount > 0)
{
user.getWorld().dropItemNaturally(user.getLocation(), itemStack);
spillAmount -= itemStack.getAmount();
}
@@ -3,12 +3,11 @@
import com.earth2me.essentials.CommandSource;
import static com.earth2me.essentials.I18n.tl;
import com.earth2me.essentials.Kit;
import com.earth2me.essentials.Trade;
import com.earth2me.essentials.User;
import com.earth2me.essentials.utils.StringUtil;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import org.bukkit.Server;


@@ -57,59 +56,51 @@ public void run(final Server server, final CommandSource sender, final String co

for (final String kitName : kits)
{
final Map<String, Object> kit = ess.getSettings().getKit(kitName);
final List<String> items = Kit.getItems(ess, userTo, kitName, kit);
Kit.expandItems(ess, userTo, items);
final Kit kit = new Kit(kitName, ess);
kit.expandItems(userTo);

sender.sendMessage(tl("kitGiveTo", kitName, userTo.getDisplayName()));
userTo.sendMessage(tl("kitReceive", kitName));
}
}
}

private void giveKits(User userTo, User userFrom, String kitNames) throws Exception
private void giveKits(final User userTo, final User userFrom, final String kitNames) throws Exception
{
if (kitNames.isEmpty())
{
throw new Exception(tl("kitError2"));
throw new Exception(tl("kitNotFound"));
}
String[] kits = kitNames.split(",");
String[] kitList = kitNames.split(",");

for (final String kitName : kits)
{
giveKit(userTo, userFrom, kitName);
}
}
List<Kit> kits = new ArrayList<Kit>();

private void giveKit(User userTo, User userFrom, String kitName) throws Exception
{
if (kitName.isEmpty())
for (final String kitName : kitList)
{
throw new Exception(tl("kitError2"));
}

final Map<String, Object> kit = ess.getSettings().getKit(kitName);
if (kitName.isEmpty())
{
throw new Exception(tl("kitNotFound"));
}

if (!userFrom.isAuthorized("essentials.kits." + kitName))
{
throw new Exception(tl("noKitPermission", "essentials.kits." + kitName));
Kit kit = new Kit(kitName, ess);
kit.checkPerms(userFrom);
kit.checkDelay(userFrom);
kit.checkAffordable(userFrom);
kits.add(kit);
}

final List<String> items = Kit.getItems(ess, userTo, kitName, kit);

final Trade charge = new Trade("kit-" + kitName, new Trade("kit-kit", ess), ess);
charge.isAffordableFor(userFrom);

Kit.checkTime(userFrom, kitName, kit);
Kit.expandItems(ess, userTo, items);
for (final Kit kit : kits)
{
kit.setTime(userFrom);
kit.expandItems(userTo);
kit.chargeUser(userTo);

charge.charge(userFrom);
if (!userFrom.equals(userTo))
{
userFrom.sendMessage(tl("kitGiveTo", kit.getName(), userTo.getDisplayName()));
}

if (!userFrom.equals(userTo))
{
userFrom.sendMessage(tl("kitGiveTo", kitName, userTo.getDisplayName()));
userTo.sendMessage(tl("kitReceive", kit.getName()));
}

userTo.sendMessage(tl("kitReceive", kitName));
}
}
@@ -59,10 +59,11 @@ protected boolean onSignInteract(final ISign sign, final User player, final Stri
charge.isAffordableFor(player);
try
{
final Map<String, Object> kit = ess.getSettings().getKit(kitName);
Kit.checkTime(player, kitName, kit);
final List<String> items = Kit.getItems(ess, player, kitName, kit);
Kit.expandItems(ess, player, items);
final Kit kit = new Kit(kitName, ess);
kit.checkDelay(player);
kit.setTime(player);
kit.expandItems(player);

charge.charge(player);
Trade.log("Sign", "Kit", "Interact", username, null, username, charge, sign.getBlock().getLocation(), ess);
}
@@ -122,9 +122,8 @@ public void run()
{
try
{
final Map<String, Object> kit = ess.getSettings().getKit(kitName.toLowerCase(Locale.ENGLISH));
final List<String> items = Kit.getItems(ess, user, kitName, kit);
Kit.expandItems(ess, user, items);
final Kit kit = new Kit(kitName.toLowerCase(Locale.ENGLISH), ess);
kit.expandItems(user);
}
catch (Exception ex)
{

0 comments on commit f6b2524

Please sign in to comment.
You can’t perform that action at this time.