Jump to content

[1.7.10][SOLVED] EventHandler not working; Enchantment


Recommended Posts

Guest Abrynos
Posted

Hey guys... I made an enchantment, that should port the hit enemy randomly somewhere around... to do this i made a EventHandler; The problem is, that it doesn't work. Here's my EventHandler:

public class SwordHandler {
@SubscribeEvent
public void hurtEvent(LivingHurtEvent event){
	if(event.source.getEntity() != null){
		if(event.source.getEntity() instanceof EntityPlayer){
			EntityPlayer player = (EntityPlayer)event.source.getEntity();
			ItemStack stack = player.getCurrentEquippedItem();
			NBTTagList ench = stack.getEnchantmentTagList();
			boolean port = false;
	        
			if (ench != null){
				for (int x = 0; x < ench.tagCount(); x++){
					NBTTagCompound nbt = (NBTTagCompound)(ench).getCompoundTagAt(x);
					int id = nbt.getInteger("id");
					if (id == EnchantmentAutosmelt.effectid){
						port = true;
					}
				}
			}

			if(port){
				long seed = ((int)event.entity.posX + (int)Math.random() & 255 + (int)Math.random()) | ((int)event.entity.posY + (int)Math.random()  & 255 + (int)Math.random() << 8 + (int)Math.random()) | event.entity.getEntityId() + (int)event.entity.posZ + (int)Math.random() << 16 + (int)Math.random();
				Random random = new Random(seed);

				int k = random.nextInt(4);
				int plusX = random.nextInt(6);
				int pluxY = random.nextInt(4);
				int plusZ = random.nextInt(6);

				if(k == 0){
					event.entity.setPosition(event.entity.posX + plusX, event.entity.posY + 3 + pluxY, event.entity.posZ + plusZ);
				}
				else if(k == 1){
					event.entity.setPosition(event.entity.posX + plusX, event.entity.posY + 3 + pluxY, event.entity.posZ - plusZ);
				}
				else if(k == 2){
					event.entity.setPosition(event.entity.posX - plusX, event.entity.posY + 3 + pluxY, event.entity.posZ + plusZ);
				}
				else if(k == 3){
					event.entity.setPosition(event.entity.posX - plusX, event.entity.posY + 3 + pluxY, event.entity.posZ - plusZ);
				}
			}
		}	
	}
}	
}

I would be happy, if you could help me finding out, what's the problem with it.

 

Thanks in advance,

Abrynos

Posted

Let me guess:

if you punch a zombie, it crashes.

 

ItemStack stack = player.getCurrentEquippedItem();
//what value does stack contain when you punch a zombie?
//what value does stack contain when a zombie punches you?
NBTTagList ench = stack.getEnchantmentTagList();

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.

Guest Abrynos
Posted

It doesn't crash... It just doesn't port the zombie

 

If i punch a zombie it contains the ItemStack, that I'm holding in my hand; if a zombie punches me it doesn't contain a value, 'cuz a zombie isn't

instanceof EntityPlayer

Posted

Actually, I bet it would crash.  You didn't check to make sure that the item in the currently equipped slot isn't null, and then you access its stackNBT.

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.

Guest Abrynos
Posted

now i checked it:

package com.Abrynos.Handler;

import java.util.Random;

import com.Abrynos.Enchantments.EnchantmentAutosmelt;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraftforge.event.entity.living.LivingHurtEvent;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;

public class SwordHandler {
@SubscribeEvent
public void hurtEvent(LivingHurtEvent event){
	if(event.source.getEntity() != null){
		if(event.source.getEntity() instanceof EntityPlayer){
			EntityPlayer player = (EntityPlayer)event.source.getEntity();
			ItemStack stack = player.getCurrentEquippedItem();
			NBTTagList ench = stack.getEnchantmentTagList();
			boolean port = false;
	        
			if(stack != null){
				if (ench != null){
					for (int x = 0; x < ench.tagCount(); x++){
						NBTTagCompound nbt = (NBTTagCompound)(ench).getCompoundTagAt(x);
						int id = nbt.getInteger("id");
						if (id == EnchantmentAutosmelt.effectid){
							port = true;
						}
					}
				}
			}
			if(port){
				long seed = ((int)event.entity.posX + (int)Math.random() & 255 + (int)Math.random()) | ((int)event.entity.posY + (int)Math.random()  & 255 + (int)Math.random() << 8 + (int)Math.random()) | event.entity.getEntityId() + (int)event.entity.posZ + (int)Math.random() << 16 + (int)Math.random();
				Random random = new Random(seed);

				int k = random.nextInt(4);
				int plusX = random.nextInt(6);
				int pluxY = random.nextInt(4);
				int plusZ = random.nextInt(6);

				if(k == 0){
					event.entity.setPosition(event.entity.posX + plusX, event.entity.posY + 3 + pluxY, event.entity.posZ + plusZ);
				}
				else if(k == 1){
					event.entity.setPosition(event.entity.posX + plusX, event.entity.posY + 3 + pluxY, event.entity.posZ - plusZ);
				}
				else if(k == 2){
					event.entity.setPosition(event.entity.posX - plusX, event.entity.posY + 3 + pluxY, event.entity.posZ + plusZ);
				}
				else if(k == 3){
					event.entity.setPosition(event.entity.posX - plusX, event.entity.posY + 3 + pluxY, event.entity.posZ - plusZ);
				}
			}
		}	
	}
}	
}

Posted

Checking the stack for null after you reference it isn't going to do anything useful.

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.

Guest Abrynos
Posted

now it looks like this:

 

ItemStack stack = player.getCurrentEquippedItem();
			if(stack != null){
				NBTTagList ench = stack.getEnchantmentTagList();
				if (ench != null){
					for (int x = 0; x < ench.tagCount(); x++){
						NBTTagCompound nbt = (NBTTagCompound)(ench).getCompoundTagAt(x);
						int id = nbt.getInteger("id");
						if (id == EnchantmentAutosmelt.effectid){
							port = true;
						}
					}
				}
			}

 

still doesn't work

Posted

Have you tried printing out what the IDs on the item are and comparing them to the ID you're looking for?

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.

Guest Abrynos
Posted

Haha got it; just missed to change a classname, 'cuz i copied part of it from another handler (autosmelting-enchantment) :/

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



×
×
  • Create New...

Important Information

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