Gold Member
![Member Member](https://2img.net/h/i654.photobucket.com/albums/uu265/hoover78/User%20ranks/member.png)
![Gold](https://2img.net/h/i654.photobucket.com/albums/uu265/hoover78/account%20for%20sale/gold.gif)
Posts : 11 Join date : 2011-05-19
![[java] Share a variable? Empty](https://2img.net/i/fa/empty.gif) | Subject: [java] Share a variable? Wed 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. |
|
SevenUp Member
![Member Member](https://2img.net/h/i654.photobucket.com/albums/uu265/hoover78/User%20ranks/member.png)
![SevenUp](https://2img.net/u/1111/11/56/92/avatars/158-87.jpg)
Posts : 2 Join date : 2011-05-11
![[java] Share a variable? Empty](https://2img.net/i/fa/empty.gif) | Subject: Re: [java] Share a variable? Wed 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. |
|
Gold Member
![Member Member](https://2img.net/h/i654.photobucket.com/albums/uu265/hoover78/User%20ranks/member.png)
![Gold](https://2img.net/h/i654.photobucket.com/albums/uu265/hoover78/account%20for%20sale/gold.gif)
Posts : 11 Join date : 2011-05-19
![[java] Share a variable? Empty](https://2img.net/i/fa/empty.gif) | Subject: Re: [java] Share a variable? Wed 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? |
|
SevenUp Member
![Member Member](https://2img.net/h/i654.photobucket.com/albums/uu265/hoover78/User%20ranks/member.png)
![SevenUp](https://2img.net/u/1111/11/56/92/avatars/158-87.jpg)
Posts : 2 Join date : 2011-05-11
![[java] Share a variable? Empty](https://2img.net/i/fa/empty.gif) | Subject: Re: [java] Share a variable? Wed 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. |
|
Gold Member
![Member Member](https://2img.net/h/i654.photobucket.com/albums/uu265/hoover78/User%20ranks/member.png)
![Gold](https://2img.net/h/i654.photobucket.com/albums/uu265/hoover78/account%20for%20sale/gold.gif)
Posts : 11 Join date : 2011-05-19
![[java] Share a variable? Empty](https://2img.net/i/fa/empty.gif) | Subject: Re: [java] Share a variable? Wed 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 |
|
Sponsored content
![[java] Share a variable? Empty](https://2img.net/i/fa/empty.gif) | Subject: Re: [java] Share a variable? ![[java] Share a variable? Empty](https://2img.net/i/empty.gif) | |
| |
|