From edd34d3d1158f43d8c078999226365bd91728721 Mon Sep 17 00:00:00 2001 From: Soper Date: Tue, 18 May 2021 14:39:34 -0400 Subject: [PATCH] shell class now handles explosion --- .../arty/event/Listener/MortarInteract.java | 21 ++---- src/xyz/soper/arty/item/Shell.java | 64 ++++++++++++++++--- 2 files changed, 63 insertions(+), 22 deletions(-) diff --git a/src/xyz/soper/arty/event/Listener/MortarInteract.java b/src/xyz/soper/arty/event/Listener/MortarInteract.java index a84bd3e..4479351 100644 --- a/src/xyz/soper/arty/event/Listener/MortarInteract.java +++ b/src/xyz/soper/arty/event/Listener/MortarInteract.java @@ -54,27 +54,20 @@ public class MortarInteract implements Listener { if(event.getEntity() instanceof Arrow && event.getEntity().getCustomName() != null && event.getEntity().getCustomName().equals("shell")){ + Shell shell = new Shell(event.getEntity()); if(event.getHitBlock() != null){ - /* TODO: This needs to have another accompanying explosion that does more player damage than physical damage. - * Maybe make this a function of arty.item.Shell? - */ - event.getHitBlock().getWorld().createExplosion( + shell.explodeShell( event.getHitBlock() .getLocation() - .add(.5, 0, .5) + .add(.5,0,.5) .add(event.getHitBlockFace() - .getDirection()), - 2.5F, - false, - true); + .getDirection() + ) + ); event.getEntity().remove(); } else if(event.getHitEntity() != null){ - event.getHitEntity().getWorld().createExplosion( - event.getHitEntity().getLocation(), - 2.5F, - false, - true); + shell.explodeShell(event.getHitEntity().getLocation()); event.getEntity().remove(); } } diff --git a/src/xyz/soper/arty/item/Shell.java b/src/xyz/soper/arty/item/Shell.java index ba32209..99623b4 100644 --- a/src/xyz/soper/arty/item/Shell.java +++ b/src/xyz/soper/arty/item/Shell.java @@ -1,8 +1,6 @@ package xyz.soper.arty.item; -import org.bukkit.ChatColor; -import org.bukkit.Material; -import org.bukkit.Nameable; +import org.bukkit.*; import org.bukkit.entity.Projectile; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.ItemMeta; @@ -69,7 +67,38 @@ public class Shell { */ public Shell(Projectile shell){ isFired = true; - + String shellName = shell.getCustomName(); + if(shellName != null && shellName.startsWith("shell-")){ + String shellStats = shellName.substring(6); + //PARSING NAME DATA + int commaCount = 0; + while(commaCount < 5){ + int lastCommaIndex = -1; + for(int i = 0; i < shellStats.length(); i++){ + if(shellStats.charAt(i) == ','){ + commaCount++; + switch(commaCount) { + case 1: //Parsing explosive power + explosivePower = Float.parseFloat(shellStats.substring(0, i)); + lastCommaIndex = i; + break; + case 2: //Parsing penetration value + penetration = Integer.parseInt(shellStats.substring(lastCommaIndex+1, i)); + lastCommaIndex = i; + break; + case 3: //Parsing incendiary type + incendiaryType = Boolean.parseBoolean(shellStats.substring(lastCommaIndex+1, i)); + lastCommaIndex = i; + break; + case 4: //Parsing base velocity and fail chance + baseVelocity = Double.parseDouble(shellStats.substring(lastCommaIndex+1, i)); + failChance = Double.parseDouble(shellStats.substring(i+1)); + break; + } + } + } + } //FINISHED NAME PARSING + } } /** @@ -78,11 +107,20 @@ public class Shell { * @return True if item is an arrow with a gray name "Shell" */ public static boolean isShell(ItemStack item){ - if(item.getType() == Material.ARROW + return item.getType() == Material.ARROW && item.hasItemMeta() && item.getItemMeta().hasDisplayName() - && item.getItemMeta().getDisplayName().equals(ChatColor.GRAY + "Shell")) return true; - return false; + && item.getItemMeta().getDisplayName().equals(ChatColor.GRAY + "Shell"); + } + + /** + * Checks if the projectile is a shell + * @param projectile Projectile to check against + * @return True if projectile has a name that starts with "shell" + */ + public static boolean isShell(Projectile projectile){ + return projectile.getCustomName() != null + && projectile.getCustomName().startsWith("shell"); } /** @@ -119,7 +157,17 @@ public class Shell { // return null; // } - public void explodeShell(){ + /** + * Creates an explosion at the specified location using this shell's parameters. + * @param location The location where the explosion will occur + */ + public void explodeShell(Location location){ + location.getWorld().createExplosion(location, explosivePower, incendiaryType, true); + float explosiveDamagePower = explosivePower; + if(explosivePower < 4.5){ + explosiveDamagePower = (float)((1/((-0.5*4.5)+4.5))*explosivePower*((-.5*explosivePower)+4.5)); + } + location.getWorld().createExplosion(location, explosiveDamagePower, false, false); } /**