diff --git a/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/effects/Effects.java b/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/effects/Effects.java index 53af604..22cf46b 100644 --- a/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/effects/Effects.java +++ b/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/effects/Effects.java @@ -14,6 +14,7 @@ import com.willfp.ecoarmor.effects.effects.MeleeDamageMultiplier; import com.willfp.ecoarmor.effects.effects.RegenerationMultiplier; import com.willfp.ecoarmor.effects.effects.SpeedMutiplier; import com.willfp.ecoarmor.effects.effects.TridentDamageMultiplier; +import com.willfp.ecoarmor.effects.effects.WarpChance; import lombok.experimental.UtilityClass; import org.jetbrains.annotations.NotNull; @@ -37,6 +38,7 @@ public class Effects { public static final Effect SPEED_MULTIPLIER = new SpeedMutiplier(); public static final Effect EXPERIENCE_MULTIPLIER = new ExperienceMultiplier(); public static final Effect REGENERATION_MULTIPLIER = new RegenerationMultiplier(); + public static final Effect WARP_CHANCE = new WarpChance(); /** * Get effect matching name. diff --git a/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/effects/effects/WarpChance.java b/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/effects/effects/WarpChance.java new file mode 100644 index 0000000..c565640 --- /dev/null +++ b/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/effects/effects/WarpChance.java @@ -0,0 +1,58 @@ +package com.willfp.ecoarmor.effects.effects; + +import com.willfp.eco.util.NumberUtils; +import com.willfp.ecoarmor.effects.Effect; +import com.willfp.ecoarmor.sets.util.ArmorUtils; +import org.bukkit.Bukkit; +import org.bukkit.Location; +import org.bukkit.block.Block; +import org.bukkit.entity.LivingEntity; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.entity.EntityDamageByEntityEvent; +import org.bukkit.util.Vector; +import org.jetbrains.annotations.NotNull; + +public class WarpChance extends Effect { + public WarpChance() { + super("warp-chance"); + } + + @EventHandler + public void onDamage(@NotNull final EntityDamageByEntityEvent event) { + if (event.isCancelled()) { + return; + } + + if (!(event.getDamager() instanceof Player)) { + return; + } + + if (!(event.getEntity() instanceof LivingEntity)) { + return; + } + + Player player = (Player) event.getDamager(); + LivingEntity victim = (LivingEntity) event.getEntity(); + + double chance = ArmorUtils.getEffectStrength(player, this); + + if (NumberUtils.randFloat(0, 100) > chance) { + return; + } + + Vector between = victim.getLocation().subtract(player.getLocation()).toVector(); + Location behind = victim.getLocation().add(between); + + behind.setYaw(player.getLocation().getYaw() + 180); + + Block head = behind.add(0, 1.4, 0).getBlock(); + + if (!head.getType().isAir()) { + return; + } + + player.getLocation().setYaw(player.getLocation().getYaw() + 180); + player.teleport(behind); + } +}