Jump to content

Recommended Posts

Posted

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

 

}

}

            }

}

            }

}

 

 

 

 

 

Posted

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;

 

}

}

}

}

}

}

 

 

 

 

Posted

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

Posted

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.

Posted

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

 

 

Posted

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.

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.