Jump to content

Recommended Posts

Posted

What Im trying to do is change the texture of a block to have the appearance of a battery charge meter, and that every 10% the block updates the textures to show progress. I have tried to follow Vswe's tutorial however I haven't had much success. Is there something I have missed or im doing wrong?

 

BatteryBlock

 


package hydroblocks.blocks;

import hydroblocks.hydroblocks;
import hydroblocks.blocks.tileentities.TileEntityBattery;
import hydroblocks.lib.ModInfo;
import hydroblocks.lib.config.Names;
import net.minecraft.block.Block;
import net.minecraft.block.BlockContainer;
import net.minecraft.block.material.Material;
import net.minecraft.client.renderer.texture.IconRegister;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.Icon;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;

public class BatteryBlock extends BlockContainer {


protected BatteryBlock(int id) {
super(id, Material.rock);
this.setUnlocalizedName(Names.batteryBlock_unlocalizedName);
this.setCreativeTab(hydroblocks.hydroblocks);
this.setHardness(2F);
this.setResistance(15F);
this.setStepSound(Block.soundMetalFootstep);

}

@Override
public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player, int par6, float par7, float par8, float par9) {

if(world.isRemote) return true;


TileEntityBattery batteryte = (TileEntityBattery) world.getBlockTileEntity(x, y, z);


if (batteryte != null) {
if (player.isSneaking()) batteryte.energyStored = 100;
player.addChatMessage("Current Energy:" + batteryte.energyStored);



}

return true;
}



@Override
public TileEntity createNewTileEntity(World world) {
return null;
}

@Override
public TileEntity createTileEntity(World world, int meta) {
return new TileEntityBattery();
}


@SideOnly(Side.CLIENT)
private Icon zeropercentIcon;
private Icon tenpercentIcon;
private Icon twentypercentIcon;
private Icon thirtypercentIcon;
private Icon fourtypercentIcon;
private Icon fiftypercentIcon;
private Icon sixtypercentIcon;
private Icon seventypercentIcon;
private Icon eightypercentIcon;
private Icon nintypercentIcon;
private Icon hunderedpercentIcon;


@Override
@SideOnly(Side.CLIENT)
public void registerIcons(IconRegister register) {
zeropercentIcon = register.registerIcon(ModInfo.ID.toLowerCase() + ":" + Names.batteryBlock_unlocalizedName + "_zero");
tenpercentIcon = register.registerIcon(ModInfo.ID.toLowerCase() + ":" + Names.batteryBlock_unlocalizedName + "_ten");
twentypercentIcon = register.registerIcon(ModInfo.ID.toLowerCase() + ":" + Names.batteryBlock_unlocalizedName + "_twenty");
thirtypercentIcon = register.registerIcon(ModInfo.ID.toLowerCase() + ":" + Names.batteryBlock_unlocalizedName + "_thirty");
fourtypercentIcon = register.registerIcon(ModInfo.ID.toLowerCase() + ":" + Names.batteryBlock_unlocalizedName + "_fourty");
fiftypercentIcon = register.registerIcon(ModInfo.ID.toLowerCase() + ":" + Names.batteryBlock_unlocalizedName + "_fifty");
sixtypercentIcon = register.registerIcon(ModInfo.ID.toLowerCase() + ":" + Names.batteryBlock_unlocalizedName + "_sixty");
seventypercentIcon = register.registerIcon(ModInfo.ID.toLowerCase() + ":" + Names.batteryBlock_unlocalizedName + "_seventy");
eightypercentIcon = register.registerIcon(ModInfo.ID.toLowerCase() + ":" + Names.batteryBlock_unlocalizedName + "_eighty");
nintypercentIcon = register.registerIcon(ModInfo.ID.toLowerCase() + ":" + Names.batteryBlock_unlocalizedName + "_ninty");
hunderedpercentIcon = register.registerIcon(ModInfo.ID.toLowerCase() + ":" + Names.batteryBlock_unlocalizedName + "_hundered");
}

@Override
@SideOnly(Side.CLIENT)
public Icon getBlockTexture(IBlockAccess world, int x, int y, int z, int side){
TileEntityBattery batteryTE = (TileEntityBattery)world.getBlockTileEntity(x,y,z);

if (batteryTE.iszeropercent()){
return zeropercentIcon;
} else

if (batteryTE.istenpercent()){
return tenpercentIcon;
} else

if (batteryTE.istwentypercent()){
return twentypercentIcon;
} else

if (batteryTE.isthirtypercent()){
return thirtypercentIcon;
} else

if (batteryTE.isfourtypercent()){
return fourtypercentIcon;
} else

if (batteryTE.isfiftypercent()){
return fiftypercentIcon;
} else

if (batteryTE.issixtypercent()){
return sixtypercentIcon;
} else

if (batteryTE.isseventypercent()){
return seventypercentIcon;
} else

if (batteryTE.iseightypercent()){
return eightypercentIcon;
} else

if (batteryTE.isnintypercent()){
return nintypercentIcon;
} else

if (batteryTE.ishunderedpercent()){
return hunderedpercentIcon;
}
return zeropercentIcon;







}

}

 

 

TileEntityBattery.

 


package hydroblocks.blocks.tileentities;

import java.util.EnumSet;

import universalelectricity.compatibility.TileEntityUniversalElectrical;
import universalelectricity.core.block.IElectrical;
import ic2.api.energy.event.EnergyTileLoadEvent;
import ic2.api.energy.event.EnergyTileUnloadEvent;
import ic2.api.energy.tile.IEnergySink;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraftforge.common.ForgeDirection;
import net.minecraftforge.common.MinecraftForge;

public class TileEntityBattery extends TileEntityUniversalElectrical implements IElectrical{

@Override
public EnumSet<ForgeDirection> getOutputDirections()
{
return EnumSet.of(ForgeDirection.UP, ForgeDirection.DOWN);
}

@Override
public float getRequest(ForgeDirection direction) {
return (float) Math.min((this.getMaxEnergyStored() - this.getEnergyStored()), 200);
}

@Override
public float getProvide(ForgeDirection direction) {
if (energyStored >= 1){
return 1;
} else
return 0;

}

@Override
public float getMaxEnergyStored() {
// TODO Auto-generated method stub
return 10000;
}

public boolean iszeropercent(){
return energyStored > 0 && energyStored < 1001;

}

public boolean istenpercent() {
return energyStored > 1001 && energyStored < 2001;

}

public boolean istwentypercent() {
return energyStored > 2001 && energyStored < 3001;

}

public boolean isthirtypercent() {
return energyStored > 3001 && energyStored < 4001;

}

public boolean isfourtypercent() {
return energyStored > 4001 && energyStored < 5001;

}

public boolean isfiftypercent() {
return energyStored > 5001 && energyStored < 6001;

}

public boolean issixtypercent() {
return energyStored > 6001 && energyStored < 7001;

}

public boolean isseventypercent() {
return energyStored > 7001 && energyStored < 8001;

}

public boolean iseightypercent() {
return energyStored > 8001 && energyStored < 9001;

}

public boolean isnintypercent() {
return energyStored > 9001 && energyStored < 10001;

}

public boolean ishunderedpercent() {
return energyStored <= 10000;


}



@Override
public void updateEntity()
{
super.updateEntity();
this.produce();
//this.receiveElectricity(5000,true);

}



}

 

Posted

Changing the texture isn't guaranteed to make the block update visually.  You will need to cause a block update (world.scheduleBlockUpdate) or vanilla won't know that something changed.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Posted

I have updated the code slightly, the main problem I'm having before I address scheduling updates (for now i'm just manually updating the texture my adding and breaking a block next to it), is that in the tool bar the block Icon appears correctly and when it's first placed it the texture changes correctly however it wont change under manual update to the correct texture.

 

I think it may have to do with

 @Override
@SideOnly(Side.CLIENT)
public Icon getBlockTexture(IBlockAccess world, int x, int y, int z, int side) {
TileEntityBattery battery = (TileEntityBattery)world.getBlockTileEntity(x, y, z);

		 if (battery.isEmpty())  return emptyIcon;
		 if (battery.isCharging()) return chargingIcon;
		 if (battery.isFull()) return fullIcon;


return blockIcon;
}

is there anther way to do this? or is this even correct

 

 

Block

 


package hydroblocks.blocks;

import hydroblocks.hydroblocks;
import hydroblocks.blocks.tileentities.TileEntityBattery;
import hydroblocks.lib.ModInfo;
import hydroblocks.lib.config.Names;
import net.minecraft.block.Block;
import net.minecraft.block.BlockContainer;
import net.minecraft.block.material.Material;
import net.minecraft.client.renderer.texture.IconRegister;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.Icon;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;

public class BatteryBlock extends BlockContainer {


protected BatteryBlock(int id) {
super(id, Material.rock);
this.setUnlocalizedName(Names.batteryBlock_unlocalizedName);
this.setCreativeTab(hydroblocks.hydroblocks);
this.setHardness(2F);
this.setResistance(15F);
this.setStepSound(Block.soundMetalFootstep);

}

@Override
public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player, int par6, float par7, float par8, float par9) {

if(world.isRemote) return true;


TileEntityBattery batteryte = (TileEntityBattery) world.getBlockTileEntity(x, y, z);


if (batteryte != null) {
if (player.isSneaking()) batteryte.energyStored = 100;
player.addChatMessage("Current Energy:" + batteryte.energyStored);



}

return true;
}


@Override
public TileEntity createTileEntity(World world, int meta) {
return new TileEntityBattery();
}



@SideOnly(Side.CLIENT)
private Icon emptyIcon;
private Icon chargingIcon;
private Icon fullIcon;


@Override
@SideOnly(Side.CLIENT)
public void registerIcons(IconRegister register) {
blockIcon = register.registerIcon(ModInfo.ID.toLowerCase() + ":" + Names.batteryBlock_unlocalizedName + "_ten");
emptyIcon = register.registerIcon(ModInfo.ID.toLowerCase() + ":" + Names.batteryBlock_unlocalizedName + "thirty");
chargingIcon = register.registerIcon(ModInfo.ID.toLowerCase() + ":" + Names.batteryBlock_unlocalizedName + "_charging");
fullIcon = register.registerIcon(ModInfo.ID.toLowerCase() + ":" + Names.batteryBlock_unlocalizedName + "_full");
}

@Override
@SideOnly(Side.CLIENT)
public Icon getBlockTexture(IBlockAccess world, int x, int y, int z, int side) {
TileEntityBattery battery = (TileEntityBattery)world.getBlockTileEntity(x, y, z);

		 if (battery.isEmpty())  return emptyIcon;
		 if (battery.isCharging()) return chargingIcon;
		 if (battery.isFull()) return fullIcon;


return blockIcon;
}




@Override
public TileEntity createNewTileEntity(World world) {
return null;
}



}


 

 

Tile Entity

 

package hydroblocks.blocks.tileentities;

import java.util.EnumSet;

import universalelectricity.compatibility.TileEntityUniversalElectrical;
import universalelectricity.core.block.IElectrical;
import ic2.api.energy.event.EnergyTileLoadEvent;
import ic2.api.energy.event.EnergyTileUnloadEvent;
import ic2.api.energy.tile.IEnergySink;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;
import net.minecraftforge.common.ForgeDirection;
import net.minecraftforge.common.MinecraftForge;

public class TileEntityBattery extends TileEntityUniversalElectrical implements IElectrical{

public boolean isEmpty() {
	return energyStored <= 1;
}

public boolean isCharging() {
	return energyStored >= 300;
}

public boolean isFull() {
	return energyStored >= 9999;
}


@Override
public EnumSet<ForgeDirection> getOutputDirections()
{
	return EnumSet.of(ForgeDirection.UP, ForgeDirection.DOWN);
}

@Override
public float getRequest(ForgeDirection direction) {
	 return (float) Math.min((this.getMaxEnergyStored() - this.getEnergyStored()), 200);
}

@Override
public float getProvide(ForgeDirection direction) {
	if (energyStored >= 1){
		return 1;
	} else
	return 0;

}

@Override
public float getMaxEnergyStored() {
	// TODO Auto-generated method stub
	return 10000;
}

@Override
public void updateEntity()
{
	super.updateEntity();
	this.produce();
	//this.receiveElectricity(5000,true);

}



}

 

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.