Posted September 16, 201312 yr 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?
September 16, 201312 yr Look into the ItemEnderPearl class for the right click part. Look into the Enchantment Table classes for the enchantment part. Kain
September 17, 201312 yr Author 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; } }
September 17, 201312 yr 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.
September 17, 201312 yr Author 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.
September 17, 201312 yr 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.
September 17, 201312 yr Which doesn't apply to the null object, obviously. if(data != null... This is in your code, if you didn't know.
September 17, 201312 yr 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.
September 18, 201312 yr Author 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?
September 18, 201312 yr 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
September 18, 201312 yr Author 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.
September 18, 201312 yr 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.