Jump to content

[1.7.10][SOLVED] EventHandler not working; Enchantment


Guest Abrynos

Recommended Posts

Guest Abrynos

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

Guest Abrynos

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

Guest Abrynos

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

Guest Abrynos

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

Link to comment
Share on other sites

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.

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

    • Hi, I want to make a client-only mod, everything is ok, but when I use shaders, none of the textures rendered in RenderLevelStageEvent nor the crow entity model are rendered, I want them to be visible, because it's a horror themed mod Here is how i render the crow model in the CrowEntityRenderer<CrowEntity>, by the time i use this method, i know is not the right method but i don't think this is the cause of the problem, the renderType i'm using is entityCutout @Override public void render(CrowEntity p_entity, float entityYaw, float partialTick, PoseStack poseStack, MultiBufferSource bufferSource, int packedLight) { super.render(p_entity, entityYaw, partialTick, poseStack, bufferSource, packedLight); ClientEventHandler.getClient().crow.renderToBuffer(poseStack, bufferSource.getBuffer(ClientEventHandler.getClient().crow .renderType(TEXTURE)), packedLight, OverlayTexture.NO_OVERLAY, Utils.rgb(255, 255, 255)); } Here renderLevelStage @Override public void renderWorld(RenderLevelStageEvent e) { horrorEvents.draw(e); } Here is how i render every event public void draw(RenderLevelStageEvent e) { for (HorrorEvent event : currentHorrorEvents) { event.tick(e.getPartialTick()); event.draw(e); } } Here is how i render the crow model on the event @Override public void draw(RenderLevelStageEvent e) { if(e.getStage() == RenderLevelStageEvent.Stage.AFTER_ENTITIES) { float arcProgress = getArcProgress(0.25f); int alpha = (int) Mth.lerp(arcProgress, 0, 255); int packedLight = LevelRenderer.getLightColor(Minecraft.getInstance().level, blockPos); VertexConsumer builder = ClientEventHandler.bufferSource.getBuffer(crow); Crow<CreepyBirdHorrorEvent> model = ClientEventHandler .getClient().crow; model.setupAnim(this); RenderHelper.renderModelInWorld(model, position, offset, e.getCamera(), e.getPoseStack(), builder, packedLight, OverlayTexture.NO_OVERLAY, alpha); builder = ClientEventHandler.bufferSource.getBuffer(eyes); RenderHelper.renderModelInWorld(model, position, offset, e.getCamera(), e.getPoseStack(), builder, 15728880, OverlayTexture.NO_OVERLAY, alpha); } } How i render the model public static void renderModelInWorld(Model model, Vector3f pos, Vector3f offset, Camera camera, PoseStack matrix, VertexConsumer builder, int light, int overlay, int alpha) { matrix.pushPose(); Vec3 cameraPos = camera.getPosition(); double finalX = pos.x - cameraPos.x + offset.x; double finalY = pos.y - cameraPos.y + offset.y; double finalZ = pos.z - cameraPos.z + offset.z; matrix.pushPose(); matrix.translate(finalX, finalY, finalZ); matrix.mulPose(Axis.XP.rotationDegrees(180f)); model.renderToBuffer(matrix, builder, light, overlay, Utils .rgba(255, 255, 255, alpha)); matrix.popPose(); matrix.popPose(); } Thanks in advance
    • Here's the link: https://mclo.gs/7L5FibL Here's the link: https://mclo.gs/7L5FibL
    • Also the mod "Connector Extras" modifies Reach-entity-attributes and can cause fatal errors when combined with ValkrienSkies mod. Disable this mod and continue to use Syntra without it.
    • Hi everyone. I was trying modify the vanilla loot of the "short_grass" block, I would like it drops seeds and vegetal fiber (new item of my mod), but I don't found any guide or tutorial on internet. Somebody can help me?
    • On 1.20.1 use ValkrienSkies mod version 2.3.0 Beta 1. I had the same issues as you and it turns out the newer beta versions have tons of unresolved incompatibilities. If you change the version you will not be required to change the versions of eureka or any other additions unless prompted at startup. This will resolve Reach-entity-attributes error sound related error and cowardly errors.
  • Topics

  • Who's Online (See full list)

    • There are no registered users currently online
×
×
  • Create New...

Important Information

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