Jump to content

[1.7.10] TileEntity Mimicing Changes on All Instances


Izzy Axel

Recommended Posts

The hell is this?

 

public class BlockLightEngine extends BlockContainer {
int light;

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

Yes (he is referring to 'light'), but local variables do not work as expected due to all Block and Item instances being singletons, i.e. effectively every local variable is static, meaning it has the same value across all of your Block classes.

 

Another thing, you should NEVER call updateEntity or any other update method, or any write to NBT methods, manually - these are all called at the appropriate times by Minecraft, and calling them manually not only makes things messy, it is apt to introduce buggy or other unwanted behavior (as well as just being less performant).

Link to comment
Share on other sites

And....what is this?

 

this.lightValue

 

$10 says that it's a class-level field of the parent Block class.  Which is still a singleton.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

Hm...then how would I make the TileEntity emit light, if it can't be done via the block?

(looking through the Block class)

Hm, what's this?

public int getLightValue(IBlockAccess world, int x, int y, int z)

Looks like you can store the light level in your TileEntity and retrieve the TileEntity's value with that lovely method from Forge ;)

Link to comment
Share on other sites

Unless I misunderstand how you're telling me to use that, the return is the light value, so I can't get the block in updateEntity in the TileEntity and call that method from it to set the value, and overriding it in the BlockContainer will have the same problem as before, due to Block being a singleton. (yeah I tried overriding it, same result)

Link to comment
Share on other sites

That's exactly what I tried:

 

@Override
public int getLightValue(IBlockAccess world, int x, int y, int z)
{
TELightEngine lightEngine = (TELightEngine)world.getTileEntity(x, y, z);
return lightEngine.getLight();
}

 

This still results in every instance of the block emitting the set light value.  I'm not sure this is possible after looking at the Redstone Lamp code, as they do it by replacing the block with one that's lit when it gets a redstone signal, and I'm not making 15 blocks just to get this to work.

Link to comment
Share on other sites

Let's not be too hard on Izzy. Despite some fundamental mistakes, his code actually looks like he could learn to be a good programmer (unlike some of the crazy code some people post that appears hopeless).

 

Izzy, you're bashing into a couple concepts here. Static, instances and singletons.

 

The first one is what a static field is. Putting static means that it is SHARED across all instances of that class. (Well actually it isn't in the instances at all, but is referenced directly through the class). Fields that are not static need to have an instance of the class to reference, and can vary between the instances.

 

An instance is created whenever you use the "new" keyword: MyClass myClassInstance = new MyClass().

 

To see the difference between accessing a static and non-static field, if MyClass has a static int myField you would reference it simply with MyClass.myField. However, if it wasn't static then you'd have to create an instance with MyClass myClassInstance = new MyClass() and then you could reference with myClassInstance.myField. See the difference?

 

Now theoretically you'd want to create an instance for everything in your program that might vary from similar things. In Minecraft you might think that you'd create a block instance for every block placed in the world. The problem is that there are so many blocks in a Minecraft world that you'd run out of memory doing it this way. Instead they only create a single instance (called a "singleton") that has the ability to change a bit by looking up a compressed map of data called metadata.

 

There are cases though where you want your blocks to do something more complicated, or store things like inventories of items. So that is where tile entities come in. But since it is expected that you won't place too many of these they allow you to create an instance for every placement.

 

Putting all this together, you should have just one custom block instance (i.e. there should only be one place through the entire mod where you use new with your class constructor) but when the method for getting the lightvalue is called you want to check the unique tile entity instance at the same position. That tile entity class shouldn't use static fields for calculating the light value, otherwise all the tile entities will report the same value.

 

Hope that helps. I feel if you can wrap your head around these three concepts -- static, singletons used for blocks and items, multiple instances of tile entities -- that you'll really have a breakthrough in your modding and programming in general.

 

 

 

The second concept is the idea of a "singleton" class.

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

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.



×
×
  • Create New...

Important Information

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