Jump to content

[1.8] Durability Bar is behaving very weird


Cerandior

Recommended Posts

I just created a basic energy Tool, which requires Energy to work. It is not really completed as for the moment it doesn't care about the amount of energy stored into the tool. I just set up the base to work with later. I was trying to show the amount of energy stored into the tool using the durability bar (as most modders have done). I am using getDurabilityForDisplay to do this. Well in the description this method says that it queries the percentage of the durability bar that should be drawn (1.0 for 100% and 0 for 0%). So i am just returning the amount of energy currently stored into the tool divided by the maximum amount of energy that the tool can hold. In theory this should make sense because if the tool is full, the stored energy is equal to max energy hence dividing them will give you 1.0 which should draw 100% of the durability bar. However this is kinda switched around. When the tool is full of energy the durability bar is empty, while when the tool is out of energy the durability bar is full. I am kinda confused with this.

 

Here is the code: (I haven't done any clean-ups!)

 

 

Base Class:

 

package com.ae.tools;

import java.util.Set;

import com.ae.main.AdvancedElectronics;

import net.minecraft.block.Block;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.BlockPos;
import net.minecraft.util.DamageSource;
import net.minecraft.world.World;

public class BaseTool extends Item{

public static float EnergyStored;
public static float MaxEnergy;
public static float EnergyPerUsage;
public static float attackDamage;
public static float efficiency;

public BaseTool(){
	this.setMaxDamage((int) MaxEnergy);
	this.setMaxStackSize(1);
	this.setHarvestLevel("pickaxe", 100);
	this.setCreativeTab(AdvancedElectronics.aeTab);
}

public void setEnergyPerUsage(int Value){
	EnergyPerUsage = Value;
}

public float getEnergyPerUsage(){
	return EnergyPerUsage;
}

@Override
public boolean showDurabilityBar(ItemStack stack) {
	return !(getEnergyStored() == getMaxEnergy());
}

public void setMaxEnergy(float Value){
	MaxEnergy = Value;
}

public float getMaxEnergy(){
	return MaxEnergy;
}

public void setEfficiency(float Value){
	efficiency = Value;
}

public float getEfficiency(){
	return efficiency;
}

public void setAttackDamage(float Value){
	attackDamage = Value;
}

public float getAttackDamage(){
	return attackDamage;
}

public float getEnergyStored(){
	return EnergyStored;
}

public void setEnergyStored(float Value){
	EnergyStored = Value;
}

@Override
public void onUpdate(ItemStack stack, World worldIn, Entity entityIn, int itemSlot, boolean isSelected) {

	if(!stack.hasTagCompound()){
		NBTTagCompound nbt = new NBTTagCompound();
		stack.setTagCompound(nbt);
		nbt.setFloat("EnergyStored", EnergyStored);
		nbt.setFloat("MaxEnergy", MaxEnergy);
		nbt.setFloat("EnergyPerUsage", EnergyPerUsage);
		nbt.setFloat("attackDamage", attackDamage);
		nbt.setFloat("efficiency", efficiency);
	}else{
		NBTTagCompound nbt = stack.getTagCompound();
		nbt.setFloat("EnergyStored", EnergyStored);
		nbt.setFloat("MaxEnergy", MaxEnergy);
		nbt.setFloat("EnergyPerUsage", EnergyPerUsage);
		nbt.setFloat("attackDamage", attackDamage);
		nbt.setFloat("efficiency", efficiency);
	}

	if(EnergyStored > MaxEnergy){
		EnergyStored = MaxEnergy;
		}

	super.onUpdate(stack, worldIn, entityIn, itemSlot, isSelected);
}
}

 

 

Tool:

 

package com.ae.tools;

import java.util.List;

import com.ae.main.AdvancedElectronics;

import net.minecraft.block.Block;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.BlockPos;
import net.minecraft.util.ChatComponentText;
import net.minecraft.util.DamageSource;
import net.minecraft.world.World;

public class testTool extends BaseTool {

public testTool(){
	this.setUnlocalizedName("testTool");
	this.setMaxEnergy(1000);
	this.setEnergyStored(500);
	this.setAttackDamage(50F);
	this.setEfficiency(100F);
	this.setEnergyPerUsage(10);
}

@Override
public void addInformation(ItemStack stack, EntityPlayer player, List list, boolean advanced) {

	if(stack.hasTagCompound()){
	NBTTagCompound nbt = stack.getTagCompound();
	list.add("Current Energy: " + nbt.getInteger("EnergyStored"));
	}
	super.addInformation(stack, player, list, advanced);
}

@Override
public float getStrVsBlock(ItemStack stack, Block block) {
	return this.efficiency;
}

@Override
public double getDurabilityForDisplay(ItemStack stack) {
	return (this.getEnergyStored() / this.getMaxEnergy());
}

@Override
public boolean onBlockDestroyed(ItemStack stack, World world, Block block, BlockPos pos,
		EntityLivingBase player) {

	if(EnergyStored >= block.getBlockHardness(world, pos)){
		if(stack.getTagCompound() != null){
			this.EnergyStored = stack.getTagCompound().getFloat("EnergyStored") - this.EnergyPerUsage;
		}
		//this.setEnergyStored((int) (EnergyStored - block.getBlockHardness(world, pos)));
		//stack.damageItem((int) block.getBlockHardness(world, pos), player);
	}else{
		if(!world.isRemote){
		player.addChatMessage(new ChatComponentText("Out of Energy"));
		}
	}

	return super.onBlockDestroyed(stack, world, block, pos, player);
}

@Override
public boolean canHarvestBlock(Block blockIn) {
	return true;
}

@Override
public ItemStack onItemRightClick(ItemStack stack, World world, EntityPlayer player) {

	if(stack.getTagCompound() != null){
		NBTTagCompound nbt = stack.getTagCompound();
		EnergyStored = nbt.getFloat("EnergyStored") + 30;
	}

	return super.onItemRightClick(stack, world, player);
}

@Override
public boolean hitEntity(ItemStack stack, EntityLivingBase target, EntityLivingBase attacker) {

	if(EnergyStored >= attackDamage){
		target.attackEntityFrom(DamageSource.generic, attackDamage);
		if(stack.getTagCompound() != null){
			if(this.EnergyStored <= this.MaxEnergy){
			this.EnergyStored = stack.getTagCompound().getFloat("EnergyStored") - this.EnergyPerUsage;
			}
		}
		//this.setEnergyStored((int) (EnergyStored - attackDamage));
		//stack.damageItem((int) attackDamage, attacker);
	}

	return super.hitEntity(stack, target, attacker);
}

}

 

 

 

Link to comment
Share on other sites

Yes, it's backwards. Just do

1 - whatever

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

Its supposed to work like that.  Vanilla tools that have not been used show a full durability bar.  Their damage is

0/XXX

.  When its used up (no bar left), damage is

XXX/XXX

.

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

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.