M1kep Posted May 17, 2013 Posted May 17, 2013 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); } } } Quote
SanAndreaP Posted May 18, 2013 Posted May 18, 2013 (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) Quote 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 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.
M1kep Posted May 19, 2013 Author Posted May 19, 2013 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? Quote
SanAndreaP Posted May 19, 2013 Posted May 19, 2013 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 Quote 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 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.
M1kep Posted May 19, 2013 Author Posted May 19, 2013 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? Quote
M1kep Posted May 19, 2013 Author Posted May 19, 2013 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 Quote
SanAndreaP Posted May 19, 2013 Posted May 19, 2013 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... Quote 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 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.
M1kep Posted May 19, 2013 Author Posted May 19, 2013 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. Quote
Noah_Beech Posted May 19, 2013 Posted May 19, 2013 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? Quote This is the creator of the Rareores mod! Be sure to check it out at
M1kep Posted May 19, 2013 Author Posted May 19, 2013 I was following a tutorial when learning forge And ok ill look at what you have to say and see if i can figure it out. Quote
SanAndreaP Posted May 19, 2013 Posted May 19, 2013 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 Quote 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 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.
M1kep Posted May 19, 2013 Author Posted May 19, 2013 Ok, so to start off thank you 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 -- Head -- ---- Minecraft Crash Report ---- // Don't do that. Time: 5/19/13 3:35 PM Description: Unexpected error java.lang.NullPointerException at m1kep.foolscraft.common.EventHookContainerClass.buildVillage(EventHookContainerClass.java:16) at net.minecraftforge.event.ASMEventHandler_4_EventHookContainerClass_buildVillage_PlayerInteractEvent.invoke(.dynamic) at net.minecraftforge.event.ASMEventHandler.invoke(ASMEventHandler.java:35) at net.minecraftforge.event.EventBus.post(EventBus.java:103) at net.minecraftforge.event.ForgeEventFactory.onPlayerInteract(ForgeEventFactory.java:41) at net.minecraft.client.Minecraft.clickMouse(Minecraft.java:1314) at net.minecraft.client.Minecraft.runTick(Minecraft.java:1799) at net.minecraft.client.Minecraft.runGameLoop(Minecraft.java:834) at net.minecraft.client.Minecraft.run(Minecraft.java:759) at java.lang.Thread.run(Thread.java:662) A detailed walkthrough of the error, its code path and all known details is as follows: --------------------------------------------------------------------------------------- -- Head -- Stacktrace: at m1kep.foolscraft.common.EventHookContainerClass.buildVillage(EventHookContainerClass.java:16) at net.minecraftforge.event.ASMEventHandler_4_EventHookContainerClass_buildVillage_PlayerInteractEvent.invoke(.dynamic) at net.minecraftforge.event.ASMEventHandler.invoke(ASMEventHandler.java:35) at net.minecraftforge.event.EventBus.post(EventBus.java:103) at net.minecraftforge.event.ForgeEventFactory.onPlayerInteract(ForgeEventFactory.java:41) at net.minecraft.client.Minecraft.clickMouse(Minecraft.java:1314) -- Affected level -- Details: Level name: MpServer All players: 1 total; [EntityClientPlayerMP['Player159'/503, l='MpServer', x=205.69, y=5.62, z=1073.52]] Chunk stats: MultiplayerChunkCache: 441 Level seed: 0 Level generator: ID 01 - flat, ver 0. Features enabled: false Level generator options: Level spawn location: World: (182,4,1081), Chunk: (at 6,0,9 in 11,67; contains blocks 176,0,1072 to 191,255,1087), Region: (0,2; contains chunks 0,64 to 31,95, blocks 0,0,1024 to 511,255,1535) Level time: 19808 game time, 5187 day time Level dimension: 0 Level storage version: 0x00000 - Unknown? Level weather: Rain time: 0 (now: false), thunder time: 0 (now: false) Level game mode: Game mode: survival (ID 0). Hardcore: false. Cheats: false Forced entities: 7 total; [EntitySheep['Sheep'/55, l='MpServer', x=205.66, y=4.00, z=1060.19], EntityPig['Pig'/54, l='MpServer', x=184.25, y=4.00, z=1058.63], EntityClientPlayerMP['Player159'/503, l='MpServer', x=486.35, y=5.62, z=1617.20], EntitySheep['Sheep'/57, l='MpServer', x=201.84, y=4.00, z=1084.56], EntitySheep['Sheep'/56, l='MpServer', x=207.02, y=4.00, z=1070.76], EntityClientPlayerMP['Player159'/503, l='MpServer', x=205.69, y=5.62, z=1073.52], EntitySlime['Slime'/12107, l='MpServer', x=277.80, y=4.96, z=1143.62]] Retry entities: 0 total; [] Stacktrace: at net.minecraft.client.multiplayer.WorldClient.addWorldInfoToCrashReport(WorldClient.java:441) at net.minecraft.client.Minecraft.addGraphicsAndWorldToCrashReport(Minecraft.java:2413) at net.minecraft.client.Minecraft.run(Minecraft.java:782) at java.lang.Thread.run(Thread.java:662) -- System Details -- Details: Minecraft Version: 1.5.2 Operating System: Windows 7 (x86) version 6.1 Java Version: 1.6.0_43, Sun Microsystems Inc. Java VM Version: Java HotSpot(TM) Client VM (mixed mode), Sun Microsystems Inc. Memory: 817483440 bytes (779 MB) / 1070399488 bytes (1020 MB) up to 1070399488 bytes (1020 MB) JVM Flags: 3 total; -Xincgc -Xmx1024M -Xms1024M AABB Pool Size: 8551 (478856 bytes; 0 MB) allocated, 2 (112 bytes; 0 MB) used Suspicious classes: FML and Forge are installed IntCache: cache: 0, tcache: 0, allocated: 0, tallocated: 0 FML: MCP v7.51 FML v5.2.5.686 Minecraft Forge 7.8.0.686 4 mods loaded, 4 mods active mcp{7.44} [Minecraft Coder Pack] (minecraft.jar) Unloaded->Constructed->Pre-initialized->Initialized->Post-initialized->Available->Available->Available->Available FML{5.2.5.686} [Forge Mod Loader] (coremods) Unloaded->Constructed->Pre-initialized->Initialized->Post-initialized->Available->Available->Available->Available Forge{7.8.0.686} [Minecraft Forge] (coremods) Unloaded->Constructed->Pre-initialized->Initialized->Post-initialized->Available->Available->Available->Available FoolsCraft{0.0.0.1} [FoolsCraft] (bin) Unloaded->Constructed->Pre-initialized->Initialized->Post-initialized->Available->Available->Available->Available LWJGL: 2.4.2 OpenGL: ATI Radeon X1200 Series GL version 2.1.8545 Release, ATI Technologies Inc. Is Modded: Definitely; Client brand changed to 'fml,forge' Type: Client (map_client.txt) Texture Pack: Default Profiler Position: N/A (disabled) Vec3 Pool Size: 75 (4200 bytes; 0 MB) allocated, 17 (952 bytes; 0 MB) used Here is my Event Class: package m1kep.foolscraft.common; import net.minecraft.client.Minecraft; import net.minecraft.creativetab.CreativeTabs; 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 ev) { if(ev.entityPlayer.getItemInUse().isItemEqual(new ItemStack(FoolsCraftMain.DebugItem1))) { ev.entityPlayer.sendChatToPlayer("You are using debug item"); }else{ ev.entityPlayer.sendChatToPlayer("You are not using debug item"); } } } And my Main Class: 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 public final static Item DebugItem1 = new DebugItem1(5000, 1, [/spoiler] 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) { } } Quote
Noah_Beech Posted May 19, 2013 Posted May 19, 2013 Just a question, Using Minecraft.getMinecraft().thePlayer is okay if your using it just on the client right? (Like for renderings) Quote This is the creator of the Rareores mod! Be sure to check it out at
SanAndreaP Posted May 19, 2013 Posted May 19, 2013 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. Quote 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 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.
Recommended Posts
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.