Easy A
Would you like to react to this message? Create an account in a few clicks or log in to continue.


 
HomeHome  Latest imagesLatest images  SearchSearch  RegisterRegister  Log inLog in  

Share
 

 [java] Share a variable?

View previous topic View next topic Go down 
AuthorMessage
Gold
Member
Member
Gold

Posts : 11
Join date : 2011-05-19

[java] Share a variable? Empty
PostSubject: [java] Share a variable?   [java] Share a variable? EmptyWed Jun 29, 2011 6:15 am

Hello, I was referred here to get some help with my java code. I am really new to java and I would search for an answer elsewhere but I'm not sure of the technical terms to use. So I'm going to ask here and hope to not get in trouble because someone could have asked this before.

I need to take a variable from one java file and use it in another. Here's the code I am working with:
itemDye.java:
This is a simple file that I cannot edit. its a class that tests a variable and then acts upon the returned value.
Code:

public class itemDye extends Item
{

** *public ItemDye(int i)
** *{
** * * *super(i);
** * * *setHasSubtypes(true);
** * * *setMaxDamage(0);
** *}

** *public boolean onItemUse(ItemStack itemstack, EntityPlayer entityplayer, World world, int i, int j, int k, int l)
** *{
** * * *if(itemstack.getItemDamage() == 15)
** * * *{
** * * * * *int i1 = world.getBlockId(i, j, k);
** * * * * *if(i1 == Block.crops.blockID)
** * * * * *{
** * * * * * * *if(!world.multiplayerWorld)
** * * * * * * *{
** * * * * * * * * *((BlockCrops)Block.crops).fertilize(world, i, j, k);
** * * * * * * * * *itemstack.stackSize--;
** * * * * * * *}
** * * * * * * *return true;
** * * * * *}
                return false;
        }
    }
}

In the file that I can edit, I need to test i1 from the onItemUse from itemDye.java. Then I need to duplicate the next 9 lines but with different values. Remember I can't edit itemDye.java or this would be very easy.
Back to top Go down
SevenUp
Member
Member
SevenUp

Posts : 2
Join date : 2011-05-11

[java] Share a variable? Empty
PostSubject: Re: [java] Share a variable?   [java] Share a variable? EmptyWed Jun 29, 2011 6:16 am

I don't understand what you are trying to duplicate here.

You cannot access a local method variable, even in an extended class. Once the method is complete, the variable is discarded as its a part of the stack memory.
Back to top Go down
Gold
Member
Member
Gold

Posts : 11
Join date : 2011-05-19

[java] Share a variable? Empty
PostSubject: Re: [java] Share a variable?   [java] Share a variable? EmptyWed Jun 29, 2011 6:19 am

Ok like I said I'm new. I'll try to re-explain.

I want to turn this:
Code:

public class itemDye extends Item
{

      public ItemDye(int i)
      {
            super(i);
            setHasSubtypes(true);
            setMaxDamage(0);
      }

      public boolean onItemUse(ItemStack itemstack, EntityPlayer entityplayer, World world, int i, int j, int k, int l)
      {
            if(itemstack.getItemDamage() == 15)
      {
                int i1 = world.getBlockId(i, j, k);
                if(i1 == Block.crops.blockID)
                {
                      if(!world.multiplayerWorld)
                      {
                            ((BlockCrops)Block.crops).fertilize(world, i, j, k);
                            itemstack.stackSize--;
                      }
                      return true;
                }
                return false;
        }
    }
}
Into this:
Code:

public class itemDye extends Item
{

      public ItemDye(int i)
      {
            super(i);
            setHasSubtypes(true);
            setMaxDamage(0);
      }

      public boolean onItemUse(ItemStack itemstack, EntityPlayer entityplayer, World world, int i, int j, int k, int l)
      {
            if(itemstack.getItemDamage() == 15)
      {
                int i1 = world.getBlockId(i, j, k);
                if(i1 == Block.crops.blockID)
                {
                      if(!world.multiplayerWorld)
                      {
                            ((BlockCrops)Block.crops).fertilize(world, i, j, k);
                            itemstack.stackSize--;
                      }
                      return true;
                }
                if(i1 == Block.crops2.blockID)
                {
                      if(!world.multiplayerWorld)
                      {
                            ((BlockCrops2)Block.crops2).fertilize(world, i, j, k);
                            itemstack.stackSize--;
                      }
                      return true;
                }
                return false;
        }
    }
}
But I need to do that from another file. I can't edit the original. So what can I do to get that code into another file?
Back to top Go down
SevenUp
Member
Member
SevenUp

Posts : 2
Join date : 2011-05-11

[java] Share a variable? Empty
PostSubject: Re: [java] Share a variable?   [java] Share a variable? EmptyWed Jun 29, 2011 6:20 am

You can't, not like that.
What you can do is create another extended class of itemDye and override the onItemUse method for it and just paste what you want (or better, call the parent method and if it returns false THEN try your new code). But that assumes that you have control of the construction of the object as well in order to direct it to use your new item versus your old item. A flat system would be a simple change of the new call, while something more extensive could use a filter to in order to decorate it as its new datatype.
Back to top Go down
Gold
Member
Member
Gold

Posts : 11
Join date : 2011-05-19

[java] Share a variable? Empty
PostSubject: Re: [java] Share a variable?   [java] Share a variable? EmptyWed Jun 29, 2011 6:21 am

I tried that but I got some strange errors. I think they have to do with mcp (program used to compile minecraft), not java.


Thanks for your help
Back to top Go down
Sponsored content




[java] Share a variable? Empty
PostSubject: Re: [java] Share a variable?   [java] Share a variable? Empty

Back to top Go down
 

[java] Share a variable?

View previous topic View next topic Back to top 
Page 1 of 1

 Similar topics

-
» PHP variable issue with database
» New to Ruby from Java
» Java and Databases
» Cfdiv Binding Variable with # sign Issue
» Ruby Java Bridge

Permissions in this forum:You cannot reply to topics in this forum
Easy A :: [java] Share a variable? Edit-trash Useless :: Trash-