Jump to content
  • Home
  • Files
  • Docs
Topics
  • All Content

  • This Topic
  • This Forum

  • Advanced Search
  • Existing user? Sign In  

    Sign In



    • Not recommended on shared computers


    • Forgot your password?

  • Sign Up
  • All Activity
  • Home
  • Mod Developer Central
  • Modder Support
  • Right click to Enchant
Currently Supported: 1.16.X (Latest) and 1.15.X (LTS)
Sign in to follow this  
Followers 0
TreeBek

Right click to Enchant

By TreeBek, September 16, 2013 in Modder Support

  • Reply to this topic
  • Start new topic

Recommended Posts

TreeBek    0

TreeBek

TreeBek    0

  • Tree Puncher
  • TreeBek
  • Members
  • 0
  • 17 posts
Posted September 16, 2013

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?

  • Quote

Share this post


Link to post
Share on other sites

TLHPoE    39

TLHPoE

TLHPoE    39

  • Dragon Slayer
  • TLHPoE
  • Members
  • 39
  • 638 posts
Posted September 16, 2013

Look into the ItemEnderPearl class for the right click part.

 

Look into the Enchantment Table classes for the enchantment part.

  • Quote

Kain

Share this post


Link to post
Share on other sites

TreeBek    0

TreeBek

TreeBek    0

  • Tree Puncher
  • TreeBek
  • Members
  • 0
  • 17 posts
Posted September 17, 2013

Thanks!!!

  • Quote

Share this post


Link to post
Share on other sites

TreeBek    0

TreeBek

TreeBek    0

  • Tree Puncher
  • TreeBek
  • Members
  • 0
  • 17 posts
Posted September 17, 2013

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

}

  • Quote

Share this post


Link to post
Share on other sites

Draco18s    2414

Draco18s

Draco18s    2414

  • Reality Controller
  • Draco18s
  • Members
  • 2414
  • 15995 posts
Posted September 17, 2013

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

  • Quote

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.

Share this post


Link to post
Share on other sites

TreeBek    0

TreeBek

TreeBek    0

  • Tree Puncher
  • TreeBek
  • Members
  • 0
  • 17 posts
Posted September 17, 2013

I get a crash when i right click.

  • Quote

Share this post


Link to post
Share on other sites

TreeBek    0

TreeBek

TreeBek    0

  • Tree Puncher
  • TreeBek
  • Members
  • 0
  • 17 posts
Posted September 17, 2013

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.

  • Quote

Share this post


Link to post
Share on other sites

GotoLink    381

GotoLink

GotoLink    381

  • World Shaper
  • GotoLink
  • Members
  • 381
  • 2012 posts
Posted September 17, 2013

You should check hasTagCompound() beforehand.

  • Quote

Share this post


Link to post
Share on other sites

Draco18s    2414

Draco18s

Draco18s    2414

  • Reality Controller
  • Draco18s
  • Members
  • 2414
  • 15995 posts
Posted September 17, 2013

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

  • Quote

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.

Share this post


Link to post
Share on other sites

GotoLink    381

GotoLink

GotoLink    381

  • World Shaper
  • GotoLink
  • Members
  • 381
  • 2012 posts
Posted September 17, 2013

Which doesn't apply to the null object, obviously.

 

if(data != null...

This is in your code, if you didn't know.  :P

  • Quote

Share this post


Link to post
Share on other sites

Draco18s    2414

Draco18s

Draco18s    2414

  • Reality Controller
  • Draco18s
  • Members
  • 2414
  • 15995 posts
Posted September 17, 2013

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

  • Quote

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.

Share this post


Link to post
Share on other sites

TreeBek    0

TreeBek

TreeBek    0

  • Tree Puncher
  • TreeBek
  • Members
  • 0
  • 17 posts
Posted September 18, 2013

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?

  • Quote

Share this post


Link to post
Share on other sites

Mew    36

Mew

Mew    36

  • Dragon Slayer
  • Mew
  • Members
  • 36
  • 567 posts
Posted September 18, 2013

Read the code that Draco18s has posted...

 

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

  • Quote

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

Share this post


Link to post
Share on other sites

TreeBek    0

TreeBek

TreeBek    0

  • Tree Puncher
  • TreeBek
  • Members
  • 0
  • 17 posts
Posted September 18, 2013

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.

  • Quote

Share this post


Link to post
Share on other sites

Draco18s    2414

Draco18s

Draco18s    2414

  • Reality Controller
  • Draco18s
  • Members
  • 2414
  • 15995 posts
Posted September 18, 2013

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

  • Quote

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.

Share this post


Link to post
Share on other sites

TreeBek    0

TreeBek

TreeBek    0

  • Tree Puncher
  • TreeBek
  • Members
  • 0
  • 17 posts
Posted September 18, 2013

Thank you very much!

  • Quote

Share this post


Link to post
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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  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.

    • Insert image from URL
×
  • Desktop
  • Tablet
  • Phone
Sign in to follow this  
Followers 0
Go To Topic Listing



  • Recently Browsing

    No registered users viewing this page.

  • Posts

    • cadbane86140
      Minecraft: Cereal UHC Season 2 Episode 5- THE FINALE!

      By cadbane86140 · Posted 3 hours ago

      Hello There! A bit of a late upload due to technical issues but ITS UP! So after a week of Cereal UHC we have finally reached the end of it! I am so thankful to be apart of this series and I can't wait for the next season! There are so many hilarious moments especially during the final battle! I hope you all enjoyed this series and if you did don't forget to like and sub for more!  
    • samjviana
      [1.16.5] How to make EnchantedBook go to Custom ItemGroup

      By samjviana · Posted 3 hours ago

      But how can i do that for an enchantment book, for normal items i was able to do that by calling the function "group" as you said, but with the enchantment seems to be different.
    • poopoodice
      [1.16.5] How to make EnchantedBook go to Custom ItemGroup

      By poopoodice · Posted 4 hours ago

      Just create an instance of ItemGroup, and set the group of the item in the item property builder.
    • poopoodice
      Trying to make a crop have an "X" shape model

      By poopoodice · Posted 4 hours ago

      Check the parent of their block model.
    • LexManos
      Forge says this file does not have an app associated with it.

      By LexManos · Posted 4 hours ago

      Some zip managers like to take control of the .jar file extension away from Java. Make sure you have Java installed and try running Jarfix once, then try the installer again.
  • Topics

    • cadbane86140
      0
      Minecraft: Cereal UHC Season 2 Episode 5- THE FINALE!

      By cadbane86140
      Started 3 hours ago

    • samjviana
      2
      [1.16.5] How to make EnchantedBook go to Custom ItemGroup

      By samjviana
      Started 7 hours ago

    • TheMajorN
      1
      Trying to make a crop have an "X" shape model

      By TheMajorN
      Started 6 hours ago

    • ESCCarp
      3
      Forge says this file does not have an app associated with it.

      By ESCCarp
      Started 8 hours ago

    • hammy3502
      0
      [1.16.4] Fluid Flowing Very Oddly

      By hammy3502
      Started 5 hours ago

  • Who's Online (See full list)

    • NEXUS5709
    • ASTRO_D3RP
    • hammy3502
    • Lucipro71
  • All Activity
  • Home
  • Mod Developer Central
  • Modder Support
  • Right click to Enchant
  • Theme

Copyright © 2019 ForgeDevelopment LLC · Ads by Longitude Ads LLC Powered by Invision Community