mirror of
https://github.com/EssentialsX/Essentials.git
synced 2025-01-09 01:48:15 +01:00
First Refactor of Kits
New sign: [Kit] / <kit name> / [group] / [charge]
This commit is contained in:
parent
221e1d3bb2
commit
b102c5f3d8
113
Essentials/src/com/earth2me/essentials/Kit.java
Normal file
113
Essentials/src/com/earth2me/essentials/Kit.java
Normal file
@ -0,0 +1,113 @@
|
||||
package com.earth2me.essentials;
|
||||
|
||||
import static com.earth2me.essentials.I18n._;
|
||||
import com.earth2me.essentials.commands.NoChargeException;
|
||||
import com.earth2me.essentials.craftbukkit.InventoryWorkaround;
|
||||
import java.util.*;
|
||||
import org.bukkit.Material;
|
||||
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
|
||||
{
|
||||
final Map<String, Object> kits = ess.getSettings().getKits();
|
||||
final StringBuilder list = new StringBuilder();
|
||||
for (String kiteItem : kits.keySet())
|
||||
{
|
||||
if (user.isAuthorized("essentials.kit." + kiteItem.toLowerCase(Locale.ENGLISH)))
|
||||
{
|
||||
list.append(" ").append(kiteItem);
|
||||
}
|
||||
}
|
||||
return list.toString();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new Exception(_("kitError"));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static void checkTime(final User user, final String kitName, final Map<String, Object> els) throws NoChargeException
|
||||
{
|
||||
final double delay = els.containsKey("delay") ? ((Number)els.get("delay")).doubleValue() : 0L;
|
||||
final Calendar c = new GregorianCalendar();
|
||||
c.add(Calendar.SECOND, -(int)delay);
|
||||
c.add(Calendar.MILLISECOND, -(int)((delay * 1000.0) % 1000.0));
|
||||
|
||||
final long mintime = c.getTimeInMillis();
|
||||
|
||||
final Long lastTime = user.getKitTimestamp(kitName);
|
||||
if (lastTime == null || lastTime < mintime)
|
||||
{
|
||||
final Calendar now = new GregorianCalendar();
|
||||
user.setKitTimestamp(kitName, now.getTimeInMillis());
|
||||
}
|
||||
else
|
||||
{
|
||||
final Calendar future = new GregorianCalendar();
|
||||
future.setTimeInMillis(lastTime);
|
||||
future.add(Calendar.SECOND, (int)delay);
|
||||
future.add(Calendar.MILLISECOND, (int)((delay * 1000.0) % 1000.0));
|
||||
user.sendMessage(_("kitTimed", Util.formatDateDiff(future.getTimeInMillis())));
|
||||
throw new NoChargeException();
|
||||
}
|
||||
}
|
||||
|
||||
public static List<String> getItems(final User user, final Map<String, Object> els) throws Exception
|
||||
{
|
||||
try
|
||||
{
|
||||
return (List<String>)els.get("items");
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
user.sendMessage(_("kitError2"));
|
||||
throw new Exception(_("kitErrorHelp"));
|
||||
}
|
||||
}
|
||||
|
||||
public static void expandItems(final IEssentials ess, final User user, final List<String> items) throws Exception
|
||||
{
|
||||
try
|
||||
{
|
||||
boolean spew = false;
|
||||
for (String d : items)
|
||||
{
|
||||
final String[] parts = d.split("[^0-9]+", 3);
|
||||
final int id = Material.getMaterial(Integer.parseInt(parts[0])).getId();
|
||||
final int amount = parts.length > 1 ? Integer.parseInt(parts[parts.length > 2 ? 2 : 1]) : 1;
|
||||
final short data = parts.length > 2 ? Short.parseShort(parts[1]) : 0;
|
||||
final Map<Integer, ItemStack> overfilled;
|
||||
if (user.isAuthorized("essentials.oversizedstacks"))
|
||||
{
|
||||
overfilled = InventoryWorkaround.addItem(user.getInventory(), true, ess.getSettings().getOversizedStackSize(), new ItemStack(id, amount, data));
|
||||
}
|
||||
else
|
||||
{
|
||||
overfilled = InventoryWorkaround.addItem(user.getInventory(), true, new ItemStack(id, amount, data));
|
||||
}
|
||||
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();
|
||||
throw new Exception(_("kitError2"));
|
||||
}
|
||||
}
|
||||
}
|
@ -1,14 +1,9 @@
|
||||
package com.earth2me.essentials.commands;
|
||||
|
||||
import com.earth2me.essentials.craftbukkit.InventoryWorkaround;
|
||||
import com.earth2me.essentials.*;
|
||||
import static com.earth2me.essentials.I18n._;
|
||||
import com.earth2me.essentials.Trade;
|
||||
import com.earth2me.essentials.User;
|
||||
import com.earth2me.essentials.Util;
|
||||
import java.util.*;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.Server;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
|
||||
public class Commandkit extends EssentialsCommand
|
||||
@ -23,131 +18,39 @@ public class Commandkit extends EssentialsCommand
|
||||
{
|
||||
if (args.length < 1)
|
||||
{
|
||||
try
|
||||
final String kitList = Kit.listKits(ess, user);
|
||||
if (kitList.length() > 0)
|
||||
{
|
||||
final Map<String, Object> kits = ess.getSettings().getKits();
|
||||
final StringBuilder list = new StringBuilder();
|
||||
for (String kiteItem : kits.keySet())
|
||||
{
|
||||
if (user.isAuthorized("essentials.kit." + kiteItem.toLowerCase(Locale.ENGLISH)))
|
||||
{
|
||||
list.append(" ").append(kiteItem);
|
||||
}
|
||||
}
|
||||
if (list.length() > 0)
|
||||
{
|
||||
user.sendMessage(_("kits", list.toString()));
|
||||
}
|
||||
else
|
||||
{
|
||||
user.sendMessage(_("noKits"));
|
||||
}
|
||||
user.sendMessage(_("kits", kitList));
|
||||
}
|
||||
catch (Exception ex)
|
||||
else
|
||||
{
|
||||
user.sendMessage(_("kitError"));
|
||||
user.sendMessage(_("noKits"));
|
||||
}
|
||||
throw new NoChargeException();
|
||||
}
|
||||
else
|
||||
{
|
||||
try
|
||||
final String kitName = args[0].toLowerCase(Locale.ENGLISH);
|
||||
final Object kit = ess.getSettings().getKit(kitName);
|
||||
|
||||
if (!user.isAuthorized("essentials.kit." + kitName))
|
||||
{
|
||||
final String kitName = args[0].toLowerCase(Locale.ENGLISH);
|
||||
final Object kit = ess.getSettings().getKit(kitName);
|
||||
List<String> items;
|
||||
|
||||
if (!user.isAuthorized("essentials.kit." + kitName))
|
||||
{
|
||||
user.sendMessage(_("noKitPermission", "essentials.kit." + kitName));
|
||||
return;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
|
||||
//System.out.println("Kit is timed");
|
||||
final Map<String, Object> els = (Map<String, Object>)kit;
|
||||
items = (List<String>)els.get("items");
|
||||
final double delay = els.containsKey("delay") ? ((Number)els.get("delay")).doubleValue() : 0L;
|
||||
final Calendar c = new GregorianCalendar();
|
||||
c.add(Calendar.SECOND, -(int)delay);
|
||||
c.add(Calendar.MILLISECOND, -(int)((delay * 1000.0) % 1000.0));
|
||||
|
||||
final long mintime = c.getTimeInMillis();
|
||||
|
||||
final Long lastTime = user.getKitTimestamp(kitName);
|
||||
if (lastTime == null || lastTime < mintime)
|
||||
{
|
||||
final Calendar now = new GregorianCalendar();
|
||||
user.setKitTimestamp(kitName, now.getTimeInMillis());
|
||||
}
|
||||
else
|
||||
{
|
||||
final Calendar future = new GregorianCalendar();
|
||||
future.setTimeInMillis(lastTime);
|
||||
future.add(Calendar.SECOND, (int)delay);
|
||||
future.add(Calendar.MILLISECOND, (int)((delay * 1000.0) % 1000.0));
|
||||
user.sendMessage(_("kitTimed", Util.formatDateDiff(future.getTimeInMillis())));
|
||||
return;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
items = (List<String>)kit;
|
||||
}
|
||||
|
||||
final Trade charge = new Trade("kit-" + kitName, ess);
|
||||
try
|
||||
{
|
||||
charge.isAffordableFor(user);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
user.sendMessage(ex.getMessage());
|
||||
return;
|
||||
}
|
||||
|
||||
boolean spew = false;
|
||||
for (String d : items)
|
||||
{
|
||||
final String[] parts = d.split("[^0-9]+", 3);
|
||||
final int id = Material.getMaterial(Integer.parseInt(parts[0])).getId();
|
||||
final int amount = parts.length > 1 ? Integer.parseInt(parts[parts.length > 2 ? 2 : 1]) : 1;
|
||||
final short data = parts.length > 2 ? Short.parseShort(parts[1]) : 0;
|
||||
final Map<Integer, ItemStack> overfilled;
|
||||
if (user.isAuthorized("essentials.oversizedstacks"))
|
||||
{
|
||||
overfilled = InventoryWorkaround.addItem(user.getInventory(), true, ess.getSettings().getOversizedStackSize(), new ItemStack(id, amount, data));
|
||||
}
|
||||
else
|
||||
{
|
||||
overfilled = InventoryWorkaround.addItem(user.getInventory(), true, new ItemStack(id, amount, data));
|
||||
}
|
||||
for (ItemStack itemStack : overfilled.values())
|
||||
{
|
||||
user.getWorld().dropItemNaturally(user.getLocation(), itemStack);
|
||||
spew = true;
|
||||
}
|
||||
}
|
||||
if (spew)
|
||||
{
|
||||
user.sendMessage(_("kitInvFull"));
|
||||
}
|
||||
try
|
||||
{
|
||||
charge.charge(user);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
user.sendMessage(ex.getMessage());
|
||||
}
|
||||
user.sendMessage(_("kitGive", kitName));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
user.sendMessage(_("kitError2"));
|
||||
user.sendMessage(_("kitErrorHelp"));
|
||||
throw new Exception(_("noKitPermission", "essentials.kit." + kitName));
|
||||
}
|
||||
final Map<String, Object> els = (Map<String, Object>)kit;
|
||||
final List<String> items = Kit.getItems(user, els);
|
||||
|
||||
Kit.checkTime(user, kitName, els);
|
||||
|
||||
final Trade charge = new Trade("kit-" + kitName, ess);
|
||||
charge.isAffordableFor(user);
|
||||
|
||||
Kit.expandItems(ess, user, items);
|
||||
|
||||
charge.charge(user);
|
||||
user.sendMessage(_("kitGive", kitName));
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -70,6 +70,7 @@ public class Commandwarp extends EssentialsCommand
|
||||
|
||||
}
|
||||
|
||||
//TODO: Use one of the new text classes, like /help ?
|
||||
private void warpList(final CommandSender sender, final String[] args) throws Exception
|
||||
{
|
||||
final Warps warps = ess.getWarps();
|
||||
|
73
Essentials/src/com/earth2me/essentials/signs/SignKit.java
Normal file
73
Essentials/src/com/earth2me/essentials/signs/SignKit.java
Normal file
@ -0,0 +1,73 @@
|
||||
package com.earth2me.essentials.signs;
|
||||
|
||||
import com.earth2me.essentials.*;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
|
||||
|
||||
public class SignKit extends EssentialsSign
|
||||
{
|
||||
public SignKit()
|
||||
{
|
||||
super("Kit");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean onSignCreate(final ISign sign, final User player, final String username, final IEssentials ess) throws SignException
|
||||
{
|
||||
validateTrade(sign, 3, ess);
|
||||
|
||||
final String kitName = sign.getLine(1).toLowerCase(Locale.ENGLISH);
|
||||
|
||||
if (kitName.isEmpty())
|
||||
{
|
||||
sign.setLine(1, "§dKit name!");
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
try
|
||||
{
|
||||
ess.getSettings().getKit(kitName);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new SignException(ex.getMessage(), ex);
|
||||
}
|
||||
final String group = sign.getLine(2);
|
||||
if ("Everyone".equalsIgnoreCase(group) || "Everybody".equalsIgnoreCase(group))
|
||||
{
|
||||
sign.setLine(2, "§2Everyone");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean onSignInteract(final ISign sign, final User player, final String username, final IEssentials ess) throws SignException, ChargeException
|
||||
{
|
||||
final String kitName = sign.getLine(1).toLowerCase(Locale.ENGLISH);
|
||||
final String group = sign.getLine(2);
|
||||
if ((!group.isEmpty() && ("§2Everyone".equals(group) || player.inGroup(group)))
|
||||
|| (group.isEmpty() && (player.isAuthorized("essentials.kit." + kitName))))
|
||||
{
|
||||
final Trade charge = getTrade(sign, 3, ess);
|
||||
charge.isAffordableFor(player);
|
||||
try
|
||||
{
|
||||
final Object kit = ess.getSettings().getKit(kitName);
|
||||
final Map<String, Object> els = (Map<String, Object>)kit;
|
||||
final List<String> items = Kit.getItems(player, els);
|
||||
Kit.expandItems(ess, player, items);
|
||||
charge.charge(player);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new SignException(ex.getMessage(), ex);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
@ -10,6 +10,7 @@ public enum Signs
|
||||
FREE(new SignFree()),
|
||||
GAMEMODE(new SignGameMode()),
|
||||
HEAL(new SignHeal()),
|
||||
KIT(new SignKit()),
|
||||
MAIL(new SignMail()),
|
||||
PROTECTION(new SignProtection()),
|
||||
SELL(new SignSell()),
|
||||
|
Loading…
Reference in New Issue
Block a user