Created Precision

This commit is contained in:
Auxilor
2020-11-21 12:20:53 +00:00
parent e3c1345672
commit 5e5b0e8a62
8 changed files with 170 additions and 4 deletions

View File

@@ -1,9 +1,8 @@
package com.willfp.ecoenchants.firewand;
package com.willfp.ecoenchants.precision;
import com.willfp.ecoenchants.EcoEnchantsPlugin;
import com.willfp.ecoenchants.enchantments.EcoEnchants;
import com.willfp.ecoenchants.enchantments.itemtypes.Spell;
import org.bukkit.entity.Fireball;
import org.bukkit.entity.Player;
import org.bukkit.entity.SmallFireball;
import org.bukkit.event.EventHandler;

View File

@@ -1,4 +1,4 @@
package com.willfp.ecoenchants.firewand;
package com.willfp.ecoenchants.precision;
import com.willfp.ecoenchants.EcoEnchantsPlugin;
import com.willfp.ecoenchants.enchantments.EcoEnchant;

View File

@@ -0,0 +1,14 @@
dependencies {
compileOnly 'org.spigotmc:spigot-api:1.15.2-R0.1-SNAPSHOT'
compileOnly project(':plugin')
}
jar{
archiveFileName = project.name + " Extension" + ".jar"
}
description = 'Precision'
tasks.withType(Jar) {
destinationDirectory = file("$rootDir/bin/")
}

View File

@@ -0,0 +1,100 @@
package com.willfp.ecoenchants.precision;
import com.willfp.ecoenchants.EcoEnchantsPlugin;
import com.willfp.ecoenchants.enchantments.EcoEnchant;
import com.willfp.ecoenchants.enchantments.EcoEnchants;
import com.willfp.ecoenchants.enchantments.util.EnchantChecks;
import com.willfp.ecoenchants.nms.TridentStack;
import com.willfp.ecoenchants.util.NumberUtils;
import org.bukkit.Bukkit;
import org.bukkit.GameMode;
import org.bukkit.entity.Enderman;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.entity.Trident;
import org.bukkit.event.EventHandler;
import org.bukkit.event.entity.ProjectileLaunchEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.util.Vector;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
@SuppressWarnings("unchecked")
public final class Precision extends EcoEnchant {
public Precision() {
super(
"precision", EnchantmentType.SPECIAL
);
}
// START OF LISTENERS
@EventHandler
public void aimingLaunch(ProjectileLaunchEvent event) {
if (!(event.getEntity().getShooter() instanceof Player))
return;
if(!(event.getEntity() instanceof Trident))
return;
if(event.isCancelled()) return;
Player player = (Player) event.getEntity().getShooter();
Trident trident = (Trident) event.getEntity();
ItemStack itemStack = TridentStack.getTridentStack(trident);
if (!EnchantChecks.item(itemStack, this)) return;
int level = EnchantChecks.getMainhandLevel(player, this);
double multiplier = this.getConfig().getDouble(EcoEnchants.CONFIG_LOCATION + "distance-per-level");
final double finalDistance = level * multiplier;
Runnable runnable = new BukkitRunnable() {
@Override
public void run() {
List<LivingEntity> nearbyEntities = (List<LivingEntity>)(List<?>) Arrays.asList(trident.getNearbyEntities(finalDistance, finalDistance, finalDistance).stream()
.filter(entity -> entity instanceof LivingEntity)
.filter(entity -> !entity.equals(player))
.filter(entity -> !(entity instanceof Enderman))
.filter(entity -> {
if (entity instanceof Player) {
return ((Player) entity).getGameMode().equals(GameMode.SURVIVAL) || ((Player) entity).getGameMode().equals(GameMode.ADVENTURE);
}
return true;
}).toArray());
if(nearbyEntities.isEmpty()) return;
LivingEntity entity = nearbyEntities.get(0);
double distance = Double.MAX_VALUE;
for(LivingEntity livingEntity : nearbyEntities) {
double currentDistance = livingEntity.getLocation().distance(trident.getLocation());
if(currentDistance >= distance) continue;
distance = currentDistance;
entity = livingEntity;
}
if(entity != null) {
Vector vector = entity.getEyeLocation().toVector().clone().subtract(trident.getLocation().toVector()).normalize();
trident.setVelocity(vector);
}
}
};
final int period = this.getConfig().getInt(EcoEnchants.CONFIG_LOCATION + "check-ticks");
final int checks = this.getConfig().getInt(EcoEnchants.CONFIG_LOCATION + "checks-per-level") * level;
AtomicInteger checksPerformed = new AtomicInteger(0);
new BukkitRunnable() {
@Override
public void run() {
checksPerformed.addAndGet(1);
if(checksPerformed.get() > checks) this.cancel();
if(trident.isDead() || trident.isInBlock() || trident.isOnGround()) this.cancel();
Bukkit.getScheduler().runTask(EcoEnchantsPlugin.getInstance(), runnable);
}
}.runTaskTimer(EcoEnchantsPlugin.getInstance(), 3, period);
}
}

View File

@@ -0,0 +1,20 @@
package com.willfp.ecoenchants.precision;
import com.willfp.ecoenchants.EcoEnchantsPlugin;
import com.willfp.ecoenchants.enchantments.EcoEnchant;
import com.willfp.ecoenchants.extensions.Extension;
import org.bukkit.Bukkit;
public class PrecisionMain extends Extension {
public static final EcoEnchant PRECISION = new Precision();
@Override
public void onEnable() {
Bukkit.getPluginManager().registerEvents(PRECISION, EcoEnchantsPlugin.getInstance());
}
@Override
public void onDisable() {
}
}

View File

@@ -0,0 +1,25 @@
#
# Precision EcoEnchant
#
name: "Precision"
description: Tridents hone in on your target.
enabled: true
obtaining:
table: true
villager: true
loot: true
rarity: special
general-config:
targets:
- trident
grindstoneable: true
conflicts: []
maximum-level: 5
config:
distance-per-level: 4 # Distance to scan per level
check-ticks: 10 # Ticks between rotations
checks-per-level: 2 # Amount of times to rotate arrow per level (prevents "floating")

View File

@@ -0,0 +1,2 @@
name: Precision
main: com.willfp.ecoenchants.precision.PrecisionMain

View File

@@ -5,7 +5,6 @@ include('API')
include('v1_16_R1')
include('v1_16_R2')
include('v1_16_R3')
include('Firewand')
project(":v1_15_R1").projectDir = file('NMS/v1_15_R1')
project(":plugin").projectDir = file('Plugin')
@@ -13,4 +12,11 @@ project(":API").projectDir = file('NMS/API')
project(":v1_16_R1").projectDir = file('NMS/v1_16_R1')
project(":v1_16_R2").projectDir = file('NMS/v1_16_R2')
project(":v1_16_R3").projectDir = file('NMS/v1_16_R3')
// Extensions
include('Firewand')
project(":Firewand").projectDir = file('Extensions/Firewand')
include('Precision')
project(":Precision").projectDir = file('Extensions/Precision')