Commit f155e652 authored by Giant's avatar Giant
Browse files

Centralize Items in GiantCore.

parent a15c6416
......@@ -2,12 +2,18 @@ package nl.giantit.minecraft.giantcore;
import nl.giantit.minecraft.giantcore.Database.Database;
import nl.giantit.minecraft.giantcore.core.Eco.Eco;
import nl.giantit.minecraft.giantcore.core.Items.Items;
import nl.giantit.minecraft.giantcore.perms.PermHandler;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.logging.Level;
/**
*
......@@ -20,7 +26,9 @@ public class GiantCore extends JavaPlugin {
private final HashMap<PermHandler.Engines, HashMap<Boolean, PermHandler>> permHandler = new HashMap<PermHandler.Engines, HashMap<Boolean, PermHandler>>();
private final HashMap<Eco.Engines, Eco> ecoHandler = new HashMap<Eco.Engines, Eco>();
private final HashMap<Plugin, Database> dbList = new HashMap<Plugin, Database>();
private Items itemHandler;
//private Messages msgHandler;
private String dir;
private void setInstance() {
GiantCore.instance = this;
......@@ -33,6 +41,15 @@ public class GiantCore extends JavaPlugin {
@Override
public void onEnable() {
//this.permHandler = new PermHandler(this, "", true);
if(!getDataFolder().exists()) {
getDataFolder().mkdir();
getDataFolder().setWritable(true);
getDataFolder().setExecutable(true);
}
this.dir = getDataFolder().toString();
itemHandler = new Items(this);
}
@Override
......@@ -44,6 +61,10 @@ public class GiantCore extends JavaPlugin {
return this.protocolVersion;
}
public String getDir() {
return this.dir;
}
public Database getDB(Plugin p) {
if(this.dbList.containsKey(p)) {
return this.dbList.get(p);
......@@ -63,6 +84,10 @@ public class GiantCore extends JavaPlugin {
return db;
}
public Items getItemHandler() {
return this.itemHandler;
}
public PermHandler getPermHandler(PermHandler.Engines permEngine) {
return this.getPermHandler(permEngine, true);
}
......@@ -101,6 +126,73 @@ public class GiantCore extends JavaPlugin {
return eH;
}
public void extract(String file) {
this.extractDefaultFile(file);
}
public void extract(File file, String sourceFile, String resPath) {
this.extractDefaultFile(file, sourceFile, resPath);
}
public void extract(File file, InputStream input) {
this.extractDefaultFile(file, input);
}
private void extractDefaultFile(String file) {
this.extractDefaultFile(new File(getDataFolder(), file), file, "/core/Default/");
}
private void extractDefaultFile(File file, String sourceFile, String resPath) {
if (!file.exists()) {
String path = resPath + sourceFile;
InputStream input = this.getClass().getResourceAsStream("/nl/giantit/minecraft/" + getDescription().getName().toLowerCase() + path);
this.extract(file, input);
}
}
private void extractDefaultFile(File file, InputStream input) {
if (!file.exists()) {
try {
file.createNewFile();
}catch(IOException e) {
getLogger().log(Level.SEVERE, "Can't extract the requested file!!", e);
}
}
if (input != null) {
FileOutputStream output = null;
try {
output = new FileOutputStream(file);
byte[] buf = new byte[8192];
int length;
while ((length = input.read(buf)) > 0) {
output.write(buf, 0, length);
}
getLogger().log(Level.INFO, "copied default file: " + file);
output.close();
} catch (Exception e) {
getServer().getPluginManager().disablePlugin(this);
getLogger().log(Level.SEVERE, "AAAAAAH!!! Can't extract the requested file!!", e);
} finally {
try {
input.close();
} catch (Exception e) {
getServer().getPluginManager().disablePlugin(this);
getLogger().log(Level.SEVERE, "AAAAAAH!!! Severe error!!", e);
}
try {
output.close();
} catch (Exception e) {
getServer().getPluginManager().disablePlugin(this);
getLogger().log(Level.SEVERE, "AAAAAAH!!! Severe error!!", e);
}
}
}
}
public static GiantCore getInstance() {
return instance;
}
......
package nl.giantit.minecraft.giantcore.core.Items;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import org.bukkit.material.MaterialData;
/**
*
* @author Giant
*/
public class ItemID {
private int id, type;
private String name;
private boolean hasType = false;
public ItemID() {
this(0, null);
}
public ItemID(int id) {
this(id, null);
}
public ItemID(int id, Integer type) {
this(id, type, null);
}
public ItemID(int id, Integer type, String name) {
this.id = id;
if(type == null) {
this.type = 0;
this.hasType = false;
}else{
this.type = type;
this.hasType = true;
}
this.name = name;
}
public int getId() {
return id;
}
public boolean hasType() {
return this.hasType;
}
public Integer getType() {
if(hasType) return type;
return null;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
public Material getMaterial() {
return Material.getMaterial(id);
}
public ItemStack getItemStack() {
return this.getItemStack(1);
}
public ItemStack getItemStack(int amount) {
ItemStack iStack;
if(type != 0) {
if(id != 373)
iStack = new MaterialData(id, (byte) type).toItemStack(amount);
else
iStack = new ItemStack(id, amount, (short) type);
}else{
iStack = new ItemStack(id, amount);
}
return iStack;
}
@Override
public String toString() {
return "Item ID: " + this.id + " Item type: " + this.type;
}
public boolean equals(ItemID key) {
Integer t = (key.getType() == null) ? 0 : key.getType();
return (key.getId() == this.id && t == this.type);
}
}
package nl.giantit.minecraft.giantcore.core.Items;
import nl.giantit.minecraft.giantcore.GiantCore;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.Material;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import java.util.regex.Pattern;
/**
*
* @author Giant
*/
public class Items {
private final GiantCore plugin;
private YamlConfiguration config;
private final double itemVersion = 0.9;
private final HashMap<ItemID, String> itemsByID = new HashMap<ItemID, String>();
private final HashMap<String, ItemID> itemsByName = new HashMap<String, ItemID>();
private final HashMap<ItemID, List<String>> itemTypes = new HashMap<ItemID, List<String>>();
private final HashMap<String, ItemID> itemsAliases = new HashMap<String, ItemID>();
private List<Integer> getTypeList(int id) {
List<Integer> types = new ArrayList<Integer>();
for(Map.Entry<ItemID, List<String>> entry : itemTypes.entrySet()) {
ItemID key = entry.getKey();
if(key.getId() == id)
types.add(key.getType());
}
return types;
}
public Items(GiantCore plugin) {
this.plugin = plugin;
File configFile = new File(plugin.getDir(), "items.yml");
if(!configFile.exists()) {
plugin.getLogger().log(Level.INFO, "Extracting new items.yml file...");
plugin.extract("items.yml");
}
config = YamlConfiguration.loadConfiguration(configFile);
double v = config.getDouble("version");
if(v < this.itemVersion) {
plugin.getLogger().log(Level.INFO, "Your items.yml has ran out of date. Updating now!");
File oconfigFile = new File(plugin.getDir(), "items.yml." + v + ".bak");
configFile.renameTo(oconfigFile);
plugin.extract("items.yml");
config = YamlConfiguration.loadConfiguration(configFile);
}
loadNames();
loadTypes();
}
public final void loadNames() {
Set<String> list = config.getConfigurationSection("names").getKeys(false);
if(list == null) {
plugin.getLogger().log(Level.INFO, "There are no item names specified in the items.yml file?!");
return;
}
for(String item : list) {
int id;
try {
id = Integer.valueOf(item.substring(4));
} catch(NumberFormatException x) {
plugin.getLogger().log(Level.WARNING, "Invalid key detected in items.yml (names." + item + ")");
continue;
}
List<String> names = config.getStringList("names." + item);
if(names.size() > 0) {
String name = names.get(0);
String namePrep = name.toLowerCase().replaceAll(" ", "_");
ItemID iID = new ItemID(id, null);
iID.setName(name);
itemsByID.put(iID, name);
itemsByName.put(namePrep, iID);
for(int i = 1; i < names.size(); i++) {
itemsAliases.put(names.get(i).toLowerCase().replaceAll(" ", "_"), iID);
}
}else{
String name = config.getString("names." + item);
String namePrep = name.toLowerCase().replaceAll(" ", "_");
ItemID iID = new ItemID(id, null);
iID.setName(name);
itemsByID.put(iID, name);
itemsByName.put(namePrep, iID);
}
}
}
public final void loadTypes() {
Set<String> list = config.getConfigurationSection("types").getKeys(false);
if(list == null) {
plugin.getLogger().log(Level.INFO, "There are no item types specified in the items.yml file?!");
return;
}
for(String item : list) {
int itemID;
try {
itemID = Integer.valueOf(item.substring(4));
} catch(NumberFormatException x) {
plugin.getLogger().log(Level.WARNING, "Invalid key detected in items.yml (types." + item + ")");
continue;
}
Set<String> types = config.getConfigurationSection("types." + item).getKeys(false);
for(String type : types) {
int typeID;
try {
typeID = Integer.valueOf(type.substring(4));
} catch(NumberFormatException x) {
plugin.getLogger().log(Level.WARNING, "Invalid key detected in items.yml (types." + item + "." + type + ")");
continue;
}
List<String> typeNames = config.getStringList("types." + item + "." + type);
if(typeNames.size() > 0) {
ItemID iID = new ItemID(itemID, typeID);
String name = typeNames.get(0).toLowerCase().replaceAll(" ", "_");
iID.setName(typeNames.get(0));
this.itemTypes.put(iID, typeNames);
this.itemsByID.put(iID, typeNames.get(0));
this.itemsByName.put(name, iID);
for(int i = 1; i < typeNames.size(); i++) {
itemsAliases.put(typeNames.get(i).toLowerCase().replaceAll(" ", "_"), iID);
}
}else{
plugin.getLogger().log(Level.WARNING, "Given type does not have any names. (types." + item + "." + type + ")");
}
}
}
}
public ArrayList<ItemID> getItemIDsByPart(String part) {
ArrayList<ItemID> iList = new ArrayList<ItemID>();
Pattern p = Pattern.compile("^(.*?)" + part + "(.*?)$", Pattern.CASE_INSENSITIVE);
for(Map.Entry<String, ItemID> e : this.itemsByName.entrySet()) {
if(p.matcher(e.getKey()).matches()) {
// Item name matches search pattern
iList.add(e.getValue());
}
}
return iList;
}
public ItemID getItemIDByName(String item) {
String name = item.toLowerCase().replaceAll(" ", "_");
if(this.itemsByName.containsKey(name))
return this.itemsByName.get(name);
if(this.itemsAliases.containsKey(name))
return this.itemsAliases.get(name);
return null;
}
public String getItemNameByID(int itemID) {
return this.getItemNameByID(itemID, null);
}
public String getItemNameByID(int itemID, Integer itemType) {
ItemID key = new ItemID(itemID, itemType);
for(Map.Entry<ItemID, String> entry : itemsByID.entrySet()) {
ItemID item = entry.getKey();
if(item.equals(key))
return entry.getValue();
}
return null;
}
public boolean isValidItem(int itemID) {
return isValidItem(itemID, null);
}
public boolean isValidItem(int itemID, Integer itemType) {
if(itemID < 0 || (itemType != null && itemType < 0))
return false;
Material mat = Material.getMaterial(itemID);
if(mat != null) {
if(itemType == null)
return true;
List<Integer> types = this.getTypeList(itemID);
if(types.contains(itemType))
return true;
}
return false;
}
public Integer getMaxStackSize(int id) {
return Material.getMaterial(id).getMaxStackSize();
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment