Hi Jimmy,
I am having trouble with creating a command that would spawn a block that I created and that I called "zero". What I want to do is to enable the player to write the command "sample 0" and that would generate a block zero at the position where he is looking. I used the event handling function that you gave in Hour 13. Please find below my code for both the handler class and the command. And I would like to thank you in advance for any help you would provide me to make this code correct, because I run the command and it doesn't do anything. I am sorry for the fact that the codes are too long, but I couldn't make them any shorter.
Best,
Sara.
------------------------------------------------------------------------------------------------------------------------------------
Event handler code:
package com.sara.myMod;
import java.util.Random;
import net.minecraft.block.Block;
import net.minecraft.block.state.IBlockState;
import net.minecraft.block.state.pattern.BlockHelper;
import net.minecraft.init.Blocks;
import net.minecraft.util.BlockPos;
import net.minecraft.world.World;
import net.minecraft.world.chunk.IChunkProvider;
import net.minecraft.world.gen.feature.WorldGenMinable;
import net.minecraftforge.fml.common.IWorldGenerator;
public class SaraEventHandler implements IWorldGenerator {
@Override
public void generate(Random random, int chunkX, int chunkZ, World world, IChunkProvider chunkGenerator, IChunkProvider chunkProvider) {
switch (world.provider.getDimensionId()){
case -1:
generateNether(world,random,chunkX * 16, chunkZ *16);
case 0:
generateSurface(world,random,chunkX * 16, chunkZ * 16);
case 1:
generateEnd(world,random,chunkX * 16, chunkZ * 16);
}
}
private void generateNether(World world, Random random, int i, int j) {
}
void generateSurface(World world, Random random, int x, int z) {
addOreSpawn(MyMod.zero.getDefaultState(),Blocks.dirt, world, random,x,z, 16, 16, 5 + random.nextInt(5),4, 20, 60);
}
private void generateEnd(World world, Random random, int x, int z) {
addOreSpawn(MyMod.zero.getDefaultState(),Blocks.netherrack, world, random,x,z, 16, 16, 5 + random.nextInt(5),4, 20, 60);
}
public void addOreSpawn(IBlockState block, Block target, World world, Random random, int blockXPos, int blockZPos, int maxX, int maxZ, int maxVeinSize, int chancesToSpawn, int minY, int maxY){
assert maxY > minY: "The maximum Y must be greater than the Minimum Y";
assert maxX > 0 && maxX <= 16: "addOreSpawn:The Maximum X must be greater than 0 and less than 16";
assert minY > 0: "addOreSpawn:The Minimum Y must be greater than 0";
assert maxY < 256 && maxY >0: "addOreSpawn:The Maximum Y must be less than 256 but greater than 0";
assert maxZ>0 && maxZ<=16: "addOreSpawn: The Maximum Z must be greater than 0 and less than 16";
int diffBtwnMinMaxY = maxY - minY ;
for(int x=0; x <chancesToSpawn; x++){
int posX = blockXPos + random.nextInt(maxX);
int posY = minY + random.nextInt(diffBtwnMinMaxY);
int posZ = blockZPos + random.nextInt(maxZ);
(new WorldGenMinable(block, maxVeinSize, BlockHelper.forBlock(target))).generate(world, random, new BlockPos(posX, posY, posZ));
}
}
}
------------------------------------------------------------------------------------------------------------------------------------
Command code
package com.sara.myMod;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import net.minecraft.command.CommandException;
import net.minecraft.command.ICommand;
import net.minecraft.command.ICommandSender;
import net.minecraft.entity.SharedMonsterAttributes;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.util.BlockPos;
import net.minecraft.util.ChatComponentText;
import net.minecraft.util.EnumChatFormatting;
import net.minecraft.world.World;
public class SampleCommand implements ICommand {
private List aliases;
public SampleCommand(){
this.aliases = new ArrayList();
this.aliases.add("spawn");
this.aliases.add("sara");
}
@Override
public int compareTo(Object o) {
// TODO Auto-generated method stub
return 0;
}
@Override
public String getName() {
return "sample";
}
@Override
public String getCommandUsage(ICommandSender sender) {
return "/sample <number>";
}
@Override
public List getAliases() {
return this.aliases;
}
@Override
public void execute(ICommandSender sender, String[] args) throws CommandException {
if(sender instanceof EntityPlayer){
EntityPlayer player = (EntityPlayer) sender;
SaraEventHandler handler = new SaraEventHandler();
//BlockZero zero = new BlockZero();
World world = player.getEntityWorld();
Random random = player.getRNG();
int x = (int) player.getLookVec().xCoord;
int y = (int) player.getLookVec().yCoord;
int z = (int) player.getLookVec().zCoord;
if(args.length ==0){
sender.addChatMessage(new ChatComponentText("Invalid arguments"));
}
else{
switch(args[0]){
case "0":
//world.makeFireworks(x, y, z, motionX, motionY, motionZ, compund)
//sender.addChatMessage(new ChatComponentText("You failed"));
//handler.generateSurface(world, random,x, z);
handler.addOreSpawn(MyMod.zero.getDefaultState(),Blocks.dirt, world, random,x,z, 16, 16, 5 + random.nextInt(5),4, 20, 60);
//handler.addOreSpawn(zero, target, world, random, blockXPos, blockZPos, maxX, maxZ, maxVeinSize, chancesToSpawn, minY, maxY)
//case "1":
// sender.addChatMessage(new ChatComponentText("You failed"));
}
}
}
}
@Override
public boolean canCommandSenderUse(ICommandSender sender) {
return true;
}
@Override
public List addTabCompletionOptions(ICommandSender sender, String[] args,
BlockPos pos) {
// TODO Auto-generated method stub
return null;
}
@Override
public boolean isUsernameIndex(String[] args, int index) {
// TODO Auto-generated method stub
return false;
}
}
------------------------------------------------------------------------------------------------------------------------------------
And in my mod file I have registered the command in the server side, using the following code:
@EventHandler
public void registerCommands(FMLServerStartingEvent event){
ServerCommandManager manager = (ServerCommandManager) event.getServer().getCommandManager();
manager.registerCommand(new CommandHealth());
manager.registerCommand(new SampleCommand());
}
------------------------------------------------------------------------------------------------------------------------------------
Looking forward to hearing from you soon.