Added ore dictionary.

This commit is contained in:
Eloraam 2011-10-18 18:56:10 +00:00
parent a64a47b24c
commit c12b2c5b11
2 changed files with 156 additions and 1 deletions

View File

@ -0,0 +1,24 @@
/**
* This software is provided under the terms of the Minecraft Forge Public
* License v1.0.
*/
package net.minecraft.src.forge;
import net.minecraft.src.ItemStack;
/** The current list of known classes.
* oreTin, oreCopper, oreSilver
* ingotTin, ingotCopper, ingotSilver
* dyeBlue
* gemRuby, gemEmerald, gemSapphire
* woodRubber
*/
public interface IOreHandler {
/** Called when a new ore is registered with the ore dictionary.
* @param oreClass The string class of the ore.
* @param ore The ItemStack for the ore.
*/
public void registerOre(String oreClass, ItemStack ore);
}

View File

@ -15,7 +15,6 @@ import java.util.*;
public class MinecraftForge {
private static LinkedList<IBucketHandler> bucketHandlers = new LinkedList<IBucketHandler>();
/**
* Register a new custom bucket handler.
*/
@ -66,6 +65,138 @@ public class MinecraftForge {
return null;
}
// Ore Dictionary
// ------------------------------------------------------------
private static LinkedList<IOreHandler> oreHandlers = new LinkedList<IOreHandler>();
private static TreeMap<String,List<ItemStack>> oreDict=
new TreeMap<String,List<ItemStack>>();
/** Register a new ore handler. This will automatically call the handler
* with all current ores during registration, and every time a new ore is
* added later.
*/
public static void registerOreHandler(IOreHandler handler) {
oreHandlers.add(handler);
for(String key : oreDict.keySet()) {
List<ItemStack> val=oreDict.get(key);
for(ItemStack ist : val) {
handler.registerOre(key,ist);
}
}
}
/** Register a new item with the ore dictionary.
* @param oreClass The string class of the ore.
* @param ore The ItemStack for the ore.
*/
public static void registerOre(String oreClass, ItemStack ore) {
List<ItemStack> orelist=oreDict.get(oreClass);
if(orelist==null) {
orelist=new ArrayList<ItemStack>();
oreDict.put(oreClass,orelist);
}
orelist.add(ore);
for(IOreHandler ioh : oreHandlers) {
ioh.registerOre(oreClass,ore);
}
}
/** Get the list of ores in a given class.
*/
public static List<ItemStack> getOreClass(String oreClass) {
return oreDict.get(oreClass);
}
public static class OreQuery implements Iterable<Object[]> {
Object[] proto;
public class OreQueryIterator implements Iterator<Object[]> {
LinkedList itering;
LinkedList output;
private OreQueryIterator() {
itering=new LinkedList();
output=new LinkedList();
for(int i=0; i<proto.length; i++) {
if(proto[i] instanceof Collection) {
Iterator it=((Collection)proto[i])
.iterator();
if(!it.hasNext()) {
output=null; break;
}
itering.addLast(it);
output.addLast(it.next());
} else {
itering.addLast(proto[i]);
output.addLast(proto[i]);
}
}
}
public boolean hasNext() {
return output!=null;
}
public Object[] next() {
Object[] tr=output.toArray();
Object to;
while(true) {
if(itering.size()==0) {
output=null;
return tr;
}
to=itering.getLast();
output.removeLast();
if(to instanceof Iterator) {
Iterator it=(Iterator)to;
if(it.hasNext()) {
output.addLast(it.next());
break;
}
}
itering.removeLast();
}
for(int i=itering.size(); i<proto.length; i++) {
if(proto[i] instanceof Collection) {
Iterator it=((Collection)proto[i])
.iterator();
if(!it.hasNext()) {
output=null; break;
}
itering.addLast(it);
output.addLast(it.next());
} else {
itering.addLast(proto[i]);
output.addLast(proto[i]);
}
}
return tr;
}
public void remove() {}
}
private OreQuery(Object[] pattern) {
proto=pattern;
}
public Iterator<Object[]> iterator() {
return new OreQueryIterator();
}
}
/** Generate all valid legal recipe combinations. Any Lists in pattern
* will be fully expanded to all valid combinations.
*/
public static OreQuery generateRecipes(Object... pattern) {
return new OreQuery(pattern);
}
// ------------------------------------------------------------
/** Register a new plant to be planted when bonemeal is used on grass.
* @param bid The block ID to plant.
* @param md The metadata to plant.