updated give to allow naming of items.

This commit is contained in:
Mini-Chiss 2015-08-20 20:38:07 +02:00
parent f97da1bdba
commit 15c873392a
2 changed files with 34 additions and 20 deletions

View File

@ -4,22 +4,24 @@ import java.util.AbstractMap;
import java.util.LinkedList;
import java.util.Map.Entry;
import mineplex.core.common.structs.ItemContainer;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
public class UtilItem
{
public static LinkedList<Entry<Material, Byte>> matchItem(Player caller, String items, boolean inform)
public static LinkedList<ItemContainer> matchItem(Player caller, String items, boolean inform)
{
LinkedList<Entry<Material, Byte>> matchList = new LinkedList<Entry<Material, Byte>>();
LinkedList<ItemContainer> matchList = new LinkedList<ItemContainer>();
String failList = "";
//Mass Search
for (String cur : items.split(","))
{
Entry<Material, Byte> match = searchItem(caller, cur, inform);
ItemContainer match = searchItem(caller, cur, inform);
if (match != null)
matchList.add(match);
@ -40,21 +42,27 @@ public class UtilItem
return matchList;
}
public static Entry<Material, Byte> searchItem(Player caller, String args, boolean inform)
public static ItemContainer searchItem(Player caller, String args, boolean inform)
{
LinkedList<Entry<Material, Byte>> matchList = new LinkedList<Entry<Material, Byte>>();
LinkedList<ItemContainer> matchList = new LinkedList<ItemContainer>();
for (Material cur : Material.values())
{
String[] arg = args.split(":");
//Get Selected Name
String name = null;
if (arg.length > 2)
name = arg[2].replaceAll("_", " ");
//By Name
if (cur.toString().equalsIgnoreCase(args))
return new AbstractMap.SimpleEntry<Material, Byte>(cur, (byte)0);
return new ItemContainer(cur, (byte)0, name);
if (cur.toString().toLowerCase().contains(args.toLowerCase()))
matchList.add(new AbstractMap.SimpleEntry<Material, Byte>(cur, (byte)0));
matchList.add(new ItemContainer(cur, (byte)0, name));
//By ID:Data
String[] arg = args.split(":");
//By ID:Data:Name
//ID
int id = 0;
@ -83,7 +91,7 @@ public class UtilItem
continue;
}
return new AbstractMap.SimpleEntry<Material, Byte>(cur, data);
return new ItemContainer(cur, data, name);
}
//No / Non-Unique
@ -102,8 +110,9 @@ public class UtilItem
if (matchList.size() > 0)
{
String matchString = "";
for (Entry<Material, Byte> cur : matchList)
matchString += F.elem(cur.getKey().toString()) + ", ";
for (ItemContainer cur : matchList)
matchString += F.elem(cur.Type.toString()) + ", ";
if (matchString.length() > 1)
matchString = matchString.substring(0 , matchString.length() - 2);

View File

@ -11,6 +11,7 @@ import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.java.JavaPlugin;
import mineplex.core.MiniPlugin;
import mineplex.core.common.structs.ItemContainer;
import mineplex.core.common.util.F;
import mineplex.core.common.util.UtilInv;
import mineplex.core.common.util.UtilItem;
@ -65,7 +66,7 @@ public class Give extends MiniPlugin
public void give(Player player, String target, String itemNames, String amount, String enchants)
{
//Item
LinkedList<Entry<Material, Byte>> itemList = new LinkedList<Entry<Material, Byte>>();
LinkedList<ItemContainer> itemList = new LinkedList<ItemContainer>();
itemList = UtilItem.matchItem(player, itemNames, true);
if (itemList.isEmpty())
return;
@ -128,11 +129,15 @@ public class Give extends MiniPlugin
if (givenList.length() > 0)
givenList = givenList.substring(0, givenList.length()-1);
for (Entry<Material, Byte> curItem : itemList)
for (ItemContainer curItem : itemList)
{
for (Player cur : giveList)
{
ItemStack stack = ItemStackFactory.Instance.CreateStack(curItem.getKey(), curItem.getValue(), count);
ItemStack stack;
if (curItem.Name == null)
stack = ItemStackFactory.Instance.CreateStack(curItem.Type, curItem.Data, count);
else
stack = ItemStackFactory.Instance.CreateStack(curItem.Type, curItem.Data, count, curItem.Name);
//Enchants
stack.addUnsafeEnchantments(enchs);
@ -142,18 +147,18 @@ public class Give extends MiniPlugin
{
//Inform
if (!cur.equals(player))
UtilPlayer.message(cur, F.main("Give", "You received " + F.item(count + " " + ItemStackFactory.Instance.GetName(curItem.getKey(), curItem.getValue(), false)) + " from " + F.elem(player.getName()) + "."));
UtilPlayer.message(cur, F.main("Give", "You received " + F.item(count + " " + ItemStackFactory.Instance.GetName(curItem.Type, curItem.Data, false)) + " from " + F.elem(player.getName()) + "."));
}
}
if (target.equalsIgnoreCase("all"))
UtilPlayer.message(player, F.main("Give", "You gave " + F.item(count + " " + ItemStackFactory.Instance.GetName(curItem.getKey(), curItem.getValue(), false)) + " to " + F.elem("ALL")) + ".");
UtilPlayer.message(player, F.main("Give", "You gave " + F.item(count + " " + ItemStackFactory.Instance.GetName(curItem.Type, curItem.Data, false)) + " to " + F.elem("ALL")) + ".");
else if (giveList.size() > 1)
UtilPlayer.message(player, F.main("Give", "You gave " + F.item(count + " " + ItemStackFactory.Instance.GetName(curItem.getKey(), curItem.getValue(), false)) + " to " + F.elem(givenList) + "."));
UtilPlayer.message(player, F.main("Give", "You gave " + F.item(count + " " + ItemStackFactory.Instance.GetName(curItem.Type, curItem.Data, false)) + " to " + F.elem(givenList) + "."));
else
UtilPlayer.message(player, F.main("Give", "You gave " + F.item(count + " " + ItemStackFactory.Instance.GetName(curItem.getKey(), curItem.getValue(), false)) + " to " + F.elem(giveList.getFirst().getName()) + "."));
UtilPlayer.message(player, F.main("Give", "You gave " + F.item(count + " " + ItemStackFactory.Instance.GetName(curItem.Type, curItem.Data, false)) + " to " + F.elem(giveList.getFirst().getName()) + "."));
}
}
}