public void addRenderer(Map var1)
{
var1.put(mcreator_knight.Entityknight.class, new RenderBiped(new ModelBiped(), 0){protected ResourceLocation getEntityTexture(Entity par1Entity){return new ResourceLocation("textures/entity/knights/knight.png");}});
}
public Entity spawnEntity(int var1, World var2, double var3, double var5, double var7)
{
if(var1==mobid)
return new mcreator_knight.Entityknight(var2);
else
return null;
}
public String getVersion()
{
return "1.0";
}
public static class Entityknight extends EntityIronGolem
{
World world = null;
public Entityknight(World var1)
{
super(var1);
world = var1;
experienceValue = 5;
this.isImmuneToFire = false;
addRandomArmor();
}
protected void addRandomArmor(){
this.setCurrentItemOrArmor(0, new ItemStack(Item.swordIron));
}
/**
* Drop 0-2 items of this living's type
*/
protected void dropFewItems(boolean var1, int var2)
{
this.entityDropItem(new ItemStack(Item.ingotIron), 0.0F);
}
/**
* Returns the sound this mob makes while it's alive.
*/
protected String getLivingSound()
{
return "";
}
/**
* Returns the sound this mob makes when it is hurt.
*/
protected String getHurtSound()
{
return "damage.hit";
}
/**
* Returns the sound this mob makes on death.
*/
protected String getDeathSound()
{
return "damage.hit";
}
public void onStruckByLightning(EntityLightningBolt entityLightningBolt){
int i = (int)this.posX;
int j = (int)this.posY;
int k = (int)this.posZ;
}
protected void fall(float l){
int i = (int)this.posX;
int j = (int)this.posY;
int k = (int)this.posZ;
super.fall(l);
}
public void onCriticalHit(Entity entity) {
int i = (int)this.posX;
int j = (int)this.posY;
int k = (int)this.posZ;
}
public void onKillEntity(EntityLiving entityLiving){
int i = (int)this.posX;
int j = (int)this.posY;
int k = (int)this.posZ;
}
public boolean interact(EntityPlayer entity){
int i = (int)this.posX;
int j = (int)this.posY;
int k = (int)this.posZ;
return true;
}
public String getEntityName(){
return "knight";
}
}
}
I have 16 textures for this biped. Named like so knight1.png through knight16.png. I want to generate a random number 1-16 and put it in the directory "textures/entity/knights/knight(random_number_variable).png" so that each knight has a bit of variance. How do I do this?
Rollback Post to RevisionRollBack
If I helped, or you just like my post, please press the button.
First of all, and you probably get this a lot, I would suggest NOT using MCreator.
Now that that's out of the way
You need 3 things: a way to store which texture the entity should use, a way for the client to know about that, and a Render class that uses that information to choose the texture.
Since the texture won't be changing after the entity has spawned, instead of using DataWatcher, you should implement IEntityAdditionalSpawnData.
First, create an extra class field for your entity to store the integer:
private int texture_index;
// create a getter as well:
public int getTextureIndex() {
return this.texture_index;
}
Then set it to a random value in your entity's constructor:
this.texture_index = rand.nextInt(16); // a value from 0 to 15, i.e. 16 total possibilities
Don't forget to save and load that class field in the #writeToNBT and #readFromNBT methods, which you need to add to your entity. When you do, be sure to call the super method or your entity will be all sorts of mangled.
Well that sets the entity's texture index on the server, but the client doesn't know about that information yet and might even decide that it has a different texture index altogether when it is constructed. To fix that, add 'implements IEntityAdditionalSpawnData' after your class name:
public static class Entityknight extends EntityIronGolem implements IEntityAdditionalSpawnData
While your at it, remove 'static' and make it a separate file all on its own. Cramming everything into one file is ugly, in all kinds of ways.
Once you've done that, be sure to import the class and then hover over EntityKnight, which should be underlined in red, and choose to 'add unimplemented methods'.
Fill in those methods with the following:
In your render class, you should have an array of ResourceLocations; after casting to your entity class, you will have access #getTextureIndex which will give you an integer between 0 and 15 - perfect for snagging a texture from the ResourceLocation array:
private static final ResourceLocation textures = new ResourceLocation[16];
static {
for (int i = 0; i < 16; i++) {
textures[ i ] = new ResourceLocation("your_mod_id", "textures/entity/knight_" + i + ".png");
}
}
@Override
public ResourceLocation getEntityTexture(Entity entity) {
int index = ((EntityKnight) entity).getTextureIndex();
return textures[index];
}
Also, get rid of the 'World world' class field in your entity - ALL entities already have a 'World worldObj' field, so yours is totally redundant.
So, like the title says, I'm trying to make my mod choose a random texture for a biped entity. Here's my code:
import cpw.*;
import cpw.mods.*;
import cpw.mods.fml.*;
import cpw.mods.fml.client.*;
import cpw.mods.fml.client.modloader.*;
import cpw.mods.fml.client.registry.*;
import cpw.mods.fml.common.*;
import cpw.mods.fml.common.asm.*;
import cpw.mods.fml.common.asm.transformers.*;
import cpw.mods.fml.common.discovery.*;
import cpw.mods.fml.common.discovery.asm.*;
import cpw.mods.fml.common.event.*;
import cpw.mods.fml.common.functions.*;
import cpw.mods.fml.common.modloader.*;
import cpw.mods.fml.common.network.*;
import cpw.mods.fml.common.registry.*;
import cpw.mods.fml.common.toposort.*;
import cpw.mods.fml.common.versioning.*;
import cpw.mods.fml.relauncher.*;
import cpw.mods.fml.server.*;
import ibxm.*;
import net.*;
import net.minecraft.*;
import net.minecraft.block.*;
import net.minecraft.block.material.*;
import net.minecraft.client.*;
import net.minecraft.client.audio.*;
import net.minecraft.client.entity.*;
import net.minecraft.client.gui.*;
import net.minecraft.client.gui.achievement.*;
import net.minecraft.client.gui.inventory.*;
import net.minecraft.client.model.*;
import net.minecraft.client.multiplayer.*;
import net.minecraft.client.particle.*;
import net.minecraft.client.renderer.*;
import net.minecraft.client.renderer.culling.*;
import net.minecraft.client.renderer.entity.*;
import net.minecraft.client.renderer.tileentity.*;
import net.minecraft.client.settings.*;
import net.minecraft.command.*;
import net.minecraft.crash.*;
import net.minecraft.creativetab.*;
import net.minecraft.dispenser.*;
import net.minecraft.enchantment.*;
import net.minecraft.entity.*;
import net.minecraft.entity.ai.*;
import net.minecraft.entity.boss.*;
import net.minecraft.entity.effect.*;
import net.minecraft.entity.item.*;
import net.minecraft.entity.monster.*;
import net.minecraft.entity.passive.*;
import net.minecraft.entity.player.*;
import net.minecraft.entity.projectile.*;
import net.minecraft.inventory.*;
import net.minecraft.item.*;
import net.minecraft.item.crafting.*;
import net.minecraft.nbt.*;
import net.minecraft.network.*;
import net.minecraft.network.packet.*;
import net.minecraft.network.rcon.*;
import net.minecraft.pathfinding.*;
import net.minecraft.potion.*;
import net.minecraft.profiler.*;
import net.minecraft.server.*;
import net.minecraft.server.dedicated.*;
import net.minecraft.server.gui.*;
import net.minecraft.server.integrated.*;
import net.minecraft.server.management.*;
import net.minecraft.src.*;
import net.minecraft.stats.*;
import net.minecraft.tileentity.*;
import net.minecraft.util.*;
import net.minecraft.village.*;
import net.minecraft.world.*;
import net.minecraft.world.biome.*;
import net.minecraft.world.chunk.*;
import net.minecraft.world.chunk.storage.*;
import net.minecraft.world.demo.*;
import net.minecraft.world.gen.*;
import net.minecraft.world.gen.feature.*;
import net.minecraft.world.gen.layer.*;
import net.minecraft.world.gen.structure.*;
import net.minecraft.world.storage.*;
import net.minecraftforge.*;
import net.minecraftforge.classloading.*;
import net.minecraftforge.client.*;
import net.minecraftforge.client.event.*;
import net.minecraftforge.client.event.sound.*;
import net.minecraftforge.common.*;
import net.minecraftforge.event.*;
import net.minecraftforge.event.entity.*;
import net.minecraftforge.event.entity.item.*;
import net.minecraftforge.event.entity.living.*;
import net.minecraftforge.event.entity.minecart.*;
import net.minecraftforge.event.entity.player.*;
import net.minecraftforge.event.terraingen.*;
import net.minecraftforge.event.world.*;
import net.minecraftforge.oredict.*;
import net.minecraftforge.transformers.*;
import java.util.Map;
import java.util.Random;
public class mcreator_knight extends BaseMod
{
public static int mobid = 0;
public BaseMod bmod = null;
public void load()
{
ModLoader.registerEntityID(mcreator_knight.Entityknight.class, "knight", mobid=ModLoader.getUniqueEntityId(),
(153 << 16) + (153 << 8) + 153,
(102 << 16) + (102 << 8) + 102);
ModLoader.addSpawn(mcreator_knight.Entityknight.class, 0, 0, 0, EnumCreatureType.creature );
ModLoader.addEntityTracker(bmod, mcreator_knight.Entityknight.class, mobid, 20, 5, true);
cpw.mods.fml.common.registry.LanguageRegistry.instance().addStringLocalization("entity.knight.name", "en_US", "Knight");
}
public void addRenderer(Map var1)
{
var1.put(mcreator_knight.Entityknight.class, new RenderBiped(new ModelBiped(), 0){protected ResourceLocation getEntityTexture(Entity par1Entity){return new ResourceLocation("textures/entity/knights/knight.png");}});
}
public Entity spawnEntity(int var1, World var2, double var3, double var5, double var7)
{
if(var1==mobid)
return new mcreator_knight.Entityknight(var2);
else
return null;
}
public String getVersion()
{
return "1.0";
}
public static class Entityknight extends EntityIronGolem
{
World world = null;
public Entityknight(World var1)
{
super(var1);
world = var1;
experienceValue = 5;
this.isImmuneToFire = false;
addRandomArmor();
}
protected void addRandomArmor(){
this.setCurrentItemOrArmor(0, new ItemStack(Item.swordIron));
}
protected void dropRareDrop(int par1){
this.dropItem(Item.swordIron.itemID, 1);
}
public boolean isAIEnabled()
{
return true;
}
/**
* Drop 0-2 items of this living's type
*/
protected void dropFewItems(boolean var1, int var2)
{
this.entityDropItem(new ItemStack(Item.ingotIron), 0.0F);
}
/**
* Returns the sound this mob makes while it's alive.
*/
protected String getLivingSound()
{
return "";
}
/**
* Returns the sound this mob makes when it is hurt.
*/
protected String getHurtSound()
{
return "damage.hit";
}
/**
* Returns the sound this mob makes on death.
*/
protected String getDeathSound()
{
return "damage.hit";
}
public void onStruckByLightning(EntityLightningBolt entityLightningBolt){
int i = (int)this.posX;
int j = (int)this.posY;
int k = (int)this.posZ;
}
protected void fall(float l){
int i = (int)this.posX;
int j = (int)this.posY;
int k = (int)this.posZ;
super.fall(l);
}
public void onCriticalHit(Entity entity) {
int i = (int)this.posX;
int j = (int)this.posY;
int k = (int)this.posZ;
}
public void onKillEntity(EntityLiving entityLiving){
int i = (int)this.posX;
int j = (int)this.posY;
int k = (int)this.posZ;
}
public boolean interact(EntityPlayer entity){
int i = (int)this.posX;
int j = (int)this.posY;
int k = (int)this.posZ;
return true;
}
public String getEntityName(){
return "knight";
}
}
}
I have 16 textures for this biped. Named like so knight1.png through knight16.png. I want to generate a random number 1-16 and put it in the directory "textures/entity/knights/knight(random_number_variable).png" so that each knight has a bit of variance. How do I do this?
If I helped, or you just like my post, please press the button.
My mods...
Now that that's out of the way
You need 3 things: a way to store which texture the entity should use, a way for the client to know about that, and a Render class that uses that information to choose the texture.
Since the texture won't be changing after the entity has spawned, instead of using DataWatcher, you should implement IEntityAdditionalSpawnData.
First, create an extra class field for your entity to store the integer: Then set it to a random value in your entity's constructor: Don't forget to save and load that class field in the #writeToNBT and #readFromNBT methods, which you need to add to your entity. When you do, be sure to call the super method or your entity will be all sorts of mangled.
Well that sets the entity's texture index on the server, but the client doesn't know about that information yet and might even decide that it has a different texture index altogether when it is constructed. To fix that, add 'implements IEntityAdditionalSpawnData' after your class name: While your at it, remove 'static' and make it a separate file all on its own. Cramming everything into one file is ugly, in all kinds of ways.
Once you've done that, be sure to import the class and then hover over EntityKnight, which should be underlined in red, and choose to 'add unimplemented methods'.
Fill in those methods with the following:
In your render class, you should have an array of ResourceLocations; after casting to your entity class, you will have access #getTextureIndex which will give you an integer between 0 and 15 - perfect for snagging a texture from the ResourceLocation array:
Also, get rid of the 'World world' class field in your entity - ALL entities already have a 'World worldObj' field, so yours is totally redundant.
Aha! I thought I had to come out of quotes to add the variable. Thanks! I'm going to have to use eclipse for this...
EDIT: I hope you don't mind me asking more questions. Because I'm sure more'll fallow.
If I helped, or you just like my post, please press the button.
My mods...