Created
July 17, 2013 08:43
-
-
Save molenzwiebel/6018889 to your computer and use it in GitHub Desktop.
A way to sort the items known in Minecraft.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package nl.thijsmolendijk.LoopThruTest; | |
import java.lang.reflect.Field; | |
import java.lang.reflect.Method; | |
import java.lang.reflect.Modifier; | |
import java.util.ArrayList; | |
import java.util.List; | |
import org.bukkit.Material; | |
import org.bukkit.inventory.ItemStack; | |
import org.bukkit.plugin.java.JavaPlugin; | |
@SuppressWarnings("rawtypes") | |
public class Main extends JavaPlugin { | |
private static Class itemClass; | |
private static String version; | |
@Override | |
public void onEnable() { | |
try { | |
//Try to get the version currently running | |
version = this.getServer().getClass().getName().replace("org.bukkit.craftbukkit.", "").replace(".CraftServer", ""); | |
System.out.println("[LoopThru] Version detected: "+version+". Based on: "+this.getServer().getClass().getName().replace("[L", "")); | |
//Get the item class | |
itemClass = Class.forName("net.minecraft.server."+version+".Item"); | |
} catch (Exception e) { | |
System.out.println("[LoopThru] Something went wrong..."); | |
e.printStackTrace(); | |
return; | |
} | |
this.loadItems(); | |
} | |
public void loadItems() { | |
try { | |
//Loop through all static variables. (The items) | |
Field[] declaredFields = itemClass.getDeclaredFields(); | |
List<Field> staticFields = new ArrayList<Field>(); | |
for (Field field : declaredFields) { | |
if (java.lang.reflect.Modifier.isStatic(field.getModifiers())) { | |
staticFields.add(field); | |
} | |
} | |
//All normal items without a subtype or durability | |
List<ItemStack> normalItems = new ArrayList<ItemStack>(); | |
//All items that can receive damage (e.g. pickaxes) | |
List<ItemStack> damagableItems = new ArrayList<ItemStack>(); | |
//All items with subtypes (potions, golden apple) | |
List<ItemStack> subtypeItems = new ArrayList<ItemStack>(); | |
//Loop through all items found | |
for (Field itemField : staticFields) { | |
//Make sure the field is a Item or a subclass of Item | |
if (isClassOrSubclass(itemField.getType(), itemClass)) { | |
makeAccessible(itemField); | |
//The actual item | |
Object item = itemField.get(null); | |
boolean hasSubtypes = false; | |
//Obfuscated hasSubtypes field | |
Field cyField = itemClass.getDeclaredField("cy"); | |
//If it has obfuscated | |
if (makeAccessible(cyField).getBoolean(item) == true) | |
hasSubtypes = true; | |
//The item id | |
int id = itemClass.getDeclaredField("id").getInt(item); | |
ItemStack stack = new ItemStack(id); | |
//Add the item to the array | |
if (Material.matchMaterial(String.valueOf(id)).getMaxDurability() != 0) { | |
damagableItems.add(stack); | |
} else if (!hasSubtypes) { | |
normalItems.add(stack); | |
} else | |
subtypeItems.add(stack); | |
} | |
} | |
//THERE YOU HAVE IT! THE 3 ARRAYS ARE NOW FILLED WITH ITEMSTACKS. I HAVE NOT YET WRITTEN A WAY TO DETERMINE THEIR MAX ID | |
} catch (Exception e) { | |
System.out.println("[LoopThru] Something went wrong..."); | |
e.printStackTrace(); | |
return; | |
} | |
} | |
public static boolean isClassOrSubclass(Class itemClass, Class itemClazz) { | |
if (itemClass.getName().equals(itemClazz.getName())) | |
return true; | |
if (itemClass.getSuperclass().getName().equals(itemClazz.getName())) | |
return true; | |
return false; | |
} | |
public static Field makeAccessible(Field field) { | |
if (!Modifier.isPublic(field.getModifiers()) || !Modifier.isPublic(field.getDeclaringClass().getModifiers())) { | |
field.setAccessible(true); | |
} | |
return field; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment