Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

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

 

}

}

            }

}

            }

}

 

 

 

 

 

  • 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;

 

}

}

}

}

}

}

 

 

 

 

  • 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.

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

  • 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.

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

 

 

  • 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.

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...

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.