Posted September 23, 201312 yr Hi Guys, on which way I can enchant items on crafting e.g. my Sword with Sharpness II or something else? Thanks for help! comfix aka SomethingSpecialLP Way is not, who knows everything, but who knows where he can look it up.
September 23, 201312 yr Sure you can. Question is: Do you want to add a specific enchantment manually or do you want to apply a random enchantment that is appropriate, or do you want to apply any random enchantment? (I happen to be in the middle of working on this myself!) 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 24, 201312 yr Author Yea i want to add a specific enchantment on my tools and armor on crafting. It should be 1 or 2 enchantents for an item e.g. Chestpalte with Protection and Projectile Protection and the tools with Efficiency. A random one sounds interesting and makes my mod a little bit harder But the item's should be still enchantable with the enchantment that was added by crafting the item. I had a full set of armor and tools plus a bow. comfix aka SomethingSpecialLP Way is not, who knows everything, but who knows where he can look it up.
September 24, 201312 yr Ok, this is fairly easy then. public void onCreated(ItemStack par1ItemStack, World par2World, EntityPlayer par3EntityPlayer) { int level = 20; //pretend the player has 20 levels to spend par1ItemStack = EnchantmentHelper.addRandomEnchantment(new Random(), par1ItemStack, level); } In theory that should work. There may be some helpers that need to be called to get your custom item recognized as valid [tool|bow|armor], I'm not sure. My items are going about it slightly differently (as I'm using one item class that results in items with varying effects, so "is it a tool?" is dependent on NBT data) so I'm actually enchanting a vanilla pickaxe/sword/armor (as appropriate) and then copying the relevant NBT data over to my item: Item item = Item.pickaxeWood; //pick a random amount of experience to use. Higher amounts are rarer. Minimum 5. int level = 4; do { ++level; } while(level < 30 && instance.rand.nextInt( != 0); //if it has the deal-damage effect, treat it as a sword if(effectsOnItem.contains(2) && instance.rand.nextBoolean()) { //get item's material switch(artifact.stackTagCompound.getInteger("material")) { case 0: item = Item.swordWood; break; case 1: item = Item.swordStone; break; case 2: item = Item.swordIron; break; case 3: item = Item.swordGold; break; case 4: item = Item.swordDiamond; break; } } //else treat it as a tool (imperfect, but acceptable; armor not coded yet) else { switch(artifact.stackTagCompound.getInteger("material")) { case 0: item = Item.pickaxeWood; break; case 1: item = Item.pickaxeStone; break; case 2: item = Item.pickaxeIron; break; case 3: item = Item.pickaxeGold; break; case 4: item = Item.pickaxeDiamond; break; } } //enchant the sword/pickaxe ItemStack stack = new ItemStack(item); stack = EnchantmentHelper.addRandomEnchantment(instance.rand, stack, level); //copy the enchantment from the temporary item to the real item artifact.stackTagCompound.setTag("ench", stack.stackTagCompound.getTag("ench").copy());/code] It's very easy if you are looking for a very specific enchantment, as ItemStack has this function: public void addEnchantment(Enchantment par1Enchantment, int par2){...} Easy peasy! 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.