Posted October 5, 201411 yr SOLVED CODE: @SubscribeEvent public void onPlayerHurt (LivingHurtEvent event) { if (event.entity instanceof EntityPlayer && event.ammount > 0) { EntityPlayer player = (EntityPlayer) event.entity; Entity mobEntity = event.source.getSourceOfDamage(); String mobString = event.source.getDamageType(); if (mobString == "explosion.player" || mobString == "fireball" || mobString == "thrown" ) { //System.out.println("if statement fired"); if (player.isUsingItem() == true) { useItem3 = player.getCurrentEquippedItem(); k = useItem3.getItemDamage(); if (useItem3 != null && useItem3.isItemEqual(new ItemStack(ModItems.woodenShield, OreDictionary.WILDCARD_VALUE, k)) ) { event.entityLiving.addPotionEffect(new PotionEffect(Potion.resistance.id, 120, 0, true)); //System.out.println("Shield activated"); if (event.source.isExplosion() == true && rand.nextInt(5) == 1) { --useItem3.stackSize; } } } } if (mobString == "mob" || mobString == "player" || mobString == "arrow" ) { Vec3 vec3 = player.getLook(1.0F).normalize(); Vec3 vec31 = Vec3.createVectorHelper(mobEntity.posX - player.posX, mobEntity.boundingBox.minY + (double)(mobEntity.height / 2.0F) - (player.posY + (double)player.getEyeHeight()), mobEntity.posZ - player.posZ); double d0 = vec31.lengthVector(); vec31 = vec31.normalize(); double d1 = vec3.dotProduct(vec31); double d2 = 1.0D - 0.025D / d0; if (d1 + 0.3 > d2) { if (player.isUsingItem() == true) { useItem3 = player.getCurrentEquippedItem(); k = useItem3.getItemDamage(); if (useItem3 != null && useItem3.isItemEqual(new ItemStack(ModItems.woodenShield, OreDictionary.WILDCARD_VALUE, k)) ) { event.entityLiving.addPotionEffect(new PotionEffect(Potion.resistance.id, 120, 0, true)); //System.out.println("Shield activated"); } } } } } }
October 5, 201411 yr Check the class of the entity. If you want to only affect vanilla entities then check the class itself, otherwise just use instanceof. BEFORE ASKING FOR HELP READ THE EAQ! I'll help if I can. Apologies if I do something obviously stupid. If you don't know basic Java yet, go and follow these tutorials.
October 6, 201411 yr Author Ok i got the damage check, im reading in the console a position for the mob. Is there a way i can get the direction a player is facing and relate it to the mobs coordinates? also can i get the mobs coordinates? @SubscribeEvent public void onPlayerHurt (LivingHurtEvent event) { if (event.entity instanceof EntityPlayer && event.ammount > 0) { EntityPlayer player = (EntityPlayer) event.entity; Entity mobEntity = event.source.getSourceOfDamage(); String mobString = event.source.getDamageType(); //System.out.println(mobEntity); //System.out.println(mobString); if (mobString == "mob" || mobString == "explosion.player" || mobString == "explosion.player" || mobString == "player" || mobString == "fireball" || mobString == "thrown") { //System.out.println("if statement fired"); if (player.isUsingItem() == true) { useItem3 = player.getCurrentEquippedItem(); k = useItem3.getItemDamage(); if (useItem3 != null && useItem3.isItemEqual(new ItemStack(ModItems.woodenShield, OreDictionary.WILDCARD_VALUE, k)) ) { event.entityLiving.addPotionEffect(new PotionEffect(Potion.resistance.id, 60, 0, true)); //System.out.println("Shield activated"); if (event.source.isExplosion() == true && rand.nextInt(5) == 1) { --useItem3.stackSize; } } } } } }
October 6, 201411 yr Author Good Morning, Still haven't made any further progress on this, if u have idea or suggestion for determining general player look direction plz let me know.
October 6, 201411 yr Hi I think this is the code that the enderman uses to tell whether the player is looking at the enderman. Should be some useful clues in there. /** * Checks to see if this enderman should be attacking this player */ private boolean shouldAttackPlayer(EntityPlayer p_70821_1_) { ItemStack itemstack = p_70821_1_.inventory.armorInventory[3]; if (itemstack != null && itemstack.getItem() == Item.getItemFromBlock(Blocks.pumpkin)) { return false; } else { Vec3 vec3 = p_70821_1_.getLook(1.0F).normalize(); Vec3 vec31 = Vec3.createVectorHelper(this.posX - p_70821_1_.posX, this.boundingBox.minY + (double)(this.height / 2.0F) - (p_70821_1_.posY + (double)p_70821_1_.getEyeHeight()), this.posZ - p_70821_1_.posZ); double d0 = vec31.lengthVector(); vec31 = vec31.normalize(); double d1 = vec3.dotProduct(vec31); return d1 > 1.0D - 0.025D / d0 && p_70821_1_.canEntityBeSeen(this); } } -TGG
October 6, 201411 yr Author indeed, i looked at this. The problem is that this needs direct line of sight while i require general direction, maybe i could make it a range based on where the entity is looking? Gonna give something like that a try.
October 6, 201411 yr Hi delete this part p_70821_1_.canEntityBeSeen(this); and I think it will probably do exactly what you want. This code takes two vectors: vec3 = the player look vector, normalised vec31 = the vector for the entity position relative to the player position it calculates the dot product between the two, which is related to the angle between the two (actually, the cosine of the angle) http://www.mathsisfun.com/algebra/vectors-dot-product.html If the cosine is greater than 1 - 0.025/(distance between player and entity), then the player is looking more or less straight at the entity. the canEntityBeSeen part just checks for blocks in the way, so you don't want that. -TGG
October 6, 201411 yr Author SOLVED @SubscribeEvent public void onPlayerHurt (LivingHurtEvent event) { if (event.entity instanceof EntityPlayer && event.ammount > 0) { EntityPlayer player = (EntityPlayer) event.entity; Entity mobEntity = event.source.getSourceOfDamage(); String mobString = event.source.getDamageType(); if (mobString == "explosion.player" || mobString == "fireball" || mobString == "thrown" ) { //System.out.println("if statement fired"); if (player.isUsingItem() == true) { useItem3 = player.getCurrentEquippedItem(); k = useItem3.getItemDamage(); if (useItem3 != null && useItem3.isItemEqual(new ItemStack(ModItems.woodenShield, OreDictionary.WILDCARD_VALUE, k)) ) { event.entityLiving.addPotionEffect(new PotionEffect(Potion.resistance.id, 120, 0, true)); //System.out.println("Shield activated"); if (event.source.isExplosion() == true && rand.nextInt(5) == 1) { --useItem3.stackSize; } } } } if (mobString == "mob" || mobString == "player" || mobString == "arrow" ) { Vec3 vec3 = player.getLook(1.0F).normalize(); Vec3 vec31 = Vec3.createVectorHelper(mobEntity.posX - player.posX, mobEntity.boundingBox.minY + (double)(mobEntity.height / 2.0F) - (player.posY + (double)player.getEyeHeight()), mobEntity.posZ - player.posZ); double d0 = vec31.lengthVector(); vec31 = vec31.normalize(); double d1 = vec3.dotProduct(vec31); double d2 = 1.0D - 0.025D / d0; if (d1 + 0.3 > d2) { if (player.isUsingItem() == true) { useItem3 = player.getCurrentEquippedItem(); k = useItem3.getItemDamage(); if (useItem3 != null && useItem3.isItemEqual(new ItemStack(ModItems.woodenShield, OreDictionary.WILDCARD_VALUE, k)) ) { event.entityLiving.addPotionEffect(new PotionEffect(Potion.resistance.id, 120, 0, true)); //System.out.println("Shield activated"); } } } } } }
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.