Skip to content
Browse files

Bug fixing and new item syntax and parser based on a regular expression.

  • Loading branch information...
1 parent d960ab4 commit 97035a3e3d8f249d33df785aeadbf14d08adcda8 @ricochet1k ricochet1k committed Feb 2, 2011
View
124 src/com/echo28/bukkit/craftdispenser/Items.java
@@ -1,20 +1,27 @@
package com.echo28.bukkit.craftdispenser;
import java.util.HashMap;
+import java.util.List;
import java.util.Map;
+import java.util.logging.Logger;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory;
+import org.bukkit.material.MaterialData;
/**
*
* @author Nijiko
*/
public class Items {
- public static iProperty Items = new iProperty("items.db");
+ public static Logger log = Logger.getLogger("Items");
+
+ public static iProperty ItemsiProp = new iProperty("items.db");
public static HashMap<String, String> items;
public Items() { }
@@ -28,7 +35,7 @@ public static void setupItems() {
items = new HashMap<String, String>();
try {
- mappedItems = Items.returnMap();
+ mappedItems = ItemsiProp.returnMap();
} catch (Exception ex) {
System.out.println("[CraftDispenser Flatfile] could not open items.db!");
ex.printStackTrace();
@@ -45,21 +52,21 @@ public static void setupItems() {
//log.info("matches");
if(right.contains(",")) {
String[] synonyms = right.split(",");
- itemName = synonyms[0].replaceAll("\\s","");
- items.put(id, itemName);
- //log.info("Added " + id + "=" + itemName);
+ itemName = synonyms[0].toLowerCase().replaceAll("\\s+", "");
+ items.put(itemName, id);
+ //log.info("Added " + itemName + "=" + id);
for(int i = 1; i < synonyms.length; i++) {
- itemName = synonyms[i].replaceAll("\\s","");
+ itemName = synonyms[i].toLowerCase().replaceAll("\\s+", "");
items.put(itemName, id);
//log.info("Added " + itemName + "=" + id);
}
} else {
- itemName = right.replaceAll("\\s","");
- items.put(id, itemName);
- //log.info("Added " + id + "=" + itemName);
+ itemName = right.toLowerCase().replaceAll("\\s+", "");
+ items.put(itemName, id);
+ //log.info("Added " + itemName + "=" + id);
}
} else {
- itemName = left.replaceAll("\\s","");
+ itemName = left.toLowerCase().replaceAll("\\s+", "");
id = right.trim();
items.put(itemName, id);
//log.info("Added " + itemName + "=" + id);
@@ -101,7 +108,7 @@ public static String name(int id, int data) {
*/
public static void setName(String id, String name) {
items.put(id, name);
- Items.setString(id, name);
+ ItemsiProp.setString(id, name);
}
/**
@@ -211,15 +218,92 @@ public static void remove(Player player, int item, int amount) {
}
}
}
+
+ public static ItemStack createItemStack(int[] item) {
+ return new ItemStack(item[0], item[3], (short)item[2], item[1] == -1? null : new Byte((byte)item[1]));
+ }
+ public static ItemStack[] createItemStacks(int[][] items) {
+ ItemStack[] itemstacks = new ItemStack[items.length];
+
+ for (int i = 0; i < items.length; i++)
+ itemstacks[i] = createItemStack(items[i]);
+
+ return itemstacks;
+ }
+
+ public static int[][] parseItemList(List<String> itemsList)
+ {
+ int[][] items = new int[itemsList.size()][];
+
+ for (int i = 0; i < itemsList.size(); i++)
+ items[i] = parseItem(itemsList.get(i));
+
+ return items;
+ }
+
+ /*public static int[] parseItem(String item) {
+ int numItems = 1;
+ int damage = -1;
+ if (item.indexOf(':') != -1) {
+ String[] parts = item.split(":");
+ item = parts[0];
+ numItems = Integer.parseInt(parts[1]);
+ if (parts.length > 2)
+ damage = Integer.parseInt(parts[2]);
+ }
+ int[] itemID = validate(item);
+ return new int[] {itemID[0], itemID[1], numItems, damage};
+ }*/
+
+ public static boolean itemMatchesStack(int[] item, ItemStack itemstack) {
+ if (itemstack.getTypeId() == item[0] && itemstack.getAmount() >= item[3]) {
+ MaterialData matdat = itemstack.getData();
+ if (matdat == null || matdat.getData() == item[1])
+ return true;
+ }
+ return false;
+ }
/**
* Validate the string for an item
*
- * @param item
- * @return -1 if false, id if true.
+ * @param itemStr
+ * @return {id, data, damage, amount}
*/
- public static int[] validate(String item) {
- int[] ret = new int[]{-1, 0};
+ private static Pattern itemPattern = Pattern.compile("([a-zA-Z ]+|\\d+)(?:\\s*\\.\\s*(\\d+))?(?:\\s*,\\s*(\\d+))?(?:\\s*\\*\\s*(\\d+))?");
+ public static int[] parseItem(String itemStr) {
+ Matcher m = itemPattern.matcher(itemStr);
+
+ int[] ret = new int[] {-1, -1, 0, 1};
+
+ if (m.matches()) {
+ String type = m.group(1);
+ try {
+ ret[0] = Integer.parseInt(type);
+ } catch (NumberFormatException e) {
+ type = type.toLowerCase().replaceAll("\\s+", "");
+ String id = items.get(type);
+ try {
+ ret[0] = Integer.parseInt(id);
+ } catch (NumberFormatException e2) {
+ System.out.printf("Fail '%s' '%s'\n", type, id);
+ }
+ }
+ for (int i = 1; i < 4; i++) {
+ try {
+ ret[i] = Integer.parseInt(m.group(i+1));
+ } catch (NumberFormatException e) {
+ // do nothing
+ }
+ }
+ //System.out.printf("Matched '%s': {%d, %d, %d, %d}\n", itemStr, ret[0], ret[1], ret[2], ret[3]);
+ } else {
+ System.out.printf("Unknown item '%s'\n", itemStr);
+ }
+
+ return ret;
+ }
+ /*int[] ret = new int[]{-1, -1};
item = item.replaceAll("\\s+", "");
@@ -244,21 +328,21 @@ public static void remove(Player player, int item, int amount) {
try {
ret[0] = Integer.valueOf(split[0]);
} catch(NumberFormatException e2) {
- System.out.printf("1: %s, %s\n", val, split[0]);
+ //System.out.printf("1: %s, %s\n", val, split[0]);
ret[0] = -1;
}
try {
ret[1] = Integer.valueOf(split[1]);
} catch(NumberFormatException e2) {
- System.out.printf("2: %s, %s\n", val, split[0]);
+ //System.out.printf("2: %s, %s\n", val, split[0]);
ret[1] = 0;
}
} else {
try {
ret[0] = Integer.valueOf(val);
} catch(NumberFormatException e2) {
- System.out.printf("3: '%s' '%s'\n", val, item);
- e2.printStackTrace();
+ //System.out.printf("3: '%s' '%s'\n", val, item);
+ //e2.printStackTrace();
ret[0] = -1;
}
}
@@ -274,7 +358,7 @@ public static void remove(Player player, int item, int amount) {
} else {
return ret;
}
- }
+ }*/
/**
* Validate the string for an item
View
37 src/com/echo28/bukkit/craftdispenser/crafts/Config.java
@@ -5,7 +5,6 @@
import java.util.List;
import org.bukkit.block.Block;
-import org.bukkit.inventory.ItemStack;
import org.bukkit.util.config.Configuration;
import com.echo28.bukkit.craftdispenser.CraftDispenser;
@@ -43,39 +42,17 @@ public boolean make()
{
if (checkConfig(config))
{
- String itemStr = config.getString("craft.item", "Missing crafted-item");
- int[] item = Items.validate(itemStr);
- if (item[0] == -1) {
- System.out.printf("[CraftDispenser] Invalid crafted-item: %s", itemStr);
- return false;
- }
- int amount = config.getInt("craft.amount", 1);
- short damage = (short)config.getInt("craft.damage", 0);
- Byte data = null;
- String dataStr = config.getString("craft.data", "null");
- if (dataStr != null && !dataStr.toLowerCase().equals("null") && !dataStr.toLowerCase().equals("none"))
- data = new Byte((byte)Integer.parseInt(dataStr));
-
- //System.out.printf("crafting %s %d #%d %d %d\n", itemStr, item[0], amount, damage, data);
-
- ItemStack itemStack = new ItemStack(item[0], amount, damage, data);
-
- //System.out.printf("'%s'\n", itemStack);
+ int[][] items = Items.parseItemList(config.getStringList("craft", null));
- CraftDispenser.dispenseItems(block, itemStack);
+ CraftDispenser.dispenseItems(block, Items.createItemStacks(items));
- int[][] outputItems = parseItemList(config.getStringList("output-items", null));
+ int[][] outputItems = Items.parseItemList(config.getStringList("output-items", null));
if (outputItems != null && outputItems.length == 9) {
for (int i = 0; i < 9; i++) {
- item = outputItems[i];
+ int[] item = outputItems[i];
if (item[0] != 0 && item[0] != -1) {
- if (item[3] == -1)
- data = null;
- else
- data = new Byte((byte)item[3]);
-
- inventory.setItem(i, new ItemStack(item[0], item[2], (short)0, data));
+ inventory.setItem(i, Items.createItemStack(item));
}
}
}
@@ -89,10 +66,10 @@ public boolean make()
private boolean checkConfig(Configuration config)
{
if (config.getProperty("input-items-vertical") != null) {
- return checkVerticalItems(parseItemList(config.getStringList("input-items-vertical", null)));
+ return checkVerticalItems(Items.parseItemList(config.getStringList("input-items-vertical", null)));
}
if (config.getProperty("input-items") != null) {
- return checkCustomItems(parseItemList(config.getStringList("input-items", null)));
+ return checkCustomItems(Items.parseItemList(config.getStringList("input-items", null)));
}
return false;
}
View
56 src/com/echo28/bukkit/craftdispenser/crafts/Craft.java
@@ -8,7 +8,6 @@
import org.bukkit.block.Dispenser;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
-import org.bukkit.material.MaterialData;
import com.echo28.bukkit.craftdispenser.CraftDispenser;
import com.echo28.bukkit.craftdispenser.Items;
@@ -79,22 +78,22 @@ protected boolean checkVerticalItems(int[][] items)
if (items.length == 2) {
for (int i = 0; i < 6; i++){
- if(itemMatchesStack(items[0], inventory.getItem(i)) &&
- itemMatchesStack(items[1], inventory.getItem(i+3))) {
- newSubtractItem(i, items[0][2]);
- newSubtractItem(i+1, items[1][2]);
+ if(Items.itemMatchesStack(items[0], inventory.getItem(i)) &&
+ Items.itemMatchesStack(items[1], inventory.getItem(i+3))) {
+ newSubtractItem(i, items[0][3]);
+ newSubtractItem(i+3, items[1][3]);
return true;
}
}
return false;
} else if (items.length == 3) {
for (int i = 0; i < 3; i++){
- if(itemMatchesStack(items[0], inventory.getItem(i)) &&
- itemMatchesStack(items[1], inventory.getItem(i+3)) &&
- itemMatchesStack(items[2], inventory.getItem(i+6))) {
- newSubtractItem(i, items[0][2]);
- newSubtractItem(i+3, items[1][2]);
- newSubtractItem(i+6, items[2][2]);
+ if(Items.itemMatchesStack(items[0], inventory.getItem(i)) &&
+ Items.itemMatchesStack(items[1], inventory.getItem(i+3)) &&
+ Items.itemMatchesStack(items[2], inventory.getItem(i+6))) {
+ newSubtractItem(i, items[0][3]);
+ newSubtractItem(i+3, items[1][3]);
+ newSubtractItem(i+6, items[2][3]);
return true;
}
}
@@ -114,8 +113,8 @@ protected boolean checkCustomItems(int[][] items)
if (item[0] == 0)
continue;
- if (itemMatchesStack(item, inventory.getItem(i)))
- slotsToSubtract[i] = item[2];
+ if (Items.itemMatchesStack(item, inventory.getItem(i)))
+ slotsToSubtract[i] = item[3];
else
return false;
}
@@ -184,36 +183,5 @@ public void emptyBucket(int slot)
}
}
- public int[][] parseItemList(List<String> itemsList)
- {
- int[][] items = new int[itemsList.size()][];
-
- for (int i = 0; i < itemsList.size(); i++)
- items[i] = parseItem(itemsList.get(i));
-
- return items;
- }
-
- public static int[] parseItem(String item) {
- int numItems = 1;
- int data = -1;
- if (item.indexOf(':') != -1) {
- String[] parts = item.split(":");
- item = parts[0];
- numItems = Integer.parseInt(parts[1]);
- if (parts.length > 2)
- data = Byte.parseByte(parts[2]);
- }
- int[] itemID = Items.validate(item);
- return new int[] {itemID[0], itemID[1], numItems, data};
- }
- public static boolean itemMatchesStack(int[] item, ItemStack itemstack) {
- if (itemstack.getTypeId() == item[0] && itemstack.getAmount() >= item[2]) {
- MaterialData matdat = itemstack.getData();
- if (matdat == null || matdat.getData() == item[1])
- return true;
- }
- return false;
- }
}
View
18 src/com/echo28/bukkit/craftdispenser/crafts/CraftRepair.java
@@ -54,7 +54,7 @@ public boolean make() {
if (plugin.repairStone) {
- if (makeTools(Material.STONE,
+ if (makeTools(Material.COBBLESTONE,
new Material[] {Material.STONE_PICKAXE, Material.STONE_SPADE,
Material.STONE_AXE, Material.STONE_SWORD, Material.STONE_HOE}))
return true;
@@ -93,21 +93,19 @@ private boolean makeArmors(Material type, Material[] armors) {
private boolean tools(Material type, Material tool)
{
int[][] items = {
- {type.getId(), 0, 1, -1},
- {tool.getId(), 0, 1, -1},
- {Material.STICK.getId(), 0, 1, -1}
+ {type.getId(), -1, 0, 1},
+ {tool.getId(), -1, 0, 1},
+ {Material.STICK.getId(), -1, 0, 1}
};
- if (checkVerticalItems(items)) { return true; }
- return false;
+ return checkVerticalItems(items);
}
private boolean armor(Material type, Material armor)
{
int[][] items = {
- {type.getId(), 0, 1, -1},
- {armor.getId(), 0, 1, -1}
+ {type.getId(), -1, 0, 1},
+ {armor.getId(), -1, 0, 1}
};
- if (checkVerticalItems(items)) { return true; }
- return false;
+ return checkVerticalItems(items);
}
}

0 comments on commit 97035a3

Please sign in to comment.
Something went wrong with that request. Please try again.