copyright © 2015 – curt hill minecraft blocks properties and methods
TRANSCRIPT
![Page 1: Copyright © 2015 – Curt Hill Minecraft Blocks Properties and Methods](https://reader035.vdocuments.us/reader035/viewer/2022081312/56649e715503460f94b6ed62/html5/thumbnails/1.jpg)
Copyright © 2015 – Curt Hill
Minecraft Blocks
Properties and Methods
![Page 2: Copyright © 2015 – Curt Hill Minecraft Blocks Properties and Methods](https://reader035.vdocuments.us/reader035/viewer/2022081312/56649e715503460f94b6ed62/html5/thumbnails/2.jpg)
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
![Page 3: Copyright © 2015 – Curt Hill Minecraft Blocks Properties and Methods](https://reader035.vdocuments.us/reader035/viewer/2022081312/56649e715503460f94b6ed62/html5/thumbnails/3.jpg)
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 hereCopyright © 2015 – Curt Hill
![Page 4: Copyright © 2015 – Curt Hill Minecraft Blocks Properties and Methods](https://reader035.vdocuments.us/reader035/viewer/2022081312/56649e715503460f94b6ed62/html5/thumbnails/4.jpg)
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
![Page 5: Copyright © 2015 – Curt Hill Minecraft Blocks Properties and Methods](https://reader035.vdocuments.us/reader035/viewer/2022081312/56649e715503460f94b6ed62/html5/thumbnails/5.jpg)
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
![Page 6: Copyright © 2015 – Curt Hill Minecraft Blocks Properties and Methods](https://reader035.vdocuments.us/reader035/viewer/2022081312/56649e715503460f94b6ed62/html5/thumbnails/6.jpg)
Hardness
Copyright © 2015 – Curt Hill
Block Hardness Block Hardness Block Hardness
Grass 0 Melon 1.0 Iron bars 5.0
Fire 0 Stone 1.5 Anvil 5.0
TNT 0 Wood 2.0 Block Iron 5
Snow 0.2 Bricks 2.0 Block Coal 5
Glass pane
0.3 Craft Table
2.5 Obsidian 50
Ladder 0.4 Gold ore 3.0 Lava 100
Ice 0.5 Coal Ore 3.0 Bedrock Infinite
![Page 7: Copyright © 2015 – Curt Hill Minecraft Blocks Properties and Methods](https://reader035.vdocuments.us/reader035/viewer/2022081312/56649e715503460f94b6ed62/html5/thumbnails/7.jpg)
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
![Page 8: Copyright © 2015 – Curt Hill Minecraft Blocks Properties and Methods](https://reader035.vdocuments.us/reader035/viewer/2022081312/56649e715503460f94b6ed62/html5/thumbnails/8.jpg)
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
![Page 9: Copyright © 2015 – Curt Hill Minecraft Blocks Properties and Methods](https://reader035.vdocuments.us/reader035/viewer/2022081312/56649e715503460f94b6ed62/html5/thumbnails/9.jpg)
Resistance
Copyright © 2015 – Curt Hill
Block Resistance
Block Resistance Block Resistance
Grass 0 Melon 5 Iron bars 30
Fire 0 Wood 10 Block Coal
30
TNT 0 Craft Table
12.5 Block Iron
30
Snow 1 Gold ore 15 Lava 500
Glass pane
1.5 Coal Ore 15 Obsidian 6000
Ladder 2 Stone 30 Anvil 6000
Ice 2.5 Bricks 30 Bedrock 18,000,000
![Page 10: Copyright © 2015 – Curt Hill Minecraft Blocks Properties and Methods](https://reader035.vdocuments.us/reader035/viewer/2022081312/56649e715503460f94b6ed62/html5/thumbnails/10.jpg)
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
![Page 11: Copyright © 2015 – Curt Hill Minecraft Blocks Properties and Methods](https://reader035.vdocuments.us/reader035/viewer/2022081312/56649e715503460f94b6ed62/html5/thumbnails/11.jpg)
Static Sounds
Copyright © 2015 – Curt Hill
soundTypeAnvil soundTypeMetal
soundTypeCloth soundTypePiston
soundTypeGlass soundTypeSandsoundTypeGrass
soundTypeSnow
soundTypeGravel
soundTypeStone
soundTypeLadder
soundTypeWood
![Page 12: Copyright © 2015 – Curt Hill Minecraft Blocks Properties and Methods](https://reader035.vdocuments.us/reader035/viewer/2022081312/56649e715503460f94b6ed62/html5/thumbnails/12.jpg)
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
![Page 13: Copyright © 2015 – Curt Hill Minecraft Blocks Properties and Methods](https://reader035.vdocuments.us/reader035/viewer/2022081312/56649e715503460f94b6ed62/html5/thumbnails/13.jpg)
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 fitsCopyright © 2015 – Curt Hill
![Page 14: Copyright © 2015 – Curt Hill Minecraft Blocks Properties and Methods](https://reader035.vdocuments.us/reader035/viewer/2022081312/56649e715503460f94b6ed62/html5/thumbnails/14.jpg)
Luninescent Block
Copyright © 2015 – Curt Hill
![Page 15: Copyright © 2015 – Curt Hill Minecraft Blocks Properties and Methods](https://reader035.vdocuments.us/reader035/viewer/2022081312/56649e715503460f94b6ed62/html5/thumbnails/15.jpg)
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
![Page 16: Copyright © 2015 – Curt Hill Minecraft Blocks Properties and Methods](https://reader035.vdocuments.us/reader035/viewer/2022081312/56649e715503460f94b6ed62/html5/thumbnails/16.jpg)
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
![Page 17: Copyright © 2015 – Curt Hill Minecraft Blocks Properties and Methods](https://reader035.vdocuments.us/reader035/viewer/2022081312/56649e715503460f94b6ed62/html5/thumbnails/17.jpg)
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
![Page 18: Copyright © 2015 – Curt Hill Minecraft Blocks Properties and Methods](https://reader035.vdocuments.us/reader035/viewer/2022081312/56649e715503460f94b6ed62/html5/thumbnails/18.jpg)
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 by @Override
Copyright © 2015 – Curt Hill
![Page 19: Copyright © 2015 – Curt Hill Minecraft Blocks Properties and Methods](https://reader035.vdocuments.us/reader035/viewer/2022081312/56649e715503460f94b6ed62/html5/thumbnails/19.jpg)
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
![Page 20: Copyright © 2015 – Curt Hill Minecraft Blocks Properties and Methods](https://reader035.vdocuments.us/reader035/viewer/2022081312/56649e715503460f94b6ed62/html5/thumbnails/20.jpg)
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
![Page 21: Copyright © 2015 – Curt Hill Minecraft Blocks Properties and Methods](https://reader035.vdocuments.us/reader035/viewer/2022081312/56649e715503460f94b6ed62/html5/thumbnails/21.jpg)
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
![Page 22: Copyright © 2015 – Curt Hill Minecraft Blocks Properties and Methods](https://reader035.vdocuments.us/reader035/viewer/2022081312/56649e715503460f94b6ed62/html5/thumbnails/22.jpg)
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
systemCopyright © 2015 – Curt Hill
![Page 23: Copyright © 2015 – Curt Hill Minecraft Blocks Properties and Methods](https://reader035.vdocuments.us/reader035/viewer/2022081312/56649e715503460f94b6ed62/html5/thumbnails/23.jpg)
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
![Page 24: Copyright © 2015 – Curt Hill Minecraft Blocks Properties and Methods](https://reader035.vdocuments.us/reader035/viewer/2022081312/56649e715503460f94b6ed62/html5/thumbnails/24.jpg)
Example
Copyright © 2015 – Curt Hill
![Page 25: Copyright © 2015 – Curt Hill Minecraft Blocks Properties and Methods](https://reader035.vdocuments.us/reader035/viewer/2022081312/56649e715503460f94b6ed62/html5/thumbnails/25.jpg)
The Ons• There are a variety of methods
that start with on• These are all event handlers• They all need to be prefixed by
@Override• 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
![Page 26: Copyright © 2015 – Curt Hill Minecraft Blocks Properties and Methods](https://reader035.vdocuments.us/reader035/viewer/2022081312/56649e715503460f94b6ed62/html5/thumbnails/26.jpg)
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
![Page 27: Copyright © 2015 – Curt Hill Minecraft Blocks Properties and Methods](https://reader035.vdocuments.us/reader035/viewer/2022081312/56649e715503460f94b6ed62/html5/thumbnails/27.jpg)
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
![Page 28: Copyright © 2015 – Curt Hill Minecraft Blocks Properties and Methods](https://reader035.vdocuments.us/reader035/viewer/2022081312/56649e715503460f94b6ed62/html5/thumbnails/28.jpg)
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)
![Page 29: Copyright © 2015 – Curt Hill Minecraft Blocks Properties and Methods](https://reader035.vdocuments.us/reader035/viewer/2022081312/56649e715503460f94b6ed62/html5/thumbnails/29.jpg)
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
![Page 30: Copyright © 2015 – Curt Hill Minecraft Blocks Properties and Methods](https://reader035.vdocuments.us/reader035/viewer/2022081312/56649e715503460f94b6ed62/html5/thumbnails/30.jpg)
Light Block Beginnings
Copyright © 2015 – Curt Hill
public class LightBlock extends Block{
// For harvestItem drop=null;// For light output boolean bright = false;
![Page 31: Copyright © 2015 – Curt Hill Minecraft Blocks Properties and Methods](https://reader035.vdocuments.us/reader035/viewer/2022081312/56649e715503460f94b6ed62/html5/thumbnails/31.jpg)
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 harvestItem drop=null;// For light output boolean bright = false;
![Page 32: Copyright © 2015 – Curt Hill Minecraft Blocks Properties and Methods](https://reader035.vdocuments.us/reader035/viewer/2022081312/56649e715503460f94b6ed62/html5/thumbnails/32.jpg)
getItemDropped
Copyright © 2015 – Curt Hill
@Overridepublic Item getItemDropped( int meta, Random random, int fortune){ return drop;}
![Page 33: Copyright © 2015 – Curt Hill Minecraft Blocks Properties and Methods](https://reader035.vdocuments.us/reader035/viewer/2022081312/56649e715503460f94b6ed62/html5/thumbnails/33.jpg)
Better
Copyright © 2015 – Curt Hill
@Overridepublic 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;}
![Page 34: Copyright © 2015 – Curt Hill Minecraft Blocks Properties and Methods](https://reader035.vdocuments.us/reader035/viewer/2022081312/56649e715503460f94b6ed62/html5/thumbnails/34.jpg)
quantityDropped
Copyright © 2015 – Curt Hill
@Overridepublic int quantityDropped (Random random){ // range is [1-4] int result = random.nextInt(3)+1; return result; } // quantity dropped
![Page 35: Copyright © 2015 – Curt Hill Minecraft Blocks Properties and Methods](https://reader035.vdocuments.us/reader035/viewer/2022081312/56649e715503460f94b6ed62/html5/thumbnails/35.jpg)
Collisions 1
Copyright © 2015 – Curt Hill
public class LightBlock@Overridepublic 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
![Page 36: Copyright © 2015 – Curt Hill Minecraft Blocks Properties and Methods](https://reader035.vdocuments.us/reader035/viewer/2022081312/56649e715503460f94b6ed62/html5/thumbnails/36.jpg)
Collisions 2
Copyright © 2015 – Curt Hill
// Flip the light emittingfloat light;if(bright) light = 0.05F;else light = 0.95F;bright = !bright;setLightLevel(light);}
![Page 37: Copyright © 2015 – Curt Hill Minecraft Blocks Properties and Methods](https://reader035.vdocuments.us/reader035/viewer/2022081312/56649e715503460f94b6ed62/html5/thumbnails/37.jpg)
Walked on
Copyright © 2015 – Curt Hill
@Overridepublic 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);}
![Page 38: Copyright © 2015 – Curt Hill Minecraft Blocks Properties and Methods](https://reader035.vdocuments.us/reader035/viewer/2022081312/56649e715503460f94b6ed62/html5/thumbnails/38.jpg)
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