Minecraft Blocks Properties and Methods Copyright 2015 Curt
Minecraft Blocks Properties and Methods Copyright © 2015 – Curt Hill
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 • block. Hardness is a float • set. Hardness(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 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 Copyright © 2015 – Curt Hill
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 block. Resistance Method is set. Resistance(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 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 Copyright © 2015 – Curt Hill
Sounds • What sound does the block make • step. Sound is Block. Sound. Type • Method: set. Step. Sound (Block. Sound. Type) • There are several static sounds • See the next table Copyright © 2015 – Curt Hill
Static Sounds sound. Type. Anvil sound. Type. Metal sound. Type. Cloth sound. Type. Piston sound. Type. Glass sound. Type. Sand sound. Type. Grass sound. Type. Snow sound. Type. Gravel sound. Type. Stone sound. Type. Ladder sound. Type. Wood Copyright © 2015 – Curt Hill
Opacity • How much light may pass through the block • boolean opaque – No method sets this – boolean is. Opaque. Cube() shows • int light. Opacity – set. Light. Opacity(int) – Also int get. Light. Opacity() • 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 light. Value set. Light. Level (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 set. Harvest. Level – Overridden method get. Item. Dropped – Overridden method quantity. Dropped Copyright © 2015 – Curt Hill
Harvest Level • • • What tool at what level breaks the block set. Harvest. Level(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 • quantity. Dropped returns the integer number to make • get. Item. Dropped returns an Item handle • Both of these should be prefixed by @Override Copyright © 2015 – Curt Hill
quantity. Dropped • Signature: public int quantity. Dropped (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: – next. X() – Where x is a type – next. Int, next. Boolean, next. Double, etc – These give a uniform distribution • next. Int(int n) gives integer in range 0 -(n 1) • next. Gaussian gives a normally distributed double in range 0 -1 Copyright © 2015 – Curt Hill
Get. Item. Dropped • Signature: public Item get. Item. Dropped (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 Game. Registry. find. Item can be used Copyright © 2015 – Curt Hill
Example Copyright © 2015 – Curt Hill
The Ons • There 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 – on. Entity. Walking – on. Entity. Collided. With. Block Copyright © 2015 – Curt Hill
on. Entity. Collided. With. Block • This event occurs when an entity collides with this block • The signature: public void on. Entity. Collided. With. Block( World w, int p_1, int p_2, int p_3, Entity ent) Copyright © 2015 – Curt Hill
on. Entity. Walking • This event occurs when an entity walks over the block • The signature: public void on. Entity. Walking( World w, int p_1, int p_2, int p_3, Entity ent) Copyright © 2015 – Curt Hill
Some others of interest • on. Block. Exploded (World w, int x, int y, int z, Explosion exp) • on. Neighbor. Change (IBlock. Access world, int x, int y, int z, int tile. X, int tile. Y, int tile. Z) Copyright © 2015 – Curt Hill
An Example • What will follow is some code on a new block: Light. Block • This uses several of these properties and methods Copyright © 2015 – Curt Hill
Light Block Beginnings public class Light. Block extends Block{ // For harvest Item drop=null; // For light output boolean bright = false; Copyright © 2015 – Curt Hill
Light Block Constructor public Light. Block(Material mat){ super(mat); set. Block. Name("Light. Block"); set. Block. Texture. Name( Curt. Mod. MODID + ": Light. Block"); set. Creative. Tab( Creative. Tabs. tab. Block); set. Harvest. Level("pickaxe", 8); set. Step. Sound(Block. sound. Type. Metal); set. Light. Level(0. 05 f); drop = Curt. Mod. Items. curt. Item; } Copyright © 2015 – Curt Hill
get. Item. Dropped @Override public Item get. Item. Dropped( int meta, Random random, int fortune){ return drop; } Copyright © 2015 – Curt Hill
Better @Override public Item get. Item. Dropped(int meta, Random random, int fortune){ Item drop=null; int r = random. next. Int(3); switch(r){ case 0: drop = Game. Registry. find. Item( "minecraft", "feather"); break; … case 2: drop = Game. Registry. find. Item( "minecraft", "emerald"); break; } // Case end return drop; Copyright © 2015 – Curt Hill }
quantity. Dropped @Override public int quantity. Dropped (Random random){ // range is [1 -4] int result = random. next. Int(3)+1; return result; } // quantity dropped Copyright © 2015 – Curt Hill
Collisions 1 public class Light. Block @Override public void on. Entity. Collided. With. Block (World w, int p_1, int p_2, int p_3, Entity ent){ super. on. Entity. Collided. With. Block (w, p_1, p_2, p_3, ent); // See next page Copyright © 2015 – Curt Hill
Collisions 2 // Flip the light emitting float light; if(bright) light = 0. 05 F; else light = 0. 95 F; bright = !bright; set. Light. Level(light); } Copyright © 2015 – Curt Hill
Walked on @Override public void on. Entity. Walking( World w, int p_1, int p_2, int p_3, Entity ent){ super. on. Entity. Walking( w, p_1, p_2, p_3, ent); float light; if(bright) light = 0. 05 F; else light = 0. 95 F; bright = !bright; set. Light. Level(light); } Copyright © 2015 – Curt Hill
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
- Slides: 38