Please Read: Ok, so there are lots of tutorials out there already showing people how to add blocks, weapons, items, and mobs to your mod. There are also lots of tutorials for using ModLoader. This is not one of those tutorials. If you need to know how to do that, Simo_415 has a list of tutorials, and there are many more.
Right Click Block Actions
Let's say you wan't to make a block that creates other blocks when clicked (or right clicked). First, create the block to be clicked.
Then, type this into your BlockExample.java file (Except change "Example" to whatever your block name is):
package net.minecraft.src;
import java.util.Random;
public class BlockExample extends Block
{
public BlockExample(int i, int j)
{
super(i, j, Material.rock);
}
public int idDropped(int i, Random random)
{
return 101;
}
public boolean blockActivated(World world, int i, int j, int k, EntityPlayer entityplayer)
{
}
}
Lets go through this line by line:
public class BlockExample extends Block
{
}
This is the main class for the file. "extends Block" means that it uses the methods created in the Block.java file.
public BlockExample(int i, int j)
{
super(i, j, Material.rock);
}
This part defines how the class can be called in other files. Don't worry about this much except that Material.rock means that it can be destroyed by anything, but only returns a block/item if it is dug by a pick (or anything that can destroy stone and give back cobblestone).
public int idDropped(int i, Random random)
{
return 101;
}
This part just telling MineCraft what block to return when BlockExample is destroyed.
public int says that it returns an integer value, which happens to be 101 in this example. 101 is the block ID that is returned.
public boolean blockActivated(World world, int i, int j, int k, EntityPlayer entityplayer)
{
}
This is the part that we will focus on. It is a boolean method (returning true or false) that defines how the block will react when right-clicked.
The stuff inside the brackets is where we will put all of the following code.
world.setBlockWithNotify(i, j, k, block);
This line (which goes inside the blockActivated() class) tells MC to set a block in the MineCraft world. Don't worry about the WithNotify part, it just needs to be there for this kind of project.
Here's what it means:
world.
This stuff is going to happen inside of the world class.
setBlockWithNotify();
This is the method used to place a block.
(i,j,k,block)
This can't go in your code exactly like this. The variable "block" has to be changed to a block ID, such as the number 4 for cobblestone. The variables i, j, and k represent the x, y, and z coordinates in the 3d MineCraft world. i and k stand for x and y, and j stands for z (up and down).
Please note that the x, y, and z equivalents of i, j, and k are based on my perspective.
I and k are for the placement relative to the ground while j is the hight above the origin. I'm not quite sure which directions in game i and k represent, but I do know that it doesn't matter which side you click from, everything faces the same way. I am going to research how to fix this (as in different facings for clicks from different directions) and then I will write a tutorial on it.
Add as many of these world.setBlockWithNotify(i,j,k,block); as you want, and then close the blockActivated method with a "return true;" statement. That's it!
That's it for this tutorial. Tell me things you want for future tutorials and I'll try to make a tutorial to match it! Also, tell me if there are any spelling errors or other errors in the tutorial above. Check out my mods to see this tutorial in action.
Redstone Functions
Note: this tutorial is untested.
I am just going to throw some stuff out on the table so I don't forget about it. This tutorial will eventually provide the methods for making a new redstone powered/powering device. This device will have the form of a flat, pressure pad block and will represent a logic gate. This gate will take two inputs and then do a Boolean operation and output a result by redstone wire.
The first step is setting up the block, which I hope you can all do. Let's call it BlockAndGate.java. Fill in the required info in the public class BlockAndGate and move out of the brackets. The class should extend Block. You might want to set the material as "circuit" (material.circuit). There are three functions that go into a redstone device: input, processing, and output. But before we get into that...
Add this to your class:
public boolean isOpaqueCube()
{
return false;
}
public boolean renderAsNormalBlock()
{
return false;
}
public int getRenderType()
{
return 5;
}
The above code renders the block like a redstone wire. I might edit this tomorrow as there might be a better render type.
So now that it is rendered all pretty like, let's move on.
The first thing we want to determine is whether we want the block to be a power source, like redstone torches, levers, pressure plates, buttons etc. For the kind of project this is, we want it to be able to process inputs and provide power depending on those inputs.
Insert this code:
public boolean canProvidePower()
{
return gateOn; //my error...I originally said false. Also, this variable is defined later in the //tutorial, so read about it there
}
This is pretty self-explanatory.
Next, in what situations do we want this block to be able to provide power to neighboring blocks? We want it like a logic gate so provide power if and only if both inputs are true (and).
public boolean isPoweringTo(IBlockAccess iblockaccess, int i, int j, int k, int l)
{
return true/false;
}
Don't worry too much about this yet. Just know we will be using the function world.getBlockId(i, j, k) to find our input and output channels. Here are some textures that work accordingly with the block we are trying to make:
- ANDoff.png
- ANDoffL.png
- ANDoffR.png
- ANDon.png
For this to work the logic must be set up like so:
The method of changing the image that is displayed is done by making four blocks, one with each texture. However, we only make one file.
Here is a line out of the Block.java file that shows how this is done:
torchRedstoneIdle = (new BlockRedstoneTorch(75, 115, false))
As you can see, the new BlockRedstoneTorch takes three inputs. Here is what that looks like in the BlockRedstoneTorch.java file:
protected BlockRedstoneTorch(int i, int j, boolean flag)
{
super(i, j);
torchActive = false;
torchActive = flag;
setTickOnLoad(true);
}
In our mod, we want there to be four options, one for each of the four pictures above.
public class BlockAndGate extends Block
{
public BlockAndGate(int i, int j, boolean input1, boolean input2, boolean output)
{
super(i,j, Material.circuits); //setting the superclass and material
gateOn = false;
i1 = false;
i2 = false; //initialize all booleans to false
gateOn = output;
i1 = input1;
i2 = input2; //set the booleans
}
//We will continue assuming that the output side is at (i-1, j, k), input1 is at (i+1, j, k-1),
// and input2 is at (i+1, j, k+1)
/*
input1 ----- input2
|
output
*/
//we make the block face us with the following code:
public void onBlockPlacedBy(World world, int i, int j, int k, EntityLiving entityliving)
{
int l = MathHelper.floor_double((double)((entityliving.rotationYaw * 4F) / 360F) + 0.5D) & 3;
if(l == 0)
{
world.setBlockMetadataWithNotify(i, j, k, 2);
}
if(l == 1)
{
world.setBlockMetadataWithNotify(i, j, k, 1);
}
if(l == 2)
{
world.setBlockMetadataWithNotify(i, j, k, 3);
}
if(l == 3)
{
world.setBlockMetadataWithNotify(i, j, k, 0);
}
}
public void onBlockAdded(World world, int i, int j, int k)
{
world.notifyBlocksOfNeighborChange(i + 1, j, k, blockID);
world.notifyBlocksOfNeighborChange(i - 1, j, k, blockID);
world.notifyBlocksOfNeighborChange(i, j, k + 1, blockID);
world.notifyBlocksOfNeighborChange(i, j, k - 1, blockID);
world.notifyBlocksOfNeighborChange(i, j - 1, k, blockID);
world.notifyBlocksOfNeighborChange(i, j + 1, k, blockID);
i1Id = world.getBlockId(i+1,j,k-1);
i2Id = world.getBlockId(i+1,j,k+1);
outputId = world.getBlockId(i-1,j,k);
} //This tells the surrounding blocks that the gate block was added.
public void onBlockRemoval(World world, int i, int j, int k)
{
world.notifyBlocksOfNeighborChange(i, j - 1, k, blockID);
world.notifyBlocksOfNeighborChange(i, j + 1, k, blockID);
world.notifyBlocksOfNeighborChange(i - 1, j, k, blockID);
world.notifyBlocksOfNeighborChange(i + 1, j, k, blockID);
world.notifyBlocksOfNeighborChange(i, j, k - 1, blockID);
world.notifyBlocksOfNeighborChange(i, j, k + 1, blockID);
}//And this the surrounding blocks that the gate block was removed.
public void updateTick(World world, int i, int j, int k, Random random)
{//the following part is going to change the color of the inputs and related things.
i1Id = 55 ? i1 = true : i1 = false; //55 is the block id for redstone. This block can only be
i2Id = 55 ? i2 = true : i2 = false; //powered by redstone wire directly.
i1&i2 ? gateOn = true : gateOn = false;
if(gateOn)
{world.setBlockWithNotify(i,j,k,mod_LogicGates.andGateOnId)}
else if(!(i1^i2))
{world.setBlockWithNotify(i,j,k,mod_LogicGates.andGateOffId)}
else if(i1)
{world.setBlockWithNotify(i,j,k,mod_LogicGates.andGate1Id)}
else
{world.setBlockWithNotify(i,j,k,mod_LogicGates.andGate2Id)}
}
public boolean isPoweringTo(IBlockAccess iblockaccess, int i, int j, int k, int l)
{//This part gets a little tricky...
//we want to set this so that only the output block receives power.
//that is, (i-1,j,k)
public boolean isPoweringTo(IBlockAccess iblockaccess, int i, int j, int k, int l)
{
if(!gateOn)
{
return false; //the gate isn't on...there shouldn't be any power going out of it...
}
int i1 = iblockaccess.getBlockMetadata(i, j, k) & 3;
if(i1 == 0 && l == 3)
{
return true;
}
if(i1 == 1 && l == 4)
{
return true;
}
if(i1 == 2 && l == 2)
{
return true;
}
return i1 == 3 && l == 5; //that is from the BlockRedstoneRepeater.java. I haven't quite
//figured this part out yet, but this will have to do for now.
}
//next we set the indirectly powered function. Because we don't want any block that is farther
//than one block away to receive power from this, we will set it up as so:
public boolean isIndirectlyPoweringTo(World world, int i, int j, int k, int l)
{
return isPoweringTo(world, i, j, k, l);
}
//almost done!
public int idDropped(int i, Random random)
{return mod_LogicGates.andGateOnId}
private int i1Id;
private int 12Id;
private int outputId;
private boolean gateOn;
private boolean i1;
private boolean i2;
}
Wow...Thats a lot of code.
But wait! There's more!
mod_LogicGates.java:
package net.minecraft.src;
public class mod_LogicGates extends BaseMod
{
public String Version()
{
return "Logic Gates Mod v0.1";
}
public static String[] ArgIDBlock() //I am using PReader.
{
String[] SubArgIDBlock = new String['A'];
try {
for (int i = 0; i < 256; i++) {
int n = i + 0;
String f = Integer.toString(n);
SubArgIDBlock[i] = f;
}
}
catch (Exception e) {
System.out.println(e);
}
return SubArgIDBlock;
}
public static String[] ArgIDBlock = ArgIDBlock();
public static int andGateOnId = PReader.PropInt(ArgIDBlock, "/mods/logicgates.properties", "And Gate On", "110");
public static int andGateOffId = PReader.PropInt(ArgIDBlock, "/mods/logicgates.properties", "And Gate Off", "111");
public static int andGate1Id = PReader.PropInt(ArgIDBlock, "/mods/logicgates.properties", "And Gate Input 1", "112");
public static int andGate2Id = PReader.PropInt(ArgIDBlock, "/mods/logicgates.properties", "And Gate Input 2", "113");
public static final Block andOn;
public static final Block andOff;
public static final Block and1;
public static final Block and2;
public mod_LogicGates()
{
ModLoader.RegisterBlock(andOn);
ModLoader.RegisterBlock(andOff);
ModLoader.RegisterBlock(and1);
ModLoader.RegisterBlock(and2);
ModLoader.AddName(andOn, "And Gate");
ModLoader.AddName(andOff, "And Gate (off)");
ModLoader.AddName(and1, "And Gate (1)");
ModLoader.AddName(and2, "And Gate (2)");
//lets add a recipe:
ModLoader.AddRecipe(new ItemStack(andOn, 1), new Object[] {
"#X#", "X#X", "X#X", Character.valueOf('#'), Block.cobblestone, Character.valueOf('X'), Item.redstone
});
}
static
{
andGateOn = (new BlockAndGate(andGateOnId,ModLoader.addOverride("/terrain.png","/gui/ANDon.png"),true,true,true)).setHardness(0.1F).setStepSound(Block.soundStoneFootstep).setBlockName("andOn");
andGateOff = (new BlockAndGate(andGateOffId,ModLoader.addOverride("/terrain.png","/gui/ANDoff.png"),false,false,false)).setHardness(0.1F).setStepSound(Block.soundStoneFootstep).setBlockName("andOff");
andGateI1 = (new BlockAndGate(andGate1Id,ModLoader.addOverride("/terrain.png","/gui/ANDoffL.png"),true,false,false)).setHardness(0.1F).setStepSound(Block.soundStoneFootstep).setBlockName("and1");
andGateI2 = (new BlockAndGate(andGate2Id,ModLoader.addOverride("/terrain.png","/gui/ANDoffR.png"),false,true,false)).setHardness(0.1F).setStepSound(Block.soundStoneFootstep).setBlockName("and2");
}//finally...
WARNING: The above tutorial is UNTESTED!
I have been having some serious trouble with mcp and modloader and I havent been able to test anything that could use modloader.
Also, don't copy/paste the stuff in the tutorial. It is just an example and I might turn it into a mod. If you want to make this mod off of my code, please pm me first and add proper credit to the post.
Thanks for the great feedback on my tutorials and I hope they are helpful.
I'm talking in terms of z as in height. That's just the way I learned it. I know that lots of 3d modelers like maya and blender use y for height though, so I will make that more clear! Thanks for the feedback.
question: is there a way to make multiple blocks spawn when you activate a block?
like say... I wanted my block to spawn lava when activated, is there a way to make it spawn like 4?
question: is there a way to make multiple blocks spawn when you activate a block?like say... I wanted my block to spawn lava when activated, is there a way to make it spawn like 4?
Of course. Just add more setBlockWithNotify after the first.
I'm just requesting a tutorial on adding blocks and items to multiplayer, so that they will work there too :smile.gif: At the moment, my mods only work on singleplayer.
Rollback Post to RevisionRollBack
I host files with DropBox I think it's easy and fast, so you should try it if you're interested
Hey, Would you be able to do a mod for creating new crafting tables of the sort? so like new craftign windows, I need to for my alchemy mod, But I am unsure of how :/
I'll try, but first I am trying to figure out how to spawn lightning. I'll work on this also though.
Thanks! This is just awesome :tongue.gif: exactly what i needed! :tongue.gif:
anyways, you could tell us how to make something active when this block is directly powered...
its a simple function too :tongue.gif:
Rollback Post to RevisionRollBack
Don't chase your dreams alone... Unless you're in single-player.
Great job we need some modding tutorials that teach people how to do more complicated thing. I was wondering could you make a tutorial on how to make modloader compatible torches?
Great job we need some modding tutorials that teach people how to do more complicated thing. I was wondering could you make a tutorial on how to make modloader compatible torches?
Do you mean torches that have different qualities like color and brightness?
By Xenone
Please Read: Ok, so there are lots of tutorials out there already showing people how to add blocks, weapons, items, and mobs to your mod. There are also lots of tutorials for using ModLoader. This is not one of those tutorials. If you need to know how to do that, Simo_415 has a list of tutorials, and there are many more.
Right Click Block Actions
Let's say you wan't to make a block that creates other blocks when clicked (or right clicked). First, create the block to be clicked.
Then, type this into your BlockExample.java file (Except change "Example" to whatever your block name is):
package net.minecraft.src;
import java.util.Random;
Lets go through this line by line:
This is the main class for the file. "extends Block" means that it uses the methods created in the Block.java file.
This part defines how the class can be called in other files. Don't worry about this much except that Material.rock means that it can be destroyed by anything, but only returns a block/item if it is dug by a pick (or anything that can destroy stone and give back cobblestone).
This part just telling MineCraft what block to return when BlockExample is destroyed.
public int says that it returns an integer value, which happens to be 101 in this example. 101 is the block ID that is returned.
This is the part that we will focus on. It is a boolean method (returning true or false) that defines how the block will react when right-clicked.
The stuff inside the brackets is where we will put all of the following code.
This line (which goes inside the blockActivated() class) tells MC to set a block in the MineCraft world. Don't worry about the WithNotify part, it just needs to be there for this kind of project.
Here's what it means:
This stuff is going to happen inside of the world class.
This is the method used to place a block.
This can't go in your code exactly like this. The variable "block" has to be changed to a block ID, such as the number 4 for cobblestone. The variables i, j, and k represent the x, y, and z coordinates in the 3d MineCraft world. i and k stand for x and y, and j stands for z (up and down).
Add as many of these world.setBlockWithNotify(i,j,k,block); as you want, and then close the blockActivated method with a "return true;" statement. That's it!
That's it for this tutorial. Tell me things you want for future tutorials and I'll try to make a tutorial to match it! Also, tell me if there are any spelling errors or other errors in the tutorial above. Check out my mods to see this tutorial in action.
-Xenone
I'm So Meta, Even This Acronym - (#917 XKCD.com)
Redstone Functions
Note: this tutorial is untested.
I am just going to throw some stuff out on the table so I don't forget about it. This tutorial will eventually provide the methods for making a new redstone powered/powering device. This device will have the form of a flat, pressure pad block and will represent a logic gate. This gate will take two inputs and then do a Boolean operation and output a result by redstone wire.
The first step is setting up the block, which I hope you can all do. Let's call it BlockAndGate.java. Fill in the required info in the public class BlockAndGate and move out of the brackets. The class should extend Block. You might want to set the material as "circuit" (material.circuit). There are three functions that go into a redstone device: input, processing, and output. But before we get into that...
Add this to your class:
The above code renders the block like a redstone wire. I might edit this tomorrow as there might be a better render type.
So now that it is rendered all pretty like, let's move on.
The first thing we want to determine is whether we want the block to be a power source, like redstone torches, levers, pressure plates, buttons etc. For the kind of project this is, we want it to be able to process inputs and provide power depending on those inputs.
Insert this code:
This is pretty self-explanatory.
Next, in what situations do we want this block to be able to provide power to neighboring blocks? We want it like a logic gate so provide power if and only if both inputs are true (and).
Don't worry too much about this yet. Just know we will be using the function world.getBlockId(i, j, k) to find our input and output channels. Here are some textures that work accordingly with the block we are trying to make:
- ANDoff.png
- ANDoffL.png
- ANDoffR.png
- ANDon.png
For this to work the logic must be set up like so:
Input A | Input B | Output
true | true | true
true | false | false
false | true | true
false | false | false
The method of changing the image that is displayed is done by making four blocks, one with each texture. However, we only make one file.
Here is a line out of the Block.java file that shows how this is done:
As you can see, the new BlockRedstoneTorch takes three inputs. Here is what that looks like in the BlockRedstoneTorch.java file:
In our mod, we want there to be four options, one for each of the four pictures above.
Wow...Thats a lot of code.
But wait! There's more!
mod_LogicGates.java:
WARNING: The above tutorial is UNTESTED!
I have been having some serious trouble with mcp and modloader and I havent been able to test anything that could use modloader.
Also, don't copy/paste the stuff in the tutorial. It is just an example and I might turn it into a mod. If you want to make this mod off of my code, please pm me first and add proper credit to the post.
Thanks for the great feedback on my tutorials and I hope they are helpful.
- Xenone
I'm So Meta, Even This Acronym - (#917 XKCD.com)
I'm So Meta, Even This Acronym - (#917 XKCD.com)
I'm So Meta, Even This Acronym - (#917 XKCD.com)
I'm So Meta, Even This Acronym - (#917 XKCD.com)
i is X coordinate
j is Y coordinate (which is height)
k is Z coordinate
True :tongue.gif:
I host files with DropBox I think it's easy and fast, so you should try it if you're interested
I'm talking in terms of z as in height. That's just the way I learned it. I know that lots of 3d modelers like maya and blender use y for height though, so I will make that more clear! Thanks for the feedback.
I'm So Meta, Even This Acronym - (#917 XKCD.com)
like say... I wanted my block to spawn lava when activated, is there a way to make it spawn like 4?
Help the little guy hatch?
Is there a way to remove a block?
And is there a way to make it where you must right click when holding an item?
http://zidmc.x10.bz/index.php
it's a forum for modders, and also players
I host files with DropBox I think it's easy and fast, so you should try it if you're interested
Of course. Just add more setBlockWithNotify after the first.
To remove blocks, replace the current coordinates of a block with air (Id 0).
For the second part, I'll work on a tutorial for it.
Lastly, kriss95, I'm not sure I understand your question. Are you asking whether a mod using this method would be multiplayer compatible?
Oh, and Saepe8, I'll work on your tutorial even though I'm not quite sure how to do it. A lightning staff would be pretty awesome though!
I'm So Meta, Even This Acronym - (#917 XKCD.com)
I host files with DropBox I think it's easy and fast, so you should try it if you're interested
I'm So Meta, Even This Acronym - (#917 XKCD.com)
I'll try, but first I am trying to figure out how to spawn lightning. I'll work on this also though.
I'm So Meta, Even This Acronym - (#917 XKCD.com)
anyways, you could tell us how to make something active when this block is directly powered...
its a simple function too :tongue.gif:
Anthrocraft http://www.minecraft...f=1021&t=231356
Green Screenhttp://www.minecraft...f=1021&t=236906
Minecraft Mojang Fix http://www.minecraft...f=1032&t=26687
Do you mean torches that have different qualities like color and brightness?
I'm So Meta, Even This Acronym - (#917 XKCD.com)