Skip to content

Commit

Permalink
Fix recoil
Browse files Browse the repository at this point in the history
  • Loading branch information
Jazzkuh committed May 22, 2024
1 parent 8fd14c4 commit 2e54c39
Show file tree
Hide file tree
Showing 9 changed files with 19 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public void fireAction(Player player, GunshellRayTraceResult rayTraceResult, Con

damage = PluginUtils.getInstance().applyProtectionModifier(damage, rayTraceResult.isHeadshot(), livingEntity);

PluginUtils.getInstance().performRecoil(livingEntity, player, 0F, this.getFireable().getKnockbackAmount());
PluginUtils.getInstance().playerKnockBack(livingEntity, player, this.getFireable().getKnockbackAmount());

if (damage > livingEntity.getHealth()) {
livingEntity.setHealth(0D);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,6 @@ public void fireAction(Player player, GunshellRayTraceResult rayTraceResult, Con
MessagesConfig.BULLET_HIT_OTHER.get(player,
new PlaceHolder("Name", livingEntity.getName()));

PluginUtils.getInstance().performRecoil(livingEntity, player, 0F, this.getFireable().getKnockbackAmount());
PluginUtils.getInstance().playerKnockBack(livingEntity, player, this.getFireable().getKnockbackAmount());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,6 @@ public void fireAction(Player player, GunshellRayTraceResult rayTraceResult, Con
MessagesConfig.BULLET_HIT_OTHER.get(player,
new PlaceHolder("Name", livingEntity.getName()));

PluginUtils.getInstance().performRecoil(livingEntity, player, 0F, this.getFireable().getKnockbackAmount());
PluginUtils.getInstance().playerKnockBack(livingEntity, player, this.getFireable().getKnockbackAmount());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public void fireAction(Player player, Location location, ConfigurationSection co
Effect.STEP_SOUND, Material.REDSTONE_BLOCK);
}

PluginUtils.getInstance().performRecoil(livingEntity, player, 0F, this.getThrowable().getKnockbackAmount());
PluginUtils.getInstance().playerKnockBack(livingEntity, player, this.getThrowable().getKnockbackAmount());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public void fireAction(Player player, Location location, ConfigurationSection co
Effect.STEP_SOUND, Material.REDSTONE_BLOCK);
}

PluginUtils.getInstance().performRecoil(livingEntity, player, 0F, this.getThrowable().getKnockbackAmount());
PluginUtils.getInstance().playerKnockBack(livingEntity, player, this.getThrowable().getKnockbackAmount());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public void fireAction(Player player, Location location, ConfigurationSection co
Effect.STEP_SOUND, Material.REDSTONE_BLOCK);
}

PluginUtils.getInstance().performRecoil(livingEntity, player, 0F, this.getThrowable().getKnockbackAmount());
PluginUtils.getInstance().playerKnockBack(livingEntity, player, this.getThrowable().getKnockbackAmount());

if (damage > livingEntity.getHealth()) {
livingEntity.setHealth(0D);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public void fireAction(Player player, Location location, ConfigurationSection co
Effect.STEP_SOUND, Material.REDSTONE_BLOCK);
}

PluginUtils.getInstance().performRecoil(livingEntity, player, 0F, this.getThrowable().getKnockbackAmount());
PluginUtils.getInstance().playerKnockBack(livingEntity, player, this.getThrowable().getKnockbackAmount());
livingEntity.addPotionEffect(XPotion.BLINDNESS.buildPotionEffect(duration, amplifier));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,8 @@ public void onFireablePreFire(FireablePreFireEvent event) {
return;
}

PluginUtils.getInstance().recoil(player, (float) fireable.getRecoilAmount(), fireable.getSelfKnockbackAmount());

FireableDamageEvent fireableDamageEvent = new FireableDamageEvent(player, rayTraceResult, fireable);
Bukkit.getPluginManager().callEvent(fireableDamageEvent);
if (fireableDamageEvent.isCancelled()) return;
Expand Down
16 changes: 10 additions & 6 deletions src/main/java/com/jazzkuh/gunshell/utils/PluginUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,20 +83,24 @@ public Location getRightHandLocation(Player player) {
return new Location(player.getWorld(), x, y, z);
}

public void performRecoil(LivingEntity livingEntity, Player player, float pitchIncrement, double knockback) {
Location location = livingEntity.getLocation();
public void recoil(Player player, float pitchIncrement, double knockback) {
Location location = player.getLocation();
if (pitchIncrement > 0) {
float pitch = location.getPitch();
location.setPitch(pitch - pitchIncrement);

// Use a cause other than PLUGIN or COMMAND because essentials sucks lol.
Vector playerVelocity = livingEntity.getVelocity();
livingEntity.teleport(location, PlayerTeleportEvent.TeleportCause.UNKNOWN);
livingEntity.setVelocity(playerVelocity);
Vector playerVelocity = player.getVelocity();
player.teleport(location, PlayerTeleportEvent.TeleportCause.UNKNOWN);
player.setVelocity(playerVelocity);
}

KnockbackUtils.applySelfKnockback(player, knockback);
}

public void playerKnockBack(LivingEntity livingEntity, Player player, double knockback) {
// Apply knockback
double finalKnockback = ( knockback * 10 ) / 2;
double finalKnockback = (knockback * 10) / 2;
KnockbackUtils.applyKnockBack(livingEntity, player, finalKnockback);
}
}

0 comments on commit 2e54c39

Please sign in to comment.