I have been writing a Minecraft replica for OpenGL practice (as many do I guess), however after writing the basic rendering API I noticed the real Minecraft uses a lot or memory - around 800MB! I can fully understand why this is with all the blocks it has to remember along with mobs and probably terrain data for the generator...I asked myself "This block is exactly the same as that block..can they be in the code?" and remembered C++ has pointers, so I have attempted to do the same in Java the only way I could think of, creating one static instance of each block and not using the new
keyword, is this the best way? It definitely seems to help..I would still like it to be better if that is possible?
Here is the class in question..
public abstract class Block {
public static DirtBlock Dirt = new DirtBlock();
public static GrassBlock Grass = new GrassBlock();
public static RedstoneOreBlock RedstoneOre = new RedstoneOreBlock();
public static TNTBlock TNT = new TNTBlock();
public static MonsterSpawnerBlock Monserspawner = new MonsterSpawnerBlock();
public static BedrockBlock Bedrock = new BedrockBlock();
public static StoneBlock Stone = new StoneBlock();
public static GlassBlock Glass = new GlassBlock();
public static SandBlock Sand = new SandBlock();
public static WaterBlock Water = new WaterBlock();
public static SnowBlock Snow = new SnowBlock();
public static SnowGrassBlock SnowyGrass = new SnowGrassBlock();
public static IceBlock Ice = new IceBlock();
public static CoalBlock Coal = new CoalBlock();
Current memory usage is about 200MB for a 100 chunk world with each chunk made up of 16 blocks wide by 64 high and 16 deep, 1,638,400 blocks total - about 128 bytes per block.