Jump to content

Recommended Posts

Posted

Hi,

 

I attempted to do a seemingly simple task; randomise mob textures. Here is what I tried to do:

package fl.renders;

import java.util.Random;

import net.minecraft.client.model.ModelBase;
import net.minecraft.client.renderer.entity.RenderLiving;
import net.minecraft.entity.Entity;
import net.minecraft.util.ResourceLocation;
import fl.main.Reference;
import fl.models.ModelGreedling;

public class RenderGreedling extends RenderLiving
{
private Random rand = new Random();
private static ResourceLocation greedlingTexture;
private static final ResourceLocation greedlingOriginal = new ResourceLocation(Reference.MOD_ID, "textures/entities/greedling/greedling_original.png");
private static final ResourceLocation greedlingEndling = new ResourceLocation(Reference.MOD_ID, "textures/entities/greedling/greedling_endling.png");
private static final ResourceLocation greedlingSpecial = new ResourceLocation(Reference.MOD_ID, "textures/entities/greedling/greedling_special.png");

public RenderGreedling(ModelBase modelBase, float par2)
{
	super(new ModelGreedling(), 0.3F);
}

@Override
protected ResourceLocation getEntityTexture(Entity ent){
	switch(rand.nextInt(3))
	{
	case 0:
		greedlingTexture = greedlingOriginal;
		break;
	case 1:
		greedlingTexture = greedlingEndling;
		break;
	case 2:
		greedlingTexture = greedlingSpecial;
		break;
	}
	return greedlingTexture;
}
}

 

However, getEntityTexture is constantly being called which makes it choose a new textures each time. I'm not sure how to get around it, advice?

Posted

Create the random texture id once, when the entity gets created (keyword: Constructor).

Okay, I setup a onEntityConstructingEvent, heres my code:

package fl.main;

import java.util.Random;

import net.minecraft.util.ResourceLocation;
import net.minecraftforge.event.ForgeSubscribe;
import net.minecraftforge.event.entity.EntityEvent.EntityConstructing;
import fl.entities.EntityGreedling;

public class ForgeEvents
{
private Random rand = new Random();
public  ResourceLocation greedlingTexture;
private ResourceLocation greedlingOriginal = new ResourceLocation(Reference.MOD_ID, "textures/entities/greedling/greedling_original.png");
private ResourceLocation greedlingEndling = new ResourceLocation(Reference.MOD_ID, "textures/entities/greedling/greedling_endling.png");
private ResourceLocation greedlingSpecial = new ResourceLocation(Reference.MOD_ID, "textures/entities/greedling/greedling_special.png");

@ForgeSubscribe
public void greedlingConstruction(EntityConstructing event){
	if(event.entity instanceof EntityGreedling)
	{
		switch(rand.nextInt(3))
		{
		case 0:
			greedlingTexture = greedlingOriginal;
		break;
		case 1:
			greedlingTexture = greedlingEndling;
		break;
		case 2:
			greedlingTexture = greedlingSpecial;
		break;
		}
	}
}
}

 

Render Class

package fl.renders;

import java.util.Random;

import net.minecraft.client.model.ModelBase;
import net.minecraft.client.renderer.entity.RenderLiving;
import net.minecraft.entity.Entity;
import net.minecraft.util.ResourceLocation;
import fl.main.ForgeEvents;
import fl.main.Reference;
import fl.models.ModelGreedling;

public class RenderGreedling extends RenderLiving
{

public RenderGreedling(ModelBase modelBase, float par2)
{
	super(new ModelGreedling(), 0.3F);
}

@Override
protected ResourceLocation getEntityTexture(Entity ent){
	ForgeEvents fe = new ForgeEvents();
	return fe.greedlingTexture;
}
}

I had to create an object to the class so I could access greedlingTexture, the alternative would be making it static which would lead to all mobs being the same texture :/ I must be missing something

 

 

However, I hit an NPE for the texture registration.

Error:

2014-01-30 00:25:45 [iNFO] [sTDERR] Caused by: java.lang.NullPointerException
2014-01-30 00:25:45 [iNFO] [sTDERR] 	at net.minecraft.client.resources.SimpleReloadableResourceManager.getResource(SimpleReloadableResourceManager.java:59)
2014-01-30 00:25:45 [iNFO] [sTDERR] 	at net.minecraft.client.renderer.texture.SimpleTexture.loadTexture(SimpleTexture.java:31)
2014-01-30 00:25:45 [iNFO] [sTDERR] 	at net.minecraft.client.renderer.texture.TextureManager.loadTexture(TextureManager.java:84)
2014-01-30 00:25:45 [iNFO] [sTDERR] 	... 21 more
2014-01-30 00:25:45 [iNFO] [sTDERR] net.minecraft.util.ReportedException: Registering texture
2014-01-30 00:25:45 [iNFO] [sTDERR] 	at net.minecraft.client.renderer.texture.TextureManager.loadTexture(TextureManager.java:99)
2014-01-30 00:25:45 [iNFO] [sTDERR] 	at net.minecraft.client.renderer.texture.TextureManager.bindTexture(TextureManager.java:41)
2014-01-30 00:25:45 [iNFO] [sTDERR] 	at net.minecraft.client.renderer.entity.Render.bindTexture(Render.java:53)
2014-01-30 00:25:45 [iNFO] [sTDERR] 	at net.minecraft.client.renderer.entity.Render.bindEntityTexture(Render.java:48)
2014-01-30 00:25:45 [iNFO] [sTDERR] 	at net.minecraft.client.renderer.entity.RendererLivingEntity.renderModel(RendererLivingEntity.java:296)
2014-01-30 00:25:45 [iNFO] [sTDERR] 	at net.minecraft.client.renderer.entity.RendererLivingEntity.doRenderLiving(RendererLivingEntity.java:156)
2014-01-30 00:25:45 [iNFO] [sTDERR] 	at net.minecraft.client.renderer.entity.RenderLiving.doRenderLiving(RenderLiving.java:28)
2014-01-30 00:25:45 [iNFO] [sTDERR] 	at net.minecraft.client.renderer.entity.RenderLiving.doRender(RenderLiving.java:142)
2014-01-30 00:25:45 [iNFO] [sTDERR] 	at net.minecraft.client.renderer.entity.RenderManager.renderEntityWithPosYaw(RenderManager.java:312)
2014-01-30 00:25:45 [iNFO] [sTDERR] 	at net.minecraft.client.renderer.entity.RenderManager.renderEntity(RenderManager.java:281)
2014-01-30 00:25:45 [iNFO] [sTDERR] 	at net.minecraft.client.renderer.RenderGlobal.renderEntities(RenderGlobal.java:524)
2014-01-30 00:25:45 [iNFO] [sTDERR] 	at net.minecraft.client.renderer.EntityRenderer.renderWorld(EntityRenderer.java:1160)
2014-01-30 00:25:45 [iNFO] [sTDERR] 	at net.minecraft.client.renderer.EntityRenderer.updateCameraAndRender(EntityRenderer.java:1006)
2014-01-30 00:25:45 [iNFO] [sTDERR] 	at net.minecraft.client.Minecraft.runGameLoop(Minecraft.java:945)
2014-01-30 00:25:45 [iNFO] [sTDERR] 	at net.minecraft.client.Minecraft.run(Minecraft.java:837)
2014-01-30 00:25:45 [iNFO] [sTDERR] 	at net.minecraft.client.main.Main.main(Main.java:93)
2014-01-30 00:25:45 [iNFO] [sTDERR] 	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
2014-01-30 00:25:45 [iNFO] [sTDERR] 	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
2014-01-30 00:25:45 [iNFO] [sTDERR] 	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
2014-01-30 00:25:45 [iNFO] [sTDERR] 	at java.lang.reflect.Method.invoke(Unknown Source)
2014-01-30 00:25:45 [iNFO] [sTDERR] 	at net.minecraft.launchwrapper.Launch.launch(Launch.java:131)
2014-01-30 00:25:45 [iNFO] [sTDERR] 	at net.minecraft.launchwrapper.Launch.main(Launch.java:27)

 

 

Posted

Nevermind, I've managed to do it.

 

For anyone who wants to know:

 

Create an int in your entity, in the constructor of the entity, randomise the int using Javas random class.

Create a method to return the int.

In your render class, in the getEntityTexture method which returns a resourcelocation, you gotta get an object of your entity class, to do that, cast it to the entity param. Then, it simples, just use an else if and check the ints value. EG:

 

@Override
protected ResourceLocation getEntityTexture(Entity ent){
	EntityGreedling greedlingObj = (EntityGreedling)ent;
	int texture = greedlingObj.getTexture();
	if(texture == 0){
		return greedlingOriginal;
	}
	else if(texture == 1){
		return greedlingEndling;
	}
	else{
		return greedlingSpecial;
	}
}

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.