Jump to content

[1.7.2]Add items if config says true


Hackbaellchen

Recommended Posts

Hey,

I want to make a flag in the config like this:

 

flag1 = configFile.get("Misc", "if false: deactivats the item", true).getBoolean(flag1);

 

and then add the item like this:

 

    public void addItems() {

    	if(flag1) {
    		final Item MyItem = new MyItem().setUnlocalizedName("MyItem").setTextureName("");
            GameRegistry.registerItem(MyItem, "MyItem");
    	}
    }

But it can't be public and I can't reach it from other classes...

 

Thanks for help!

Link to comment
Share on other sites

Where have you declared your flag? Try making it like that:

Configloader

public class YourConfig{
public YourConfig(){
}
public static void initConfig(File f){
	c = new Configuration(f);
	try{
		c.load();
		addItems = c.get("Misc", "Add items?", true).getBoolean(true);
		FMLCommonHandler.instance().getFMLLogger().log(Level.INFO, "[Yourmod] Done loading the config");
	}catch(final Exception e){
		FMLCommonHandler.instance().getFMLLogger().log(Level.ERROR, "[Yourmod] Error occured while loading the config");
	}finally{
		if(c.hasChanged()){
			c.save();
		}
	}
}
public static Configuration c;
public static boolean addItems;
}

Place where you register items:

final Item MyItem = new MyItem().setUnlocalizedName("MyItem").setTextureName("");
if(YourConfig.addItems){
   GameRegistry.registerItem(MyItem, "MyItem");
}

If i helped you, don't forget pressing "Thank You" button. Thanks for your time.

Link to comment
Share on other sites

I might misunderstand you, but your problem is that you are defining and initializing the variable MyItem in the function.

And than trying to access MyItem from some other mod related class but you can't because it doesn't see it?

 

If that is so, your mistake was putting definition of the MyItem inside function.

All that is inside function, stays inside.

You have to define the variable outside the function boundary to make it visible to other classes.

 

If you are afraid that if flag1 is false and item dosen't get created, and other classes that utilize it fail to find it and error crash on you.

Just init it with null first and than when time comes just check

 if (AwesomeMod.MyItem != null) {/*item exists.*/}

~I was here~

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.