From 09eecbb8eee1a72779d76503584fd8a7ccd8a188 Mon Sep 17 00:00:00 2001 From: C_Corp2002 Date: Fri, 5 Jan 2024 21:59:11 -0500 Subject: [PATCH] Updated Some stuff, Ported over my Changes Hi! --- .../alathra/siegeengines/SiegeEngines.java | 816 ++++---- .../command/SiegeEnginesCommand.java | 258 ++- .../siegeengines/listeners/ClickHandler.java | 1709 +++++++++-------- .../listeners/RotationHandler.java | 195 +- 4 files changed, 1500 insertions(+), 1478 deletions(-) diff --git a/src/main/java/com/github/alathra/siegeengines/SiegeEngines.java b/src/main/java/com/github/alathra/siegeengines/SiegeEngines.java index f66046b..0a56187 100644 --- a/src/main/java/com/github/alathra/siegeengines/SiegeEngines.java +++ b/src/main/java/com/github/alathra/siegeengines/SiegeEngines.java @@ -1,405 +1,411 @@ -package com.github.alathra.siegeengines; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.HashMap; -import java.util.List; -import java.util.Random; -import java.util.UUID; -import java.util.concurrent.TimeUnit; - -import com.github.alathra.siegeengines.command.CommandHandler; -import org.bukkit.Bukkit; -import org.bukkit.Location; -import org.bukkit.Material; -import org.bukkit.NamespacedKey; -import org.bukkit.Particle; -import org.bukkit.Sound; -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.ArmorStand; -import org.bukkit.entity.ArmorStand.LockType; -import org.bukkit.entity.Entity; -import org.bukkit.entity.EntityType; -import org.bukkit.entity.LivingEntity; -import org.bukkit.entity.Player; -import org.bukkit.event.EventHandler; -import org.bukkit.inventory.EquipmentSlot; -import org.bukkit.inventory.ItemStack; -import org.bukkit.inventory.meta.ItemMeta; -import org.bukkit.persistence.PersistentDataType; -import org.bukkit.plugin.Plugin; -import org.bukkit.plugin.java.JavaPlugin; -import org.bukkit.potion.PotionEffect; -import org.bukkit.potion.PotionEffectType; -import org.bukkit.metadata.MetadataValueAdapter; -import org.bukkit.metadata.FixedMetadataValue; - -//import com.palmergames.bukkit.towny.Towny; - -import com.github.alathra.siegeengines.listeners.ClickHandler; -import com.github.alathra.siegeengines.listeners.RotationHandler; -import com.github.alathra.siegeengines.projectile.EntityProjectile; -import com.github.alathra.siegeengines.projectile.ExplosiveProjectile; -import com.github.alathra.siegeengines.projectile.GunnersProjectile; -//import GunnersProjectiles.EntityProjectile; -//import GunnersProjectiles.GunnersProjectile; -//import GunnersProjectiles.ExplosiveProjectile; -//import GunnersProjectiles.PotionProjectile; - - -public class SiegeEngines extends JavaPlugin { - - public static SiegeEngines instance; - - public static Plugin plugin; - public static MetadataValueAdapter metadata; - public static Random random = new Random(); - private static String Path; - private CommandHandler commandHandler; - //public static Towny towny; - - - public static SiegeEngines getInstance() { - return instance; - } - - @Override - public void onLoad() { - plugin = this; - commandHandler = new CommandHandler(this); - commandHandler.onLoad(); - } - - @SuppressWarnings("deprecation") - @Override - public void onEnable() { - instance = this; - Path = this.getDataFolder().getAbsolutePath(); - //this.getCommand("SiegeEngines").setExecutor(new GunnersCommand()); // Fixed Start-up - getServer().getPluginManager().registerEvents(new RotationHandler(), this); - getServer().getPluginManager().registerEvents(new ClickHandler(), this); - //StorageManager.setup(Path, plugin); - equipment.clear(); - TrackedStands.clear(); - DefinedEquipment.clear(); - //AddDefaults(); - //HashMap projObj = new HashMap<>(); - //GunnerEquipment equip = CreateNewGun(null, null, null, null, null, null, projObj); - for (SiegeEquipment i : DefinedEquipment.values()) { - System.out.println("§eEnabled Weapon : " + i.EquipmentName); - System.out.println("§eWeapon Propellant/\"Fuel\" ItemStacks : " + i.FuelMaterial); - for (ItemStack proj : i.Projectiles.keySet()) { - System.out.println("§eWeapon Projectile ItemStacks : " + proj); - } - } - commandHandler.onEnable(); - } - - @SuppressWarnings("deprecation") - @Override - public void onDisable() { - commandHandler.onDisable(); - for (SiegeEquipment equip : equipment.values()) { - if (equip.Entity != null) { - ItemStack item = new ItemStack(Material.CARVED_PUMPKIN); - ItemMeta meta = item.getItemMeta(); - equip.AmmoHolder = new EquipmentMagazine(); - - meta.setCustomModelData(equip.ReadyModelNumber); - meta.setDisplayName("§e" + equip.EquipmentName + " Item"); - List Lore = new ArrayList(); - Lore.add("§ePlace as a block to spawn a " + equip.EquipmentName + " or put on an Armor Stand."); - meta.setLore(Lore); - item.setItemMeta(meta); - - ((LivingEntity) equip.Entity).getEquipment().setHelmet(item); - } - } - } - - public static FixedMetadataValue addMetaDataValue(Object value) { - return new FixedMetadataValue(Bukkit.getServer().getPluginManager().getPlugin("SiegeEngines"), value); - } - - public static SiegeEquipment CreateClone(Integer ModelId) { - try { - return DefinedEquipment.get(ModelId).clone(); - } catch (CloneNotSupportedException e) { - // TODO Auto-generated catch block - - } - return null; - } - - public static HashMap DefinedEquipment = new HashMap(); - - public static HashMap> TrackedStands = new HashMap>(); - - public static HashMap equipment = new HashMap(); - - public static String convertTime(long time) { - - long days = TimeUnit.MILLISECONDS.toDays(time); - time -= TimeUnit.DAYS.toMillis(days); - - long hours = TimeUnit.MILLISECONDS.toHours(time); - time -= TimeUnit.HOURS.toMillis(hours); - - long minutes = TimeUnit.MILLISECONDS.toMinutes(time); - time -= TimeUnit.MINUTES.toMillis(minutes); - - long seconds = TimeUnit.MILLISECONDS.toSeconds(time); - String timeLeftFormatted = String.format("§e" + minutes + " Minutes " + seconds + " Seconds§f"); - - return timeLeftFormatted; - } - - public static SiegeEquipment CreateNewGun(String name, Integer XOffset, Integer YOffset, Integer fuelMax, Float fuelVelocityMod, Integer customModelId, HashMap projObj) { - SiegeEquipment equip = new SiegeEquipment(); - if (customModelId == null || customModelId == 0) - customModelId = 150; - if (name == null) - name = "Light Turret"; - if (XOffset == null) - XOffset = 0; - if (YOffset == null) - YOffset = 0; - if (fuelMax == null) - fuelMax = 0; - if (fuelVelocityMod == null) - fuelVelocityMod = 0.95f; - equip.EquipmentName = name; - equip.EquipmentId = equip.EquipmentName.replaceAll(" ", "_").toLowerCase(); - equip.XOffset = XOffset; - equip.YOffset = YOffset; - equip.RotateStandHead = true; - equip.RotateSideways = true; - equip.PlacementOffsetY = -1.125f; - equip.ReadyModelNumber = customModelId; - equip.ModelNumberToFireAt = customModelId; - equip.FiringModelNumbers = new ArrayList(); - equip.VelocityPerFuel = fuelVelocityMod; - equip.FuelMaterial = new ItemStack(Material.GUNPOWDER); - equip.CycleThroughModelsBeforeFiring = false; - equip.AllowInvisibleStand = false; - equip.MaxFuel = fuelMax; - equip.shotAmount = 1; - equip.AmmoHolder.LoadedFuel = fuelMax; - EntityProjectile defaultProj = new EntityProjectile(); - defaultProj.EntityCount = 2; - defaultProj.EntityTyp = EntityType.SHULKER_BULLET; - defaultProj.ParticleType = Particle.WHITE_ASH; - defaultProj.SoundType = Sound.ENTITY_BLAZE_SHOOT; - if (projObj.keySet().isEmpty() || projObj.isEmpty()) - projObj.put(new ItemStack(Material.GUNPOWDER), defaultProj); - for (ItemStack mat : projObj.keySet()) { - equip.Projectiles.put(mat, projObj.get(mat)); - } - DefinedEquipment.put(equip.ReadyModelNumber, equip); - return equip; - } - - public static void AddDefaults() { - SiegeEquipment equip = new SiegeEquipment(); - equip.EquipmentName = "Trebuchet"; - equip.XOffset = 5; - equip.YOffset = 5; - equip.VelocityPerFuel = 0.3f; - ExplosiveProjectile proj = new ExplosiveProjectile(); - proj.ExplodePower = 2; - equip.shotAmount = 1; - equip.RotateStandHead = false; - equip.RotateSideways = true; - equip.FuelMaterial = new ItemStack(Material.STRING); - equip.Projectiles.put(new ItemStack(Material.COBBLESTONE), proj); - equip.ReadyModelNumber = 122; - equip.ModelNumberToFireAt = 135; - equip.MillisecondsBetweenFiringStages = 2; - equip.MillisecondsBetweenReloadingStages = 10; - equip.FiringModelNumbers = new ArrayList<>(Arrays.asList( - 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139 - )); - equip.CycleThroughModelsBeforeFiring = true; - DefinedEquipment.put(equip.ReadyModelNumber, equip); - - equip = new SiegeEquipment(); - equip.EquipmentName = "Naval Cannon"; - equip.Projectiles.put(new ItemStack(Material.COBBLESTONE), proj); - equip.PlacementOffsetY = -1; - equip.shotAmount = 1; - equip.ReadyModelNumber = 142; - equip.ModelNumberToFireAt = 142; - equip.FiringModelNumbers = new ArrayList(); - equip.RotateStandHead = true; - equip.RotateSideways = false; - proj = new ExplosiveProjectile(); - proj.ExplodePower = 1; - proj.ProjectilesCount = 3; - proj.DelayedFire = true; - proj.Inaccuracy = 0.75f; - equip.Projectiles.put(new ItemStack(Material.TNT), proj); - proj = new ExplosiveProjectile(); - proj.ExplodePower = 4; - proj.ProjectilesCount = 1; - equip.Projectiles.put(new ItemStack(Material.COPPER_BLOCK), proj); - - EntityProjectile fireProj = new EntityProjectile(); - fireProj.EntityCount = 2; - fireProj.EntityTyp = EntityType.SMALL_FIREBALL; - fireProj.ParticleType = Particle.WHITE_ASH; - fireProj.SoundType = Sound.ENTITY_BLAZE_SHOOT; - equip.Projectiles.put(new ItemStack(Material.FIRE_CHARGE), fireProj); - DefinedEquipment.put(equip.ReadyModelNumber, equip); - equip = new SiegeEquipment(); - equip.EquipmentName = "Siege Cannon"; - proj.ExplodePower = 2; - equip.Projectiles.put(new ItemStack(Material.COBBLESTONE), proj); - equip.shotAmount = 1; - equip.PlacementOffsetY = -1; - equip.ReadyModelNumber = 141; - equip.ModelNumberToFireAt = 141; - equip.FiringModelNumbers = new ArrayList(); - equip.RotateStandHead = true; - equip.RotateSideways = true; - proj = new ExplosiveProjectile(); - proj.ExplodePower = 1; - proj.ProjectilesCount = 3; - proj.DelayedFire = true; - proj.Inaccuracy = 0.5f; - equip.Projectiles.put(new ItemStack(Material.TNT), proj); - proj = new ExplosiveProjectile(); - proj.ExplodePower = 4; - proj.ProjectilesCount = 1; - equip.Projectiles.put(new ItemStack(Material.COPPER_BLOCK), proj); - equip.Projectiles.put(new ItemStack(Material.GRAVEL), new EntityProjectile()); - DefinedEquipment.put(equip.ReadyModelNumber, equip); -// EntityProjectile pig = new EntityProjectile(); -// pig.EntityCount = 100; -// pig.DelayedFire = true; -// pig.DelayTime = 3; -// pig.EntityTyp = EntityType.ARROW; -// pig.SoundType = Sound.ITEM_CROSSBOW_SHOOT; -// pig.ParticleType = Particle.GLOW; -// equip.Projectiles.put(Material.PORKCHOP, pig); -// equip.Projectiles.put(Material.BONE, new PotionProjectile()); - - } - - public static Boolean CreateCannon(Entity player, int CustomModelData, Location l) { - //l.setY(l.getY() - 1); - l.add(0.5, 0, 0.5); - NamespacedKey key = new NamespacedKey(SiegeEngines.plugin, "cannons"); - SiegeEquipment equip = CreateClone(CustomModelData); - if (equip == null || !equip.Enabled) { - return false; - } - l.setY(l.getY() + 1); - int maxNearby = 0; - - l.setDirection(player.getFacing().getDirection()); - ItemStack item = new ItemStack(Material.CARVED_PUMPKIN); - ItemMeta meta = item.getItemMeta(); - String id = ""; - equip.AmmoHolder = new EquipmentMagazine(); - if (equip.HaseBaseStand) { - - Location l2 = l; - l2.setY(l.getY() + equip.BaseStandOffset); - Entity entity3 = player.getWorld().spawnEntity(l, EntityType.ARMOR_STAND); - - LivingEntity ent = (LivingEntity) entity3; - ArmorStand stand = (ArmorStand) ent; - id = entity3.getUniqueId().toString(); - meta.setCustomModelData(equip.BaseStandModelNumber); - stand.addEquipmentLock(EquipmentSlot.HEAD, LockType.REMOVING_OR_CHANGING); - stand.addEquipmentLock(EquipmentSlot.LEGS, LockType.ADDING_OR_CHANGING); - stand.addEquipmentLock(EquipmentSlot.CHEST, LockType.ADDING_OR_CHANGING); - stand.addEquipmentLock(EquipmentSlot.FEET, LockType.ADDING_OR_CHANGING); - stand.addEquipmentLock(EquipmentSlot.HAND, LockType.REMOVING_OR_CHANGING); - stand.addEquipmentLock(EquipmentSlot.OFF_HAND, LockType.ADDING_OR_CHANGING); - stand.addEquipmentLock(EquipmentSlot.HAND, LockType.REMOVING_OR_CHANGING); - stand.addEquipmentLock(EquipmentSlot.OFF_HAND, LockType.REMOVING_OR_CHANGING); - stand.setBasePlate(false); - // stand.setSmall(true); - item.setItemMeta(meta); - stand.setInvisible(equip.AllowInvisibleStand); - ent.getEquipment().setHelmet(item); - stand.setGravity(false); - stand.setMarker(true); - } - l.setY(l.getY() + equip.PlacementOffsetY); - Entity entity2 = player.getWorld().spawnEntity(l, EntityType.ARMOR_STAND); - if (id != "") { - entity2.getPersistentDataContainer().set(key, PersistentDataType.STRING, id); - } - meta.setCustomModelData(equip.ReadyModelNumber); - meta.setDisplayName("§e" + equip.EquipmentName + " Item"); - List Lore = new ArrayList(); - Lore.add("§ePlace as a block to spawn a " + equip.EquipmentName + " or put on an Armor Stand."); - meta.setLore(Lore); - item.setItemMeta(meta); - - // entity3.addPassenger(entity2); - - LivingEntity ent = (LivingEntity) entity2; - ArmorStand stand = (ArmorStand) ent; - equip.Entity = entity2; - for (Entity le : l.getNearbyEntities(5, 5, 5)) { - if (maxNearby >= 4) { - ent.damage(100.0d, player); - return false; - } - if (le instanceof ArmorStand) - maxNearby++; - } - - equip.EntityId = entity2.getUniqueId(); - stand.addEquipmentLock(EquipmentSlot.HEAD, LockType.REMOVING_OR_CHANGING); - stand.addEquipmentLock(EquipmentSlot.LEGS, LockType.ADDING_OR_CHANGING); - stand.addEquipmentLock(EquipmentSlot.CHEST, LockType.ADDING_OR_CHANGING); - stand.addEquipmentLock(EquipmentSlot.FEET, LockType.ADDING_OR_CHANGING); - stand.addEquipmentLock(EquipmentSlot.HAND, LockType.REMOVING_OR_CHANGING); - stand.addEquipmentLock(EquipmentSlot.OFF_HAND, LockType.ADDING_OR_CHANGING); - stand.addEquipmentLock(EquipmentSlot.HAND, LockType.REMOVING_OR_CHANGING); - stand.addEquipmentLock(EquipmentSlot.OFF_HAND, LockType.REMOVING_OR_CHANGING); - stand.setInvisible(equip.AllowInvisibleStand); - stand.setBasePlate(false); - ent.addPotionEffect(new PotionEffect(PotionEffectType.INVISIBILITY, 2000000, 1)); - // stand.setSmall(true); - - stand.setGravity(false); - //stand.setSmall(true); - ent.getEquipment().setHelmet(item); - if (TrackedStands.containsKey(player.getUniqueId())) { - List entities = TrackedStands.get(player.getUniqueId()); - entities.add(entity2); - TrackedStands.put(player.getUniqueId(), entities); - } else { - List newList = new ArrayList(); - newList.add(entity2); - TrackedStands.put(player.getUniqueId(), newList); - } - equipment.put(entity2.getUniqueId(), equip); - return true; - } - - public static void UpdateEntityIdModel(Entity ent, int modelNumber, String WorldName) { - if (ent instanceof LivingEntity) { - LivingEntity liv = (LivingEntity) ent; - ItemStack Helmet = liv.getEquipment().getHelmet(); - if (Helmet != null) { - ItemMeta meta = Helmet.getItemMeta(); - meta.setCustomModelData(modelNumber); - Helmet.setItemMeta(meta); - liv.getEquipment().setHelmet(Helmet); - // plugin.getLogger().log(Level.INFO, "Updating stand?"); - } - } - } - -} - - - +package com.github.alathra.siegeengines; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Random; +import java.util.UUID; +import java.util.concurrent.TimeUnit; + +import com.github.alathra.siegeengines.command.CommandHandler; +import org.bukkit.Bukkit; +import org.bukkit.Location; +import org.bukkit.Material; +import org.bukkit.NamespacedKey; +import org.bukkit.Particle; +import org.bukkit.Sound; +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.ArmorStand; +import org.bukkit.entity.ArmorStand.LockType; +import org.bukkit.entity.Entity; +import org.bukkit.entity.EntityType; +import org.bukkit.entity.LivingEntity; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.inventory.EquipmentSlot; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.ItemMeta; +import org.bukkit.persistence.PersistentDataType; +import org.bukkit.plugin.Plugin; +import org.bukkit.plugin.java.JavaPlugin; +import org.bukkit.potion.PotionEffect; +import org.bukkit.potion.PotionEffectType; +import org.bukkit.metadata.MetadataValueAdapter; +import org.bukkit.metadata.FixedMetadataValue; + +//import com.palmergames.bukkit.towny.Towny; + +import com.github.alathra.siegeengines.listeners.ClickHandler; +import com.github.alathra.siegeengines.listeners.RotationHandler; +import com.github.alathra.siegeengines.projectile.EntityProjectile; +import com.github.alathra.siegeengines.projectile.ExplosiveProjectile; +import com.github.alathra.siegeengines.projectile.GunnersProjectile; +//import GunnersProjectiles.EntityProjectile; +//import GunnersProjectiles.GunnersProjectile; +//import GunnersProjectiles.ExplosiveProjectile; +//import GunnersProjectiles.PotionProjectile; + + +public class SiegeEngines extends JavaPlugin { + + public static SiegeEngines instance; + + public static Plugin plugin; + public static MetadataValueAdapter metadata; + public static Random random = new Random(); + private static String Path; + private CommandHandler commandHandler; + //public static Towny towny; + + + public static SiegeEngines getInstance() { + return instance; + } + + @Override + public void onLoad() { + plugin = this; + commandHandler = new CommandHandler(this); + commandHandler.onLoad(); + } + + @SuppressWarnings("deprecation") + @Override + public void onEnable() { + instance = this; + Path = this.getDataFolder().getAbsolutePath(); + //this.getCommand("SiegeEngines").setExecutor(new GunnersCommand()); // Fixed Start-up + getServer().getPluginManager().registerEvents(new RotationHandler(), this); + getServer().getPluginManager().registerEvents(new ClickHandler(), this); + //StorageManager.setup(Path, plugin); + equipment.clear(); + TrackedStands.clear(); + DefinedEquipment.clear(); + //AddDefaults(); + //HashMap projObj = new HashMap<>(); + //GunnerEquipment equip = CreateNewGun(null, null, null, null, null, null, projObj); + for (SiegeEquipment i : DefinedEquipment.values()) { + System.out.println("§eEnabled Weapon : " + i.EquipmentName); + System.out.println("§eWeapon Propellant/\"Fuel\" ItemStacks : " + i.FuelMaterial); + for (ItemStack proj : i.Projectiles.keySet()) { + System.out.println("§eWeapon Projectile ItemStacks : " + proj); + } + } + commandHandler.onEnable(); + } + + @SuppressWarnings("deprecation") + @Override + public void onDisable() { + commandHandler.onDisable(); + for (SiegeEquipment equip : equipment.values()) { + if (equip.Entity != null) { + ItemStack item = new ItemStack(Material.CARVED_PUMPKIN); + ItemMeta meta = item.getItemMeta(); + equip.AmmoHolder = new EquipmentMagazine(); + + meta.setCustomModelData(equip.ReadyModelNumber); + meta.setDisplayName("§e" + equip.EquipmentName + " Item"); + List Lore = new ArrayList(); + Lore.add("§ePlace as a block to spawn a " + equip.EquipmentName + " or put on an Armor Stand."); + meta.setLore(Lore); + item.setItemMeta(meta); + + ((LivingEntity) equip.Entity).getEquipment().setHelmet(item); + } + } + } + + public static FixedMetadataValue addMetaDataValue(Object value) { + return new FixedMetadataValue(Bukkit.getServer().getPluginManager().getPlugin("SiegeEngines"), value); + } + + public static SiegeEquipment CreateClone(Integer ModelId) { + try { + return DefinedEquipment.get(ModelId).clone(); + } catch (CloneNotSupportedException e) { + // TODO Auto-generated catch block + + } + return null; + } + + public static HashMap DefinedEquipment = new HashMap(); + + public static HashMap> TrackedStands = new HashMap>(); + + public static HashMap equipment = new HashMap(); + + public static String convertTime(long time) { + + long days = TimeUnit.MILLISECONDS.toDays(time); + time -= TimeUnit.DAYS.toMillis(days); + + long hours = TimeUnit.MILLISECONDS.toHours(time); + time -= TimeUnit.HOURS.toMillis(hours); + + long minutes = TimeUnit.MILLISECONDS.toMinutes(time); + time -= TimeUnit.MINUTES.toMillis(minutes); + + long seconds = TimeUnit.MILLISECONDS.toSeconds(time); + String timeLeftFormatted = String.format("§e" + minutes + " Minutes " + seconds + " Seconds§f"); + + return timeLeftFormatted; + } + + public static SiegeEquipment CreateNewGun(String name, Integer XOffset, Integer YOffset, Integer fuelMax, Float fuelVelocityMod, Integer customModelId, HashMap projObj) { + SiegeEquipment equip = new SiegeEquipment(); + if (customModelId == null || customModelId == 0) + customModelId = 150; + if (name == null) + name = "Unnamed Cannon"; + if (XOffset == null) + XOffset = 0; + if (YOffset == null) + YOffset = 0; + if (fuelMax == null) + fuelMax = 0; + if (fuelVelocityMod == null) + fuelVelocityMod = 0.95f; + equip.EquipmentName = name; + equip.EquipmentId = equip.EquipmentName.replaceAll(" ", "_").toLowerCase(); + equip.XOffset = XOffset; + equip.YOffset = YOffset; + equip.RotateStandHead = true; + equip.RotateSideways = true; + equip.PlacementOffsetY = -1.125f; + equip.ReadyModelNumber = customModelId; + equip.ModelNumberToFireAt = customModelId; + equip.FiringModelNumbers = new ArrayList(); + equip.VelocityPerFuel = fuelVelocityMod; + equip.FuelMaterial = new ItemStack(Material.GUNPOWDER); + equip.CycleThroughModelsBeforeFiring = false; + equip.AllowInvisibleStand = false; + equip.MaxFuel = fuelMax; + equip.shotAmount = 1; + equip.AmmoHolder.LoadedFuel = fuelMax; + EntityProjectile defaultProj = new EntityProjectile(); + defaultProj.EntityCount = 2; + defaultProj.EntityTyp = EntityType.SHULKER_BULLET; + defaultProj.ParticleType = Particle.WHITE_ASH; + defaultProj.SoundType = Sound.ENTITY_BLAZE_SHOOT; + if (projObj.keySet().isEmpty() || projObj.isEmpty()) + projObj.put(new ItemStack(Material.GUNPOWDER), defaultProj); + for (ItemStack mat : projObj.keySet()) { + equip.Projectiles.put(mat, projObj.get(mat)); + } + DefinedEquipment.put(equip.ReadyModelNumber, equip); + return equip; + } + + public static void AddDefaults() { + SiegeEquipment equip = new SiegeEquipment(); + equip.EquipmentName = "Trebuchet"; + equip.XOffset = 5; + equip.YOffset = 5; + equip.VelocityPerFuel = 0.3f; + ExplosiveProjectile proj = new ExplosiveProjectile(); + proj.ExplodePower = 1; + equip.shotAmount = 2; + equip.RotateStandHead = false; + equip.RotateSideways = true; + equip.FuelMaterial = new ItemStack(Material.STRING); + equip.Projectiles.put(new ItemStack(Material.COBBLESTONE), proj); + equip.ReadyModelNumber = 122; + equip.ModelNumberToFireAt = 135; + equip.MillisecondsBetweenFiringStages = 2; + equip.MillisecondsBetweenReloadingStages = 10; + equip.FiringModelNumbers = new ArrayList<>(Arrays.asList( + 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139 + )); + equip.CycleThroughModelsBeforeFiring = true; + DefinedEquipment.put(equip.ReadyModelNumber, equip); + + equip = new SiegeEquipment(); + equip.EquipmentName = "Naval Cannon"; + equip.Projectiles.put(new ItemStack(Material.COBBLESTONE), proj); + equip.PlacementOffsetY = -1; + equip.shotAmount = 1; + equip.ReadyModelNumber = 142; + equip.ModelNumberToFireAt = 142; + equip.FiringModelNumbers = new ArrayList(); + equip.RotateStandHead = true; + equip.RotateSideways = false; + proj = new ExplosiveProjectile(); + proj.ExplodePower = 1; + proj.ProjectilesCount = 5; + proj.DelayedFire = true; + proj.Inaccuracy = 0.75f; + equip.Projectiles.put(new ItemStack(Material.TNT), proj); + proj = new ExplosiveProjectile(); + proj.ExplodePower = 3; + proj.ProjectilesCount = 1; + equip.Projectiles.put(new ItemStack(Material.IRON_BLOCK), proj); + + EntityProjectile fireProj = new EntityProjectile(); + fireProj.EntityCount = 4; + fireProj.EntityTyp = EntityType.SMALL_FIREBALL; + fireProj.ParticleType = Particle.WHITE_ASH; + fireProj.SoundType = Sound.ENTITY_BLAZE_SHOOT; + fireProj.Inaccuracy = 0.75f; + equip.Projectiles.put(new ItemStack(Material.FIRE_CHARGE), fireProj); + DefinedEquipment.put(equip.ReadyModelNumber, equip); + equip = new SiegeEquipment(); + equip.EquipmentName = "Siege Cannon"; + proj.ExplodePower = 2; + equip.Projectiles.put(new ItemStack(Material.COBBLESTONE), proj); + equip.shotAmount = 1; + equip.PlacementOffsetY = -1; + equip.ReadyModelNumber = 141; + equip.ModelNumberToFireAt = 141; + equip.FiringModelNumbers = new ArrayList(); + equip.RotateStandHead = true; + equip.RotateSideways = true; + proj = new ExplosiveProjectile(); + proj.ExplodePower = 2; + proj.ProjectilesCount = 3; + proj.DelayedFire = true; + proj.Inaccuracy = 0.75f; + equip.Projectiles.put(new ItemStack(Material.TNT), proj); + proj = new ExplosiveProjectile(); + proj.ExplodePower = 3; + proj.ProjectilesCount = 1; + equip.Projectiles.put(new ItemStack(Material.IRON_BLOCK), proj); + EntityProjectile scatterShot = new EntityProjectile(); + scatterShot.Inaccuracy = 0.5f; + scatterShot.EntityCount = 24; + scatterShot.ParticleType = Particle.ELECTRIC_SPARK; + scatterShot.SoundType = Sound.ITEM_CROSSBOW_SHOOT; + equip.Projectiles.put(new ItemStack(Material.GRAVEL), scatterShot); + DefinedEquipment.put(equip.ReadyModelNumber, equip); +// EntityProjectile pig = new EntityProjectile(); +// pig.EntityCount = 100; +// pig.DelayedFire = true; +// pig.DelayTime = 3; +// pig.EntityTyp = EntityType.ARROW; +// pig.SoundType = Sound.ITEM_CROSSBOW_SHOOT; +// pig.ParticleType = Particle.GLOW; +// equip.Projectiles.put(Material.PORKCHOP, pig); +// equip.Projectiles.put(Material.BONE, new PotionProjectile()); + + } + + public static Boolean CreateCannon(Entity player, int CustomModelData, Location l) { + //l.setY(l.getY() - 1); + l.add(0.5, 0, 0.5); + NamespacedKey key = new NamespacedKey(SiegeEngines.plugin, "cannons"); + SiegeEquipment equip = CreateClone(CustomModelData); + if (equip == null || !equip.Enabled) { + return false; + } + l.setY(l.getY() + 1); + int maxNearby = 0; + + l.setDirection(player.getFacing().getDirection()); + ItemStack item = new ItemStack(Material.CARVED_PUMPKIN); + ItemMeta meta = item.getItemMeta(); + String id = ""; + equip.AmmoHolder = new EquipmentMagazine(); + if (equip.HaseBaseStand) { + + Location l2 = l; + l2.setY(l.getY() + equip.BaseStandOffset); + Entity entity3 = player.getWorld().spawnEntity(l, EntityType.ARMOR_STAND); + + LivingEntity ent = (LivingEntity) entity3; + ArmorStand stand = (ArmorStand) ent; + id = entity3.getUniqueId().toString(); + meta.setCustomModelData(equip.BaseStandModelNumber); + stand.addEquipmentLock(EquipmentSlot.HEAD, LockType.REMOVING_OR_CHANGING); + stand.addEquipmentLock(EquipmentSlot.LEGS, LockType.ADDING_OR_CHANGING); + stand.addEquipmentLock(EquipmentSlot.CHEST, LockType.ADDING_OR_CHANGING); + stand.addEquipmentLock(EquipmentSlot.FEET, LockType.ADDING_OR_CHANGING); + stand.addEquipmentLock(EquipmentSlot.HAND, LockType.REMOVING_OR_CHANGING); + stand.addEquipmentLock(EquipmentSlot.OFF_HAND, LockType.ADDING_OR_CHANGING); + stand.addEquipmentLock(EquipmentSlot.HAND, LockType.REMOVING_OR_CHANGING); + stand.addEquipmentLock(EquipmentSlot.OFF_HAND, LockType.REMOVING_OR_CHANGING); + stand.setBasePlate(false); + // stand.setSmall(true); + item.setItemMeta(meta); + stand.setInvisible(equip.AllowInvisibleStand); + ent.getEquipment().setHelmet(item); + stand.setGravity(false); + stand.setMarker(true); + } + l.setY(l.getY() + equip.PlacementOffsetY); + Entity entity2 = player.getWorld().spawnEntity(l, EntityType.ARMOR_STAND); + if (id != "") { + entity2.getPersistentDataContainer().set(key, PersistentDataType.STRING, id); + } + meta.setCustomModelData(equip.ReadyModelNumber); + meta.setDisplayName("§e" + equip.EquipmentName + " Item"); + List Lore = new ArrayList(); + Lore.add("§ePlace as a block to spawn a " + equip.EquipmentName + " or put on an Armor Stand."); + meta.setLore(Lore); + item.setItemMeta(meta); + + // entity3.addPassenger(entity2); + + LivingEntity ent = (LivingEntity) entity2; + ArmorStand stand = (ArmorStand) ent; + equip.Entity = entity2; + for (Entity le : l.getNearbyEntities(5, 5, 5)) { + if (maxNearby >= 4) { + ent.damage(100.0d, player); + return false; + } + if (le instanceof ArmorStand) + maxNearby++; + } + + equip.EntityId = entity2.getUniqueId(); + stand.addEquipmentLock(EquipmentSlot.HEAD, LockType.REMOVING_OR_CHANGING); + stand.addEquipmentLock(EquipmentSlot.LEGS, LockType.ADDING_OR_CHANGING); + stand.addEquipmentLock(EquipmentSlot.CHEST, LockType.ADDING_OR_CHANGING); + stand.addEquipmentLock(EquipmentSlot.FEET, LockType.ADDING_OR_CHANGING); + stand.addEquipmentLock(EquipmentSlot.HAND, LockType.REMOVING_OR_CHANGING); + stand.addEquipmentLock(EquipmentSlot.OFF_HAND, LockType.ADDING_OR_CHANGING); + stand.addEquipmentLock(EquipmentSlot.HAND, LockType.REMOVING_OR_CHANGING); + stand.addEquipmentLock(EquipmentSlot.OFF_HAND, LockType.REMOVING_OR_CHANGING); + stand.setInvisible(equip.AllowInvisibleStand); + stand.setBasePlate(false); + ent.addPotionEffect(new PotionEffect(PotionEffectType.INVISIBILITY, 2000000, 1)); + // stand.setSmall(true); + + stand.setGravity(false); + //stand.setSmall(true); + ent.getEquipment().setHelmet(item); + if (TrackedStands.containsKey(player.getUniqueId())) { + List entities = TrackedStands.get(player.getUniqueId()); + entities.add(entity2); + TrackedStands.put(player.getUniqueId(), entities); + } else { + List newList = new ArrayList(); + newList.add(entity2); + TrackedStands.put(player.getUniqueId(), newList); + } + equipment.put(entity2.getUniqueId(), equip); + return true; + } + + public static void UpdateEntityIdModel(Entity ent, int modelNumber, String WorldName) { + if (ent instanceof LivingEntity) { + LivingEntity liv = (LivingEntity) ent; + ItemStack Helmet = liv.getEquipment().getHelmet(); + if (Helmet != null) { + ItemMeta meta = Helmet.getItemMeta(); + meta.setCustomModelData(modelNumber); + Helmet.setItemMeta(meta); + liv.getEquipment().setHelmet(Helmet); + // plugin.getLogger().log(Level.INFO, "Updating stand?"); + } + } + } + +} + + + diff --git a/src/main/java/com/github/alathra/siegeengines/command/SiegeEnginesCommand.java b/src/main/java/com/github/alathra/siegeengines/command/SiegeEnginesCommand.java index 61c3e5b..22e6c42 100644 --- a/src/main/java/com/github/alathra/siegeengines/command/SiegeEnginesCommand.java +++ b/src/main/java/com/github/alathra/siegeengines/command/SiegeEnginesCommand.java @@ -1,131 +1,129 @@ -package com.github.alathra.siegeengines.command; - -import com.github.alathra.siegeengines.SiegeEquipment; -import com.github.alathra.siegeengines.config.Config; -import com.github.alathra.siegeengines.projectile.GunnersProjectile; -import com.github.milkdrinkers.colorparser.ColorParser; -import dev.jorel.commandapi.CommandAPIBukkit; -import dev.jorel.commandapi.CommandAPICommand; -import dev.jorel.commandapi.arguments.ArgumentSuggestions; -import dev.jorel.commandapi.arguments.PlayerArgument; -import dev.jorel.commandapi.arguments.StringArgument; -import dev.jorel.commandapi.exceptions.WrapperCommandSyntaxException; -import dev.jorel.commandapi.executors.CommandArguments; -import net.kyori.adventure.text.Component; -import org.bukkit.Material; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; -import org.bukkit.inventory.ItemStack; -import org.bukkit.inventory.meta.ItemMeta; - -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; - -import static com.github.alathra.siegeengines.SiegeEngines.*; - -public class SiegeEnginesCommand { - public SiegeEnginesCommand() { - new CommandAPICommand("siegeengines") - .withFullDescription("Example command.") - .withShortDescription("Example command.") - .withPermission("siegeengines.command") - .withSubcommands( - commandGet(), - commandGetAll(), - commandReload() - ) - .executesPlayer(this::onExecute) - .register(); - } - - private void onExecute(CommandSender sender, CommandArguments args) { - sender.sendMessage(ColorParser.of("Incorrect usage, /SiegeEngines get, /SiegeEngines reload").build()); - } - - private CommandAPICommand commandGet() { - return new CommandAPICommand("get") - .withPermission("siegeengines.command.get") - .withArguments( - new StringArgument("equipmentid") - .replaceSuggestions( - ArgumentSuggestions.strings( - DefinedEquipment.values().stream().map(gunnerEquipment -> gunnerEquipment.EquipmentId).toList() - ) - ), - new PlayerArgument("target") - .setOptional(true) - ) - .executesPlayer(this::onGet); - } - - private CommandAPICommand commandGetAll() { - return new CommandAPICommand("getAll") - .withPermission("siegeengines.command.getAll") - .executesPlayer(this::onGetAll); - } - - private CommandAPICommand commandReload() { - return new CommandAPICommand("reload") - .withPermission("siegeengines.command.reload") - .executesPlayer(this::onReload); - } - - private void onGet(CommandSender sender, CommandArguments args) throws WrapperCommandSyntaxException { - if (!(args.get("equipmentid") instanceof String equipmentId)) - throw CommandAPIBukkit.failWithAdventureComponent(ColorParser.of("Invalid equipment id specified.").build()); - - Player player = (Player) args.getOptional("target").orElse(sender); - - for (SiegeEquipment gunnerEquipment : DefinedEquipment.values()) { - if (gunnerEquipment.equals(equipmentId)) { - giveEquipment(player, gunnerEquipment); - break; - } - } - } - - private void onGetAll(CommandSender sender, CommandArguments args) throws WrapperCommandSyntaxException { - if (!(sender instanceof Player player)) - throw CommandAPIBukkit.failWithAdventureComponent(ColorParser.of("Only players can use this command.").build()); - - for (SiegeEquipment i : DefinedEquipment.values()) { - giveEquipment(player, i); - } - } - - private void onReload(CommandSender sender, CommandArguments args) throws WrapperCommandSyntaxException { - if (!(sender instanceof Player)) - throw CommandAPIBukkit.failWithAdventureComponent(ColorParser.of("Only players can use this command.").build()); - - equipment.clear(); - TrackedStands.clear(); - DefinedEquipment.clear(); - AddDefaults(); - HashMap projObj = new HashMap<>(); - SiegeEquipment equip = CreateNewGun(null, null, null, null, null, null, projObj); - for (SiegeEquipment i : DefinedEquipment.values()) { - sender.sendMessage(ColorParser.of("Enabled Weapon : %s".formatted(i.EquipmentName)).build()); - sender.sendMessage(ColorParser.of("Weapon Propellant/\"Fuel\" ItemStacks : %s".formatted(i.FuelMaterial)).build()); - for (ItemStack proj : i.Projectiles.keySet()) { - sender.sendMessage(ColorParser.of("Weapon Projectile ItemStacks : %s".formatted(proj)).build()); - } - } - Config.reload(); - sender.sendMessage("Gunners Core configs reloaded"); - } - - private void giveEquipment(Player player, SiegeEquipment gunnerEquipment) { - ItemStack item = new ItemStack(Material.CARVED_PUMPKIN); - ItemMeta meta = item.getItemMeta(); - meta.setCustomModelData(gunnerEquipment.ReadyModelNumber); - meta.displayName(ColorParser.of("%s Item".formatted(gunnerEquipment.EquipmentName)).build()); - List lore = new ArrayList<>(); - lore.add(ColorParser.of("Place as a block to spawn a '%s'".formatted(gunnerEquipment.EquipmentName)).build()); - lore.add(ColorParser.of("or put on an Armor Stand.").build()); - lore.add(ColorParser.of("Right click to toggle visibility of stand.").build()); - meta.lore(lore); - item.setItemMeta(meta); - player.getInventory().addItem(item); - } +package com.github.alathra.siegeengines.command; + +import com.github.alathra.siegeengines.SiegeEquipment; +import com.github.alathra.siegeengines.projectile.GunnersProjectile; +import com.github.milkdrinkers.colorparser.ColorParser; +import dev.jorel.commandapi.CommandAPIBukkit; +import dev.jorel.commandapi.CommandAPICommand; +import dev.jorel.commandapi.arguments.ArgumentSuggestions; +import dev.jorel.commandapi.arguments.PlayerArgument; +import dev.jorel.commandapi.arguments.StringArgument; +import dev.jorel.commandapi.exceptions.WrapperCommandSyntaxException; +import dev.jorel.commandapi.executors.CommandArguments; +import net.kyori.adventure.text.Component; +import org.bukkit.Material; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.ItemMeta; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +import static com.github.alathra.siegeengines.SiegeEngines.*; + +public class SiegeEnginesCommand { + public SiegeEnginesCommand() { + new CommandAPICommand("siegeengines") + .withFullDescription("Example command.") + .withShortDescription("Example command.") + .withPermission("siegeengines.command") + .withSubcommands( + commandGet(), + commandGetAll(), + commandReload() + ) + .executesPlayer(this::onExecute) + .register(); + } + + private void onExecute(CommandSender sender, CommandArguments args) { + sender.sendMessage(ColorParser.of("Incorrect usage, /SiegeEngines get, /SiegeEngines reload").build()); + } + + private CommandAPICommand commandGet() { + return new CommandAPICommand("get") + .withPermission("siegeengines.command.get") + .withArguments( + new StringArgument("equipmentid") + .replaceSuggestions( + ArgumentSuggestions.strings( + DefinedEquipment.values().stream().map(SiegeEquipment -> SiegeEquipment.EquipmentId).toList() + ) + ), + new PlayerArgument("target") + .setOptional(true) + ) + .executesPlayer(this::onGet); + } + + private CommandAPICommand commandGetAll() { + return new CommandAPICommand("getAll") + .withPermission("siegeengines.command.getAll") + .executesPlayer(this::onGetAll); + } + + private CommandAPICommand commandReload() { + return new CommandAPICommand("reload") + .withPermission("siegeengines.command.reload") + .executesPlayer(this::onReload); + } + + private void onGet(CommandSender sender, CommandArguments args) throws WrapperCommandSyntaxException { + if (!(args.get("equipmentid") instanceof String equipmentId)) + throw CommandAPIBukkit.failWithAdventureComponent(ColorParser.of("Invalid equipment id specified.").build()); + + Player player = (Player) args.getOptional("target").orElse(sender); + + for (SiegeEquipment SiegeEquipment : DefinedEquipment.values()) { + if (SiegeEquipment.equals(equipmentId)) { + giveEquipment(player, SiegeEquipment); + break; + } + } + } + + private void onGetAll(CommandSender sender, CommandArguments args) throws WrapperCommandSyntaxException { + if (!(sender instanceof Player player)) + throw CommandAPIBukkit.failWithAdventureComponent(ColorParser.of("Only players can use this command.").build()); + + for (SiegeEquipment i : DefinedEquipment.values()) { + giveEquipment(player, i); + } + } + + private void onReload(CommandSender sender, CommandArguments args) throws WrapperCommandSyntaxException { + if (!(sender instanceof Player)) + throw CommandAPIBukkit.failWithAdventureComponent(ColorParser.of("Only players can use this command.").build()); + + equipment.clear(); + TrackedStands.clear(); + DefinedEquipment.clear(); + AddDefaults(); + HashMap projObj = new HashMap<>(); + //SiegeEquipment equip = CreateNewGun(null, null, null, null, null, null, projObj); + for (SiegeEquipment i : DefinedEquipment.values()) { + sender.sendMessage(ColorParser.of("Enabled Weapon : %s".formatted(i.EquipmentName)).build()); + sender.sendMessage(ColorParser.of("Weapon Propellant/\"Fuel\" ItemStacks : %s".formatted(i.FuelMaterial)).build()); + for (ItemStack proj : i.Projectiles.keySet()) { + sender.sendMessage(ColorParser.of("Weapon Projectile ItemStacks : %s".formatted(proj)).build()); + } + } + sender.sendMessage("Gunners Core configs reloaded"); + } + + private void giveEquipment(Player player, SiegeEquipment SiegeEquipment) { + ItemStack item = new ItemStack(Material.CARVED_PUMPKIN); + ItemMeta meta = item.getItemMeta(); + meta.setCustomModelData(SiegeEquipment.ReadyModelNumber); + meta.displayName(ColorParser.of("%s Item".formatted(SiegeEquipment.EquipmentName)).build()); + List lore = new ArrayList<>(); + lore.add(ColorParser.of("Place as a block to spawn a '%s'".formatted(SiegeEquipment.EquipmentName)).build()); + lore.add(ColorParser.of("or put on an Armor Stand.").build()); + lore.add(ColorParser.of("Right click to toggle visibility of stand.").build()); + meta.lore(lore); + item.setItemMeta(meta); + player.getInventory().addItem(item); + } } \ No newline at end of file diff --git a/src/main/java/com/github/alathra/siegeengines/listeners/ClickHandler.java b/src/main/java/com/github/alathra/siegeengines/listeners/ClickHandler.java index 416480f..7475a85 100644 --- a/src/main/java/com/github/alathra/siegeengines/listeners/ClickHandler.java +++ b/src/main/java/com/github/alathra/siegeengines/listeners/ClickHandler.java @@ -1,847 +1,862 @@ -package com.github.alathra.siegeengines.listeners; - -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Random; -import java.util.UUID; - -import com.github.alathra.siegeengines.EquipmentMagazine; -import com.github.alathra.siegeengines.SiegeEngines; -import org.bukkit.Bukkit; -import org.bukkit.Location; -import org.bukkit.Material; -import org.bukkit.NamespacedKey; -import org.bukkit.World; -import org.bukkit.block.Block; -import org.bukkit.block.Container; -import org.bukkit.block.Sign; -import org.bukkit.block.TileState; -import org.bukkit.entity.ArmorStand; -import org.bukkit.entity.ArmorStand.LockType; -import org.bukkit.entity.Entity; -import org.bukkit.entity.EntityType; -import org.bukkit.entity.LivingEntity; -import org.bukkit.entity.Player; -import org.bukkit.entity.Projectile; -import org.bukkit.entity.Snowball; -import org.bukkit.entity.TNTPrimed; -import org.bukkit.event.EventHandler; -import org.bukkit.event.EventPriority; -import org.bukkit.event.Listener; -import org.bukkit.event.block.Action; -import org.bukkit.event.block.SignChangeEvent; -import org.bukkit.event.entity.EntityDamageByEntityEvent; -import org.bukkit.event.entity.EntityDeathEvent; -import org.bukkit.event.entity.EntityExplodeEvent; -import org.bukkit.event.entity.ProjectileHitEvent; -import org.bukkit.event.player.PlayerInteractAtEntityEvent; -import org.bukkit.event.player.PlayerInteractEvent; -import org.bukkit.inventory.EquipmentSlot; -import org.bukkit.inventory.Inventory; -import org.bukkit.inventory.ItemStack; -import org.bukkit.persistence.PersistentDataType; -import org.bukkit.util.EulerAngle; - -import com.github.alathra.siegeengines.SiegeEquipment; -import com.github.alathra.siegeengines.config.Config; -import com.github.alathra.siegeengines.projectile.ExplosiveProjectile; - -//import com.palmergames.bukkit.towny.TownyAPI; -//import com.palmergames.bukkit.towny.event.actions.TownyDestroyEvent; -//import com.palmergames.bukkit.towny.object.TownBlock; - -//import GunnersProjectiles.ExplosiveProjectile; - -public class ClickHandler implements Listener { - - public float MinDelay = 5; - - public static HashMap projectiles = new HashMap(); - - @EventHandler - public void onExplode(EntityExplodeEvent e) { - for (Entity entity : e.getEntity().getNearbyEntities(e.getYield() + 1, e.getYield() + 1, e.getYield() + 1)) { - if (entity instanceof ArmorStand) { - ArmorStand stand = (ArmorStand) entity; - /*if (SiegeEngines.towny != null) { - TownBlock block = TownyAPI.getInstance().getTownBlock(stand.getLocation()); - if (block != null) { - if (block.hasTown()) { - if (!block.getTownBlockOwner().getPermissions().explosion) { - return; - } - } - } - }*/ - stand.eject(); - stand.remove(); - } - } - } - - @EventHandler - public void onHit(ProjectileHitEvent event) { - if ((event.getEntity() instanceof Snowball) && projectiles.containsKey(event.getEntity().getUniqueId())) { - ExplosiveProjectile proj = projectiles.get(event.getEntity().getUniqueId()); - Entity snowball = event.getEntity(); - Snowball ball = (Snowball) snowball; - Entity player = (Entity) ball.getShooter(); - if (player instanceof Player) { - player.sendMessage("§eDistance to impact: " + String.format("%.2f", player.getLocation().distance(ball.getLocation()))); - } - Location loc = snowball.getLocation(); - World world = event.getEntity().getWorld(); - // world.createExplosion(loc, proj.Radius, proj.DoFire); - Entity tnt = event.getEntity().getWorld().spawnEntity(loc, EntityType.PRIMED_TNT); - - projectiles.remove(event.getEntity().getUniqueId()); - - if (proj.PlaceBlocks) { - TNTPrimed tntEnt = (TNTPrimed) tnt; - tntEnt.setYield(0); - tntEnt.setFuseTicks(0); - /*if (SiegeEngines.towny != null) { - TownBlock block = TownyAPI.getInstance().getTownBlock(loc); - if (block != null) { - if (block.hasTown()) { - if (!block.getTownBlockOwner().getPermissions().explosion) { - return; - } - } - } - }*/ - if (event.getHitBlock() != null) { - List Blocks = sphere(event.getHitBlock().getLocation(), (int) proj.ExplodePower); - for (int i = 0; i < proj.BlocksToPlaceAmount; i++) { - Block replace = getRandomElement(Blocks); - replace.setType(proj.BlockToPlace); - } - } - } else { - TNTPrimed tntEnt = (TNTPrimed) tnt; - tntEnt.setYield(proj.ExplodePower); - tntEnt.setFuseTicks(0); - } - } - } - - public ArrayList sphere(final Location center, final int radius) { - ArrayList sphere = new ArrayList(); - for (int Y = -radius; Y < radius; Y++) { - for (int X = -radius; X < radius; X++) { - for (int Z = -radius; Z < radius; Z++) { - if (Math.sqrt((X * X) + (Y * Y) + (Z * Z)) <= radius) { - final Block block = center.getWorld().getBlockAt(X + center.getBlockX(), Y + center.getBlockY(), Z + center.getBlockZ()); - if (block.getType() == Material.AIR) { - sphere.add(block); - } - } - } - } - } - return sphere; - } - - public Block getRandomElement(List list) { - Random rand = new Random(); - return list.get(rand.nextInt(list.size())); - } - - @EventHandler(priority = EventPriority.HIGH) - public void BlockPlaceEvent(org.bukkit.event.block.BlockPlaceEvent event) { - Player thePlayer = event.getPlayer(); - if (event.isCancelled()) - return; - if (event.getPlayer().getInventory().getItemInMainHand().getType() == Material.CARVED_PUMPKIN) { - ItemStack item = event.getPlayer().getInventory().getItemInMainHand(); - if (item.getItemMeta() != null && item.getItemMeta().hasCustomModelData()) { - int customModel = item.getItemMeta().getCustomModelData(); - Boolean created = SiegeEngines.CreateCannon(thePlayer, customModel, event.getBlockAgainst().getLocation()); - if (created) { - item.setAmount(item.getAmount() - 1); - thePlayer.getInventory().setItemInMainHand(item); - thePlayer.sendMessage("§eGunner Equipment spawned!"); - } else { - thePlayer.sendMessage("§eGunner Equipment could not be spawned, is it enabled?"); - } - event.setCancelled(true); - } - } - } - - - public void Shoot(Entity player, long delay) { - float actualDelay = delay; - Boolean FirstShot = true; - int counter = 0; - if (SiegeEngines.TrackedStands.get(player.getUniqueId()) == null) { - for (Entity ent : player.getNearbyEntities(5, 5, 5)) { - if (ent instanceof ArmorStand) { - TakeControl(player, ent); - } - } - } - if (SiegeEngines.TrackedStands.get(player.getUniqueId()) == null) - return; - List entities = new ArrayList<>(SiegeEngines.TrackedStands.get(player.getUniqueId())); - for (Entity ent : entities) { - if (ent == null || ent.isDead()) { - SiegeEngines.TrackedStands.get(player.getUniqueId()).remove(ent); - continue; - } - if (((LivingEntity) ent).getEquipment().getHelmet().getType() != Material.CARVED_PUMPKIN) { - SiegeEngines.TrackedStands.get(player.getUniqueId()).remove(ent); - continue; - } - double distance = player.getLocation().distance(ent.getLocation()); - if (distance >= 127) { - continue; - } - SiegeEquipment siege = SiegeEngines.equipment.get(ent.getUniqueId()); - if (counter > 4) - return; - if (ent == null || ent.isDead()) { - SiegeEngines.TrackedStands.get(player.getUniqueId()).remove(ent); - continue; - } - if (siege.isLoaded()) { - if (player instanceof Player) { - siege.Fire(player, actualDelay, 1); - actualDelay += delay; - } else { - siege.Fire(player, 15, 1); - } - counter++; - } else { - if (player instanceof Player) { - } - //player.sendMessage("§eAmmunition Not Loaded"); - } - // player.sendMessage(String.format("§e" +actualDelay)); - - } - } - - public static ItemStack[] updateContents(Inventory inv, ItemStack m, int toRemove) { - ItemStack[] contents = inv.getStorageContents(); - for (int i = 0; i < contents.length; i++) { - if (contents[i] == null) continue; - if (contents[i].isSimilar(m)) { - System.out.println(m.toString()); - int amountInInv = contents[i].getAmount(); - if (toRemove >= amountInInv) { - contents[i].setType(Material.AIR); - toRemove -= amountInInv; - } else { - contents[i].setAmount(amountInInv - toRemove); - toRemove = 0; - break; - } - } - if (toRemove <= 0) break; - } - return contents; - } - - public static boolean hasItem(Inventory inv, ItemStack m) { - ItemStack[] contents = inv.getStorageContents(); - for(int i = 0; i < contents.length; i++) { - if(contents[i] == null) continue; - if(contents[i].isSimilar(m)) { - System.out.println(m.toString()); - return true; - } - } - return false; - } - - @EventHandler - public void interact(PlayerInteractEvent event) { - Player player = event.getPlayer(); - - ItemStack ItemInHand = event.getPlayer().getInventory().getItemInMainHand(); - if (ItemInHand == null) { - return; - } - - // if (ItemInHand.getType() == Material.PAPER) { - // ItemMeta meta = ItemInHand.getItemMeta(); - // if (meta.hasCustomModelData() && meta.getCustomModelData() == 505050505) { - // SiegeEngines.CreateCannon(player); - // ItemInHand.setAmount(ItemInHand.getAmount() - 1); - // return; - // } - // } - - - if (event.getAction() == Action.RIGHT_CLICK_AIR) { - if (ItemInHand.getType() != Config.controlItem) { - return; - } - if (!player.isSneaking()) - Shoot((Entity) player, 7); - if (SiegeEngines.TrackedStands.containsKey(player.getUniqueId())) { - List entities = SiegeEngines.TrackedStands.get(player.getUniqueId()); - for (Entity entity : entities) { - boolean foundAmmo = false; - if (SiegeEngines.equipment.containsKey(entity.getUniqueId())) { - SiegeEquipment equip = SiegeEngines.equipment.get(entity.getUniqueId()); - if (equip == null || !equip.Enabled) { - continue; - } - if (entity.getLocation().getBlock().getType() == Material.BARREL || entity.getLocation().getBlock().getType() == Material.CHEST) { - Block inv = entity.getLocation().getBlock(); - Container state = ((Container) inv.getState()); - for (ItemStack stack : equip.Projectiles.keySet()) { - if (foundAmmo) - continue; - if (!hasItem((Inventory)state.getInventory(),stack)) - continue; - state.getInventory().removeItem(stack);//setStorageContents(updateContents(state.getInventory(),stack,1)); - //state.update(true,true); - equip.AmmoHolder.LoadedProjectile = 1; - equip.AmmoHolder.MaterialName = stack; - foundAmmo = true; - } - //state.update(true,true); - } - if (foundAmmo) - continue; - if (entity.getLocation().getBlock().getRelative(0, -1, 0).getType() == Material.BARREL || entity.getLocation().getBlock().getRelative(0, -1, 0).getType() == Material.CHEST) { - Block inv = entity.getLocation().getBlock().getRelative(0, -1, 0); - Container state = ((Container) inv.getState()); - for (ItemStack stack : equip.Projectiles.keySet()) { - if (foundAmmo) - continue; - if (!hasItem((Inventory)state.getInventory(),stack)) - continue; - state.getInventory().removeItem(stack);//setStorageContents(updateContents(state.getInventory(),stack,1)); - //state.update(true,true); - equip.AmmoHolder.LoadedProjectile = 1; - equip.AmmoHolder.MaterialName = stack; - foundAmmo = true; - } - //state.update(true,true); - } - if (foundAmmo) - continue; - if (entity.getLocation().getBlock().getRelative(0, 1, 0).getType() == Material.BARREL || entity.getLocation().getBlock().getRelative(0, 1, 0).getType() == Material.CHEST) { - Block inv = entity.getLocation().getBlock().getRelative(0, 1, 0); - Container state = ((Container) inv.getState()); - for (ItemStack stack : equip.Projectiles.keySet()) { - if (foundAmmo) - continue; - if (!hasItem((Inventory)state.getInventory(),stack)) - continue; - state.getInventory().removeItem(stack);//setStorageContents(updateContents(state.getInventory(),stack,1)); - //state.update(true,true); - equip.AmmoHolder.LoadedProjectile = 1; - equip.AmmoHolder.MaterialName = stack; - foundAmmo = true; - } - //state.update(true,true); - } - if (foundAmmo) - continue; - if (!player.isSneaking()) - return; - if (!foundAmmo) { - for (ItemStack inventoryItem : event.getPlayer().getInventory().getContents()) { - for (ItemStack stack : equip.Projectiles.keySet()) { - if (foundAmmo) - continue; - if (!hasItem(event.getPlayer().getInventory(),stack)) - continue; - if (stack.isSimilar(inventoryItem) && equip.AmmoHolder.LoadedProjectile == 0) { - equip.AmmoHolder.LoadedProjectile = 1; - equip.AmmoHolder.MaterialName = stack; - inventoryItem.setAmount(inventoryItem.getAmount() - 1); - foundAmmo = true; - } - } - } - } - } - } - player.sendMessage("§eReloading Ammunition"); - for (Entity entity : entities) { - boolean foundAmmo = false; - if (SiegeEngines.equipment.containsKey(entity.getUniqueId())) { - SiegeEquipment equip = SiegeEngines.equipment.get(entity.getUniqueId()); - if (equip == null || !equip.Enabled) { - continue; - } - if (entity.getLocation().getBlock().getType() == Material.BARREL || entity.getLocation().getBlock().getType() == Material.CHEST) { - Block inv = entity.getLocation().getBlock(); - Container state = ((Container) inv.getState()); - ItemStack stack = equip.FuelMaterial; - if (foundAmmo) - continue; - if (!hasItem((Inventory)state.getInventory(),stack)) - continue; - state.getInventory().removeItem(stack);//setStorageContents(updateContents(state.getInventory(),stack,1)); - //state.update(true,true); - equip.AmmoHolder.LoadedFuel += 1; - equip.AmmoHolder.MaterialName = stack; - foundAmmo = true; - //state.update(true,true); - } - if (entity.getLocation().getBlock().getRelative(0, -1, 0).getType() == Material.BARREL || entity.getLocation().getBlock().getRelative(0, -1, 0).getType() == Material.CHEST) { - Block inv = entity.getLocation().getBlock().getRelative(0, -1, 0);; - Container state = ((Container) inv.getState()); - ItemStack stack = equip.FuelMaterial; - if (foundAmmo) - continue; - if (!hasItem((Inventory)state.getInventory(),stack)) - continue; - state.getInventory().removeItem(stack);//setStorageContents(updateContents(state.getInventory(),stack,1)); - //state.update(true,true); - equip.AmmoHolder.LoadedFuel += 1; - equip.AmmoHolder.MaterialName = stack; - foundAmmo = true; - //state.update(true,true); - } - if (entity.getLocation().getBlock().getRelative(0, 1, 0).getType() == Material.BARREL || entity.getLocation().getBlock().getRelative(0, 1, 0).getType() == Material.CHEST) { - Block inv = entity.getLocation().getBlock().getRelative(0, 1, 0); - Container state = ((Container) inv.getState()); - ItemStack stack = equip.FuelMaterial; - if (foundAmmo) - continue; - if (!hasItem((Inventory)state.getInventory(),stack)) - continue; - state.getInventory().removeItem(stack);//setStorageContents(updateContents(state.getInventory(),stack,1)); - //state.update(true,true); - equip.AmmoHolder.LoadedFuel += 1; - equip.AmmoHolder.MaterialName = stack; - foundAmmo = true; - //state.update(true,true); - } - if (foundAmmo) - continue; - if (!player.isSneaking()) - return; - if (!foundAmmo) { - for (ItemStack inventoryItem : event.getPlayer().getInventory().getContents()) { - if (foundAmmo) - continue; - if (!equip.CanLoadFuel()) - continue; - if (equip.FuelMaterial.isSimilar(inventoryItem)) { - equip.AmmoHolder.LoadedFuel += 1; - inventoryItem.setAmount(inventoryItem.getAmount() - 1); - foundAmmo = true; - } - } - } - } - } - player.sendMessage("§eReloading Propellant"); - return; - } - - } - } - - @EventHandler(priority = EventPriority.NORMAL) - public void onSignChange(SignChangeEvent event) { - String topline = event.getLine(0); - if (topline == null) topline = ""; - Player player = event.getPlayer(); - String toplinetrimmed = topline.trim(); - if (toplinetrimmed.equals("[Cannon]")) { - SaveCannons(player, event.getBlock()); - } - - } - - @EventHandler - public void damage(EntityDamageByEntityEvent event) { - if (event.getEntity() instanceof ArmorStand) { - if (event.getDamager() instanceof Projectile) { - event.setDamage(0); - event.setCancelled(true); - } - } - } - - - public static void TakeControl(Entity player, Entity entity) { - LivingEntity living = (LivingEntity) entity; - if (SiegeEngines.TrackedStands.containsKey(player.getUniqueId())) { - List entities = SiegeEngines.TrackedStands.get(player.getUniqueId()); - if (entities.contains(entity)) { - return; - } - } - - if (living.getEquipment().getHelmet() != null && living.getEquipment().getHelmet().getType() == Material.CARVED_PUMPKIN) { - if (living.getEquipment() == null || living.getEquipment().getHelmet() == null || living.getEquipment().getHelmet().getItemMeta() == null) { - return; - } - - ArmorStand stand = (ArmorStand) entity; - SiegeEquipment equip; - stand.addEquipmentLock(EquipmentSlot.HEAD, LockType.REMOVING_OR_CHANGING); - stand.addEquipmentLock(EquipmentSlot.LEGS, LockType.ADDING_OR_CHANGING); - stand.addEquipmentLock(EquipmentSlot.CHEST, LockType.ADDING_OR_CHANGING); - stand.addEquipmentLock(EquipmentSlot.FEET, LockType.ADDING_OR_CHANGING); - stand.setBasePlate(false); - - if (SiegeEngines.equipment.containsKey(entity.getUniqueId())) { - equip = SiegeEngines.equipment.get(entity.getUniqueId()); - if (equip == null || !equip.Enabled) { - return; - } - } else { - equip = SiegeEngines.CreateClone(living.getEquipment().getHelmet().getItemMeta().getCustomModelData()); - if (equip == null || !equip.Enabled) { - return; - } - equip.AmmoHolder = new EquipmentMagazine(); - equip.Entity = entity; - equip.EntityId = entity.getUniqueId(); - } - stand.addEquipmentLock(EquipmentSlot.HEAD, LockType.REMOVING_OR_CHANGING); - stand.addEquipmentLock(EquipmentSlot.LEGS, LockType.ADDING_OR_CHANGING); - stand.addEquipmentLock(EquipmentSlot.CHEST, LockType.ADDING_OR_CHANGING); - stand.addEquipmentLock(EquipmentSlot.FEET, LockType.ADDING_OR_CHANGING); - stand.setBasePlate(false); - if (SiegeEngines.TrackedStands.containsKey(player.getUniqueId())) { - List entities = SiegeEngines.TrackedStands.get(player.getUniqueId()); - entities.add(entity); - SiegeEngines.TrackedStands.put(player.getUniqueId(), entities); - } else { - List newList = new ArrayList(); - newList.add(entity); - SiegeEngines.TrackedStands.put(player.getUniqueId(), newList); - } - SiegeEngines.equipment.put(entity.getUniqueId(), equip); - //player.sendMessage("§eNow controlling the equipment."); - } - } - - public void SaveCannons(Player player, Block block) { - List Ids = new ArrayList(); - if (!SiegeEngines.TrackedStands.containsKey(player.getUniqueId())) { - return; - } - for (Entity ent : SiegeEngines.TrackedStands.get(player.getUniqueId())) { - Ids.add(ent.getUniqueId().toString()); - } - TileState state = ((TileState) block.getState()); - NamespacedKey key = new NamespacedKey(SiegeEngines.plugin, "cannons"); - - for (Entity ent : SiegeEngines.TrackedStands.get(player.getUniqueId())) { - if (ent.isDead()) { - continue; - } - DoAimUp(ent, 0, player); - } - } - - public void DoAimUp(Entity ent, float amount, Entity player) { - Location loc = ent.getLocation(); - ArmorStand stand = (ArmorStand) ent; - // player.sendMessage(String.format("" + loc.getPitch())); - if (loc.getPitch() == -85 || loc.getPitch() - amount < -85) { - return; - } - loc.setPitch((float) (loc.getPitch() - amount)); - SiegeEquipment equipment = SiegeEngines.equipment.get(ent.getUniqueId()); - if (equipment != null) { - if (player instanceof Player) - equipment.ShowFireLocation(player); - if (equipment.RotateStandHead) { - stand.setHeadPose(new EulerAngle(loc.getDirection().getY() * (-1), 0, 0)); - } - ent.teleport(loc); - } - } - - public void DoAimDown(Entity ent, float amount, Entity player) { - Location loc = ent.getLocation(); - ArmorStand stand = (ArmorStand) ent; - // player.sendMessage(String.format("" + loc.getPitch())); - if (loc.getPitch() == 85 || loc.getPitch() - amount < 65) { - return; - } - loc.setPitch((float) (loc.getPitch() - amount)); - SiegeEquipment equipment = SiegeEngines.equipment.get(ent.getUniqueId()); - if (equipment != null) { - if (player instanceof Player) - equipment.ShowFireLocation(player); - if (equipment.RotateStandHead) { - stand.setHeadPose(new EulerAngle(loc.getDirection().getY() * (-1), 0, 0)); - } - ent.teleport(loc); - } - } - - public void FirstShotDoAimDown(Entity ent, float amount, Entity player) { - Location loc = ent.getLocation(); - ArmorStand stand = (ArmorStand) ent; - // player.sendMessage(String.format("" + loc.getPitch())); - if (loc.getPitch() == 85 || loc.getPitch() + amount > 85) { - return; - } - SiegeEquipment equipment = SiegeEngines.equipment.get(ent.getUniqueId()); - if (equipment != null) { - if (player instanceof Player) { - equipment.ShowFireLocation((Player) player); - } - if (equipment.RotateStandHead) { - stand.setHeadPose(new EulerAngle(loc.getDirection().getY() * (-1), 0, 0)); - - } - } - loc.setPitch((float) (loc.getPitch() + amount)); - - ent.teleport(loc); - } - - NamespacedKey key = new NamespacedKey(SiegeEngines.plugin, "cannons"); - - @EventHandler - public void DeathEvent(EntityDeathEvent event) { - Boolean removeStands = false; - List items = event.getDrops(); - if (event.getEntity() instanceof ArmorStand) { - if (event.getEntity().getPersistentDataContainer().has(key, PersistentDataType.STRING)) { - Entity base = Bukkit.getEntity(UUID.fromString(event.getEntity().getPersistentDataContainer().get(key, PersistentDataType.STRING))); - base.remove(); - } - if (SiegeEngines.equipment.containsKey(event.getEntity().getUniqueId())) { - removeStands = true; - } else { - for (ItemStack i : items) { - if (i.getType() == Material.CARVED_PUMPKIN && i.hasItemMeta() && i.getItemMeta().hasCustomModelData()) { - if (SiegeEngines.DefinedEquipment.containsKey(i.getItemMeta().getCustomModelData())) { - removeStands = true; - break; - } - - } - } - } - if (removeStands) { - for (ItemStack i : items) { - if (i.getType() == Material.ARMOR_STAND) { - i.setAmount(0); - return; - } - } - } - } - } - - public void AimUp(Entity player, float amount) { - - for (Entity ent : SiegeEngines.TrackedStands.get(player.getUniqueId())) { - if (ent.isDead()) { - continue; - } - DoAimUp(ent, amount, player); - } - } - - public void AimDown(Entity player, float amount) { - - for (Entity ent : SiegeEngines.TrackedStands.get(player.getUniqueId())) { - if (ent.isDead()) { - continue; - } - DoAimDown(ent, amount, player); - } - } - - public void LoadCannonsWithPowder(Entity player) { - for (Entity ent : SiegeEngines.TrackedStands.get(player.getUniqueId())) { - if (ent.isDead()) { - continue; - } - - SiegeEquipment equipment = SiegeEngines.equipment.get(ent.getUniqueId()); - if (equipment != null) { - equipment.LoadFuel(player); - } - } - } - - public void LoadCannonsWithProjectile(Entity player, ItemStack projectile) { - for (Entity ent : SiegeEngines.TrackedStands.get(player.getUniqueId())) { - if (ent.isDead()) { - continue; - } - SiegeEquipment equipment = SiegeEngines.equipment.get(ent.getUniqueId()); - if (equipment != null) { - equipment.LoadProjectile(player, projectile); - } - } - } - - @EventHandler - public void onPlayerClickSign(PlayerInteractEvent event) { - Player player = event.getPlayer(); - if (event.getClickedBlock() != null && event.getClickedBlock().getType().toString().contains("SIGN")) { - Sign sign = (Sign) event.getClickedBlock().getState(); - if (sign.getLine(0).equalsIgnoreCase("[Fire]") && event.getAction() == Action.RIGHT_CLICK_BLOCK) { - if (!SiegeEngines.TrackedStands.containsKey(player.getUniqueId())) { - return; - } - try { - Long delay = Long.parseLong(sign.getLine(1)); - if (delay < 6) { - delay = 6l; - } - Shoot((Entity) player, delay); - } catch (Exception e) { - Shoot((Entity) player, 6); - } - return; - } - if (sign.getLine(0).equalsIgnoreCase("[Load]")) { - if (event.getAction() == Action.LEFT_CLICK_BLOCK) { - LoadCannonsWithPowder((Entity) player); - return; - } - if (event.getAction() == Action.RIGHT_CLICK_BLOCK) { - //load proj - ItemStack itemInHand = player.getInventory().getItemInMainHand(); - if (itemInHand == null) { - return; - } - - - LoadCannonsWithProjectile((Entity) player, player.getInventory().getItemInMainHand()); - return; - } - } - - if (sign.getLine(0).equalsIgnoreCase("[Aim]")) { - if (!SiegeEngines.TrackedStands.containsKey(player.getUniqueId())) { - return; - } - float amount; - try { - amount = Float.parseFloat(sign.getLine(1)); - if (player.isSneaking()) { - AimDown((Entity) player, amount); - } else { - AimUp((Entity) player, amount); - } - } catch (Exception e) { - // TODO Auto-generated catch block - player.sendMessage("§eCould not parse number on second line."); - } - - return; - } - - if (sign.getLine(0).equalsIgnoreCase("[Cannon]")) { - if (event.getAction() == Action.LEFT_CLICK_BLOCK) { - SiegeEngines.TrackedStands.remove(player.getUniqueId()); - player.sendMessage("§eReleasing the equipment!"); - return; - } - - if (event.getAction() == Action.RIGHT_CLICK_BLOCK) { - if (player.isSneaking()) { - SaveCannons(player, event.getClickedBlock()); - return; - } - - NamespacedKey key = new NamespacedKey(SiegeEngines.plugin, "cannons"); - TileState state = (TileState) sign.getBlock().getState(); - SiegeEngines.TrackedStands.remove(player.getUniqueId()); - List temp = new ArrayList(); - if (!state.getPersistentDataContainer().has(key, PersistentDataType.STRING)) { - return; - } - String[] split = state.getPersistentDataContainer().get(key, PersistentDataType.STRING).replace("[", "").replace("]", "").split(","); - for (String s : split) { - temp.add(UUID.fromString(s.trim())); - } - for (UUID Id : temp) { - List entities = new ArrayList(); - Entity ent = Bukkit.getEntity(Id); - if (ent != null) { - TakeControl(player, ent); - } - } - player.sendMessage("§eNow controlling nearby equipment."); - return; - } - } - - } - } - - @EventHandler - public void onEntityClick(PlayerInteractAtEntityEvent event) { - - Player player = event.getPlayer(); - ItemStack itemInHand = player.getInventory().getItemInMainHand(); - Entity entity = event.getRightClicked(); - if (entity == null) { - return; - } - if (entity.getType() == EntityType.ARMOR_STAND) { - TakeControl(player, entity); - if (SiegeEngines.equipment.containsKey(entity.getUniqueId())) { - if (itemInHand.getType() == Material.CLOCK) { - if (player.isSneaking()) { - DoAimDown(entity, 1, player); - } else { - DoAimUp(entity, 1, player); - } - return; - } - - SiegeEquipment equipment = SiegeEngines.equipment.get(entity.getUniqueId()); - event.setCancelled(true); - - if (itemInHand == null || itemInHand.getType() == Material.AIR) { - ArmorStand stand = (ArmorStand) entity; - if (stand.isInvisible()) { - stand.setInvisible(false); - player.sendMessage("§eEquipment is now breakable"); - } else { - if (equipment.AllowInvisibleStand) { - stand.setInvisible(true); - player.sendMessage("§eEquipment is no longer breakable"); - } - } - return; - } - if (itemInHand.isSimilar(equipment.FuelMaterial)) { - if (!equipment.LoadFuel(player)) { - player.sendMessage("§eCould not load Propellant."); - return; - } - } - /*if (itemInHand.isSimilar(new ItemStack(Material.FLINT))) { - if (equipment.isLoaded()) { - equipment.Fire((Entity) player, 6, 1); - } - else { - player.sendMessage("§eEquipment is not loaded"); - } - return; - }*/ - for (ItemStack stack : equipment.Projectiles.keySet()) { - if (stack.isSimilar(itemInHand) && equipment.AmmoHolder.LoadedProjectile == 0) { - equipment.AmmoHolder.LoadedProjectile = 1; - equipment.AmmoHolder.MaterialName = stack; - player.sendMessage("§eAdding Ammo to Weapon"); - itemInHand.setAmount(itemInHand.getAmount() - 1); - return; - } - - } - player.sendMessage("§eNow controlling this equipment."); - return; - } - } - } -} +package com.github.alathra.siegeengines.listeners; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Random; +import java.util.UUID; + +import com.github.alathra.siegeengines.config.Config; +import com.github.alathra.siegeengines.EquipmentMagazine; +import com.github.alathra.siegeengines.SiegeEngines; +import org.bukkit.Bukkit; +import org.bukkit.Location; +import org.bukkit.Material; +import org.bukkit.NamespacedKey; +import org.bukkit.World; +import org.bukkit.block.Block; +import org.bukkit.block.Container; +import org.bukkit.block.Sign; +import org.bukkit.block.TileState; +import org.bukkit.entity.ArmorStand; +import org.bukkit.entity.ArmorStand.LockType; +import org.bukkit.entity.Entity; +import org.bukkit.entity.EntityType; +import org.bukkit.entity.LivingEntity; +import org.bukkit.entity.Player; +import org.bukkit.entity.Projectile; +import org.bukkit.entity.Snowball; +import org.bukkit.entity.TNTPrimed; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.Listener; +import org.bukkit.event.block.Action; +import org.bukkit.event.block.SignChangeEvent; +import org.bukkit.event.entity.EntityDamageByEntityEvent; +import org.bukkit.event.entity.EntityDeathEvent; +import org.bukkit.event.entity.EntityExplodeEvent; +import org.bukkit.event.entity.ProjectileHitEvent; +import org.bukkit.event.player.PlayerInteractAtEntityEvent; +import org.bukkit.event.player.PlayerInteractEvent; +import org.bukkit.inventory.EquipmentSlot; +import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.ItemStack; +import org.bukkit.persistence.PersistentDataType; +import org.bukkit.util.EulerAngle; + +import com.github.alathra.siegeengines.SiegeEquipment; +import com.github.alathra.siegeengines.projectile.ExplosiveProjectile; + +//import com.palmergames.bukkit.towny.TownyAPI; +//import com.palmergames.bukkit.towny.event.actions.TownyDestroyEvent; +//import com.palmergames.bukkit.towny.object.TownBlock; + +//import GunnersProjectiles.ExplosiveProjectile; + +public class ClickHandler implements Listener { + + public float MinDelay = 5; + + public static HashMap projectiles = new HashMap(); + + @EventHandler + public void onExplode(EntityExplodeEvent e) { + for (Entity entity : e.getEntity().getNearbyEntities(e.getYield() + 1, e.getYield() + 1, e.getYield() + 1)) { + if (entity instanceof ArmorStand) { + ArmorStand stand = (ArmorStand) entity; + /*if (SiegeEngines.towny != null) { + TownBlock block = TownyAPI.getInstance().getTownBlock(stand.getLocation()); + if (block != null) { + if (block.hasTown()) { + if (!block.getTownBlockOwner().getPermissions().explosion) { + return; + } + } + } + }*/ + stand.eject(); + stand.remove(); + } + } + } + + @EventHandler + public void onHit(ProjectileHitEvent event) { + if ((event.getEntity() instanceof Snowball) && projectiles.containsKey(event.getEntity().getUniqueId())) { + ExplosiveProjectile proj = projectiles.get(event.getEntity().getUniqueId()); + Entity snowball = event.getEntity(); + Snowball ball = (Snowball) snowball; + Entity player = (Entity) ball.getShooter(); + if (player instanceof Player) { + player.sendMessage("§eDistance to impact: " + String.format("%.2f", player.getLocation().distance(ball.getLocation()))); + } + Location loc = snowball.getLocation(); + World world = event.getEntity().getWorld(); + // world.createExplosion(loc, proj.Radius, proj.DoFire); + Entity tnt = event.getEntity().getWorld().spawnEntity(loc, EntityType.PRIMED_TNT); + + projectiles.remove(event.getEntity().getUniqueId()); + + if (proj.PlaceBlocks) { + TNTPrimed tntEnt = (TNTPrimed) tnt; + tntEnt.setYield(0); + tntEnt.setFuseTicks(0); + /*if (SiegeEngines.towny != null) { + TownBlock block = TownyAPI.getInstance().getTownBlock(loc); + if (block != null) { + if (block.hasTown()) { + if (!block.getTownBlockOwner().getPermissions().explosion) { + return; + } + } + } + }*/ + if (event.getHitBlock() != null) { + List Blocks = sphere(event.getHitBlock().getLocation(), (int) proj.ExplodePower); + for (int i = 0; i < proj.BlocksToPlaceAmount; i++) { + Block replace = getRandomElement(Blocks); + replace.setType(proj.BlockToPlace); + } + } + } else { + TNTPrimed tntEnt = (TNTPrimed) tnt; + tntEnt.setYield(proj.ExplodePower); + tntEnt.setFuseTicks(0); + } + } + } + + public ArrayList sphere(final Location center, final int radius) { + ArrayList sphere = new ArrayList(); + for (int Y = -radius; Y < radius; Y++) { + for (int X = -radius; X < radius; X++) { + for (int Z = -radius; Z < radius; Z++) { + if (Math.sqrt((X * X) + (Y * Y) + (Z * Z)) <= radius) { + final Block block = center.getWorld().getBlockAt(X + center.getBlockX(), Y + center.getBlockY(), Z + center.getBlockZ()); + if (block.getType() == Material.AIR) { + sphere.add(block); + } + } + } + } + } + return sphere; + } + + public Block getRandomElement(List list) { + Random rand = new Random(); + return list.get(rand.nextInt(list.size())); + } + + @EventHandler(priority = EventPriority.HIGH) + public void BlockPlaceEvent(org.bukkit.event.block.BlockPlaceEvent event) { + Player thePlayer = event.getPlayer(); + if (event.isCancelled()) + return; + if (event.getPlayer().getInventory().getItemInMainHand().getType() == Material.CARVED_PUMPKIN) { + ItemStack item = event.getPlayer().getInventory().getItemInMainHand(); + if (item.getItemMeta() != null && item.getItemMeta().hasCustomModelData()) { + int customModel = item.getItemMeta().getCustomModelData(); + Boolean created = SiegeEngines.CreateCannon(thePlayer, customModel, event.getBlockAgainst().getLocation()); + if (created) { + item.setAmount(item.getAmount() - 1); + thePlayer.getInventory().setItemInMainHand(item); + thePlayer.sendMessage("§eGunner Equipment spawned!"); + } else { + thePlayer.sendMessage("§eGunner Equipment could not be spawned, is it enabled?"); + } + event.setCancelled(true); + } + } + } + + + public void Shoot(Entity player, long delay) { + float actualDelay = delay; + Boolean FirstShot = true; + int counter = 0; + if (SiegeEngines.TrackedStands.get(player.getUniqueId()) == null) { + for (Entity ent : player.getNearbyEntities(5, 5, 5)) { + if (ent instanceof ArmorStand) { + TakeControl(player, ent); + } + } + } + if (SiegeEngines.TrackedStands.get(player.getUniqueId()) == null) + return; + List entities = new ArrayList<>(SiegeEngines.TrackedStands.get(player.getUniqueId())); + for (Entity ent : entities) { + if (ent == null || ent.isDead()) { + SiegeEngines.TrackedStands.get(player.getUniqueId()).remove(ent); + continue; + } + if (((LivingEntity) ent).getEquipment().getHelmet().getType() != Material.CARVED_PUMPKIN) { + SiegeEngines.TrackedStands.get(player.getUniqueId()).remove(ent); + continue; + } + double distance = player.getLocation().distance(ent.getLocation()); + if (distance >= 64) { + continue; + } + SiegeEquipment siege = SiegeEngines.equipment.get(ent.getUniqueId()); + if (counter > 4) + return; + if (ent == null || ent.isDead()) { + SiegeEngines.TrackedStands.get(player.getUniqueId()).remove(ent); + continue; + } + if (siege.isLoaded()) { + if (player instanceof Player) { + siege.Fire(player, actualDelay, 1); + actualDelay += delay; + } else { + siege.Fire(player, 15, 1); + } + counter++; + } else { + if (player instanceof Player) { + } + //player.sendMessage("§eAmmunition Not Loaded"); + } + // player.sendMessage(String.format("§e" +actualDelay)); + + } + } + + public static ItemStack[] updateContents(Inventory inv, ItemStack m, int toRemove) { + ItemStack[] contents = inv.getStorageContents(); + for (int i = 0; i < contents.length; i++) { + if (contents[i] == null) continue; + if (contents[i].isSimilar(m)) { + int amountInInv = contents[i].getAmount(); + if (toRemove >= amountInInv) { + contents[i].setType(Material.AIR); + toRemove -= amountInInv; + } else { + contents[i].setAmount(amountInInv - toRemove); + toRemove = 0; + break; + } + } + if (toRemove <= 0) break; + } + return contents; + } + + public static boolean hasItem(Inventory inv, ItemStack m) { + ItemStack[] contents = inv.getStorageContents(); + for(int i = 0; i < contents.length; i++) { + if(contents[i] == null) continue; + if(contents[i].isSimilar(m)) { + return true; + } + } + return false; + } + + @EventHandler + public void interact(PlayerInteractEvent event) { + Player player = event.getPlayer(); + + ItemStack ItemInHand = event.getPlayer().getInventory().getItemInMainHand(); + if (ItemInHand == null) { + return; + } + + // if (ItemInHand.getType() == Material.PAPER) { + // ItemMeta meta = ItemInHand.getItemMeta(); + // if (meta.hasCustomModelData() && meta.getCustomModelData() == 505050505) { + // SiegeEngines.CreateCannon(player); + // ItemInHand.setAmount(ItemInHand.getAmount() - 1); + // return; + // } + // } + + + if (event.getAction() == Action.RIGHT_CLICK_AIR) { + if (ItemInHand.getType() != Config.controlItem) { + return; + } + if (!player.isSneaking()) { + List entities = SiegeEngines.TrackedStands.get(player.getUniqueId()); + for (Entity entity : entities) { + if (SiegeEngines.equipment.containsKey(entity.getUniqueId())) { + SiegeEquipment equip = SiegeEngines.equipment.get(entity.getUniqueId()); + if (equip != null && equip.Enabled && !(entity.isDead())) { + equip.Fire(player,10f,1); + } + } + } + } + if (SiegeEngines.TrackedStands.containsKey(player.getUniqueId())) { + List entities = SiegeEngines.TrackedStands.get(player.getUniqueId()); + player.sendMessage("§eReloading Ammunition"); + for (Entity entity : entities) { + boolean foundAmmo = false; + if ((entity.isDead())) continue; + if (SiegeEngines.equipment.containsKey(entity.getUniqueId())) { + SiegeEquipment equip = SiegeEngines.equipment.get(entity.getUniqueId()); + if (equip == null || !equip.Enabled) { + continue; + } + if (entity.getLocation().getBlock().getType() == Material.BARREL || entity.getLocation().getBlock().getType() == Material.CHEST) { + Block inv = entity.getLocation().getBlock(); + Container state = ((Container) inv.getState()); + for (ItemStack stack : equip.Projectiles.keySet()) { + if (foundAmmo) + continue; + if (!hasItem((Inventory)state.getInventory(),stack)) + continue; + state.getInventory().removeItem(stack);//setStorageContents(updateContents(state.getInventory(),stack,1)); + //state.update(true,true); + equip.AmmoHolder.LoadedProjectile = 1; + equip.AmmoHolder.MaterialName = stack; + foundAmmo = true; + } + //state.update(true,true); + } + if (foundAmmo) + continue; + if (entity.getLocation().getBlock().getRelative(0, -1, 0).getType() == Material.BARREL || entity.getLocation().getBlock().getRelative(0, -1, 0).getType() == Material.CHEST) { + Block inv = entity.getLocation().getBlock().getRelative(0, -1, 0); + Container state = ((Container) inv.getState()); + for (ItemStack stack : equip.Projectiles.keySet()) { + if (foundAmmo) + continue; + if (!hasItem((Inventory)state.getInventory(),stack)) + continue; + state.getInventory().removeItem(stack);//setStorageContents(updateContents(state.getInventory(),stack,1)); + //state.update(true,true); + equip.AmmoHolder.LoadedProjectile = 1; + equip.AmmoHolder.MaterialName = stack; + foundAmmo = true; + } + //state.update(true,true); + } + if (foundAmmo) + continue; + if (entity.getLocation().getBlock().getRelative(0, 1, 0).getType() == Material.BARREL || entity.getLocation().getBlock().getRelative(0, 1, 0).getType() == Material.CHEST) { + Block inv = entity.getLocation().getBlock().getRelative(0, 1, 0); + Container state = ((Container) inv.getState()); + for (ItemStack stack : equip.Projectiles.keySet()) { + if (foundAmmo) + continue; + if (!hasItem((Inventory)state.getInventory(),stack)) + continue; + state.getInventory().removeItem(stack);//setStorageContents(updateContents(state.getInventory(),stack,1)); + //state.update(true,true); + equip.AmmoHolder.LoadedProjectile = 1; + equip.AmmoHolder.MaterialName = stack; + foundAmmo = true; + } + //state.update(true,true); + } + if (foundAmmo) + continue; + if (!player.isSneaking()) + return; + if (!foundAmmo) { + for (ItemStack inventoryItem : event.getPlayer().getInventory().getContents()) { + for (ItemStack stack : equip.Projectiles.keySet()) { + if (foundAmmo) + continue; + if (!hasItem(event.getPlayer().getInventory(),stack)) + continue; + if (stack.isSimilar(inventoryItem) && equip.AmmoHolder.LoadedProjectile == 0) { + equip.AmmoHolder.LoadedProjectile = 1; + equip.AmmoHolder.MaterialName = stack; + inventoryItem.setAmount(inventoryItem.getAmount() - 1); + foundAmmo = true; + } + } + } + } + } + } + player.sendMessage("§eReloading Propellant"); + for (Entity entity : entities) { + boolean foundAmmo = false; + if ((entity.isDead())) continue; + if (SiegeEngines.equipment.containsKey(entity.getUniqueId())) { + SiegeEquipment equip = SiegeEngines.equipment.get(entity.getUniqueId()); + if (equip == null || !equip.Enabled) { + continue; + } + if (entity.getLocation().getBlock().getType() == Material.BARREL || entity.getLocation().getBlock().getType() == Material.CHEST) { + Block inv = entity.getLocation().getBlock(); + Container state = ((Container) inv.getState()); + ItemStack stack = equip.FuelMaterial; + if (foundAmmo) + continue; + if (!hasItem((Inventory)state.getInventory(),stack)) + continue; + state.getInventory().removeItem(stack);//setStorageContents(updateContents(state.getInventory(),stack,1)); + //state.update(true,true); + equip.AmmoHolder.LoadedFuel += 1; + foundAmmo = true; + //state.update(true,true); + } + if (entity.getLocation().getBlock().getRelative(0, -1, 0).getType() == Material.BARREL || entity.getLocation().getBlock().getRelative(0, -1, 0).getType() == Material.CHEST) { + Block inv = entity.getLocation().getBlock().getRelative(0, -1, 0);; + Container state = ((Container) inv.getState()); + ItemStack stack = equip.FuelMaterial; + if (foundAmmo) + continue; + if (!hasItem((Inventory)state.getInventory(),stack)) + continue; + state.getInventory().removeItem(stack);//setStorageContents(updateContents(state.getInventory(),stack,1)); + //state.update(true,true); + equip.AmmoHolder.LoadedFuel += 1; + foundAmmo = true; + //state.update(true,true); + } + if (entity.getLocation().getBlock().getRelative(0, 1, 0).getType() == Material.BARREL || entity.getLocation().getBlock().getRelative(0, 1, 0).getType() == Material.CHEST) { + Block inv = entity.getLocation().getBlock().getRelative(0, 1, 0); + Container state = ((Container) inv.getState()); + ItemStack stack = equip.FuelMaterial; + if (foundAmmo) + continue; + if (!hasItem((Inventory)state.getInventory(),stack)) + continue; + state.getInventory().removeItem(stack);//setStorageContents(updateContents(state.getInventory(),stack,1)); + //state.update(true,true); + equip.AmmoHolder.LoadedFuel += 1; + foundAmmo = true; + //state.update(true,true); + } + if (foundAmmo) + continue; + if (!player.isSneaking()) + return; + if (!foundAmmo) { + for (ItemStack inventoryItem : event.getPlayer().getInventory().getContents()) { + if (foundAmmo) + continue; + if (!equip.CanLoadFuel()) + continue; + if (equip.FuelMaterial.isSimilar(inventoryItem)) { + equip.AmmoHolder.LoadedFuel += 1; + inventoryItem.setAmount(inventoryItem.getAmount() - 1); + foundAmmo = true; + } + } + } + } + } + return; + } + + } + } + + @EventHandler(priority = EventPriority.NORMAL) + public void onSignChange(SignChangeEvent event) { + String topline = event.getLine(0); + if (topline == null) topline = ""; + Player player = event.getPlayer(); + String toplinetrimmed = topline.trim(); + if (toplinetrimmed.equalsIgnoreCase("[Cannon]")) { + SaveCannons(player, event.getBlock()); + } + + } + + @EventHandler + public void damage(EntityDamageByEntityEvent event) { + if (event.getEntity() instanceof ArmorStand) { + if (event.getDamager() instanceof Projectile) { + event.setDamage(0); + event.setCancelled(true); + } + } + } + + + public static void TakeControl(Entity player, Entity entity) { + LivingEntity living = (LivingEntity) entity; + if (SiegeEngines.TrackedStands.containsKey(player.getUniqueId())) { + List entities = SiegeEngines.TrackedStands.get(player.getUniqueId()); + if (entities.contains(entity)) { + return; + } + } + + if (living.getEquipment().getHelmet() != null && living.getEquipment().getHelmet().getType() == Material.CARVED_PUMPKIN) { + if (living.getEquipment() == null || living.getEquipment().getHelmet() == null || living.getEquipment().getHelmet().getItemMeta() == null) { + return; + } + + ArmorStand stand = (ArmorStand) entity; + SiegeEquipment equip; + stand.addEquipmentLock(EquipmentSlot.HEAD, LockType.REMOVING_OR_CHANGING); + stand.addEquipmentLock(EquipmentSlot.LEGS, LockType.ADDING_OR_CHANGING); + stand.addEquipmentLock(EquipmentSlot.CHEST, LockType.ADDING_OR_CHANGING); + stand.addEquipmentLock(EquipmentSlot.FEET, LockType.ADDING_OR_CHANGING); + stand.setBasePlate(false); + + if (SiegeEngines.equipment.containsKey(entity.getUniqueId())) { + equip = SiegeEngines.equipment.get(entity.getUniqueId()); + if (equip == null || !equip.Enabled) { + return; + } + } else { + equip = SiegeEngines.CreateClone(living.getEquipment().getHelmet().getItemMeta().getCustomModelData()); + if (equip == null || !equip.Enabled) { + return; + } + equip.AmmoHolder = new EquipmentMagazine(); + equip.Entity = entity; + equip.EntityId = entity.getUniqueId(); + } + stand.addEquipmentLock(EquipmentSlot.HEAD, LockType.REMOVING_OR_CHANGING); + stand.addEquipmentLock(EquipmentSlot.LEGS, LockType.ADDING_OR_CHANGING); + stand.addEquipmentLock(EquipmentSlot.CHEST, LockType.ADDING_OR_CHANGING); + stand.addEquipmentLock(EquipmentSlot.FEET, LockType.ADDING_OR_CHANGING); + stand.setBasePlate(false); + if (SiegeEngines.TrackedStands.containsKey(player.getUniqueId())) { + List entities = SiegeEngines.TrackedStands.get(player.getUniqueId()); + entities.add(entity); + SiegeEngines.TrackedStands.put(player.getUniqueId(), entities); + } else { + List newList = new ArrayList(); + newList.add(entity); + SiegeEngines.TrackedStands.put(player.getUniqueId(), newList); + } + SiegeEngines.equipment.put(entity.getUniqueId(), equip); + //player.sendMessage("§eNow controlling the equipment."); + } + } + + public void SaveCannons(Player player, Block block) { + List Ids = new ArrayList(); + if (!SiegeEngines.TrackedStands.containsKey(player.getUniqueId())) { + return; + } + for (Entity ent : SiegeEngines.TrackedStands.get(player.getUniqueId())) { + if (ent.isDead()) { + continue; + } + Ids.add(ent.getUniqueId().toString()); + } + TileState state = ((TileState) block.getState()); + NamespacedKey key = new NamespacedKey(SiegeEngines.plugin, "cannons"); + + for (Entity ent : SiegeEngines.TrackedStands.get(player.getUniqueId())) { + if (ent.isDead()) { + continue; + } + DoAimUp(ent, 0, player); + } + } + + public void DoAimUp(Entity ent, float amount, Entity player) { + Location loc = ent.getLocation(); + ArmorStand stand = (ArmorStand) ent; + // player.sendMessage(String.format("" + loc.getPitch())); + if (loc.getPitch() == -85 || loc.getPitch() - amount < -85) { + return; + } + loc.setPitch((float) (loc.getPitch() - amount)); + SiegeEquipment equipment = SiegeEngines.equipment.get(ent.getUniqueId()); + if (equipment != null) { + if (player instanceof Player) + equipment.ShowFireLocation(player); + if (equipment.RotateStandHead) { + stand.setHeadPose(new EulerAngle(loc.getDirection().getY() * (-1), 0, 0)); + } + ent.teleport(loc); + } + } + + public void DoAimDown(Entity ent, float amount, Entity player) { + Location loc = ent.getLocation(); + ArmorStand stand = (ArmorStand) ent; + // player.sendMessage(String.format("" + loc.getPitch())); + if (loc.getPitch() == 85 || loc.getPitch() - amount < 65) { + return; + } + loc.setPitch((float) (loc.getPitch() - amount)); + SiegeEquipment equipment = SiegeEngines.equipment.get(ent.getUniqueId()); + if (equipment != null) { + if (player instanceof Player) + equipment.ShowFireLocation(player); + if (equipment.RotateStandHead) { + stand.setHeadPose(new EulerAngle(loc.getDirection().getY() * (-1), 0, 0)); + } + ent.teleport(loc); + } + } + + public void FirstShotDoAimDown(Entity ent, float amount, Entity player) { + Location loc = ent.getLocation(); + ArmorStand stand = (ArmorStand) ent; + // player.sendMessage(String.format("" + loc.getPitch())); + if (loc.getPitch() == 85 || loc.getPitch() + amount > 85) { + return; + } + SiegeEquipment equipment = SiegeEngines.equipment.get(ent.getUniqueId()); + if (equipment != null) { + if (player instanceof Player) { + equipment.ShowFireLocation((Player) player); + } + if (equipment.RotateStandHead) { + stand.setHeadPose(new EulerAngle(loc.getDirection().getY() * (-1), 0, 0)); + + } + } + loc.setPitch((float) (loc.getPitch() + amount)); + + ent.teleport(loc); + } + + NamespacedKey key = new NamespacedKey(SiegeEngines.plugin, "cannons"); + + @EventHandler + public void DeathEvent(EntityDeathEvent event) { + Boolean removeStands = false; + List items = event.getDrops(); + if (event.getEntity() instanceof ArmorStand) { + if (event.getEntity().getPersistentDataContainer().has(key, PersistentDataType.STRING)) { + Entity base = Bukkit.getEntity(UUID.fromString(event.getEntity().getPersistentDataContainer().get(key, PersistentDataType.STRING))); + base.remove(); + } + if (SiegeEngines.equipment.containsKey(event.getEntity().getUniqueId())) { + removeStands = true; + } else { + for (ItemStack i : items) { + if (i.getType() == Material.CARVED_PUMPKIN && i.hasItemMeta() && i.getItemMeta().hasCustomModelData()) { + if (SiegeEngines.DefinedEquipment.containsKey(i.getItemMeta().getCustomModelData())) { + removeStands = true; + break; + } + + } + } + } + if (removeStands) { + for (ItemStack i : items) { + if (i.getType() == Material.ARMOR_STAND) { + i.setAmount(0); + return; + } + } + } + } + } + + public void AimUp(Entity player, float amount) { + + for (Entity ent : SiegeEngines.TrackedStands.get(player.getUniqueId())) { + if (ent.isDead()) { + continue; + } + DoAimUp(ent, amount, player); + } + } + + public void AimDown(Entity player, float amount) { + + for (Entity ent : SiegeEngines.TrackedStands.get(player.getUniqueId())) { + if (ent.isDead()) { + continue; + } + DoAimDown(ent, amount, player); + } + } + + public void LoadCannonsWithPowder(Entity player) { + for (Entity ent : SiegeEngines.TrackedStands.get(player.getUniqueId())) { + if (ent.isDead()) { + continue; + } + + SiegeEquipment equipment = SiegeEngines.equipment.get(ent.getUniqueId()); + if (equipment != null) { + equipment.LoadFuel(player); + } + } + } + + public void LoadCannonsWithProjectile(Entity player, ItemStack projectile) { + for (Entity ent : SiegeEngines.TrackedStands.get(player.getUniqueId())) { + if (ent.isDead()) { + continue; + } + SiegeEquipment equipment = SiegeEngines.equipment.get(ent.getUniqueId()); + if (equipment != null) { + equipment.LoadProjectile(player, projectile); + } + } + } + + @EventHandler + public void onPlayerClickSign(PlayerInteractEvent event) { + Player player = event.getPlayer(); + if (event.getClickedBlock() != null && event.getClickedBlock().getType().toString().contains("SIGN")) { + Sign sign = (Sign) event.getClickedBlock().getState(); + if (sign.getLine(0).equalsIgnoreCase("[Fire]") && event.getAction() == Action.RIGHT_CLICK_BLOCK) { + if (!SiegeEngines.TrackedStands.containsKey(player.getUniqueId())) { + return; + } + try { + Long delay = Long.parseLong(sign.getLine(1)); + if (delay < 6) { + delay = 6l; + } + Shoot((Entity) player, delay); + } catch (Exception e) { + Shoot((Entity) player, 6); + } + return; + } + if (sign.getLine(0).equalsIgnoreCase("[Load]")) { + if (event.getAction() == Action.LEFT_CLICK_BLOCK) { + LoadCannonsWithPowder((Entity) player); + return; + } + if (event.getAction() == Action.RIGHT_CLICK_BLOCK) { + //load proj + ItemStack itemInHand = player.getInventory().getItemInMainHand(); + if (itemInHand == null) { + return; + } + + + LoadCannonsWithProjectile((Entity) player, player.getInventory().getItemInMainHand()); + return; + } + } + + if (sign.getLine(0).equalsIgnoreCase("[Aim]")) { + if (!SiegeEngines.TrackedStands.containsKey(player.getUniqueId())) { + return; + } + float amount; + try { + amount = Float.parseFloat(sign.getLine(1)); + if (player.isSneaking()) { + AimDown((Entity) player, amount); + } else { + AimUp((Entity) player, amount); + } + } catch (Exception e) { + // TODO Auto-generated catch block + player.sendMessage("§eCould not parse number on second line."); + } + + return; + } + + if (sign.getLine(0).equalsIgnoreCase("[Cannon]")) { + if (event.getAction() == Action.LEFT_CLICK_BLOCK) { + SiegeEngines.TrackedStands.remove(player.getUniqueId()); + player.sendMessage("§eReleasing the equipment!"); + return; + } + + if (event.getAction() == Action.RIGHT_CLICK_BLOCK) { + if (player.isSneaking()) { + SaveCannons(player, event.getClickedBlock()); + return; + } + + NamespacedKey key = new NamespacedKey(SiegeEngines.plugin, "cannons"); + TileState state = (TileState) sign.getBlock().getState(); + SiegeEngines.TrackedStands.remove(player.getUniqueId()); + List temp = new ArrayList(); + if (!state.getPersistentDataContainer().has(key, PersistentDataType.STRING)) { + return; + } + String[] split = state.getPersistentDataContainer().get(key, PersistentDataType.STRING).replace("[", "").replace("]", "").split(","); + for (String s : split) { + temp.add(UUID.fromString(s.trim())); + } + for (UUID Id : temp) { + List entities = new ArrayList(); + Entity ent = Bukkit.getEntity(Id); + if (ent != null) { + TakeControl(player, ent); + } + } + player.sendMessage("§eNow controlling nearby equipment."); + return; + } + } + + } + } + + @EventHandler + public void onEntityClick(PlayerInteractAtEntityEvent event) { + + Player player = event.getPlayer(); + ItemStack itemInHand = player.getInventory().getItemInMainHand(); + Entity entity = event.getRightClicked(); + if (entity == null) { + return; + } + if (entity.getType() == EntityType.ARMOR_STAND) { + if (itemInHand.getType() != Config.controlItem) { + SiegeEngines.TrackedStands.get(player.getUniqueId()).remove(entity); + player.sendMessage("§eThis Equipment is no longer commanded by you."); + return; + } else { + TakeControl(player, entity); + } + if (SiegeEngines.equipment.containsKey(entity.getUniqueId())) { + if (itemInHand.getType() == Material.CLOCK) { + if (player.isSneaking()) { + DoAimDown(entity, 1, player); + } else { + DoAimUp(entity, 1, player); + } + return; + } + + SiegeEquipment equipment = SiegeEngines.equipment.get(entity.getUniqueId()); + event.setCancelled(true); + + if (itemInHand == null || itemInHand.getType() == Material.AIR) { + ArmorStand stand = (ArmorStand) entity; + if (stand.isInvisible()) { + stand.setInvisible(false); + player.sendMessage("§eEquipment is now breakable"); + } else { + if (equipment.AllowInvisibleStand) { + stand.setInvisible(true); + player.sendMessage("§eEquipment is no longer breakable"); + } + } + return; + } + if (itemInHand.isSimilar(equipment.FuelMaterial)) { + if (!equipment.LoadFuel(player)) { + player.sendMessage("§eCould not load Propellant."); + return; + } + } + /*if (itemInHand.isSimilar(new ItemStack(Material.FLINT))) { + if (equipment.isLoaded()) { + equipment.Fire((Entity) player, 6, 1); + } + else { + player.sendMessage("§eEquipment is not loaded"); + } + return; + }*/ + for (ItemStack stack : equipment.Projectiles.keySet()) { + if (stack.isSimilar(itemInHand) && equipment.AmmoHolder.LoadedProjectile == 0) { + equipment.AmmoHolder.LoadedProjectile = 1; + equipment.AmmoHolder.MaterialName = stack; + player.sendMessage("§eAdding Ammo to Weapon"); + itemInHand.setAmount(itemInHand.getAmount() - 1); + return; + } + + } + player.sendMessage("§eNow controlling this equipment."); + return; + } + } + } +} diff --git a/src/main/java/com/github/alathra/siegeengines/listeners/RotationHandler.java b/src/main/java/com/github/alathra/siegeengines/listeners/RotationHandler.java index 7fb4ebb..024513f 100644 --- a/src/main/java/com/github/alathra/siegeengines/listeners/RotationHandler.java +++ b/src/main/java/com/github/alathra/siegeengines/listeners/RotationHandler.java @@ -1,96 +1,99 @@ -package com.github.alathra.siegeengines.listeners; - -import java.util.UUID; - -import com.github.alathra.siegeengines.SiegeEngines; -import org.bukkit.Bukkit; -import org.bukkit.Location; -import org.bukkit.NamespacedKey; -import org.bukkit.entity.ArmorStand; -import org.bukkit.entity.Entity; -import org.bukkit.entity.LivingEntity; -import org.bukkit.entity.Player; -import org.bukkit.event.EventHandler; -import org.bukkit.event.Listener; -import org.bukkit.event.player.PlayerMoveEvent; -import org.bukkit.inventory.ItemStack; -import org.bukkit.persistence.PersistentDataType; -import org.bukkit.util.EulerAngle; -import org.bukkit.util.Vector; - -import com.github.alathra.siegeengines.SiegeEquipment; -import com.github.alathra.siegeengines.config.Config; - -public class RotationHandler implements Listener { - - @EventHandler - public void playerMove(PlayerMoveEvent event) { - Player player = event.getPlayer(); - NamespacedKey key = new NamespacedKey(SiegeEngines.plugin, "cannons"); - if (SiegeEngines.TrackedStands.containsKey(player.getUniqueId())) { - ItemStack itemInHand = player.getInventory().getItemInMainHand(); - if (itemInHand != null) { - if (itemInHand.getType() != Config.controlItem) { - // TrackedStands.remove(player.getUniqueId()); - - return; - } - for (Entity ent : SiegeEngines.TrackedStands.get(player.getUniqueId())) { - if (ent != null) { - SiegeEquipment equipment = SiegeEngines.equipment.get(ent.getUniqueId()); - if (ent.isDead()) { - continue; - } - double distance = player.getLocation().distance(ent.getLocation()); - if (distance <= 250) { - // player.sendMessage("§egot id"); - LivingEntity living = (LivingEntity) ent; - Location loc = ent.getLocation(); - - if (equipment.RotateSideways) { - Location direction = player.getLocation().add(player.getLocation().getDirection().multiply(50)); - - Vector dirBetweenLocations = direction.toVector().subtract(loc.toVector()); - if (ent.getPersistentDataContainer().has(key, PersistentDataType.STRING)) { - Entity base = Bukkit.getEntity(UUID.fromString(ent.getPersistentDataContainer().get(key, PersistentDataType.STRING))); - Location baseloc = base.getLocation(); - baseloc.setDirection(dirBetweenLocations); - base.teleport(baseloc); - } - loc.setDirection(dirBetweenLocations); - } - //loc.setYaw(player.getLocation().getYaw()); - //loc.setPitch(player.getLocation().getPitch()); - - - ArmorStand stand = (ArmorStand) living; - - if (equipment.RotateUpDown) { - loc.setPitch(player.getLocation().getPitch()); - if (loc.getPitch() < -85) { - loc.setPitch(-85); - } - if (loc.getPitch() > 85) { - loc.setPitch(85); - } - if (equipment.RotateStandHead) { - stand.setHeadPose(new EulerAngle(loc.getDirection().getY() * (-1), 0, 0)); - } - } - - - living.teleport(loc); - equipment.ShowFireLocation(player); - } - } - } - } - } - } - - Vector getDirectionBetweenLocations(Location Start, Location End) { - Vector from = Start.toVector(); - Vector to = End.toVector(); - return to.subtract(from); - } -} +package com.github.alathra.siegeengines.listeners; + +import java.util.UUID; + +import com.github.alathra.siegeengines.SiegeEngines; +import org.bukkit.Bukkit; +import org.bukkit.Location; +import org.bukkit.NamespacedKey; +import org.bukkit.entity.ArmorStand; +import org.bukkit.entity.Entity; +import org.bukkit.entity.LivingEntity; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerMoveEvent; +import org.bukkit.inventory.ItemStack; +import org.bukkit.persistence.PersistentDataType; +import org.bukkit.util.EulerAngle; +import org.bukkit.util.Vector; + +import com.github.alathra.siegeengines.SiegeEquipment; +import com.github.alathra.siegeengines.config.Config; + +public class RotationHandler implements Listener { + + @EventHandler + public void playerMove(PlayerMoveEvent event) { + Player player = event.getPlayer(); + NamespacedKey key = new NamespacedKey(SiegeEngines.plugin, "cannons"); + if (SiegeEngines.TrackedStands.containsKey(player.getUniqueId())) { + ItemStack itemInHand = player.getInventory().getItemInMainHand(); + if (itemInHand != null) { + if (itemInHand.getType() != Config.controlItem) { + // TrackedStands.remove(player.getUniqueId()); + + return; + } + for (Entity ent : SiegeEngines.TrackedStands.get(player.getUniqueId())) { + if (ent != null) { + SiegeEquipment equipment = SiegeEngines.equipment.get(ent.getUniqueId()); + if (ent.isDead()) { + continue; + } + double distance = player.getLocation().distance(ent.getLocation()); + if (distance <= 32) { + // player.sendMessage("§egot id"); + LivingEntity living = (LivingEntity) ent; + Location loc = ent.getLocation(); + + if (equipment.RotateSideways) { + Location direction = player.getLocation().add(player.getLocation().getDirection().multiply(50)); + + Vector dirBetweenLocations = direction.toVector().subtract(loc.toVector()); + if (ent.getPersistentDataContainer().has(key, PersistentDataType.STRING)) { + Entity base = Bukkit.getEntity(UUID.fromString(ent.getPersistentDataContainer().get(key, PersistentDataType.STRING))); + Location baseloc = base.getLocation(); + baseloc.setDirection(dirBetweenLocations); + base.teleport(baseloc); + } + loc.setDirection(dirBetweenLocations); + } + //loc.setYaw(player.getLocation().getYaw()); + //loc.setPitch(player.getLocation().getPitch()); + + + ArmorStand stand = (ArmorStand) living; + + if (equipment.RotateUpDown) { + loc.setPitch(player.getLocation().getPitch()); + if (loc.getPitch() < -85) { + loc.setPitch(-85); + } + if (loc.getPitch() > 85) { + loc.setPitch(85); + } + if (equipment.RotateStandHead) { + stand.setHeadPose(new EulerAngle(loc.getDirection().getY() * (-1), 0, 0)); + } + } + + + living.teleport(loc); + equipment.ShowFireLocation(player); + } else { + SiegeEngines.TrackedStands.get(player.getUniqueId()).remove(ent); + continue; + } + } + } + } + } + } + + public Vector getDirectionBetweenLocations(Location Start, Location End) { + Vector from = Start.toVector(); + Vector to = End.toVector(); + return to.subtract(from); + } +}