Jump to content

Recommended Posts

Posted

Hello i have been trying to get my tools/armor so that when  i right click it enchants said tool/armor with an enchantment suitable for that item but no matter how hard i try i can not figure it out can someone help me?

Posted

Well it kind works but i can't figure out how to do it to where i can only give it one enchantment this is what i have so far

 

package Harwood;

import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import net.minecraft.client.renderer.texture.IconRegister;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.enchantment.Enchantment;
import net.minecraft.entity.EntityLiving;
import net.minecraft.entity.item.EntityEnderPearl;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.EnumAction;
import net.minecraft.item.EnumToolMaterial;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.ItemSword;
import net.minecraft.world.World;

public class ItemDiamondHarwoodSword extends ItemSword {

public ItemDiamondHarwoodSword(int par1,
		EnumToolMaterial par2EnumToolMaterial) {
	super(par1, par2EnumToolMaterial);
	this.setCreativeTab(HarwoodBase.HarwoodMod);
	}

@SideOnly(Side.CLIENT)
public void registerIcons(IconRegister par1IconRegister){
	this.itemIcon = par1IconRegister.registerIcon(HarwoodBase.modid + ":" + (this.getUnlocalizedName().substring(5)));
}

public ItemStack onItemRightClick(ItemStack par1ItemStack, World par2World, EntityPlayer par3EntityPlayer)
    {

	int enchantnum =  (itemRand.nextInt(6) + 1);
	int counter = 0;
	//debug
	System.out.println("Enchant num " + enchantnum);
	System.out.println("Counter num " + counter);
	//debug
	if(counter <= 0){
		switch(enchantnum){

		case 1: {par1ItemStack.addEnchantment(Enchantment.fireAspect, 2);} counter++; break;
		case 2: {par1ItemStack.addEnchantment(Enchantment.baneOfArthropods, 2);} counter++; break;
		case 3: {par1ItemStack.addEnchantment(Enchantment.knockback, 2);} counter++; break;
		case 4: {par1ItemStack.addEnchantment(Enchantment.looting, 2);} counter++; break;
		case 5: {par1ItemStack.addEnchantment(Enchantment.sharpness, 2);} counter++; break;
		case 6: {par1ItemStack.addEnchantment(Enchantment.smite, 2);} counter++;  break;


		};counter =  1;
	}
            return par1ItemStack;
    }

}

Posted

Couple of things:

That counter is a local variable to that function.  Every time the function is run that value will be 0, thus it will add a new enchantment:

 

int counter = 0;
if(counter <= 0){

 

Second even though you're incrementing the counter each time it adds an enchantment, you reset the counter to 1 at the end:

 

case 6: {par1ItemStack.addEnchantment(Enchantment.smite, 2);} counter++;  break;
};counter =  1;

 

In order to get what you want, you will need to store the counter in the NBT data for the itemstack:

 

NBTTagCompound nbt = par1ItemStack.stackTagCompound;
int counter = nbt.getInteger("EnchantmentCounter"); //returns 0 if tag doesn't exist, which is fine
if(counter <= 0) {
     //add an enchantment
     counter++;
}
nbt.setInteger("EnchantmentCounter",counter);

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

It seems to originate in line

int counter = nbt.getInteger("EnchantmentCounter"); //returns 0 if tag doesn't exist, which is fine	

 

Btw Draco I liked your Phase stone mod i really enjoyed using it.

Posted

You should check hasTagCompound() beforehand.

 

Javadoc:

 

NBTTagCompound.getInteger(par1Str)

Retrieves an integer value using the specified key, or 0 if no such key was stored.

Parameters:

par1Str

 

I happen to be using the "or 0" effect in my current mod:

 

public ItemStack onItemRightClick(ItemStack par1ItemStack, World par2World, EntityPlayer par3EntityPlayer)
    {
	NBTTagCompound data = par1ItemStack.getTagCompound();
	int effectID = 0;
	//oddly this function runs on the server, but directly altering the player entity has no effect.
	//so instead we end up using packets
	if(data != null && par2World.isRemote) {
		effectID = data.getInteger("onItemRightClick");
		if(effectID != 0) {
			//do stuff
		}
	}
        return par1ItemStack;
    }

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

Right, of course.

 

And just because I did it right and forgot the null check when posting the first time doesn't mean I don't know.  It just means that you shouldn't be blindingly copy-pasting. ;)

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

So uh i have no idea what you guys are talking about i have a general understanding of java(just started coding a couple of weeks ago) do you think you could explain how this works?

Posted

Read the code that Draco18s has posted...

 

What he forgot to tell you was to do the null check.

I am Mew. The Legendary Psychic. I behave oddly and am always playing practical jokes.

 

I have also found that I really love making extremely long and extremely but sometimes not so descriptive variables. Sort of like what I just did there xD

Posted

SO i added the null check and it no longer crashes which is good but i don't understand how to get ntb to be something other than null that way my switch statement will be executed.

 

Edit* Well i have been working to figure this out and i have gotten ntb to be things other than null but it only enchants the Sword for a couple milliseconds and then goes back to not being enchanted and lets me do the process over again.

Posted

Edit* Well i have been working to figure this out and i have gotten ntb to be things other than null but it only enchants the Sword for a couple milliseconds and then goes back to not being enchanted and lets me do the process over again.

 

Probably because you're doing it client side. ;)

 

This is the code I pasted

 

if(data != null && par2World.isRemote) {

 

This line checks to make sure that the client is running it because...

 

		//oddly this function runs on the server, but directly altering the player entity has no effect.
	//so instead we end up using packets

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.

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.