I'm new to the minecraft modding community, and I have been messing around with various example mods, but I am stumped on how to properly spawn entities.
For practice, I started making a small mod that adds a /spawn <mob-name> console command to minecraft so that I could spawn mobs in a single player game. The command works fine, but any entities that I spawn are "ghosted"; they are not affected by collisions, damage, and do not exhibit any AI whatsoever.
Below is the code I am using to spawn the entities:
World world = ModLoader.getMinecraftInstance().theWorld;
Entity entity;
entity = new EntityCreeper(world);
entity.setPosition(
sender.getPlayerCoordinates().posX+2,
sender.getPlayerCoordinates().posY,
sender.getPlayerCoordinates().posZ
);
world.spawnEntityInWorld(entity);
Because the above code resides within the processCommand method of my class that inherits from CommandBase, I do not have access to a world instance like I would in other methods, so I resorted to using ModLoader.getMinecraftInstance().theWorld which I assume is the correct instance, though I am not entirely sure.
I also read that entities should ALWAYS be added on the server side, never the client side, or else the server will not get updated about the presence of the entities, and therefore they will remain "ghosted". However, I am in singleplayer, and I don't know how to add entities on the internal server.
When I wrap the entity spawning code in a conditional such as if(!world.isRemote) to ensure the entity is not spawned client side, then nothing gets spawned.
Any help would be appreciated.
Below is the full code for my mod: mod_CommandSpawn.java
package net.minecraft.src;
import net.minecraft.client.Minecraft;
public class mod_CommandSpawn extends BaseMod {
//Create an instance of our command
public static ICommand commandSpawn = new CommandSpawn();
//Any code related to setting up our mod should go in the load() method
public void load() {
//Register the command with Minecraft
ModLoader.addCommand(commandSpawn);
}
//Returns our mod name and version
public String getVersion() {
return "Spawn Command v.0.1";
}
}
CommandSpawn.java
package net.minecraft.src;
import java.util.List;
import net.minecraft.client.Minecraft;
import net.minecraft.server.MinecraftServer;
public class CommandSpawn extends CommandBase
{
public String getCommandName()
{
return "spawn";
}
public String getCommandUsage(ICommandSender par1ICommandSender)
{
return "/spawn <mob_name>";
}
public void processCommand(ICommandSender sender, String[] args)
{
World world = ModLoader.getMinecraftInstance().theWorld;
if (args.length == 1)
{
Entity entity;
if(args[0] == "creeper")
{
entity = new EntityCreeper(world);
}
else
{
entity = new EntityChicken(world);
}
entity.setPosition(
sender.getPlayerCoordinates().posX+2,
sender.getPlayerCoordinates().posY,
sender.getPlayerCoordinates().posZ
);
world.spawnEntityInWorld(entity);
}
else
{
throw new WrongUsageException("/spawn <mob_name>", new Object[0]);
}
}
}
ModLoader.getMinecraftInstance() gets an instance of the Minecraft client, use ModLoader.getMinecraftServerInstance() to get the server instead. If you do this, you will need to specify which world you want to use.
Though in this case, you should be able to derive the world the player is in from the command sender. argument.
public void processCommand(ICommandSender icommandsender, String[] astring)
{
if (icommandsender instanceof EntityPlayer) {
EntityPlayer player = (EntityPlayer)icommandsender;
World world = player.worldObj;
// ...
}
}
I'm new to the minecraft modding community, and I have been messing around with various example mods, but I am stumped on how to properly spawn entities.
For practice, I started making a small mod that adds a /spawn <mob-name> console command to minecraft so that I could spawn mobs in a single player game. The command works fine, but any entities that I spawn are "ghosted"; they are not affected by collisions, damage, and do not exhibit any AI whatsoever.
Below is the code I am using to spawn the entities:
Because the above code resides within the processCommand method of my class that inherits from CommandBase, I do not have access to a world instance like I would in other methods, so I resorted to using ModLoader.getMinecraftInstance().theWorld which I assume is the correct instance, though I am not entirely sure.
I also read that entities should ALWAYS be added on the server side, never the client side, or else the server will not get updated about the presence of the entities, and therefore they will remain "ghosted". However, I am in singleplayer, and I don't know how to add entities on the internal server.
When I wrap the entity spawning code in a conditional such as if(!world.isRemote) to ensure the entity is not spawned client side, then nothing gets spawned.
Any help would be appreciated.
Below is the full code for my mod:
mod_CommandSpawn.java
Are you sure?
Works fine for me. And Eclipse doesn't recognize a isRemote() method:
Though in this case, you should be able to derive the world the player is in from the command sender. argument.