Copyright © 2015 – Curt Hill Minecraft Blocks Properties and Methods
Introduction Blocks are basic to Minecraft The environment is constructed from blocks We have seen how to construct blocks and to give them textures We now delve into some of their properties and methods Most properties have a set method Copyright © 2015 – Curt Hill
Recall There are several things that you should recall from the first mod presentation –Making a derivation of Block –Building the constructor –Invoking the ancestral constructor with super –Setting the name –Setting the texture –Putting into a tab –Registering the block We will not redo those here Copyright © 2015 – Curt Hill
Generally The properties and methods are generally in Block We change using a set method –Sometimes there is a get method, other times not If this is a derived class we may go after properties directly Sometimes the property has a different type than the set method We may add properties and methods but do not expect the game to use them Copyright © 2015 – Curt Hill
Hardness blockHardness is a float setHardness(float) is the method Determines difficulty in breaking the block –Higher numbers indicate harder items The next screen has a partial table of hardnesses Copyright © 2015 – Curt Hill
Hardness Copyright © 2015 – Curt Hill BlockHardnessBlockHardnessBlockHardness Grass0Melon1.0Iron bars5.0 Fire0Stone1.5Anvil5.0 TNT0Wood2.0Block Iron5 Snow0.2Bricks2.0Block Coal5 Glass pane0.3Craft Table2.5Obsidian50 Ladder0.4Gold ore3.0Lava100 Ice0.5Coal Ore3.0BedrockInfinite
Reality Hardness determines how many time you must hit it depending on tool –(In reality, we have hardness and brittleness and others as well – consider glass and iron) The hardness determines the wear on tools Copyright © 2015 – Curt Hill
Resistance How does it hold up to a blast Field is float blockResistance Method is setResistance(float) Resistance is similar to hardness –The higher the number the less damage The resistance and hardness values do not have to correlate Copyright © 2015 – Curt Hill
Resistance Copyright © 2015 – Curt Hill BlockResistanceBlockResistanceBlockResistance Grass0Melon5Iron bars30 Fire0Wood10Block Coal 30 TNT0Craft Table 12.5Block Iron 30 Snow1Gold ore15Lava500 Glass pane 1.5Coal Ore15Obsidian6000 Ladder2Stone30Anvil6000 Ice2.5Bricks30Bedrock18,000,000
Sounds What sound does the block make stepSound is Block.SoundType Method: setStepSound (Block.SoundType) There are several static sounds See the next table Copyright © 2015 – Curt Hill
Static Sounds Copyright © 2015 – Curt Hill soundTypeAnvilsoundTypeMetal soundTypeClothsoundTypePiston soundTypeGlasssoundTypeSand soundTypeGrasssoundTypeSnow soundTypeGravelsoundTypeStone soundTypeLaddersoundTypeWood
Opacity How much light may pass through the block boolean opaque –No method sets this –boolean isOpaqueCube() shows int lightOpacity –setLightOpacity(int) –Also int getLightOpacity() Opaque is 16, translucent is 0 The texture must allow light through This may affect reflectivity Copyright © 2015 – Curt Hill
Light Level How much light does this emit? –Normally none int lightValue setLightLevel (float) No light is 0, full sunlight is 1.0 Notice local variable and method do not match –It appears that 0 is no light and 15 is full –Exceeding 0-1 bounds gives the rendering engine fits Copyright © 2015 – Curt Hill
Luninescent Block Copyright © 2015 – Curt Hill
Behavior Most of what we have seen so far are static properties We typically set them near to construction time and then never touch them again Next we look at dynamic things –Harvesting –Event handlers These do not typically work in creative modeg Copyright © 2015 – Curt Hill
The Harvest Harvesting is when a block is broken and yields an item This is managed by a several things: –Method setHarvestLevel –Overridden method getItemDropped –Overridden method quantityDropped Copyright © 2015 – Curt Hill
Harvest Level What tool at what level breaks the block setHarvestLevel(String, int) The string is a tool name The int determines how long it takes Any registered tool should work Copyright © 2015 – Curt Hill
Dropping an Item Once the block is broken two methods are called to drop something quantityDropped returns the integer number to make getItemDropped returns an Item handle Both of these should be prefixed Copyright © 2015 – Curt Hill
quantityDropped Signature: public int quantityDropped (Random r); We will consider Random next Use random to determine how many you return There are others as well Copyright © 2015 – Curt Hill
Random This is not part of the game, but is in java.util.Random Gives a variety of random number forms Has a variety of methods that give random values Copyright © 2015 – Curt Hill
Random Methods Has a variety of methods of form: –nextX() –Where x is a type –nextInt, nextBoolean, nextDouble, etc –These give a uniform distribution nextInt(int n) gives integer in range 0-(n-1) nextGaussian gives a normally distributed double in range 0-1 Copyright © 2015 – Curt Hill
GetItemDropped Signature: public Item getItemDropped (int meta, Random random, int fortune) Where: –meta is metadata about the item –fortune is intended to increase production The return is a registered item –An unregistered item crashes the system Copyright © 2015 – Curt Hill
The Item You may return any item you want that is registered If there is only one type of Item to return then have a static variable hold it You may use the Random variable to choose one of several items to return This is where GameRegistry.findItem can be used Copyright © 2015 – Curt Hill
Example Copyright © 2015 – Curt Hill
The Ons There are a variety of methods that start with on These are all event handlers They all need to be prefixed Typically they need to also call the super.on… method to make sure other functions continue Today we look at two –onEntityWalking –onEntityCollidedWithBlock Copyright © 2015 – Curt Hill
onEntityCollidedWithBlock This event occurs when an entity collides with this block The signature: public void onEntityCollidedWithBlock( World w, int p_1, int p_2, int p_3, Entity ent) Copyright © 2015 – Curt Hill
onEntityWalking This event occurs when an entity walks over the block The signature: public void onEntityWalking( World w, int p_1, int p_2, int p_3, Entity ent) Copyright © 2015 – Curt Hill
Some others of interest Copyright © 2015 – Curt Hill onBlockExploded (World w, int x, int y, int z, Explosion exp) onNeighborChange (IBlockAccess world, int x, int y, int z, int tileX, int tileY, int tileZ)
An Example What will follow is some code on a new block: LightBlock This uses several of these properties and methods Copyright © 2015 – Curt Hill
Light Block Beginnings Copyright © 2015 – Curt Hill public class LightBlock extends Block{ // For harvest Item drop=null; // For light output boolean bright = false;
Light Block Constructor Copyright © 2015 – Curt Hill public LightBlock(Material mat){ super(mat); setBlockName("LightBlock"); setBlockTextureName( CurtMod.MODID + ":LightBlock"); setCreativeTab( CreativeTabs.tabBlock); setHarvestLevel("pickaxe",8); setStepSound(Block.soundTypeMetal); setLightLevel(0.05f); drop = CurtModItems.curtItem; } // For harvest Item drop=null; // For light output boolean bright = false;
getItemDropped Copyright © 2015 – Curt public Item getItemDropped( int meta, Random random, int fortune){ return drop; }
Better Copyright © 2015 – Curt public Item getItemDropped(int meta, Random random, int fortune){ Item drop=null; int r = random.nextInt(3); switch(r){ case 0: drop = GameRegistry.findItem( "minecraft", "feather"); break; … case 2: drop = GameRegistry.findItem( "minecraft", "emerald"); break; } // Case end return drop; }
quantityDropped Copyright © 2015 – Curt public int quantityDropped (Random random){ // range is [1-4] int result = random.nextInt(3)+1; return result; } // quantity dropped
Collisions 1 Copyright © 2015 – Curt Hill public class public void onEntityCollidedWithBlock (World w, int p_1, int p_2, int p_3, Entity ent){ super.onEntityCollidedWithBlock (w,p_1, p_2, p_3,ent); // See next page
Collisions 2 Copyright © 2015 – Curt Hill // Flip the light emitting float light; if(bright) light = 0.05F; else light = 0.95F; bright = !bright; setLightLevel(light); }
Walked on Copyright © 2015 – Curt public void onEntityWalking( World w, int p_1, int p_2, int p_3, Entity ent){ super.onEntityWalking( w, p_1,p_2,p_3,ent); float light; if(bright) light = 0.05F; else light = 0.95F; bright = !bright; setLightLevel(light); }
More to Come Another thing that is fundamental to the play of the game is recipes –How are simpler things transformed into tools? This we must cover in another presentation Copyright © 2015 – Curt Hill