Added support for <item>:<metadata>:<amount>, such as cocoa beans

This commit is contained in:
Garbage Mule 2011-07-08 01:04:55 +02:00
parent 36c27cc587
commit 5dc1c31056
3 changed files with 48 additions and 29 deletions

Binary file not shown.

View File

@ -977,6 +977,20 @@ public class Arena
public void onPlayerInteract(PlayerInteractEvent event) public void onPlayerInteract(PlayerInteractEvent event)
{ {
/*if (running && livePlayers.contains(event.getPlayer()))
{
Player p = event.getPlayer();
Action a = event.getAction();
if (a == Action.RIGHT_CLICK_AIR || a == Action.RIGHT_CLICK_BLOCK)
{
if (p.getItemInHand().getType() == Material.BOW)
p.shootArrow();
event.setUseItemInHand(Result.DENY);
event.setCancelled(true);
}
return;
}*/
if (running || !livePlayers.contains(event.getPlayer())) if (running || !livePlayers.contains(event.getPlayer()))
return; return;

View File

@ -23,6 +23,7 @@ import org.bukkit.command.CommandSender;
import org.bukkit.craftbukkit.CraftWorld; import org.bukkit.craftbukkit.CraftWorld;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.DyeColor;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.Location; import org.bukkit.Location;
@ -168,24 +169,29 @@ public class MAUtils
List<ItemStack> result = new LinkedList<ItemStack>(); List<ItemStack> result = new LinkedList<ItemStack>();
if (string == null) return result; if (string == null) return result;
// Trim commas and whitespace, and split items by commas
string = string.trim(); string = string.trim();
if (string.endsWith(",")) if (string.endsWith(","))
string = string.substring(0, string.length()-1); string = string.substring(0, string.length()-1);
String[] items = string.split(","); String[] items = string.split(",");
for (String item : items) for (String item : items)
{ {
// Trim whitespace and split by colons.
item = item.trim(); item = item.trim();
String[] parts = item.split(":"); String[] parts = item.split(":");
// Grab the amount. // Grab the amount.
int amount = (parts.length == 2 && parts[1].matches("[0-9]+")) ? int amount = 1;
Integer.parseInt(parts[1]) : if (parts.length == 2 && parts[1].matches("(-)?[0-9]+"))
1; amount = Integer.parseInt(parts[1]);
else if (parts.length == 3 && parts[2].matches("(-)?[0-9]+"))
amount = Integer.parseInt(parts[2]);
// Make the ItemStack. // Make the ItemStack.
ItemStack stack = (parts[0].matches("[0-9]+")) ? ItemStack stack = (parts.length == 3) ?
makeItemStack(Integer.parseInt(parts[0]), amount) : makeItemStack(parts[0], amount, parts[1]) :
makeItemStack(parts[0], amount); makeItemStack(parts[0], amount);
result.add(stack); result.add(stack);
@ -329,12 +335,6 @@ public class MAUtils
// Grab the contents. // Grab the contents.
ItemStack[] armor = p.getInventory().getArmorContents(); ItemStack[] armor = p.getInventory().getArmorContents();
ItemStack[] items = p.getInventory().getContents(); ItemStack[] items = p.getInventory().getContents();
/*p.getLocation().getBlock().getRelative(BlockFace.UP).setType(Material.CHEST);
Chest chest = (Chest) p.getLocation().getBlock().getRelative(BlockFace.UP).getState();
for (ItemStack stack : p.getInventory().getContents())
if (stack != null)
chest.getInventory().addItem(stack);*/
String invPath = "plugins" + sep + "MobArena" + sep + "inventories"; String invPath = "plugins" + sep + "MobArena" + sep + "inventories";
new File(invPath).mkdir(); new File(invPath).mkdir();
@ -476,7 +476,13 @@ public class MAUtils
//if (SWORDS_TYPE.contains(stack.getType())) //if (SWORDS_TYPE.contains(stack.getType()))
if (WEAPONS_TYPE.contains(stack.getType())) if (WEAPONS_TYPE.contains(stack.getType()))
stack.setDurability((short) -32768); stack.setDurability((short) -32768);
/*if (stack.getAmount() < 0)
{
stack = new ItemStack(stack.getType(), 32768);
inv.setItem(inv.firstEmpty(), stack);
continue;
}*/
inv.addItem(stack); inv.addItem(stack);
} }
} }
@ -522,12 +528,24 @@ public class MAUtils
} }
/* Helper methods for making ItemStacks out of strings and ints */ /* Helper methods for making ItemStacks out of strings and ints */
private static ItemStack makeItemStack(String name, int amount) private static ItemStack makeItemStack(String name, int amount, String data)
{ {
try try
{ {
Material material = Material.valueOf(name.toUpperCase()); byte offset = 0;
return new ItemStack(material, amount);
Material material = (name.matches("[0-9]+")) ?
Material.getMaterial(Integer.parseInt(name)) :
Material.valueOf(name.toUpperCase());
if (material == Material.INK_SACK)
offset = 15;
DyeColor dye = (data.matches("[0-9]+")) ?
DyeColor.getByData(Byte.parseByte(data)) :
DyeColor.valueOf(data.toUpperCase());
return new ItemStack(material, amount, (byte) Math.abs((offset - dye.getData())));
} }
catch (Exception e) catch (Exception e)
{ {
@ -535,20 +553,7 @@ public class MAUtils
return null; return null;
} }
} }
private static ItemStack makeItemStack(String name, int amount) { return makeItemStack(name, amount, "0"); }
private static ItemStack makeItemStack(int id, int amount)
{
try
{
Material material = Material.getMaterial(id);
return new ItemStack(material, amount);
}
catch (Exception e)
{
System.out.println("[MobArena] ERROR! Could not create item with id " + id + ". Check config.yml");
return null;
}
}
/* Helper method for grabbing a random reward */ /* Helper method for grabbing a random reward */
public static ItemStack getRandomReward(List<ItemStack> rewards) public static ItemStack getRandomReward(List<ItemStack> rewards)