Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update fireball system #474

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.andrei1058.bedwars.listeners;

import com.andrei1058.bedwars.BedWars;
import com.andrei1058.bedwars.api.arena.GameState;
import com.andrei1058.bedwars.api.arena.IArena;
import com.andrei1058.bedwars.api.configuration.ConfigPath;
import com.andrei1058.bedwars.arena.Arena;
Expand All @@ -11,120 +13,170 @@
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.entity.ExplosionPrimeEvent;
import org.bukkit.event.entity.ProjectileHitEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.metadata.FixedMetadataValue;
import org.bukkit.projectiles.ProjectileSource;
import org.bukkit.util.Vector;

import java.util.*;

import static com.andrei1058.bedwars.BedWars.config;
import static com.andrei1058.bedwars.BedWars.getAPI;
import java.util.Collection;

public class FireballListener implements Listener {

private final double fireballExplosionSize;
private final double fireballExplosionSize, fireballHorizontal, fireballVertical;
private final double damageSelf, damageEnemy, damageTeammates;
private final double fireballSpeedMultiplier, fireballCooldown;
private final boolean fireballMakeFire;
private final double fireballHorizontal;
private final double fireballVertical;

private final double damageSelf;
private final double damageEnemy;
private final double damageTeammates;

public FireballListener() {
this.fireballExplosionSize = config.getYml().getDouble(ConfigPath.GENERAL_FIREBALL_EXPLOSION_SIZE);
this.fireballMakeFire = config.getYml().getBoolean(ConfigPath.GENERAL_FIREBALL_MAKE_FIRE);
this.fireballHorizontal = config.getYml().getDouble(ConfigPath.GENERAL_FIREBALL_KNOCKBACK_HORIZONTAL) * -1;
this.fireballVertical = config.getYml().getDouble(ConfigPath.GENERAL_FIREBALL_KNOCKBACK_VERTICAL);
this.fireballExplosionSize = BedWars.config.getYml().getDouble(ConfigPath.GENERAL_FIREBALL_EXPLOSION_SIZE);
this.fireballMakeFire = BedWars.config.getYml().getBoolean(ConfigPath.GENERAL_FIREBALL_MAKE_FIRE);
this.fireballHorizontal = BedWars.config.getYml().getDouble(ConfigPath.GENERAL_FIREBALL_KNOCKBACK_HORIZONTAL) * -1;
this.fireballVertical = BedWars.config.getYml().getDouble(ConfigPath.GENERAL_FIREBALL_KNOCKBACK_VERTICAL);
this.damageSelf = BedWars.config.getYml().getDouble(ConfigPath.GENERAL_FIREBALL_DAMAGE_SELF);
this.damageEnemy = BedWars.config.getYml().getDouble(ConfigPath.GENERAL_FIREBALL_DAMAGE_ENEMY);
this.damageTeammates = BedWars.config.getYml().getDouble(ConfigPath.GENERAL_FIREBALL_DAMAGE_TEAMMATES);
this.fireballSpeedMultiplier = BedWars.config.getYml().getDouble(ConfigPath.GENERAL_FIREBALL_SPEED_MULTIPLIER);
this.fireballCooldown = BedWars.config.getYml().getDouble(ConfigPath.GENERAL_FIREBALL_COOLDOWN);
}

this.damageSelf = config.getYml().getDouble(ConfigPath.GENERAL_FIREBALL_DAMAGE_SELF);
this.damageEnemy = config.getYml().getDouble(ConfigPath.GENERAL_FIREBALL_DAMAGE_ENEMY);
this.damageTeammates = config.getYml().getDouble(ConfigPath.GENERAL_FIREBALL_DAMAGE_TEAMMATES);
@EventHandler
public void onFireballInteract(PlayerInteractEvent e) {
Player player = e.getPlayer();
ItemStack handItem = e.getItem();
if (!(e.getAction() == Action.RIGHT_CLICK_BLOCK || e.getAction() == Action.RIGHT_CLICK_AIR)) {
return;
}
if (handItem == null) {
return;
}
IArena arena = Arena.getArenaByPlayer(player);
if (arena == null) {
return;
}
if (arena.getStatus() != GameState.playing) {
return;
}
if (handItem.getType() != BedWars.nms.materialFireball()) {
return;
}
e.setCancelled(true);
if (System.currentTimeMillis() - arena.getFireballCooldowns().getOrDefault(player.getUniqueId(), 0L) <= (fireballCooldown * 1000)) {
return;
}
arena.getFireballCooldowns().put(player.getUniqueId(), System.currentTimeMillis());
Fireball fireball = player.launchProjectile(Fireball.class);
Vector direction = player.getEyeLocation().getDirection();
fireball = BedWars.nms.setFireballDirection(fireball, direction);
fireball.setVelocity(fireball.getDirection().multiply(fireballSpeedMultiplier));
//fireball.setIsIncendiary(false); // apparently this on <12 makes the fireball not explode on hit. wtf bukkit?
fireball.setYield((float) fireballExplosionSize);
fireball.setMetadata("bw1058", new FixedMetadataValue(BedWars.plugin, "ceva"));
BedWars.nms.minusAmount(player, handItem, 1);
}

@EventHandler
public void fireballHit(ProjectileHitEvent e) {
if(!(e.getEntity() instanceof Fireball)) return;
if (!(e.getEntity() instanceof Fireball)) {
return;
}
Location location = e.getEntity().getLocation();

ProjectileSource projectileSource = e.getEntity().getShooter();
if(!(projectileSource instanceof Player)) return;
if (!(projectileSource instanceof Player)) {
return;
}
Player source = (Player) projectileSource;

IArena arena = Arena.getArenaByPlayer(source);

if (arena == null) {
return;
}
if (arena.getStatus() != GameState.playing) {
return;
}
Vector vector = location.toVector();

World world = location.getWorld();

assert world != null;
Collection<Entity> nearbyEntities = world
.getNearbyEntities(location, fireballExplosionSize, fireballExplosionSize, fireballExplosionSize);
for(Entity entity : nearbyEntities) {
if(!(entity instanceof Player)) continue;
if (world == null) {
return;
}
Collection<Entity> nearbyEntities = world.getNearbyEntities(location, fireballExplosionSize, fireballExplosionSize, fireballExplosionSize);
for (Entity entity : nearbyEntities) {
if (!(entity instanceof Player)) {
continue;
}
Player player = (Player) entity;
if(!getAPI().getArenaUtil().isPlaying(player)) continue;


if (!Arena.isInArena(player)) {
continue;
}
Vector playerVector = player.getLocation().toVector();
Vector normalizedVector = vector.subtract(playerVector).normalize();
Vector horizontalVector = normalizedVector.multiply(fireballHorizontal);
double y = normalizedVector.getY();
if(y < 0 ) y += 1.5;
if(y <= 0.5) {
if (y < 0) {
y += 1.5;
}
if (y <= 0.5) {
y = fireballVertical*1.5; // kb for not jumping
} else {
y = y*fireballVertical*1.5; // kb for jumping
}
player.setVelocity(horizontalVector.setY(y));

LastHit lh = LastHit.getLastHit(player);
if (lh != null) {
lh.setDamager(source);
lh.setTime(System.currentTimeMillis());
} else {
new LastHit(player, source, System.currentTimeMillis());
}

if(player.equals(source)) {
if(damageSelf > 0) {
if (player.equals(source)) {
if (damageSelf > 0) {
player.damage(damageSelf); // damage shooter
}
} else if(arena.getTeam(player).equals(arena.getTeam(source))) {
if(damageTeammates > 0) {
} else if (arena.getTeam(player).equals(arena.getTeam(source))) {
if (damageTeammates > 0) {
player.damage(damageTeammates); // damage teammates
}
} else {
if(damageEnemy > 0) {
if (damageEnemy > 0) {
player.damage(damageEnemy); // damage enemies
}
}
}
}


@EventHandler
public void fireballDirectHit(EntityDamageByEntityEvent e) {
if(!(e.getDamager() instanceof Fireball)) return;
if(!(e.getEntity() instanceof Player)) return;

if(Arena.getArenaByPlayer((Player) e.getEntity()) == null) return;

if (!(e.getDamager() instanceof Fireball)) {
return;
}
if (!(e.getEntity() instanceof Player)) {
return;
}
Player player = (Player) e.getEntity();
if (Arena.isInArena(player)) {
return;
}
e.setCancelled(true);
}

@EventHandler
public void fireballPrime(ExplosionPrimeEvent e) {
if(!(e.getEntity() instanceof Fireball)) return;
ProjectileSource shooter = ((Fireball)e.getEntity()).getShooter();
if(!(shooter instanceof Player)) return;
if (!(e.getEntity() instanceof Fireball)) {
return;
}
Fireball fireball = (Fireball) e.getEntity();
ProjectileSource shooter = fireball.getShooter();
if (!(shooter instanceof Player)) {
return;
}
Player player = (Player) shooter;

if(Arena.getArenaByPlayer(player) == null) return;

if (Arena.isInArena(player)) {
return;
}
e.setFire(fireballMakeFire);
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
import org.bukkit.block.Block;
import org.bukkit.block.Sign;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Fireball;
import org.bukkit.entity.ItemFrame;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
Expand All @@ -49,24 +48,12 @@
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.material.Openable;
import org.bukkit.metadata.FixedMetadataValue;
import org.bukkit.util.Vector;

import static com.andrei1058.bedwars.BedWars.*;
import static com.andrei1058.bedwars.api.language.Language.getMsg;

public class Interact implements Listener {

private final double fireballSpeedMultiplier;
private final double fireballCooldown;
private final float fireballExplosionSize;

public Interact() {
this.fireballSpeedMultiplier = config.getYml().getDouble(ConfigPath.GENERAL_FIREBALL_SPEED_MULTIPLIER);
this.fireballCooldown = config.getYml().getDouble(ConfigPath.GENERAL_FIREBALL_COOLDOWN);
this.fireballExplosionSize = (float) config.getYml().getDouble(ConfigPath.GENERAL_FIREBALL_EXPLOSION_SIZE);
}

@EventHandler
/* Handle custom items with commands on them */
public void onItemCommand(PlayerInteractEvent e) {
Expand Down Expand Up @@ -191,33 +178,6 @@ public void onInteract(PlayerInteractEvent e) {
}
}
}
//check hand
ItemStack inHand = e.getItem();
if (e.getAction() == Action.RIGHT_CLICK_BLOCK || e.getAction() == Action.RIGHT_CLICK_AIR) {
if (inHand == null) return;
IArena a = Arena.getArenaByPlayer(p);
if (a != null) {
if (a.isPlayer(p)) {
if (inHand.getType() == nms.materialFireball()) {

e.setCancelled(true);

if(System.currentTimeMillis() - a.getFireballCooldowns().getOrDefault(p.getUniqueId(), 0L) > (fireballCooldown*1000)) {
a.getFireballCooldowns().put(p.getUniqueId(), System.currentTimeMillis());
Fireball fb = p.launchProjectile(Fireball.class);
Vector direction = p.getEyeLocation().getDirection();
fb = nms.setFireballDirection(fb, direction);
fb.setVelocity(fb.getDirection().multiply(fireballSpeedMultiplier));
//fb.setIsIncendiary(false); // apparently this on <12 makes the fireball not explode on hit. wtf bukkit?
fb.setYield(fireballExplosionSize);
fb.setMetadata("bw1058", new FixedMetadataValue(plugin, "ceva"));
nms.minusAmount(p, inHand, 1);
}

}
}
}
}
}


Expand Down