Update /ci to support vanilla syntax

This commit is contained in:
GunfighterJ 2013-01-23 16:44:32 -06:00
parent 2361b9e724
commit 133fdac343
2 changed files with 103 additions and 14 deletions

View file

@ -2,10 +2,12 @@ package com.earth2me.essentials.commands;
import static com.earth2me.essentials.I18n._; import static com.earth2me.essentials.I18n._;
import com.earth2me.essentials.User; import com.earth2me.essentials.User;
import com.earth2me.essentials.Util;
import java.util.List; import java.util.List;
import org.bukkit.Server; import org.bukkit.Server;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
public class Commandclearinventory extends EssentialsCommand public class Commandclearinventory extends EssentialsCommand
@ -22,38 +24,71 @@ public class Commandclearinventory extends EssentialsCommand
if (args.length > 0 && user.isAuthorized("essentials.clearinventory.others")) if (args.length > 0 && user.isAuthorized("essentials.clearinventory.others"))
{ {
//TODO: Fix fringe user match case. //TODO: Fix fringe user match case.
if (args[0].length() >= 3) if (args[0].contentEquals("*") && user.isAuthorized("essentials.clearinventory.all"))
{
if (args.length > 1)
{
for (Player onlinePlayer : server.getOnlinePlayers())
{
clearInventory(onlinePlayer, args[1]);
}
user.sendMessage("Cleared everyone's inventory");
}
else
{
throw new NotEnoughArgumentsException();
}
}
else if (args[0].length() >= 3)
{ {
List<Player> online = server.matchPlayer(args[0]); List<Player> online = server.matchPlayer(args[0]);
if (!online.isEmpty()) if (!online.isEmpty())
{ {
for (Player p : online) for (Player p : online)
{
if (args.length > 1)
{
clearInventory(p, args[1]);
}
else
{ {
p.getInventory().clear(); p.getInventory().clear();
}
user.sendMessage(_("inventoryClearedOthers", p.getDisplayName())); user.sendMessage(_("inventoryClearedOthers", p.getDisplayName()));
} }
return;
} }
throw new Exception(_("playerNotFound")); else
{
clearInventory(user, args[0]);
user.sendMessage(_("inventoryCleared"));
}
} }
else else
{ {
Player p = server.getPlayer(args[0]); Player p = server.getPlayer(args[0]);
if (p != null) if (p != null)
{ {
p.getInventory().clear(); clearInventory(p, args[1]);
user.sendMessage(_("inventoryClearedOthers", p.getDisplayName())); user.sendMessage(_("inventoryClearedOthers", p.getDisplayName()));
} }
else else
{ {
throw new Exception(_("playerNotFound")); clearInventory(user, args[0]);
user.sendMessage(_("inventoryCleared"));
} }
} }
} }
else else
{
if (args.length > 0)
{
clearInventory(user, args[0]);
}
else
{ {
user.getInventory().clear(); user.getInventory().clear();
}
user.sendMessage(_("inventoryCleared")); user.sendMessage(_("inventoryCleared"));
} }
} }
@ -66,27 +101,51 @@ public class Commandclearinventory extends EssentialsCommand
throw new NotEnoughArgumentsException(); throw new NotEnoughArgumentsException();
} }
if (args[0].length() >= 3) if (args[0].contentEquals("*"))
{
if (args.length > 1)
{
for (Player onlinePlayer : server.getOnlinePlayers())
{
clearInventory(onlinePlayer, args[1]);
}
sender.sendMessage("Cleared everyone's inventory");
}
else
{
throw new NotEnoughArgumentsException();
}
}
else if (args[0].length() >= 3)
{ {
List<Player> online = server.matchPlayer(args[0]); List<Player> online = server.matchPlayer(args[0]);
if (!online.isEmpty()) if (!online.isEmpty())
{ {
for (Player p : online) for (Player p : online)
{
if (args.length > 1)
{
clearInventory(p, args[1]);
}
else
{ {
p.getInventory().clear(); p.getInventory().clear();
}
sender.sendMessage(_("inventoryClearedOthers", p.getDisplayName())); sender.sendMessage(_("inventoryClearedOthers", p.getDisplayName()));
} }
return;
} }
else
{
throw new Exception(_("playerNotFound")); throw new Exception(_("playerNotFound"));
} }
}
else else
{ {
Player u = server.getPlayer(args[0]); Player u = server.getPlayer(args[0]);
if (u != null) if (u != null)
{ {
u.getInventory().clear(); clearInventory(u, args[0]);
sender.sendMessage(_("inventoryClearedOthers", u.getDisplayName())); sender.sendMessage(_("inventoryClearedOthers", u.getDisplayName()));
} }
else else
@ -95,4 +154,34 @@ public class Commandclearinventory extends EssentialsCommand
} }
} }
} }
public void clearInventory(Player player, String arg) throws Exception
{
final String[] split = arg.split(":");
if (arg.contentEquals("*"))
{
player.getInventory().clear();
}
else
{
final ItemStack item = ess.getItemDb().get(split[0]);
final int type = item.getTypeId();
if (split.length > 1 && Util.isInt(arg.replace(":", "")))
{
player.getInventory().clear(type, Integer.parseInt(split[1]));
}
else
{
if (Util.isInt(split[0]))
{
player.getInventory().clear(type, -1);
}
else
{
player.getInventory().clear(type, item.getDurability());
}
}
}
}
} }

View file

@ -62,7 +62,7 @@ commands:
clearinventory: clearinventory:
description: Clear all items in your inventory. description: Clear all items in your inventory.
usage: /<command> usage: /<command>
aliases: [ci,eci,clearinvent,clean,eclean,eclearinvent,eclearinventory] aliases: [clear,ci,eci,clearinvent,clean,eclean,eclearinvent,eclearinventory]
compass: compass:
description: Describes your current bearing. description: Describes your current bearing.
usage: /<command> usage: /<command>