Jump to content

[1.8] Unorganized creative inventory


DonkeyCore

Recommended Posts

I'm using 1.8 Forge and when registering my items/blocks, it appears crazily in the creative inventory. I know it sorts by item IDs, but I don't know how to register the blocks/items in the way I want so the IDs are sorted correctly.

Here is a picture of how it looks now:

1xbQ7I0.png

 

My main mod class [Fusion.java]:

package donkeycore.fusion;

import java.io.File;
import java.io.FileInputStream;
import java.lang.reflect.Field;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.jar.JarEntry;
import java.util.jar.JarInputStream;

import net.minecraft.block.Block;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.item.Item;
import net.minecraft.item.ItemArmor;
import net.minecraft.item.ItemArmor.ArmorMaterial;
import net.minecraft.item.ItemPickaxe;
import net.minecraft.util.ChatComponentText;
import net.minecraft.util.DamageSource;
import net.minecraft.util.EnumChatFormatting;
import net.minecraft.world.World;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.common.config.Configuration;
import net.minecraftforge.common.util.EnumHelper;
import net.minecraftforge.event.ForgeEventFactory;
import net.minecraftforge.event.world.BlockEvent;
import net.minecraftforge.fml.common.FMLCommonHandler;
import net.minecraftforge.fml.common.FMLLog;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.Mod.EventHandler;
import net.minecraftforge.fml.common.event.FMLInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPostInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
import net.minecraftforge.fml.common.event.FMLServerStartingEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.gameevent.PlayerEvent;
import net.minecraftforge.fml.common.registry.GameRegistry;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

import org.apache.logging.log4j.Level;

import donkeycore.fusion.blocks.DegradedFusionBlock.Stages;
import donkeycore.fusion.command.FusionCommand;

@Mod(modid = Fusion.MODID, version = Fusion.VERSION)
public final class Fusion {

@Mod.Instance("fusion")
public static Fusion instance;
public static Configuration config;
public static final String MODID = "fusion";
public static final String VERSION = "0.1";
public static final String BUILD = "beta_01";
public static final Random r = new Random();
public static Side SIDE;
private static boolean initpre = false, init = false, initpost = false;
public static final List<IFusionObject> fusionObjects = new ArrayList<IFusionObject>();
public static final DamageSource sourceRadiation = new DamageSource("fusion.radiation");
public static final DamageSource sourceStrongRadiation = new DamageSource("fusion.strongradiation");
public static final ArmorMaterial fusionArmorMaterial = EnumHelper.addArmorMaterial("fusionArmorMaterial", MODID + ":textures/armor/fusion", 30, new int[]{3, 8, 6, 3}, 15);
public static final CreativeTabs tabFusion = new CreativeTabs("tabFusion") {

	@Override
	@SideOnly(Side.CLIENT)
	public Item getTabIconItem() {
		return getItemFromId("fusion_ingot");
	}
};

{
	instance = this;
}

@EventHandler
public void init(FMLPreInitializationEvent event) {
	if (initpre)
		throw new IllegalStateException("Already preinitialized!");
	initpre = true;
	SIDE = event.getSide();
	info("Reading configuration...");
	config = new Configuration(event.getSuggestedConfigurationFile());
	Config.sync();
	info("Registering blocks/items...");
	try {
		String[] packages = new String[]{"blocks", "items.armor", "items.materials", "items.tools"};
		boolean jar = Fusion.class.getResource("Fusion.class").toString().startsWith("jar:");
		for(int i = 0; i < packages.length; i++) {
			List<Class<?>> cl = new ArrayList<Class<?>>();
			if (jar)
				cl = getClassesInJar("donkeycore.fusion." + packages[i]);
			else
				cl = getClasses("donkeycore.fusion." + packages[i]);
			if(cl.size() < 1)
				bigWarning("There was a problem registering Fusion blocks/items!");
			for(Class<?> c : cl) {
				if (ItemArmor.class.isAssignableFrom(c)) {
					boolean active = c.getSimpleName().contains("Active");
					String[] types = new String[]{"helmet", "chestplate", "leggings", "boots"};
					int x = 0;
					for(String type : types)
						fusionObjects.add((IFusionObject) c.getConstructor(String.class, int.class).newInstance((active ? "active_" : "") + "fusion_" + type, x++));
				} else if(Item.class.isAssignableFrom(c) || Block.class.isAssignableFrom(c))
					fusionObjects.add((IFusionObject) c.newInstance());
				else
					warning("Unknown class: " + c.getSimpleName());
			}
		}
		Collections.sort(fusionObjects);
	} catch(Throwable t) {
		bigWarning("UNABLE TO REGISTER ITEMS!!");
		t.printStackTrace();
	}
	for(IFusionObject f : fusionObjects) {
		f.preinit();
	}
}

@EventHandler
public void init(FMLInitializationEvent event) {
	if (init)
		throw new IllegalStateException("Already initialized!");
	init = true;
	info("Initializing objects...");
	MinecraftForge.EVENT_BUS.register(this);
	FMLCommonHandler.instance().bus().register(this);
	GameRegistry.registerWorldGenerator(new OreGenerator(), 1);
	for(IFusionObject f : fusionObjects)
		f.init();
}

@EventHandler
public void init(FMLPostInitializationEvent event) {
	if (initpost)
		throw new IllegalStateException("Already postinitialized!");
	initpost = true;
	info("Fusion mod v" + VERSION + " enabled!");
}

@EventHandler
public void init(FMLServerStartingEvent event) {
	event.registerServerCommand(new FusionCommand());
}

public static Item getItemFromId(String id) {
	return (Item) getFromId(id, Item.class);
}

public static ItemArmor getArmorFromId(String id) {
	return (ItemArmor) getFromId(id, ItemArmor.class);
}

public static Block getBlockFromId(String id) {
	return (Block) getFromId(id, Block.class);
}

public static Object getFromId(String id, Class<?> type) {
	for(IFusionObject o : fusionObjects) {
		try {
			Class<?> c = o.getClass();
			if(type.isAssignableFrom(c)) {
				Field f = c.getDeclaredField("name");
				f.setAccessible(true);
				String s = (String) f.get(o);
				if(s == null)
					s = (String) f.get(null);
				if(id.equals(s))
					return o;
			}
		} catch(Throwable t) {
			t.printStackTrace();
			return null;
		}
	}
	return null;
}

@SubscribeEvent
public void onJoin(PlayerEvent.PlayerLoggedInEvent event) {
	if (!event.player.worldObj.isRemote && Config.loginMessage)
		event.player.addChatComponentMessage(new ChatComponentText(EnumChatFormatting.DARK_GREEN + "[Fusion v" + VERSION + "] " + EnumChatFormatting.GREEN + "This mod is in beta, so please report bugs!"));
}

@SubscribeEvent
public void onBreak(BlockEvent.BreakEvent event) {
	if (event.getPlayer() == null || event.isCanceled())
		return;
	if (Stages.has(event.state)) {
		if (event.state.getBlock().getUnlocalizedName().equals(getBlockFromId("degraded_fusion_block").getUnlocalizedName()) && Stages.from(event.state) != Stages.STABLE)
			explode(event);
	}
	if (event.state.getBlock().getUnlocalizedName().equals(getBlockFromId("fusion_ore").getUnlocalizedName()) && event.getPlayer().inventory.getCurrentItem() == null) {
		explode(event);
		return;
	}
	if (event.getPlayer().inventory.getCurrentItem() == null)
		return;
	Item i = event.getPlayer().inventory.getCurrentItem().getItem();
	if (event.state.getBlock().getUnlocalizedName().equals(getBlockFromId("fusion_ore").getUnlocalizedName()) && ((!(i instanceof ItemPickaxe)) || (i instanceof ItemPickaxe && ((ItemPickaxe) i).getToolMaterial().getHarvestLevel() < 2))) {
		explode(event);
		return;
	}
}

private static final List<Class<?>> getClassesInJar(String packageName) {
	ArrayList<Class<?>> classes = new ArrayList<Class<?>>();
	packageName = packageName.replaceAll("\\.", "/");
	try {
		String jarName = new File(Fusion.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath()).getAbsolutePath();
		JarInputStream jarFile = new JarInputStream(new FileInputStream(jarName));
		JarEntry jarEntry;
		while(true) {
			jarEntry = jarFile.getNextJarEntry();
			if (jarEntry == null)
				break;
			if ((jarEntry.getName().startsWith(packageName)) && (jarEntry.getName().endsWith(".class")))
				classes.add(Class.forName(jarEntry.getName().replaceAll("/", "\\.")));
		}
		jarFile.close();
	} catch(Throwable t) {
		t.printStackTrace();
	}
	return classes;
}

public static final List<Class<?>> getClasses(String scannedPackage) {
	String scannedPath = scannedPackage.replace(".", "/");
	URL scannedUrl = Thread.currentThread().getContextClassLoader().getResource(scannedPath);
	if (scannedUrl == null)
		throw new IllegalArgumentException(String.format("Unable to get resources from path '%s'.", scannedPath));
	File scannedDir = new File(scannedUrl.getFile());
	List<Class<?>> classes = new ArrayList<Class<?>>();
	for(File file : scannedDir.listFiles())
		classes.addAll(getClasses(file, scannedPackage));
	return classes;
}

private static final List<Class<?>> getClasses(File file, String scannedPackage) {
	List<Class<?>> classes = new ArrayList<Class<?>>();
	String resource = scannedPackage + "." + file.getName();
	if (file.isDirectory()) {
		for(File child : file.listFiles())
			classes.addAll(getClasses(child, resource));
	} else if (resource.endsWith(".class")) {
		int endIndex = resource.length() - ".class".length();
		String className = resource.substring(0, endIndex);
		if (!className.contains("$")) {
			try {
				classes.add(Class.forName(className));
			} catch(ClassNotFoundException ignore) {}
		}
	}
	return classes;
}

private static final FusionExplosion explode(BlockEvent.BreakEvent event) {
	event.setCanceled(true);
	event.world.setBlockToAir(event.pos);
	return createFusionExplosion(event.world, event.pos.getX(), event.pos.getY(), event.pos.getZ(), 1.5F, false, false);
}

public static void info(String format, Object... data) {
	FMLLog.info("[Fusion] " + format, data);
}

public static void warning(String format, Object... data) {
	FMLLog.warning("[Fusion] " + format, data);
}

public static void severe(String format, Object... data) {
	FMLLog.severe("[Fusion] " + format, data);
}

public static void bigWarning(String format, Object... data) {
	FMLLog.bigWarning("[Fusion] " + format, data);
}

public static void info(Level level, String format, Object... data) {
	FMLLog.log(level, "[Fusion] " + format, data);
}

public static final FusionExplosion createFusionExplosion(World w, double x, double y, double z, float size, boolean fire, boolean smoke) {
	FusionExplosion explosion = new FusionExplosion(w, null, x, y, z, size, fire, smoke);
	if (ForgeEventFactory.onExplosionStart(w, explosion))
		return explosion;
	explosion.doExplosionA();
	explosion.doExplosionB(true);
	return explosion;
}
}

 

And my base item template [FusionItem.java]:

package donkeycore.fusion.items;

import net.minecraft.client.Minecraft;
import net.minecraft.client.resources.model.ModelResourceLocation;
import net.minecraft.item.Item;
import net.minecraft.item.ItemArmor;
import net.minecraft.item.ItemTool;
import net.minecraftforge.fml.common.registry.GameRegistry;
import net.minecraftforge.fml.relauncher.Side;
import donkeycore.fusion.Fusion;
import donkeycore.fusion.IFusionObject;

public abstract class FusionItem extends Item implements IFusionObject {

{
	setMaxStackSize(64);
	setCreativeTab(Fusion.tabFusion);
}

private final String name;

public FusionItem(String name) {
	this.name = name;
	setUnlocalizedName(name);
}

public void preinit() {
	GameRegistry.registerItem(this, name);
}

public void init() {
	if(Fusion.SIDE == Side.CLIENT)
		Minecraft.getMinecraft().getRenderItem().getItemModelMesher().register(this, 0, new ModelResourceLocation(Fusion.MODID + ":" + name, "inventory"));
}

public static final void init(Item i, String name) {
	if(Fusion.SIDE == Side.CLIENT)
		Minecraft.getMinecraft().getRenderItem().getItemModelMesher().register(i, 0, new ModelResourceLocation(Fusion.MODID + ":" + name, "inventory"));
}

@Override
public int compareTo(IFusionObject o) {
	return o instanceof ItemArmor || o instanceof ItemTool ? -1 : 1;
}

}

 

Any ideas of why the items aren't ordering correctly in the inventory? Thanks in advance :)

~DonkeyCore

Link to comment
Share on other sites

	try {
		String[] packages = new String[]{"blocks", "items.armor", "items.materials", "items.tools"};
		boolean jar = Fusion.class.getResource("Fusion.class").toString().startsWith("jar:");
		for(int i = 0; i < packages.length; i++) {
			List<Class<?>> cl = new ArrayList<Class<?>>();
			if (jar)
				cl = getClassesInJar("donkeycore.fusion." + packages[i]);
			else
				cl = getClasses("donkeycore.fusion." + packages[i]);
			if(cl.size() < 1)
				bigWarning("There was a problem registering Fusion blocks/items!");
			for(Class<?> c : cl) {
				if (ItemArmor.class.isAssignableFrom(c)) {
					boolean active = c.getSimpleName().contains("Active");
					String[] types = new String[]{"helmet", "chestplate", "leggings", "boots"};
					int x = 0;
					for(String type : types)
						fusionObjects.add((IFusionObject) c.getConstructor(String.class, int.class).newInstance((active ? "active_" : "") + "fusion_" + type, x++));
				} else if(Item.class.isAssignableFrom(c) || Block.class.isAssignableFrom(c))
					fusionObjects.add((IFusionObject) c.newInstance());
				else
					warning("Unknown class: " + c.getSimpleName());
			}
		}
		Collections.sort(fusionObjects);
	} catch(Throwable t) {
		bigWarning("UNABLE TO REGISTER ITEMS!!");
		t.printStackTrace();
	}
	for(IFusionObject f : fusionObjects) {
		f.preinit();
	}

 

I realise that this is making it so you won't have to do GameRegistry.registerItem and GameRegistry.registerBlock every single time you add a new item/block. However the items and blocks appears in the creative tab in the order they were registered, which is why yours is so unorganized.

Link to comment
Share on other sites

Just create a seperate method and call it in the order you want the items to show in the creative inventory.

If I ever say something stupid, or simply incorrect, please excuse me. I don't know anything about 1.8 modding, and I don't know much about entities either, But I try to help when I can.

Link to comment
Share on other sites

This is not true. Items appear in the order of IDs, which can be random.

Here is a tutorial on how to make them sorted: http://www.minecraftforge.net/forum/index.php/topic,23782.0.html

 

(also, that Item registering code is a joke, right? Please tell me it is a joke...)

 

Really? I thought the IDs were created in the order of how you specified it via GameRegistry, the only reason I can see them beginning to get mixed up is if you already had a world created and a mod update added a new item say in the middle of the list, it would be at the end of the creative tab because it didn't exist until later.

 

AjOr4AQ.png

1fVuCsI.png

tLuugjg.png

KIlwbO0.png

Link to comment
Share on other sites

Thank you all for helping! diesieben07 had the answer I needed, but you guys are all awesome for helping :D

 

This is not true. Items appear in the order of IDs, which can be random.

Here is a tutorial on how to make them sorted: http://www.minecraftforge.net/forum/index.php/topic,23782.0.html

 

(also, that Item registering code is a joke, right? Please tell me it is a joke...)

 

And yeah, the item registering code is temporary just so I could experiment with the concept... I'm not really big on reflection and I don't do it much, what was a major problem with the way I did it?

~DonkeyCore

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.