From fc745501e51a10c34712f6d52ede7de2fa494cbf Mon Sep 17 00:00:00 2001 From: Zino Date: Tue, 26 Nov 2013 07:37:14 +0100 Subject: [PATCH] * Item system improved. --- .../java/me/blackvein/quests/ItemData.java | 74 +++---------------- .../java/me/blackvein/quests/Quester.java | 10 ++- .../quests/prompts/ItemStackPrompt.java | 63 ++++++++-------- 3 files changed, 48 insertions(+), 99 deletions(-) diff --git a/src/main/java/me/blackvein/quests/ItemData.java b/src/main/java/me/blackvein/quests/ItemData.java index 60845fc9f..c4d889f83 100644 --- a/src/main/java/me/blackvein/quests/ItemData.java +++ b/src/main/java/me/blackvein/quests/ItemData.java @@ -1,75 +1,19 @@ package me.blackvein.quests; -import java.util.HashMap; -import java.util.Scanner; +import org.bukkit.Material; public class ItemData { - public HashMap map = new HashMap(); - private static ItemData instance = null; - - public static ItemData getInstance() { - if (instance != null) { - return instance; - } else { - instance = new ItemData(); - return instance; - } - } - - private ItemData() { - Scanner scanner = new Scanner(this.getClass().getClassLoader().getResourceAsStream("items.txt")); - - while(scanner.hasNextLine()) { - String[] line = scanner.nextLine().split("="); - if (line.length > 1) { - String[] name = line[0].split(","); - Data data; - - String[] str = line[1].split(","); - - int id = -1; - byte mdata = 0; - try { - id = Integer.parseInt(str[0]); - } catch (NumberFormatException e) { - } - if (str.length > 1) { - mdata = Byte.parseByte(str[1]); - } - - for (String n : name) { - data = new Data(n, id, mdata); - map.put(n, data); + public static Material getMaterial(String name) { + Material material = Material.matchMaterial(name); + if (material == null) { + name = name.toUpperCase().replace(" ", "_"); + for (Material mat : Material.values()) { + if (mat.toString().contains(name)) { + return mat; } } } - } - public Data getItem(String input) { - if (map.containsKey(input)) { - return map.get(input); - } else { - return null; - } - } - - public class Data { - private final String name_; - private final int id_; - private final byte data_; - - public Data(String name, int id, byte mdata) { - this.name_ = name; - this.id_ = id; - this.data_ = mdata; - } - - public int getId() { - return id_; - } - - public byte getData() { - return data_; - } + return material; } } diff --git a/src/main/java/me/blackvein/quests/Quester.java b/src/main/java/me/blackvein/quests/Quester.java index dd9579ea1..07c05e00d 100644 --- a/src/main/java/me/blackvein/quests/Quester.java +++ b/src/main/java/me/blackvein/quests/Quester.java @@ -5,8 +5,10 @@ import java.io.IOException; import java.util.*; import java.util.Map.Entry; import java.util.logging.Level; + import me.blackvein.quests.util.ItemUtil; import net.citizensnpcs.api.npc.NPC; + import org.bukkit.ChatColor; import org.bukkit.DyeColor; import org.bukkit.Location; @@ -1169,7 +1171,7 @@ public class Quester { fishCaught = 0; if (currentStage.itemsToEnchant.isEmpty() == false) { - for (Entry e : currentStage.itemsToEnchant.entrySet()) { + for (Entry, Integer> e : currentStage.itemsToEnchant.entrySet()) { Map map = (Map) e.getKey(); itemsEnchanted.put(map, 0); @@ -1700,11 +1702,11 @@ public class Quester { LinkedList itemIds = new LinkedList(); LinkedList enchAmounts = new LinkedList(); - for (Entry e : itemsEnchanted.entrySet()) { + for (Entry, Integer> e : itemsEnchanted.entrySet()) { Map enchMap = (Map) e.getKey(); enchAmounts.add(itemsEnchanted.get(enchMap)); - for (Entry e2 : enchMap.entrySet()) { + for (Entry e2 : enchMap.entrySet()) { enchantments.add(Quester.prettyEnchantmentString((Enchantment) e2.getKey())); itemIds.add(((Material) e2.getValue()).getId()); @@ -1847,7 +1849,7 @@ public class Quester { LinkedList potionIds = new LinkedList(); LinkedList potionAmounts = new LinkedList(); - for (Entry entry : potionsBrewed.entrySet()) { + for (Entry entry : potionsBrewed.entrySet()) { potionIds.add((Integer) entry.getKey()); potionAmounts.add((Integer) entry.getValue()); diff --git a/src/main/java/me/blackvein/quests/prompts/ItemStackPrompt.java b/src/main/java/me/blackvein/quests/prompts/ItemStackPrompt.java index f54ca8f09..8fc5d77f9 100644 --- a/src/main/java/me/blackvein/quests/prompts/ItemStackPrompt.java +++ b/src/main/java/me/blackvein/quests/prompts/ItemStackPrompt.java @@ -6,11 +6,14 @@ import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Map.Entry; -import me.blackvein.quests.util.ColorUtil; + +import javax.xml.crypto.Data; + import me.blackvein.quests.ItemData; -import me.blackvein.quests.ItemData.Data; import me.blackvein.quests.Quester; import me.blackvein.quests.Quests; +import me.blackvein.quests.util.ColorUtil; + import org.bukkit.ChatColor; import org.bukkit.Material; import org.bukkit.conversations.ConversationContext; @@ -225,38 +228,38 @@ public class ItemStackPrompt extends FixedSetPrompt implements ColorUtil{ @Override public Prompt acceptInput(ConversationContext cc, String input) { - if(input.equalsIgnoreCase("cancel") == false){ + if(input.equalsIgnoreCase("cancel") == false){ - try{ - Material mat = Material.getMaterial(Integer.parseInt(input)); - if(mat == null){ - cc.getForWhom().sendRawMessage(RED + "Invalid item ID!"); - return new IDPrompt(); - }else{ - cc.setSessionData("tempId", Integer.parseInt(input)); - return new ItemStackPrompt(oldPrompt); - } + String dataString = null; + if (input.contains(":")) { + String[] splitInput = input.split(":"); + input = splitInput[0]; + if (splitInput.length > 1) { + dataString = splitInput[1]; + } + } - }catch(NumberFormatException e){ - try { - Data data = ItemData.getInstance().getItem(input); - if (data == null) { - cc.getForWhom().sendRawMessage(RED + "Invalid item ID!"); - return new IDPrompt(); - } else { + Material mat = ItemData.getMaterial(input); + if(mat == null){ + cc.getForWhom().sendRawMessage(RED + "Invalid item ID!"); + return new IDPrompt(); + } else { - cc.setSessionData("tempId", data.getId()); - cc.setSessionData("tempData", (data.getData() == 0) ? null : (short)data.getData()); - return new ItemStackPrompt(oldPrompt); - } - } catch (Exception e1) { - e1.printStackTrace(); - cc.getForWhom().sendRawMessage(RED + "Invalid item ID!"); - return new IDPrompt(); - } - } + cc.setSessionData("tempId", mat.getId()); - }else{ + if (dataString != null) { + try { + short data = Short.parseShort(dataString); + cc.setSessionData("tempData", data); + } catch (NumberFormatException e) { + cc.getForWhom().sendRawMessage(RED + "Invalid item data!"); + return new IDPrompt(); + } + } + return new ItemStackPrompt(oldPrompt); + } + + }else{ return new ItemStackPrompt(oldPrompt);