2011-03-19 22:39:51 +00:00
|
|
|
package com.earth2me.essentials;
|
|
|
|
|
|
|
|
import java.io.BufferedReader;
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileReader;
|
|
|
|
import java.io.FileWriter;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.util.*;
|
|
|
|
import java.util.logging.Logger;
|
2011-05-01 19:09:38 +00:00
|
|
|
import org.bukkit.Material;
|
2011-03-19 22:39:51 +00:00
|
|
|
import org.bukkit.inventory.ItemStack;
|
|
|
|
|
|
|
|
|
|
|
|
public class ItemDb
|
|
|
|
{
|
|
|
|
private final static Logger logger = Logger.getLogger("Minecraft");
|
|
|
|
private static Map<String, Integer> items = new HashMap<String, Integer>();
|
|
|
|
private static Map<String, Short> durabilities = new HashMap<String, Short>();
|
|
|
|
|
|
|
|
@SuppressWarnings("LoggerStringConcat")
|
|
|
|
public static void load(File folder, String fname) throws IOException
|
|
|
|
{
|
|
|
|
folder.mkdirs();
|
|
|
|
File file = new File(folder, fname);
|
|
|
|
|
|
|
|
if (!file.exists())
|
|
|
|
{
|
|
|
|
file.createNewFile();
|
|
|
|
InputStream res = ItemDb.class.getResourceAsStream("/items.csv");
|
|
|
|
FileWriter tx = new FileWriter(file);
|
|
|
|
try
|
|
|
|
{
|
|
|
|
for (int i = 0; (i = res.read()) > 0;) tx.write(i);
|
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
tx.flush();
|
|
|
|
tx.close();
|
|
|
|
res.close();
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
BufferedReader rx = new BufferedReader(new FileReader(file));
|
|
|
|
try
|
|
|
|
{
|
|
|
|
items.clear();
|
|
|
|
|
|
|
|
for (int i = 0; rx.ready(); i++)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
String line = rx.readLine().trim().toLowerCase();
|
|
|
|
if (line.startsWith("#"))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
String[] parts = line.split("[^a-z0-9]");
|
|
|
|
if (parts.length < 2)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
int numeric = Integer.parseInt(parts[1]);
|
|
|
|
|
2011-04-15 21:11:41 +00:00
|
|
|
durabilities.put(parts[0].toLowerCase(), parts.length > 2 && !parts[2].equals("0") ? Short.parseShort(parts[2]) : 0);
|
|
|
|
items.put(parts[0].toLowerCase(), numeric);
|
2011-03-19 22:39:51 +00:00
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
logger.warning("Error parsing " + fname + " on line " + i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
|
|
|
rx.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static ItemStack get(String id, int quantity) throws Exception {
|
2011-04-15 21:11:41 +00:00
|
|
|
ItemStack retval = get(id.toLowerCase());
|
2011-03-19 22:39:51 +00:00
|
|
|
retval.setAmount(quantity);
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static ItemStack get(String id) throws Exception
|
|
|
|
{
|
2011-05-07 13:03:45 +00:00
|
|
|
int itemid;
|
|
|
|
short metaData =0;
|
|
|
|
if(id.matches("^\\d+:\\d+$"))
|
|
|
|
{
|
|
|
|
itemid = getUnsafe(id.split(":")[0]);
|
|
|
|
metaData = (short)getUnsafe(id.split(":")[1]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
itemid = getUnsafe(id);
|
|
|
|
}
|
|
|
|
|
2011-05-01 19:09:38 +00:00
|
|
|
Material mat = Material.getMaterial(itemid);
|
|
|
|
if (mat == null) {
|
|
|
|
throw new Exception("Unknown item id: "+itemid);
|
|
|
|
}
|
|
|
|
ItemStack retval = new ItemStack(mat);
|
This is a big refactoring of the user class and more.
Many commands have been cleaned.
File changes:
- all user data has been moved from users.yml to userdata folder
- all files in userdata folder are lower case
Both changes should be done automatically.
Class changes:
- Moved all user data functions to UserData class
- Moved all user teleport functions to Teleport class
- Moved the user list to Essentials class
- Less static functions for better testing
- EssentialsCommand now has ess Property (Essentials class)
- New NotEnoughArgumentsException, that will show command description and syntax
New commands:
- /seen, shows the last login or logout
- /tempban, temporarily ban someone
- /tjail and mute, temporarily option added
Other changes:
- ban reason is saved
- don't show "You have xxx mail" on login, if user doesn't have essentials.mail permission
- time will be parsed: years, months (mo), weeks, days, hours, minutes (m), seconds, these can be shortened and combined, example: 2 days 5h 30m
git-svn-id: https://svn.java.net/svn/essentials~svn/trunk@1300 e251c2fe-e539-e718-e476-b85c1f46cddb
2011-05-01 21:07:30 +00:00
|
|
|
retval.setAmount(Essentials.getStatic().getSettings().getDefaultStackSize());
|
2011-05-07 13:03:45 +00:00
|
|
|
retval.setDurability(metaData !=0 ? metaData :(durabilities.containsKey(id.toLowerCase()) ? durabilities.get(id.toLowerCase()) : 0));
|
2011-05-01 19:09:38 +00:00
|
|
|
return retval;
|
2011-03-19 22:39:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private static int getUnsafe(String id) throws Exception
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
return Integer.parseInt(id);
|
|
|
|
}
|
|
|
|
catch (NumberFormatException ex)
|
|
|
|
{
|
2011-04-15 21:11:41 +00:00
|
|
|
if (items.containsKey(id.toLowerCase())) return items.get(id.toLowerCase());
|
2011-03-19 22:39:51 +00:00
|
|
|
throw new Exception("Unknown item name: " + id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|