Jump to content

Cant create item stack in Event Hook Container Class, please help


Recommended Posts

Posted

So im trying to add a EventHook that checks if the player is using my item when they right click but i cant make the item stack in the if statement :/ please help

[spoiler=FoolsCraftMain]

package m1kep.foolscraft.common;

import net.minecraft.client.Minecraft;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.item.Item;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.ForgeSubscribe;
import net.minecraftforge.event.entity.player.PlayerInteractEvent;
import cpw.mods.fml.common.Mod;
import cpw.mods.fml.common.Mod.Init;
import cpw.mods.fml.common.Mod.Instance;
import cpw.mods.fml.common.Mod.PostInit;
import cpw.mods.fml.common.Mod.PreInit;
import cpw.mods.fml.common.SidedProxy;
import cpw.mods.fml.common.event.FMLInitializationEvent;
import cpw.mods.fml.common.event.FMLPostInitializationEvent;
import cpw.mods.fml.common.event.FMLPreInitializationEvent;
import cpw.mods.fml.common.network.NetworkMod;
import cpw.mods.fml.common.registry.LanguageRegistry;

@Mod(modid = "FoolsCraft", name = "FoolsCraft", version = "0.0.0.1")
@NetworkMod(clientSideRequired = true, serverSideRequired = false)
public class FoolsCraftMain {

@Instance("FoolsCraft")
public static FoolsCraftMain instance;

// Items
private final static Item DebugItem1 = new DebugItem1(5000, 1,
		CreativeTabs.tabMisc, 0, "DebugItem1");

@SidedProxy(clientSide = "m1kep.foolscraft.client.FoolsCraftClientProxy", serverSide = "m1kep.foolscraft.common.FoolsCraftCommonProxy")
public static FoolsCraftCommonProxy proxy;

@PreInit
public void preInit(FMLPreInitializationEvent event) {

}

@Init
public void load(FMLInitializationEvent event) {
	LanguageRegistry.addName(DebugItem1, "DebugItem - A");
	MinecraftForge.EVENT_BUS.register(new EventHookContainerClass());

}

@PostInit
public static void postInit(FMLPostInitializationEvent event) {

}


}

 

 

[spoiler=DebugItem1]

package m1kep.foolscraft.common;

import net.minecraft.client.Minecraft;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.world.World;

public class DebugItem1 extends Item {

public DebugItem1(int id, int maxStackSize, CreativeTabs tab, int texture,
		String name) {
	super(id);
	// TODO Auto-generated constructor stub
	setMaxStackSize(maxStackSize);
	setCreativeTab(tab);
	setUnlocalizedName(name);
}

public boolean onItemUse(ItemStack par1ItemStack,
		EntityPlayer par2EntityPlayer, World par3World, int par4, int par5,
		int par6, int par7, float par8, float par9, float par10) {
	World world = par3World;
	EntityPlayer ply = par2EntityPlayer;
	double posX = ply.lastTickPosX;
	double posY = ply.lastTickPosY;
	double posZ = ply.lastTickPosZ;
	System.out.println("X: " + posX);
	System.out.println("Y: " + posY);
	System.out.println("Z: " + posZ);
	System.out.println("Int 1: " + par4);
	System.out.println("Int 2: " + par5);
	System.out.println("Int 3: " + par6);
	System.out.println("Int 4: " + par7);
	System.out.println("Float 1: " + par8);
	System.out.println("Float 2: " + par9);
	System.out.println("Float 3: " + par10);
	System.out.println(Minecraft.getMinecraft().thePlayer.getItemInUse());
	return false;
}

}

 

 

[spoiler=EventHookContainerClass]

package m1kep.foolscraft.common;

import net.minecraft.client.Minecraft;
import net.minecraft.item.ItemStack;
import net.minecraftforge.event.ForgeSubscribe;
import net.minecraftforge.event.entity.player.PlayerInteractEvent;

public class EventHookContainerClass {
@ForgeSubscribe
public void buildVillage(PlayerInteractEvent event) {

	if ((Minecraft.getMinecraft().thePlayer.getCurrentEquippedItem() == new ItemStack(DebugItem1))) {
		Minecraft.getMinecraft().thePlayer.setHealth(-1);
	}
}
}

 

Posted

(Minecraft.getMinecraft().thePlayer.getCurrentEquippedItem() == new ItemStack(DebugItem1))

This is completely wrong. It will always be false. Use either object.equals(anotherObject) or in case of ItemStacks itemStack.isItemStackEqual(anotherItemStack)

Don't ask for support per PM! They'll get ignored! | If a post helped you, click the "Thank You" button at the top right corner of said post! |

mah twitter

  Quote

This thread makes me sad because people just post copy-paste-ready code when it's obvious that the OP has little to no programming experience. This is not how learning works.

Posted

Ok,so thats not really the problem, my problem is that my event hook class does not know of my DebugItem1, so what should i do?

Posted
  On 5/19/2013 at 7:07 PM, M1kep said:

Anybody?

 

You don't specify which class holds this variable (since you don't specify it in the class where you call it).

ExampleClass.ExampleStaticVariable

exampleClassInstance.ExampleNonStaticVariable

Don't ask for support per PM! They'll get ignored! | If a post helped you, click the "Thank You" button at the top right corner of said post! |

mah twitter

  Quote

This thread makes me sad because people just post copy-paste-ready code when it's obvious that the OP has little to no programming experience. This is not how learning works.

Posted

So in my main class file i have:

 private final static Item DebugItem1 = new DebugItem1(5000, 1,
   CreativeTabs.tabMisc, 0, "DebugItem1");

So what do i need to do in my EventHook class to specify this?

Posted
  On 5/19/2013 at 8:20 PM, SanAndreasP said:

  Quote

Anybody?

 

You don't specify which class holds this variable (since you don't specify it in the class where you call it).

ExampleClass.ExampleStaticVariable

exampleClassInstance.ExampleNonStaticVariable

I dont really understand what you are trying to say

Posted
  On 5/19/2013 at 8:56 PM, M1kep said:

  Quote

  Quote

Anybody?

 

You don't specify which class holds this variable (since you don't specify it in the class where you call it).

ExampleClass.ExampleStaticVariable

exampleClassInstance.ExampleNonStaticVariable

I dont really understand what you are trying to say

 

http://www.javatutorialhub.com/java-static-variable-methods.html

You should learn some basics...

Don't ask for support per PM! They'll get ignored! | If a post helped you, click the "Thank You" button at the top right corner of said post! |

mah twitter

  Quote

This thread makes me sad because people just post copy-paste-ready code when it's obvious that the OP has little to no programming experience. This is not how learning works.

Posted

Can you show me what I'm doing wrong? Cause i understand the basics of java, I have a feeling this is a stupid mistake but I can't seem to figure out what I'm doing wrong, that tutorial page did not help make much sense of my current problem.

Posted

I'm going to say this now, but I have 2 recommendations. 1. Learn some basic JAVA or at least basic C based code.

2. Look at itembow or entityplayer and see how it is done there, We don't just give out free code to people because they don't know something :3

 

If you are really stumped I can help you though, but look at what I said and try to figure it out for yourself. Also, your getting Item and Itemstack variables mixed up.

 

Edit: Why the hell is the Item declaration In your main mod file PRIVATE?

This is the creator of the Rareores mod! Be sure to check it out at

Posted

okay, since I have time now, I'll go into detail:

You have this:

		if ((Minecraft.getMinecraft().thePlayer.getCurrentEquippedItem() == new ItemStack(DebugItem1))) {
		Minecraft.getMinecraft().thePlayer.setHealth(-1);
	}

 

The very first problem I see is that you use Minecraft.getMinecraft().thePlayer to get the player. That is wrong, because with this your mod won't be multiplayer compatible. The event you use ships the player instance already, so you can use event.entityPlayer.

 

The second thing is that you try to compare two (completely different) instances with the == operator, which is only true if you compare two variables which have the same primitive datatype (e.g. int) and the same value associated with, or if the variables contain the exact same instance of an object. So getCurrentEquippedItem() == getCurrentEquippedItem() would be true, getCurrentEquippedItem() == new ItemStack(...) would be false (since new indicates a new instance).

For ItemStacks I suggest you use the isItemEqual method provided by the ItemStack in order to compare those two: getCurrentEquippedItem().isItemEqual(new ItemStack(...)). For any other object, use either the method provided by the objects class or use the globally applicable .equals(...) method.

 

The third issue is the DebugItem1. It shows you the error, because this variable is not declared in the class where the code is. It would be declared if you would do public <type> DebugItem1; somewhere in your class body. But that would be wrong, since you want to get your item instance (whose field is declared as static by you) inside your main mod class. To get this static field you have to reference the class where it lies:

FoolsCraftMain.DebugItem1. Ah yeah, in order to access the field, make it public, not private nor protected.

 

 

Now you should be able to fix this piece of code you have there with the information I gave you above, since that's the most detailed info I can give you unless I fix your code, and this isn't my interest, since you should learn what you've done wrong ;)

Don't ask for support per PM! They'll get ignored! | If a post helped you, click the "Thank You" button at the top right corner of said post! |

mah twitter

  Quote

This thread makes me sad because people just post copy-paste-ready code when it's obvious that the OP has little to no programming experience. This is not how learning works.

Posted

Ok, so to start off thank you :D

But now theres another problem that i have NO IDEA where to start

The game crashes weather i right click with a bone, or my item.

Here's the crash log

 

  Reveal hidden contents

 

 

Here is my Event Class:

 

  Reveal hidden contents

 

And my Main Class:

 

  Reveal hidden contents
Posted

Just a question, Using Minecraft.getMinecraft().thePlayer is okay if your using it just on the client right? (Like for renderings)

This is the creator of the Rareores mod! Be sure to check it out at

Posted
  On 5/19/2013 at 10:32 PM, Noah_Beech said:

Just a question, Using Minecraft.getMinecraft().thePlayer is okay if your using it just on the client right? (Like for renderings)

 

Yup, if it's intended to be used only client-sided then it's fine.

Check if getCurrentEquippedItem() is not null, then execute your code.

 

 

Don't ask for support per PM! They'll get ignored! | If a post helped you, click the "Thank You" button at the top right corner of said post! |

mah twitter

  Quote

This thread makes me sad because people just post copy-paste-ready code when it's obvious that the OP has little to no programming experience. This is not how learning works.

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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • I should have specified in my last post, the current build is the latest that Ars Nouvaeu supports. Sorry for the trouble!
    • New users at Temu receive a $100 discount on orders over $100 Use the code [aci789589] during checkout to get Temu Coupon Code $100 off For New Users. Yes, Temu offers $100 off coupon code “aci789589” for first-time users. Temu 100% Off coupon code "aci789589" will save you $100 on your order. To get a discount, click on the item to purchase and enter the code. Yes, Temu offers $100 off coupon code “aci789589” for first-time users. You can get a$100 bonus plus 30% off any purchase at Temu with the$100 Coupon Bundle at Temu if you sign up with the referral code [aci789589] and make a first purchase of$50 or more. The Temu $100 Off coupon code (aci789589) will save you $100 on your order. To get a discount, click on the item to purchase and enter the code. Yes Temu offers $100 Off Coupon Code “aci789589” for First Time Users. Yes, Temu offers $100 off coupon code {aci789589} for first-time users. You can get a $100 bonus plus 100% off any purchase at Temu with the $100 Coupon Bundle if you sign up with the referral code [aci789589] and make a first purchase of $100 or more. If you are who wish to join Temu, then you should use this exclusive Temu coupon code $100 off (aci789589) and get $100 off on your purchase with Temu. You can get a $100 discount with Temu coupon code {aci789589}. This exclusive offer is for existing customers and can be used for a $100 reduction on your total purchase. Enter coupon code {aci789589} at checkout to avail of the discount. You can use the code {aci789589} to get a $100 off Temu coupon as a new customer. Apply this Temu coupon code $100 off (aci789589) to get a $100 discount on your shopping with Temu. If you’re a first-time user and looking for a Temu coupon code $100 first time user(aci789589) then using this code will give you a flat $100 Off and a 90% discount on your Temu shopping. Temu $100% Off Coupon Code "aci789589" will save you $100 on your order. To get a discount, click on the item to purchase and enter the code. Temu coupon code$100off-{aci789589} Temu coupon code -{aci789589} Temu coupon code$50 off-{aci789589} Temu Coupon code [aci789589] for existing users can get up to 50% discount on product during checkout. Temu Coupon Codes for Existing Customers-aci789589 Temu values its loyal customers and offers various promo codes, including the Legit Temu Coupon Code (aci789589]) or (aci789589), which existing users can use. This ensures that repeat shoppers can also benefit from significant discounts on their purchases. Keep an eye out for special promotions and offers that are periodically available to enhance your shopping experience.
    • New users at Temu receive a $100 discount on orders over $100 Use the code [aci789589] during checkout to get Temu Coupon Code $100 off For New Users. Yes, Temu offers $100 off coupon code “aci789589” for first-time users. Temu 100% Off coupon code "aci789589" will save you $100 on your order. To get a discount, click on the item to purchase and enter the code. Yes, Temu offers $100 off coupon code “aci789589” for first-time users. You can get a$100 bonus plus 30% off any purchase at Temu with the$100 Coupon Bundle at Temu if you sign up with the referral code [aci789589] and make a first purchase of$50 or more. The Temu $100 Off coupon code (aci789589) will save you $100 on your order. To get a discount, click on the item to purchase and enter the code. Yes Temu offers $100 Off Coupon Code “aci789589” for First Time Users. Yes, Temu offers $100 off coupon code {aci789589} for first-time users. You can get a $100 bonus plus 100% off any purchase at Temu with the $100 Coupon Bundle if you sign up with the referral code [aci789589] and make a first purchase of $100 or more. If you are who wish to join Temu, then you should use this exclusive Temu coupon code $100 off (aci789589) and get $100 off on your purchase with Temu. You can get a $100 discount with Temu coupon code {aci789589}. This exclusive offer is for existing customers and can be used for a $100 reduction on your total purchase. Enter coupon code {aci789589} at checkout to avail of the discount. You can use the code {aci789589} to get a $100 off Temu coupon as a new customer. Apply this Temu coupon code $100 off (aci789589) to get a $100 discount on your shopping with Temu. If you’re a first-time user and looking for a Temu coupon code $100 first time user(aci789589) then using this code will give you a flat $100 Off and a 90% discount on your Temu shopping. Temu $100% Off Coupon Code "aci789589" will save you $100 on your order. To get a discount, click on the item to purchase and enter the code. Temu coupon code$100off-{aci789589} Temu coupon code -{aci789589} Temu coupon code$50 off-{aci789589} Temu Coupon code [aci789589] for existing users can get up to 50% discount on product during checkout. Temu Coupon Codes for Existing Customers-aci789589 Temu values its loyal customers and offers various promo codes, including the Legit Temu Coupon Code (aci789589]) or (aci789589), which existing users can use. This ensures that repeat shoppers can also benefit from significant discounts on their purchases. Keep an eye out for special promotions and offers that are periodically available to enhance your shopping experience.
    • Try other builds: https://www.curseforge.com/minecraft/mc-mods/geckolib/files/all?page=1&pageSize=20&version=1.16.5&gameVersionTypeId=1 Currently, you are using build 96 - the latest one that I linked is build 106 So try 97 to 105  
    • New users at Temu receive a $100 discount on orders over $100 Use the code [aci789589] during checkout to get Temu Coupon Code $100 off For New Users. Yes, Temu offers $100 off coupon code “aci789589” for first-time users. Temu 100% Off coupon code "aci789589" will save you $100 on your order. To get a discount, click on the item to purchase and enter the code. Yes, Temu offers $100 off coupon code “aci789589” for first-time users. You can get a$100 bonus plus 30% off any purchase at Temu with the$100 Coupon Bundle at Temu if you sign up with the referral code [aci789589] and make a first purchase of$50 or more. The Temu $100 Off coupon code (aci789589) will save you $100 on your order. To get a discount, click on the item to purchase and enter the code. Yes Temu offers $100 Off Coupon Code “aci789589” for First Time Users. Yes, Temu offers $100 off coupon code {aci789589} for first-time users. You can get a $100 bonus plus 100% off any purchase at Temu with the $100 Coupon Bundle if you sign up with the referral code [aci789589] and make a first purchase of $100 or more. If you are who wish to join Temu, then you should use this exclusive Temu coupon code $100 off (aci789589) and get $100 off on your purchase with Temu. You can get a $100 discount with Temu coupon code {aci789589}. This exclusive offer is for existing customers and can be used for a $100 reduction on your total purchase. Enter coupon code {aci789589} at checkout to avail of the discount. You can use the code {aci789589} to get a $100 off Temu coupon as a new customer. Apply this Temu coupon code $100 off (aci789589) to get a $100 discount on your shopping with Temu. If you’re a first-time user and looking for a Temu coupon code $100 first time user(aci789589) then using this code will give you a flat $100 Off and a 90% discount on your Temu shopping. Temu $100% Off Coupon Code "aci789589" will save you $100 on your order. To get a discount, click on the item to purchase and enter the code. Temu coupon code$100off-{aci789589} Temu coupon code -{aci789589} Temu coupon code$50 off-{aci789589} Temu Coupon code [aci789589] for existing users can get up to 50% discount on product during checkout. Temu Coupon Codes for Existing Customers-aci789589 Temu values its loyal customers and offers various promo codes, including the Legit Temu Coupon Code (aci789589]) or (aci789589), which existing users can use. This ensures that repeat shoppers can also benefit from significant discounts on their purchases. Keep an eye out for special promotions and offers that are periodically available to enhance your shopping experience.
  • Topics

  • Who's Online (See full list)

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.