Vectors from explosions?

Discussion in 'Spigot Plugin Development' started by WulfGamesYT, Jun 22, 2018.

  1. Hello, currently I have a feature where players can throw tnt which spawns a PrimedTNT Entity and throws it where the player is looking. However when the entity explodes I cancel it using the EntityExplodeEvent but you still get a massive amount of knockback. I would prefer the player to just take damage and only move via a vector a little bit instead of getting hit by the tnt and flying really far away from the explosion power.

    Currently I'm using:
    Code (Java):
        @EventHandler(ignoreCancelled = true)
        public void onEntityExplosion(EntityExplodeEvent e) {
            if((e.getEntityType() == EntityType.WITHER) || (e.getEntityType() == EntityType.PRIMED_TNT)
                    || (e.getEntityType() == EntityType.MINECART_TNT) || (e.getEntityType() == EntityType.CREEPER)) {
                double x = e.getEntity().getLocation().getX();
                double y = e.getEntity().getLocation().getY();
                double z = e.getEntity().getLocation().getZ();

                e.getEntity().getWorld().createExplosion(x, y, z, 1.0F, false, false);
                e.getWorld().playEffect(e.getLocation(), Effect.LARGE_SMOKE, 100);
    Also for the entity damage by entity event I'm using:
    Code (Java):
        @EventHandler(ignoreCancelled = true)
        public void onEntityDamageByTNT(EntityDamageByEntityEvent e) {
            if(e.getEntity() != null && e.getCause().equals(DamageCause.ENTITY_EXPLOSION)) {
                Bukkit.broadcastMessage("[Debug] " + e.getEntity().getName() + " taken damage from " + e.getCause().toString() + ", vector set to 0.");
                e.getEntity().setVelocity(new Vector(0, 0, 0));
  2. I never cancelled an EntityExplosionEvent event, so I guess that's normal players take knockback. If so, the only way to cancel it is by the way you used, setting manually to all players that took damage 0 velocity.
  3. I've used the setVelocity method but how would I set a power of 0 in the direction: 0, 0, 0 ?
  4. 0,0,0 will just resets his velocity, but I don't understand what you are actually trying to do, which direction are you talking about? The direction the player got knockbacked to?
  5. I want to decrease the power of the knockback basically @Zodiak :)
  6. If you use Paper there was a new event just added EntityKnockbackByEntityEvent. If you don't use Paper, I highly suggest making the switch. ;)
    • Useful Useful x 1
  7. If you want to decrease the power of the velocity just reduce the player's velocity by doing some simple maths, I think that should work and divide by two the amount of knockback the player will receive:

    Code (Java):
    Vector v = e.getEntity().getVelocity();
    e.getEntity().setVelocity(new Vector(v.getX() / 2, v.getY() / 2, v.getZ() / 2));
    • Useful Useful x 1
  8. Another option would be to delete the entity right before it explodes, then just add some particles and a sound effect.
    Simply grab where its location was, and calculate a vector based on that subtracted by the actual player's distance, and set it to its reciprocal.
    • Informative Informative x 1