Jump to content

[Solved] Changeing custom rendered item model depending on what 'mode' it's in


chimera27

Recommended Posts

I'm trying to make an item that is 3D rendered, but changes it's model depending on what 'mode' the player is in, where mode is a int in the item's file that changes how the item behaves. I have a custom renderer working, and have the item's 'mode' feature working, but have run into a roadblock in getting the custom renderer to render the right mode. The problem is, to be accessible by the renderer class, the variable 'mode' has to be static, but for the mode to be able to be changed in game, it can't be static!! I'm probs just being a stupid java noob and the answer is likely obvious to more seasoned modders, but I can't see a good way around it other than making a new item for each 'mode' and having a tick handler switch what item it is on button press (which get's VERY cumbersome and broken seeing as there are a number of upgrades for each mode, all of which are accounted for in the item file as is, but would ALL require they're own item file if I were to do this). I have been able to change the item's icon based on mode, and would assume there would be SOME way to change it's model aswell. I'll post my item and renderer file below, as well as anything else anyone asks me to put up, but I can't figure out a good solution for this! Any advice would be greatly appreciated!!

 

Item file:

 

package project.main;

import java.util.List;

import net.minecraft.client.renderer.texture.IconRegister;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.enchantment.Enchantment;
import net.minecraft.enchantment.EnchantmentHelper;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.projectile.EntityArrow;
import net.minecraft.item.EnumAction;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.EnumChatFormatting;
import net.minecraft.util.Icon;
import net.minecraft.world.World;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.entity.player.ArrowLooseEvent;
import net.minecraftforge.event.entity.player.ArrowNockEvent;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;

public class ItemBeam extends Item {

public ItemBeam(int par1) {
	super(par1);
	this.maxStackSize = 1;
}
public static int mode = 1;

int t = 0;
boolean canuseice = false;
boolean canusewave = false;
boolean canuseplasma = false;
public void onPlayerStoppedUsing(ItemStack par1ItemStack, World par2World,
		EntityPlayer par3EntityPlayer, int par4) {

	EntityPowerBolt entitypowerbolt = new EntityPowerBolt(par2World,
			par3EntityPlayer);
	EntityChargedPowerBolt entitychargedpowerbolt = new EntityChargedPowerBolt(
			par2World, par3EntityPlayer);


	EntityIceBolt entityicebolt = new EntityIceBolt(par2World,
			par3EntityPlayer);
	EntityChargedIceBolt entitychargedicebolt = new EntityChargedIceBolt(
			par2World, par3EntityPlayer);


	EntityWaveBolt entitywavebolt = new EntityWaveBolt(par2World,
			par3EntityPlayer);
	EntityChargedWaveBolt entitychargedwavebolt = new EntityChargedWaveBolt(
			par2World, par3EntityPlayer);


	EntityPlasmaBolt entityplasmabolt = new EntityPlasmaBolt(par2World,
			par3EntityPlayer);
	EntityChargedPlasmaBolt entitychargedplasmabolt = new EntityChargedPlasmaBolt(
			par2World, par3EntityPlayer);









	// Insert fireing sound here

	if (!par2World.isRemote) {

		if (t < 60) {
			if (mode == 1){
				par2World.spawnEntityInWorld(entitypowerbolt);
			}
			if (mode == 2){
				par2World.spawnEntityInWorld(entityicebolt);
			}
			if (mode == 3){
				par2World.spawnEntityInWorld(entitywavebolt);
			}
			if (mode == 4){
				par2World.spawnEntityInWorld(entityplasmabolt);
			}



			t = 0;

		} else if (t >= 60) {

			if (mode == 1){
				par2World.spawnEntityInWorld(entitychargedpowerbolt);
			}
			if (mode == 2){
				par2World.spawnEntityInWorld(entitychargedicebolt);
			}
			if (mode == 3){
				par2World.spawnEntityInWorld(entitychargedwavebolt);
			}
			if (mode == 4){
				par2World.spawnEntityInWorld(entitychargedplasmabolt);
			}

			t = 0;


		}
	}

}


public int getMaxItemUseDuration(ItemStack par1ItemStack) {
	return 72000;
}


public EnumAction getItemUseAction(ItemStack par1ItemStack) {
	return null;
}


boolean released = false;
public ItemStack onItemRightClick(ItemStack par1ItemStack, World par2World,
		EntityPlayer par3EntityPlayer) {
	EntityPhazonBeam entityphazonbeam = new EntityPhazonBeam(par2World,
			par3EntityPlayer);

	if (par3EntityPlayer.isPotionActive(Main.hypermode) && !par2World.isRemote) {
			par2World.spawnEntityInWorld(entityphazonbeam);
			return par1ItemStack;
	}
	par3EntityPlayer.setItemInUse(par1ItemStack, this.getMaxItemUseDuration(par1ItemStack));


	return par1ItemStack;
}

public void onUsingItemTick(ItemStack stack, EntityPlayer player, int count) {
	t = t + 1;
	if(MissileLauncherKeyBind.keyPressed == false){
		released = true;
	}
	if (t < 60 && MissileLauncherKeyBind.keyPressed == true && (player.inventory.hasItem(Main.ItemMissile.itemID) || player.capabilities.isCreativeMode) && released == true) {
		 player.worldObj.spawnEntityInWorld(new EntityMissile(player.worldObj, player));
		 player.inventory.consumeInventoryItem(Main.ItemMissile.itemID);
		 released = false;
		 t = 0;
	}
	if (t > 60 && (player.inventory.hasItem(Main.ItemMissile.itemID)|| player.capabilities.isCreativeMode) && MissileLauncherKeyBind.keyPressed == true && released == true){
		player.inventory.consumeInventoryItem(Main.ItemMissile.itemID);
		player.worldObj.spawnEntityInWorld(new EntitySuperMissile(player.worldObj, player));
		released = false;
		t = 0;


	}
	if (player.inventory.hasItem(Main.ItemIceUpgrade.itemID)){
		canuseice = true;
	}
	if (player.inventory.hasItem(Main.ItemWaveUpgrade.itemID)){
		canusewave = true;
	}
	if (player.inventory.hasItem(Main.ItemPlasmaUpgrade.itemID)){
		canuseplasma = true;
	}

	if(PowerBeamKeyBind.keyPressed == true){
		mode = 1;
	}
	if(IceBeamKeyBind.keyPressed == true && canuseice == true){
		mode = 2;
	}
	if(WaveBeamKeyBind.keyPressed == true && canusewave == true){
		mode = 3;
	}
	if(PlasmaBeamKeyBind.keyPressed == true && canuseplasma == true){
		mode = 4;
	}














}

@SideOnly(Side.CLIENT)
private Icon[] Texture = new Icon[5];
private Icon iconIndex;

public void registerIcons(IconRegister iconRegister)

{
	this.itemIcon = iconRegister.registerIcon("chimera27metroid:powerbeam");

		this.Texture[0] = iconRegister.registerIcon("chimera27metroid:" + "powerbeam");
		this.Texture[1] = iconRegister.registerIcon("chimera27metroid:" + "icebeam");
		this.Texture[2] = iconRegister.registerIcon("chimera27metroid:" + "wavebeam");
		this.Texture[3] = iconRegister.registerIcon("chimera27metroid:" + "plasmabeam");
		this.Texture[4] = iconRegister.registerIcon("chimera27metroid:" + "hyperbeam");


}

public Icon getIcon(ItemStack stack, int renderPass, EntityPlayer player,
		ItemStack usingItem, int useRemaining) {



	if (mode == 5){
		return Texture[4];
	} else if (mode == 4) {
		return Texture[3];
	} else if (mode == 3) {
		return Texture[2];
	} else if (mode == 2) {
		return Texture[1];
	}
	return Texture[0];
}

@Override
public void addInformation(ItemStack stack, EntityPlayer player, List list,
		boolean par4) {
	list.add(EnumChatFormatting.AQUA + "Mode:");
	if(mode == 1){
		list.add(EnumChatFormatting.GOLD + "Power Beam!");
	}
	if(mode == 2){
		list.add(EnumChatFormatting.AQUA + "Ice Beam!");
	}
	if(mode == 3){
		list.add(EnumChatFormatting.DARK_PURPLE + "Wave Beam!");
	}

	if(mode == 4){
		list.add(EnumChatFormatting.RED + "Plasma Beam!");
	}

	if(mode == 5){
		list.add(EnumChatFormatting.BLUE + "Phazon Beam!");
	}

}
}

 

 

And the renderer file:

 

package project.main.client.renderers;

import org.lwjgl.opengl.GL11;

import project.main.ItemBeam;
import project.main.Main;
import project.main.models.ModelIceBeam;
import project.main.models.ModelPlasmaBeam;
import project.main.models.ModelPowerBeam;
import project.main.models.ModelWaveBeam;
import net.minecraft.client.Minecraft;
import net.minecraft.entity.Entity;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.client.IItemRenderer;

public class BeamRenderer implements IItemRenderer {

protected ModelPowerBeam powerbeam;
protected ModelIceBeam icebeam;
protected ModelWaveBeam wavebeam;
protected ModelPlasmaBeam plasmabeam;
public static ResourceLocation powertexture = new ResourceLocation(
		"chimera27metroid:item3d/powerbeam.png");
public static ResourceLocation icetexture = new ResourceLocation(
		"chimera27metroid:item3d/icebeam.png");
public static ResourceLocation wavetexture = new ResourceLocation(
		"chimera27metroid:item3d/wavebeam.png");
public static ResourceLocation plasmatexture = new ResourceLocation(
		"chimera27metroid:item3d/plasmabeam.png");

public BeamRenderer() {
	powerbeam = new ModelPowerBeam();
	icebeam = new ModelIceBeam();
	wavebeam = new ModelWaveBeam();
	plasmabeam = new ModelPlasmaBeam();
}

@Override
public boolean handleRenderType(ItemStack item, ItemRenderType type) {
	switch (type) {
	case EQUIPPED:
		return true;
	case EQUIPPED_FIRST_PERSON:
		return true;
	case ENTITY:
		return true;
	default:
		return false;
	}
}

@Override
public boolean shouldUseRenderHelper(ItemRenderType type, ItemStack item,
		ItemRendererHelper helper) {
	return false;
}

@Override
public void renderItem(ItemRenderType type, ItemStack item, Object... data) {
	switch (type) {
	case EQUIPPED: {
		GL11.glPushMatrix();
		if(ItemBeam.mode == 1){
		Minecraft.getMinecraft().renderEngine.bindTexture(powertexture); 
		}
		if(ItemBeam.mode == 2){
			Minecraft.getMinecraft().renderEngine.bindTexture(icetexture); 
			}
		if(ItemBeam.mode == 3){
			Minecraft.getMinecraft().renderEngine.bindTexture(wavetexture); 
			}
		if(ItemBeam.mode == 4){
			Minecraft.getMinecraft().renderEngine.bindTexture(plasmatexture); 
			}
		GL11.glRotatef(15f, 4.0F, 0.0F, 0.0F);
		GL11.glRotatef(0f, 0.0F, 1.0F, 0.0F);
		GL11.glRotatef(0f, 0.0F, 0.0F, 1.0F); 
		GL11.glTranslatef(.6F, -0F, -0.0F); 
		float scale = 1F;
		GL11.glScalef(scale, scale, scale); 
		if(ItemBeam.mode == 1){
		powerbeam.render((Entity) data[1], 0.0F, 0.0F, 0.0F, 0.0F, 0.0F, 0.0625F);
		}
		if(ItemBeam.mode == 2){
			icebeam.render((Entity) data[1], 0.0F, 0.0F, 0.0F, 0.0F, 0.0F, 0.0625F);
			}
		if(ItemBeam.mode == 3){
			wavebeam.render((Entity) data[1], 0.0F, 0.0F, 0.0F, 0.0F, 0.0F, 0.0625F);
			}
		if(ItemBeam.mode == 4){
			plasmabeam.render((Entity) data[1], 0.0F, 0.0F, 0.0F, 0.0F, 0.0F, 0.0625F);
			}
		GL11.glPopMatrix();
		break;
	}
	case ENTITY:
	case EQUIPPED_FIRST_PERSON: {
		GL11.glPushMatrix();
		if(ItemBeam.mode == 1){
			Minecraft.getMinecraft().renderEngine.bindTexture(powertexture); 
			}
			if(ItemBeam.mode == 2){
				Minecraft.getMinecraft().renderEngine.bindTexture(icetexture); 
				}
			if(ItemBeam.mode == 3){
				Minecraft.getMinecraft().renderEngine.bindTexture(wavetexture); 
				}
			if(ItemBeam.mode == 4){
				Minecraft.getMinecraft().renderEngine.bindTexture(plasmatexture); 
				}			GL11.glRotatef(182f, 1.0F, 0.0F, 0.0F);
		GL11.glRotatef(12f, 0.0F, 1.0F, 0.0F);
		GL11.glRotatef(-115f, 0.0F, 0.0F, 1.0F); 
		GL11.glTranslatef(.15F, -.6F, .5F); 
		float scale = 1.0F;
		GL11.glScalef(scale, scale, scale); 
		if(ItemBeam.mode == 1){
			powerbeam.render((Entity) data[1], 0.0F, 0.0F, 0.0F, 0.0F, 0.0F, 0.0625F);
			}
			if(ItemBeam.mode == 2){
				icebeam.render((Entity) data[1], 0.0F, 0.0F, 0.0F, 0.0F, 0.0F, 0.0625F);
				}
			if(ItemBeam.mode == 3){
				wavebeam.render((Entity) data[1], 0.0F, 0.0F, 0.0F, 0.0F, 0.0F, 0.0625F);
				}
			if(ItemBeam.mode == 4){
				plasmabeam.render((Entity) data[1], 0.0F, 0.0F, 0.0F, 0.0F, 0.0F, 0.0625F);
				}			GL11.glPopMatrix();
	}

	default:
		break;

	}
}

}

 

Creator of Metroid Cubed! Power Suits, Beams, Hypermode and more!

width=174 height=100http://i.imgur.com/ghgWmA3.jpg[/img]

Link to comment
Share on other sites

Hi

 

The answer is "it depends on what you want".

If you want all ItemBeams in the game to render exactly the same, so when you change one mode you change them all, then you could use the static variable you currently have.

If you want different ItemBeams to have different modes eg the player can have two ItemBeams in different modes in their hotbar at the same time, then you will need to store that information somewhere else - often this can be the item damage (if this isn't needed for tool usage) - for example like ItemCloth, or a non-static member variable (like in ItemSword).  You have access to this through the ItemStack that the renderer receives.

 

-TGG

 

 

 

 

Link to comment
Share on other sites

Hi

 

The answer is "it depends on what you want".

If you want all ItemBeams in the game to render exactly the same, so when you change one mode you change them all, then you could use the static variable you currently have.

If you want different ItemBeams to have different modes eg the player can have two ItemBeams in different modes in their hotbar at the same time, then you will need to store that information somewhere else - often this can be the item damage (if this isn't needed for tool usage) - for example like ItemCloth, or a non-static member variable (like in ItemSword).  You have access to this through the ItemStack that the renderer receives.

 

-TGG

The problem is that I can only change what 3D model to use by making that variable static, and I can't switch modes in game with the variable being static... which defies the point having it use different models because the mode can't be changed! Even if I did use the damage I wouldn't be able to access that from RenderBeam because it isn't static :(

Creator of Metroid Cubed! Power Suits, Beams, Hypermode and more!

width=174 height=100http://i.imgur.com/ghgWmA3.jpg[/img]

Link to comment
Share on other sites

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.