Jump to content

Chaning block texture


jibbity

Recommended Posts

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);

}



}

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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);

}



}

 

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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • Hello. I'm using IntelliJ IDEA and making a mod for Forge 1.20.1 and I'm trying to make a GUI. I found that the Minecraft-related codes are all obfuscated and when setting up the workspace, Gradle did not execute Deobfuscation tasks like createMcpToSrg. Is this normal or a bug? Can anyone help me with this?
    • Hello! I'm trying to get a list of all entries registered in the infusion upgrade registry. Code can be found in github: https://github.com/ChamoisEST/MiningMadness/tree/1.20.1 Registry in common/registries/MMRegistries.java Event that calls the registry is in MiningMadness.java List of registered entries should be called in common/capabilities/infusion/InfusionCapabilityImplementation.java:getActiveInfusions() in the for loop. Is there any way to accomplish that? Thanks in advance!  
    • So, I am trying to create and add tags to my custom damage type, but looks like I made something wrong, since it crashing with error that "cannot register" damage type. Im trying to register it, and in datagen add tags, but it everytime crashing due to not register "grimtales:entropy". Full code there - https://github.com/undertakerJ/Grim-Tales-Forge ModDamageTypes class public class ModDamageTypes { public static final DeferredRegister<DamageType> DAMAGE_TYPES = DeferredRegister.create(Registries.DAMAGE_TYPE, GrimTales.MOD_ID); public static final ResourceKey<DamageType> ENTROPY_KEY = ResourceKey.create(Registries.DAMAGE_TYPE, new ResourceLocation(GrimTales.MOD_ID, "entropy")); public static final RegistryObject<DamageType> ENTROPY = DAMAGE_TYPES.register( "entropy", () -> new DamageType("entropy_effect", DamageScaling.ALWAYS, 1f, DamageEffects.HURT)); // // public static final DamageSource ENTROPY_SOURCE = new DamageSource(Holder.direct(ENTROPY.get())); public static void register(IEventBus eventBus) { DAMAGE_TYPES.register(eventBus); } } Datagen for damageTags public class ModDamageTagsProvider extends DamageTypeTagsProvider { public ModDamageTagsProvider( PackOutput p_270719_, CompletableFuture<HolderLookup.Provider> p_270256_, @Nullable ExistingFileHelper existingFileHelper) { super(p_270719_, p_270256_, GrimTales.MOD_ID, existingFileHelper); } @Override protected void addTags(HolderLookup.Provider pProvider) { this.tag(DamageTypeTags.BYPASSES_INVULNERABILITY).add(ModDamageTypes.ENTROPY_KEY); } } Datagen class it self   @Mod.EventBusSubscriber(modid = GrimTales.MOD_ID, bus = Mod.EventBusSubscriber.Bus.MOD) public class DataGenerators { @SubscribeEvent public static void gatherData(GatherDataEvent event){ DataGenerator generator = event.getGenerator(); PackOutput packOutput = generator.getPackOutput(); ExistingFileHelper helper = event.getExistingFileHelper(); CompletableFuture<HolderLookup.Provider> lookupProvider = event.getLookupProvider(); generator.addProvider(event.includeServer(), new ModDamageTagsProvider(packOutput, lookupProvider, helper)); } } And register class in main class public GrimTales() { IEventBus modEventBus = FMLJavaModLoadingContext.get().getModEventBus(); ModDamageTypes.register(modEventBus); modEventBus.addListener(this::commonSetup); MinecraftForge.EVENT_BUS.register(this); modEventBus.addListener(this::addCreative); }  
    • Looking for a semi-vanilla Minecraft server? Maybe one with keep inventory and land claims? I have one for you! Let me introduce you to Safe Survival! Come on and join us at mc.safesurvival.net. It is bedrock supported too!
    • Hi. I'm trying to play RLCraft with Optifine but my game keeps crashing on startup with Exit Code 1. The modpack was working fine yesterday, not sure what changed to make it not wanna work anymore. I was hoping someone here could help me figure out what's going wrong. If I remove Optifine, nothing changes, I still get the same errors. Thanks in advance. Crash Report (I only included the errors+fatal, I’m not sure if more is needed, please let me know): https://pastebin.ai/66ivknz0f4  
  • Topics

×
×
  • Create New...

Important Information

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