diff --git a/src/main/java/com/github/alathra/siegeengines/SiegeEngine.java b/src/main/java/com/github/alathra/siegeengines/SiegeEngine.java new file mode 100644 index 0000000..a3a665b --- /dev/null +++ b/src/main/java/com/github/alathra/siegeengines/SiegeEngine.java @@ -0,0 +1,432 @@ +package com.github.alathra.siegeengines; + +import java.util.ArrayList; +import java.util.List; +import java.util.HashMap; +import java.util.Map; +import java.util.UUID; + +import com.github.alathra.siegeengines.projectile.EntityProjectile; +import com.github.alathra.siegeengines.projectile.GunnersProjectile; +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.entity.ArmorStand; +import org.bukkit.entity.Entity; +import org.bukkit.entity.EntityType; +import org.bukkit.entity.LivingEntity; +import org.bukkit.entity.Player; +import org.bukkit.entity.ArmorStand.LockType; +import org.bukkit.inventory.EquipmentSlot; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.ItemMeta; +import org.bukkit.persistence.PersistentDataType; +import org.bukkit.util.Vector; +import org.jetbrains.annotations.NotNull; + + +public class SiegeEngine implements Cloneable { + + // Core variables + public Boolean enabled; + public String id; + public String worldName; + public UUID entityId; + public Entity entity; + public int yOffset; + public int xOffset; + public int pitchOffset; + public int maxFuel; + public double placementOffsetY; + public float velocityPerFuel; + + // Firing variables + public int shotAmount; + public long nextShotTime; + public int millisecondsBetweenFiringStages; + public int millisecondsBetweenReloadingStages; + public int millisecondsToLoad; + public int modelNumberToFireAt; + public boolean cycleThroughModelsBeforeFiring; + public boolean rotateSideways; + public boolean rotateUpDown; + public boolean rotateStandHead; + public boolean hasFired; + public boolean hasReloaded; + public int taskNumber; + public List firingModelNumbers; + public int readyModelNumber; + public int nextModelNumber; + public SiegeEngineAmmoHolder ammoHolder; + + // Optional variables + public boolean allowInvisibleStand; + public boolean hasBaseStand; + public double baseStandOffset; + public int baseStandModelNumber; + + // Passed parameters + public String name = "Unnammed Siege Engine"; + public ItemStack fuelItem = new ItemStack(Material.GUNPOWDER); + public Map projectiles = new HashMap<>(); + public int customModelID = 150; + + @Override + public SiegeEngine clone() throws CloneNotSupportedException { + return (SiegeEngine) super.clone(); + } + + public SiegeEngine(@NotNull String name, @NotNull HashMap newProjectiles, @NotNull ItemStack fuelItem, int customModelID) { + //String name, Integer XOffset, Integer YOffset, Integer fuelMax, Float fuelVelocityMod, Integer customModelId, HashMap projObj + + // Default custom model data id if it is passes as null + if (customModelID == 0) { + customModelID = 150; + } + + // Set Default values + xOffset = 0; + yOffset = 2; + maxFuel = 5; + velocityPerFuel = 1.0125f; + id = name.replaceAll(" ", "_").toLowerCase(); + rotateStandHead = true; + rotateSideways = true; + placementOffsetY = -1.125f; + readyModelNumber = customModelID; + modelNumberToFireAt = customModelID; + firingModelNumbers = new ArrayList(); + ammoHolder = new SiegeEngineAmmoHolder(); + //fuelItem = new ItemStack(Material.GUNPOWDER); + cycleThroughModelsBeforeFiring = false; + allowInvisibleStand = false; + shotAmount = 1; + ammoHolder.loadedFuel = maxFuel; + enabled = true; + cycleThroughModelsBeforeFiring = false; + rotateSideways = false; + rotateUpDown = true; + rotateStandHead = true; + hasFired = false; + hasReloaded = false; + allowInvisibleStand = false; + hasBaseStand = false; + baseStandOffset = 0; + baseStandModelNumber = 147; + nextShotTime = System.currentTimeMillis(); + EntityProjectile defaultProj = new EntityProjectile(); + defaultProj.EntityCount = 2; + defaultProj.EntityTyp = EntityType.SHULKER_BULLET; + defaultProj.ParticleType = Particle.WHITE_ASH; + defaultProj.SoundType = Sound.ENTITY_BLAZE_SHOOT; + + // set passed parameters as object variables + this.name = name; + this.fuelItem = fuelItem; + this.customModelID = customModelID; + // set projectiles + if (newProjectiles.keySet().isEmpty() || newProjectiles.isEmpty()) { + this.projectiles.put(new ItemStack(Material.GUNPOWDER), defaultProj); + } else { + this.projectiles = newProjectiles; + } + + } + + public boolean equals(String EquipmentId) { + return id.equals(EquipmentId); + } + + public Boolean isLoaded() { + if (this.maxFuel <= 0 && ammoHolder.loadedProjectile >= 1) + return true; + return (ammoHolder.loadedFuel > 0 && ammoHolder.loadedProjectile >= 1); + } + + public Boolean CanLoadFuel() { + return ammoHolder.loadedFuel < maxFuel; + } + + public Boolean LoadFuel(Entity player) { + if (ammoHolder.loadedFuel == maxFuel) { + return true; + } + if (!(player instanceof Player)) { + ammoHolder.loadedFuel = maxFuel; + return true; + } + if (((Player) player).getInventory().containsAtLeast(fuelItem, 1) || fuelItem.getType() == Material.AIR) { + if (CanLoadFuel()) { + this.ammoHolder.loadedFuel += 1; + //SaveState(); + ((Player) player).getInventory().removeItem(fuelItem); + ((Player) player).sendMessage("§eLoaded " + ammoHolder.loadedFuel + "/" + maxFuel); + return true; + } else { + return false; + } + } + + return false; + + } + + public Boolean LoadProjectile(Entity player, ItemStack itemInHand) { + if (itemInHand.getAmount() <= 0) { + return false; + } + if (!(player instanceof Player)) { + ammoHolder.loadedProjectile = 1; + ammoHolder.loadedFuel = maxFuel; + ammoHolder.materialName = itemInHand; + return true; + } + for (ItemStack im : projectiles.keySet()) { + if(im.getType().equals(itemInHand.getType()) && ammoHolder.loadedProjectile == 0) { + ammoHolder.loadedProjectile = 1; + ammoHolder.materialName = itemInHand; + ((Player) player).sendMessage("§eAdding Ammunition to Weapon"); + itemInHand.setAmount(itemInHand.getAmount() - 1); + return true; + } + } + return false; + } + + public Location GetFireLocation(LivingEntity living) { + if (living == null) + return null; + Location loc = living.getEyeLocation(); + Vector direction = living.getLocation().getDirection().multiply(xOffset); + loc.add(direction); + if (yOffset > 0) { + loc.setY(loc.getY() + yOffset); + } else { + loc.setY(loc.getY() - yOffset); + } + loc.setPitch(loc.getPitch() + pitchOffset); + return loc; + } + + public void ShowFireLocation(Entity player) { + LivingEntity living = (LivingEntity) entity; + if (player instanceof Player) { + if (living != null) { + Location origin = this.GetFireLocation(living); + Vector direction = origin.getDirection(); + direction.multiply(5 /* the range */); + direction.normalize(); + for (int i = 0; i < 3 /* range */; i += 1) { + Location spawn = origin.add(direction); + ((Player) player).spawnParticle(Particle.CLOUD, spawn, 1, 0, 0, 0, 0); + } + } + } + } + + public void Fire(Entity player, float delay, Integer amount) { + if (amount == null || amount == 0) + amount = this.shotAmount; + this.shotAmount = amount; + if (System.currentTimeMillis() < nextShotTime) { + return; + } + if (ammoHolder.loadedFuel <= 0) + ammoHolder.loadedFuel = 0; + float loadedFuel = ammoHolder.loadedFuel; + ItemStack LoadedProjectile = ammoHolder.materialName; + + + LivingEntity living = (LivingEntity) entity; + if (living == null || living.getEquipment() == null || living.getEquipment().getHelmet() == null || living.getEquipment().getHelmet().getItemMeta() == null) { + return; + } + + if (living.getEquipment().getHelmet().getItemMeta().getCustomModelData() != readyModelNumber) { + if (player instanceof Player) { + ((Player) player).sendMessage("§eCannot fire yet!"); + } + return; + } + ammoHolder.loadedFuel = 0; + ammoHolder.loadedProjectile = 0; + ammoHolder.materialName = new ItemStack(Material.AIR, 1); + + worldName = entity.getWorld().getName(); + nextShotTime = System.currentTimeMillis() + 1000; + //for (int i = 0; i <= this.shotAmount /* range */; i += 1) { + if (living == null || living.isDead()) { + return; + } + if (cycleThroughModelsBeforeFiring) { + + this.taskNumber = Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(SiegeEngines.getInstance(), () -> { + if (living == null || living.isDead()) { + Bukkit.getServer().getScheduler().cancelTask(taskNumber); + return; + } + + if (hasFired) { + Bukkit.getServer().getScheduler().cancelTask(taskNumber); + taskNumber = Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(SiegeEngines.getInstance(), () -> { + if (living == null || living.isDead()) { + Bukkit.getServer().getScheduler().cancelTask(taskNumber); + return; + } + // player.sendMessage("§etask"); + if (hasReloaded) { + Bukkit.getServer().getScheduler().cancelTask(taskNumber); + hasReloaded = false; + hasFired = false; + nextModelNumber = 0; + SiegeEnginesUtil.UpdateEntityIdModel(entity, readyModelNumber, worldName); + taskNumber = 0; + } else { + //firing stages + if (nextModelNumber - 1 <= firingModelNumbers.size() && nextModelNumber - 1 >= 0) { + int modelData = firingModelNumbers.get(nextModelNumber - 1); + // player.sendMessage("§e" + modelData); + SiegeEnginesUtil.UpdateEntityIdModel(entity, modelData, worldName); + nextModelNumber -= 1; + + } else { + // plugin.getLogger().log(Level.INFO, "its reloaded"); + hasReloaded = true; + } + } + }, 0, millisecondsBetweenReloadingStages); + + } else { + //firing stages + if (nextModelNumber < firingModelNumbers.size()) { + + int modelData = firingModelNumbers.get(nextModelNumber); + // player.sendMessage("§e" + modelData); + SiegeEnginesUtil.UpdateEntityIdModel(entity, modelData, worldName); + if (modelData == modelNumberToFireAt) { + // player.sendMessage("§efiring" + modelData); + if (LoadedProjectile == null) return; + if (LoadedProjectile.getType() == Material.AIR) return; + GunnersProjectile projType = projectiles.get(LoadedProjectile); + if (projType == null) return; + projType.Shoot(player, entity, this.GetFireLocation(living), loadedFuel * velocityPerFuel); + } + nextModelNumber += 1; + + } else { + hasFired = true; + + } + } + }, 0, millisecondsBetweenFiringStages); + } else { + taskNumber = Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(SiegeEngines.getInstance(), () -> { + //player.sendMessage("§etask"); + if (living == null || living.isDead()) { + Bukkit.getServer().getScheduler().cancelTask(taskNumber); + return; + } + + Location loc = living.getEyeLocation(); + Vector direction = entity.getLocation().getDirection().multiply(xOffset); + + loc.add(direction); + + nextModelNumber = 0; + if (LoadedProjectile == null) return; + if (LoadedProjectile.getType() == Material.AIR) return; + GunnersProjectile projType = projectiles.get(LoadedProjectile); + if (projType == null) return; + projType.Shoot(player, entity, this.GetFireLocation(living), loadedFuel * velocityPerFuel); + }, (long) delay); + + } + } + + @SuppressWarnings("deprecation") + public Boolean place(Entity player, Location l) { + l.add(0.5, 0, 0.5); + NamespacedKey key = new NamespacedKey(SiegeEngines.getInstance(), "siege_engines"); + if (this == null || !this.enabled) { + return false; + } + l.setY(l.getY() + 1); + l.setDirection(player.getFacing().getDirection()); + ItemStack item = new ItemStack(Material.CARVED_PUMPKIN); + ItemMeta meta = item.getItemMeta(); + String id = ""; + this.ammoHolder = new SiegeEngineAmmoHolder(); + if (this.hasBaseStand) { + + Location l2 = l; + l2.setY(l.getY() + this.baseStandOffset); + Entity entity3 = player.getWorld().spawnEntity(l, EntityType.ARMOR_STAND); + + LivingEntity ent = (LivingEntity) entity3; + ArmorStand stand = (ArmorStand) ent; + id = entity3.getUniqueId().toString(); + meta.setCustomModelData(this.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(this.allowInvisibleStand); + ent.getEquipment().setHelmet(item); + stand.setGravity(false); + stand.setMarker(true); + } + l.setY(l.getY() + this.placementOffsetY); + Entity entity2 = player.getWorld().spawnEntity(l, EntityType.ARMOR_STAND); + if (id != "") { + entity2.getPersistentDataContainer().set(key, PersistentDataType.STRING, id); + } + meta.setCustomModelData(this.readyModelNumber); + meta.setDisplayName("§e" + this.name + " Item"); + List Lore = new ArrayList(); + Lore.add("§ePlace as a block to spawn a " + this.name + " or put on an Armor Stand."); + meta.setLore(Lore); + item.setItemMeta(meta); + + // entity3.addPassenger(entity2); + + LivingEntity ent = (LivingEntity) entity2; + ArmorStand stand = (ArmorStand) ent; + this.entity = entity2; + + this.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(this.allowInvisibleStand); + stand.setBasePlate(true); + + stand.setGravity(false); + ent.getEquipment().setHelmet(item); + if (SiegeEngines.trackedStands.containsKey(player.getUniqueId())) { + List entities = SiegeEngines.trackedStands.get(player.getUniqueId()); + entities.add(entity2); + SiegeEngines.trackedStands.put(player.getUniqueId(), entities); + } else { + List newList = new ArrayList(); + newList.add(entity2); + SiegeEngines.trackedStands.put(player.getUniqueId(), newList); + } + SiegeEngines.activeSiegeEngines.put(entity2.getUniqueId(), this); + return true; + } +} diff --git a/src/main/java/com/github/alathra/siegeengines/SiegeEngineAmmoHolder.java b/src/main/java/com/github/alathra/siegeengines/SiegeEngineAmmoHolder.java new file mode 100644 index 0000000..45760bb --- /dev/null +++ b/src/main/java/com/github/alathra/siegeengines/SiegeEngineAmmoHolder.java @@ -0,0 +1,17 @@ +package com.github.alathra.siegeengines; + +import org.bukkit.Material; +import org.bukkit.inventory.ItemStack; + +public class SiegeEngineAmmoHolder { + + public int loadedFuel = 0; + public int loadedProjectile = 0; + public ItemStack materialName = new ItemStack(Material.AIR, 1); + + public SiegeEngineAmmoHolder() { + loadedFuel = 0; + loadedProjectile = 0; + materialName = new ItemStack(Material.AIR, 1); + } +} diff --git a/src/main/java/com/github/alathra/siegeengines/SiegeEngines.java b/src/main/java/com/github/alathra/siegeengines/SiegeEngines.java index e3de635..56f4eda 100644 --- a/src/main/java/com/github/alathra/siegeengines/SiegeEngines.java +++ b/src/main/java/com/github/alathra/siegeengines/SiegeEngines.java @@ -6,59 +6,37 @@ 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; + private CommandHandler commandHandler; - public static Plugin plugin; + public static SiegeEngines instance; public static MetadataValueAdapter metadata; public static Random random = new Random(); - private static String Path; - private CommandHandler commandHandler; - //public static Towny towny; + + public static HashMap definedSiegeEngines = new HashMap(); + public static HashMap activeSiegeEngines = new HashMap(); + public static HashMap> trackedStands = new HashMap>(); public static SiegeEngines getInstance() { @@ -67,30 +45,23 @@ public class SiegeEngines extends JavaPlugin { @Override public void onLoad() { - plugin = this; commandHandler = new CommandHandler(this); commandHandler.onLoad(); + instance = this; } - @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(); + activeSiegeEngines.clear(); + trackedStands.clear(); + definedSiegeEngines.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()) { + for (SiegeEngine i : definedSiegeEngines.values()) { + System.out.println("§eEnabled Weapon : " + i.name); + System.out.println("§eWeapon Propellant/\"Fuel\" ItemStacks : " + i.fuelItem); + for (ItemStack proj : i.projectiles.keySet()) { System.out.println("§eWeapon Projectile ItemStacks : " + proj); } } @@ -101,149 +72,26 @@ public class SiegeEngines extends JavaPlugin { @Override public void onDisable() { commandHandler.onDisable(); - for (SiegeEquipment equip : equipment.values()) { - if (equip.Entity != null) { + for (SiegeEngine siegeEngine : activeSiegeEngines.values()) { + if (siegeEngine.entity != null) { ItemStack item = new ItemStack(Material.CARVED_PUMPKIN); ItemMeta meta = item.getItemMeta(); - equip.AmmoHolder = new EquipmentMagazine(); + siegeEngine.ammoHolder = new SiegeEngineAmmoHolder(); - meta.setCustomModelData(equip.ReadyModelNumber); - meta.setDisplayName("§e" + equip.EquipmentName + " Item"); + meta.setCustomModelData(siegeEngine.readyModelNumber); + meta.setDisplayName("§e" + siegeEngine.name + " Item"); List Lore = new ArrayList(); - Lore.add("§ePlace as a block to spawn a " + equip.EquipmentName + " or put on an Armor Stand."); + Lore.add("§ePlace as a block to spawn a " + siegeEngine.name + " or put on an Armor Stand."); meta.setLore(Lore); item.setItemMeta(meta); - ((LivingEntity) equip.Entity).getEquipment().setHelmet(item); + ((LivingEntity) siegeEngine.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); + HashMap projectileMap = new HashMap<>(); EntityProjectile fireProj = new EntityProjectile(); fireProj.EntityCount = 4; @@ -251,149 +99,80 @@ public class SiegeEngines extends JavaPlugin { 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); + projectileMap.put(new ItemStack(Material.FIRE_CHARGE), fireProj); + + ExplosiveProjectile proj = new ExplosiveProjectile(); + proj.ExplodePower = 1; + projectileMap.put(new ItemStack(Material.COBBLESTONE), proj); + + SiegeEngine trebuchet = new SiegeEngine("Trebuchet", projectileMap, new ItemStack(Material.GUNPOWDER), 122); + trebuchet.xOffset = 3; + trebuchet.yOffset = 3; + trebuchet.placementOffsetY = 0.0; + trebuchet.velocityPerFuel = 0.3f; + trebuchet.shotAmount = 1; + trebuchet.rotateStandHead = false; + trebuchet.rotateSideways = true; + trebuchet.fuelItem = new ItemStack(Material.STRING); + trebuchet.readyModelNumber = 122; + trebuchet.modelNumberToFireAt = 135; + trebuchet.millisecondsBetweenFiringStages = 2; + trebuchet.millisecondsBetweenReloadingStages = 10; + trebuchet.firingModelNumbers = new ArrayList<>(Arrays.asList( + 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139 + )); + trebuchet.cycleThroughModelsBeforeFiring = true; + + definedSiegeEngines.put(trebuchet.readyModelNumber, trebuchet); + + + ExplosiveProjectile tntProj = new ExplosiveProjectile(); + tntProj.ExplodePower = 1; + tntProj.ProjectilesCount = 5; + tntProj.DelayedFire = true; + tntProj.Inaccuracy = 0.75f; + projectileMap.put(new ItemStack(Material.TNT), tntProj); + + ExplosiveProjectile breachShot = new ExplosiveProjectile(); + breachShot.ExplodePower = 3; + breachShot.ProjectilesCount = 1; + projectileMap.put(new ItemStack(Material.IRON_BLOCK), breachShot); + + 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()); + projectileMap.put(new ItemStack(Material.GRAVEL), scatterShot); + ExplosiveProjectile repeatingShot = new ExplosiveProjectile(); + repeatingShot.ExplodePower = 2; + repeatingShot.ProjectilesCount = 3; + repeatingShot.DelayedFire = true; + repeatingShot.Inaccuracy = 0.35f; + projectileMap.put(new ItemStack(Material.TNT), repeatingShot); + + SiegeEngine siegeCannon = new SiegeEngine("Siege Cannon", projectileMap, new ItemStack(Material.GUNPOWDER), 141); + siegeCannon.shotAmount = 1; + siegeCannon.placementOffsetY = -1; + siegeCannon.readyModelNumber = 141; + siegeCannon.modelNumberToFireAt = 141; + siegeCannon.firingModelNumbers = new ArrayList(); + siegeCannon.rotateStandHead = true; + siegeCannon.rotateSideways = true; + definedSiegeEngines.put(siegeCannon.readyModelNumber, siegeCannon); + + SiegeEngine navalCannon = new SiegeEngine("Naval Cannon", projectileMap, new ItemStack(Material.GUNPOWDER), 142); + navalCannon.projectiles.put(new ItemStack(Material.COBBLESTONE), proj); + navalCannon.placementOffsetY = -1; + navalCannon.shotAmount = 1; + navalCannon.readyModelNumber = 142; + navalCannon.modelNumberToFireAt = 142; + navalCannon.firingModelNumbers = new ArrayList(); + navalCannon.rotateStandHead = true; + navalCannon.rotateSideways = false; + definedSiegeEngines.put(navalCannon.readyModelNumber, navalCannon); } - 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.HasBaseStand) { - - 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; - - 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(true); - - stand.setGravity(false); - 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?"); - } - } - } - -} - - - +} \ No newline at end of file diff --git a/src/main/java/com/github/alathra/siegeengines/SiegeEnginesLogger.java b/src/main/java/com/github/alathra/siegeengines/SiegeEnginesLogger.java index 85f05b4..1551d22 100644 --- a/src/main/java/com/github/alathra/siegeengines/SiegeEnginesLogger.java +++ b/src/main/java/com/github/alathra/siegeengines/SiegeEnginesLogger.java @@ -5,14 +5,14 @@ import org.bukkit.Bukkit; public class SiegeEnginesLogger { public static void info(String msg) { - Bukkit.getLogger().info("[" + SiegeEngines.plugin.getName() + "]" + msg); + Bukkit.getLogger().info("[" + SiegeEngines.getInstance().getName() + "]" + msg); } public static void warn(String msg) { - Bukkit.getLogger().warning("[" + SiegeEngines.plugin.getName() + "]" + msg); + Bukkit.getLogger().warning("[" + SiegeEngines.getInstance().getName() + "]" + msg); } public static void severe(String msg) { - Bukkit.getLogger().severe("[" + SiegeEngines.plugin.getName() + "]" + msg); + Bukkit.getLogger().severe("[" + SiegeEngines.getInstance().getName() + "]" + msg); } } diff --git a/src/main/java/com/github/alathra/siegeengines/SiegeEnginesUtil.java b/src/main/java/com/github/alathra/siegeengines/SiegeEnginesUtil.java new file mode 100644 index 0000000..78a83b5 --- /dev/null +++ b/src/main/java/com/github/alathra/siegeengines/SiegeEnginesUtil.java @@ -0,0 +1,51 @@ +package com.github.alathra.siegeengines; + +import java.util.concurrent.TimeUnit; + +import org.bukkit.Bukkit; +import org.bukkit.entity.Entity; +import org.bukkit.entity.LivingEntity; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.ItemMeta; +import org.bukkit.metadata.FixedMetadataValue; + +public class SiegeEnginesUtil { + + 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); + } + } + } + + public static FixedMetadataValue addMetaDataValue(Object value) { + return new FixedMetadataValue(Bukkit.getServer().getPluginManager().getPlugin("SiegeEngines"), value); + } + + 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 SiegeEngine createCloneFromCustomModelData(Integer ModelId) { + try { + return SiegeEngines.definedSiegeEngines.get(ModelId).clone(); + } catch (CloneNotSupportedException e) { + + } + return null; + } +} 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 22e6c42..cbb4945 100644 --- a/src/main/java/com/github/alathra/siegeengines/command/SiegeEnginesCommand.java +++ b/src/main/java/com/github/alathra/siegeengines/command/SiegeEnginesCommand.java @@ -1,7 +1,6 @@ package com.github.alathra.siegeengines.command; -import com.github.alathra.siegeengines.SiegeEquipment; -import com.github.alathra.siegeengines.projectile.GunnersProjectile; +import com.github.alathra.siegeengines.SiegeEngine; import com.github.milkdrinkers.colorparser.ColorParser; import dev.jorel.commandapi.CommandAPIBukkit; import dev.jorel.commandapi.CommandAPICommand; @@ -18,7 +17,6 @@ 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.*; @@ -49,7 +47,7 @@ public class SiegeEnginesCommand { new StringArgument("equipmentid") .replaceSuggestions( ArgumentSuggestions.strings( - DefinedEquipment.values().stream().map(SiegeEquipment -> SiegeEquipment.EquipmentId).toList() + definedSiegeEngines.values().stream().map(SiegeEquipment -> SiegeEquipment.id).toList() ) ), new PlayerArgument("target") @@ -76,7 +74,7 @@ public class SiegeEnginesCommand { Player player = (Player) args.getOptional("target").orElse(sender); - for (SiegeEquipment SiegeEquipment : DefinedEquipment.values()) { + for (SiegeEngine SiegeEquipment : definedSiegeEngines.values()) { if (SiegeEquipment.equals(equipmentId)) { giveEquipment(player, SiegeEquipment); break; @@ -88,7 +86,7 @@ public class SiegeEnginesCommand { if (!(sender instanceof Player player)) throw CommandAPIBukkit.failWithAdventureComponent(ColorParser.of("Only players can use this command.").build()); - for (SiegeEquipment i : DefinedEquipment.values()) { + for (SiegeEngine i : definedSiegeEngines.values()) { giveEquipment(player, i); } } @@ -97,29 +95,27 @@ public class SiegeEnginesCommand { if (!(sender instanceof Player)) throw CommandAPIBukkit.failWithAdventureComponent(ColorParser.of("Only players can use this command.").build()); - equipment.clear(); - TrackedStands.clear(); - DefinedEquipment.clear(); + activeSiegeEngines.clear(); + trackedStands.clear(); + definedSiegeEngines.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()) { + for (SiegeEngine i : definedSiegeEngines.values()) { + sender.sendMessage(ColorParser.of("Enabled Weapon : %s".formatted(i.name)).build()); + sender.sendMessage(ColorParser.of("Weapon Propellant/\"Fuel\" ItemStacks : %s".formatted(i.fuelItem)).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) { + private void giveEquipment(Player player, SiegeEngine 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()); + meta.setCustomModelData(SiegeEquipment.readyModelNumber); + meta.displayName(ColorParser.of("%s Item".formatted(SiegeEquipment.name)).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("Place as a block to spawn a '%s'".formatted(SiegeEquipment.name)).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); diff --git a/src/main/java/com/github/alathra/siegeengines/config/Config.java b/src/main/java/com/github/alathra/siegeengines/config/Config.java index 6f96695..478badb 100644 --- a/src/main/java/com/github/alathra/siegeengines/config/Config.java +++ b/src/main/java/com/github/alathra/siegeengines/config/Config.java @@ -16,6 +16,10 @@ public class Config { public static Material controlItem = Material.COMPASS; + public static int controlDistance = 64; + + public static int rotateDistance = 32; + public static void initConfigVals() { // init config config = SiegeEngines.getInstance().getConfig(); @@ -28,6 +32,9 @@ public class Config { controlItem = Material.COMPASS; } + controlDistance = config.getInt("ControlDistance"); + + rotateDistance = config.getInt("RotateDistance"); } public static void reload() { @@ -35,7 +42,7 @@ public class Config { initConfigVals(); } - private FileConfiguration getConfig() { + public FileConfiguration getConfig() { return config; } 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 0321d2c..33d70df 100644 --- a/src/main/java/com/github/alathra/siegeengines/listeners/ClickHandler.java +++ b/src/main/java/com/github/alathra/siegeengines/listeners/ClickHandler.java @@ -1,23 +1,21 @@ package com.github.alathra.siegeengines.listeners; - -import java.util.HashSet; -import java.util.EnumSet; import java.util.ArrayList; +import java.util.EnumSet; import java.util.HashMap; import java.util.List; -import java.util.Set; 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.SiegeEngineAmmoHolder; import com.github.alathra.siegeengines.SiegeEngines; +import com.github.alathra.siegeengines.SiegeEnginesUtil; + 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; @@ -48,15 +46,9 @@ 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.SiegeEngine; 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; @@ -69,16 +61,6 @@ public class ClickHandler implements Listener { 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(); } @@ -96,8 +78,6 @@ public class ClickHandler implements Listener { 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()); @@ -106,16 +86,6 @@ public class ClickHandler implements Listener { 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++) { @@ -156,26 +126,37 @@ public class ClickHandler implements Listener { @EventHandler(priority = EventPriority.HIGH) public void BlockPlaceEvent(org.bukkit.event.block.BlockPlaceEvent event) { Player thePlayer = event.getPlayer(); + Material replaced = event.getBlockReplacedState().getType(); if (fluidMaterials.contains(replaced)) { thePlayer.sendMessage("§eGunner Equipment cannot be spawned in Fluid Blocks."); event.setCancelled(true); } - if (event.isCancelled()) + if (event.isCancelled()) { return; - Material replaced = event.getBlockReplacedState().getType(); + } 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) { + SiegeEngine siegeEngine = null; + // Search for match in custom model data value in defined siege engines + for (SiegeEngine entry : SiegeEngines.definedSiegeEngines.values()) { + if (entry.customModelID == customModel) { + try { + siegeEngine = entry.clone(); + } catch (CloneNotSupportedException e) { + } + break; + } + } + // If siege engine found, place it + if (siegeEngine != null) { + siegeEngine.place(thePlayer, event.getBlockAgainst().getLocation()); 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); } - event.setCancelled(true); } } } @@ -183,37 +164,36 @@ public class ClickHandler implements Listener { public void Shoot(Entity player, long delay) { float actualDelay = delay; - Boolean FirstShot = true; int counter = 0; - if (SiegeEngines.TrackedStands.get(player.getUniqueId()) == null) { + 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) + if (SiegeEngines.trackedStands.get(player.getUniqueId()) == null) return; - List entities = new ArrayList<>(SiegeEngines.TrackedStands.get(player.getUniqueId())); + 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); + SiegeEngines.trackedStands.get(player.getUniqueId()).remove(ent); continue; } if (((LivingEntity) ent).getEquipment().getHelmet().getType() != Material.CARVED_PUMPKIN) { - SiegeEngines.TrackedStands.get(player.getUniqueId()).remove(ent); + SiegeEngines.trackedStands.get(player.getUniqueId()).remove(ent); continue; } double distance = player.getLocation().distance(ent.getLocation()); - if (distance >= 32) { - SiegeEngines.TrackedStands.get(player.getUniqueId()).remove(ent); + if (distance >= Config.controlDistance) { + SiegeEngines.trackedStands.get(player.getUniqueId()).remove(ent); continue; } - SiegeEquipment siege = SiegeEngines.equipment.get(ent.getUniqueId()); + SiegeEngine siege = SiegeEngines.activeSiegeEngines.get(ent.getUniqueId()); if (counter > 4) return; if (ent == null || ent.isDead()) { - SiegeEngines.TrackedStands.get(player.getUniqueId()).remove(ent); + SiegeEngines.trackedStands.get(player.getUniqueId()).remove(ent); continue; } if (siege.isLoaded()) { @@ -227,9 +207,7 @@ public class ClickHandler implements Listener { } else { if (player instanceof Player) { } - //player.sendMessage("§eAmmunition Not Loaded"); } - // player.sendMessage(String.format("§e" +actualDelay)); } } @@ -255,16 +233,7 @@ public class ClickHandler implements Listener { } 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; - } - }*/ - //m.setAmount(1); return inv.containsAtLeast(m,m.getAmount()); - //return false; } @EventHandler @@ -276,38 +245,28 @@ public class ClickHandler implements Listener { 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()); + List entities = SiegeEngines.trackedStands.get(player.getUniqueId()); if (entities == null) { return; } for (Entity entity : entities) { - if (SiegeEngines.equipment.containsKey(entity.getUniqueId())) { - SiegeEquipment equip = SiegeEngines.equipment.get(entity.getUniqueId()); - if (equip != null && equip.Enabled && !(entity.isDead())) { + if (SiegeEngines.activeSiegeEngines.containsKey(entity.getUniqueId())) { + SiegeEngine equip = SiegeEngines.activeSiegeEngines.get(entity.getUniqueId()); + if (equip != null && equip.enabled && !(entity.isDead())) { equip.Fire(player,10f,1); } } } return; } - if (SiegeEngines.TrackedStands.containsKey(player.getUniqueId())) { - List entities = SiegeEngines.TrackedStands.get(player.getUniqueId()); + if (SiegeEngines.trackedStands.containsKey(player.getUniqueId())) { + List entities = SiegeEngines.trackedStands.get(player.getUniqueId()); if (entities == null) { return; } @@ -316,23 +275,23 @@ public class ClickHandler implements Listener { for (Entity entity : entities) { foundAmmo = false; if ((entity.isDead())) continue; - if (SiegeEngines.equipment.containsKey(entity.getUniqueId())) { - SiegeEquipment equip = SiegeEngines.equipment.get(entity.getUniqueId()); - if (equip == null || !equip.Enabled) { + if (SiegeEngines.activeSiegeEngines.containsKey(entity.getUniqueId())) { + SiegeEngine equip = SiegeEngines.activeSiegeEngines.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()) { + 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; + equip.ammoHolder.loadedProjectile = 1; + equip.ammoHolder.materialName = stack; foundAmmo = true; } //state.update(true,true); @@ -342,15 +301,15 @@ public class ClickHandler implements Listener { 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()) { + 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; + equip.ammoHolder.loadedProjectile = 1; + equip.ammoHolder.materialName = stack; foundAmmo = true; } //state.update(true,true); @@ -360,15 +319,15 @@ public class ClickHandler implements Listener { 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()) { + 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; + equip.ammoHolder.loadedProjectile = 1; + equip.ammoHolder.materialName = stack; foundAmmo = true; } //state.update(true,true); @@ -379,14 +338,14 @@ public class ClickHandler implements Listener { break; if (!foundAmmo) { for (ItemStack inventoryItem : event.getPlayer().getInventory().getContents()) { - for (ItemStack stack : equip.Projectiles.keySet()) { + 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; + if (stack.isSimilar(inventoryItem) && equip.ammoHolder.loadedProjectile == 0) { + equip.ammoHolder.loadedProjectile = 1; + equip.ammoHolder.materialName = stack; inventoryItem.setAmount(inventoryItem.getAmount() - 1); foundAmmo = true; break; @@ -403,12 +362,12 @@ public class ClickHandler implements Listener { for (Entity entity : entities) { foundAmmo = false; if ((entity.isDead())) continue; - if (SiegeEngines.equipment.containsKey(entity.getUniqueId())) { - SiegeEquipment equip = SiegeEngines.equipment.get(entity.getUniqueId()); - if (equip == null || !equip.Enabled) { + if (SiegeEngines.activeSiegeEngines.containsKey(entity.getUniqueId())) { + SiegeEngine equip = SiegeEngines.activeSiegeEngines.get(entity.getUniqueId()); + if (equip == null || !equip.enabled) { continue; } - ItemStack stack = equip.FuelMaterial; + ItemStack stack = equip.fuelItem; if (entity.getLocation().getBlock().getType() == Material.BARREL || entity.getLocation().getBlock().getType() == Material.CHEST) { Block inv = entity.getLocation().getBlock(); Container state = ((Container) inv.getState()); @@ -420,7 +379,7 @@ public class ClickHandler implements Listener { continue; state.getInventory().removeItem(stack);//setStorageContents(updateContents(state.getInventory(),stack,1)); //state.update(true,true); - equip.AmmoHolder.LoadedFuel += 1; + equip.ammoHolder.loadedFuel += 1; foundAmmo = true; //state.update(true,true); } @@ -437,7 +396,7 @@ public class ClickHandler implements Listener { //state.update(true,true); if (!equip.CanLoadFuel()) continue; - equip.AmmoHolder.LoadedFuel += 1; + equip.ammoHolder.loadedFuel += 1; foundAmmo = true; //state.update(true,true); } @@ -454,7 +413,7 @@ public class ClickHandler implements Listener { continue; state.getInventory().removeItem(stack);//setStorageContents(updateContents(state.getInventory(),stack,1)); //state.update(true,true); - equip.AmmoHolder.LoadedFuel += 1; + equip.ammoHolder.loadedFuel += 1; foundAmmo = true; //state.update(true,true); } @@ -468,7 +427,7 @@ public class ClickHandler implements Listener { break; if (!equip.CanLoadFuel()) continue; - equip.AmmoHolder.LoadedFuel += 1; + equip.ammoHolder.loadedFuel += 1; stack.setAmount(1); event.getPlayer().getInventory().removeItem(stack); foundAmmo = true; @@ -488,7 +447,8 @@ public class ClickHandler implements Listener { @EventHandler(priority = EventPriority.NORMAL) public void onSignChange(SignChangeEvent event) { - String topline = event.getLine(0); + @SuppressWarnings("deprecation") + String topline = event.getLine(0); if (topline == null) topline = ""; Player player = event.getPlayer(); String toplinetrimmed = topline.trim(); @@ -511,8 +471,8 @@ public class ClickHandler implements Listener { 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 (SiegeEngines.trackedStands.containsKey(player.getUniqueId())) { + List entities = SiegeEngines.trackedStands.get(player.getUniqueId()); if (entities.contains(entity)) { return; } @@ -524,61 +484,58 @@ public class ClickHandler implements Listener { } ArmorStand stand = (ArmorStand) entity; - SiegeEquipment equip; + SiegeEngine 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) { + if (SiegeEngines.activeSiegeEngines.containsKey(entity.getUniqueId())) { + equip = SiegeEngines.activeSiegeEngines.get(entity.getUniqueId()); + if (equip == null || !equip.enabled) { return; } } else { - equip = SiegeEngines.CreateClone(living.getEquipment().getHelmet().getItemMeta().getCustomModelData()); - if (equip == null || !equip.Enabled) { + equip = SiegeEnginesUtil.createCloneFromCustomModelData(living.getEquipment().getHelmet().getItemMeta().getCustomModelData()); + if (equip == null || !equip.enabled) { return; } - equip.AmmoHolder = new EquipmentMagazine(); - equip.Entity = entity; - equip.EntityId = entity.getUniqueId(); + equip.ammoHolder = new SiegeEngineAmmoHolder(); + 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()); + if (SiegeEngines.trackedStands.containsKey(player.getUniqueId())) { + List entities = SiegeEngines.trackedStands.get(player.getUniqueId()); entities.add(entity); - SiegeEngines.TrackedStands.put(player.getUniqueId(), entities); + SiegeEngines.trackedStands.put(player.getUniqueId(), entities); } else { List newList = new ArrayList(); newList.add(entity); - SiegeEngines.TrackedStands.put(player.getUniqueId(), newList); + SiegeEngines.trackedStands.put(player.getUniqueId(), newList); } - SiegeEngines.equipment.put(entity.getUniqueId(), equip); + SiegeEngines.activeSiegeEngines.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())) { + if (!SiegeEngines.trackedStands.containsKey(player.getUniqueId())) { return; } - for (Entity ent : SiegeEngines.TrackedStands.get(player.getUniqueId())) { + 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())) { + for (Entity ent : SiegeEngines.trackedStands.get(player.getUniqueId())) { if (ent.isDead()) { continue; } @@ -594,11 +551,11 @@ public class ClickHandler implements Listener { return; } loc.setPitch((float) (loc.getPitch() - amount)); - SiegeEquipment equipment = SiegeEngines.equipment.get(ent.getUniqueId()); + SiegeEngine equipment = SiegeEngines.activeSiegeEngines.get(ent.getUniqueId()); if (equipment != null) { if (player instanceof Player) equipment.ShowFireLocation(player); - if (equipment.RotateStandHead) { + if (equipment.rotateStandHead) { stand.setHeadPose(new EulerAngle(loc.getDirection().getY() * (-1), 0, 0)); } ent.teleport(loc); @@ -613,11 +570,11 @@ public class ClickHandler implements Listener { return; } loc.setPitch((float) (loc.getPitch() - amount)); - SiegeEquipment equipment = SiegeEngines.equipment.get(ent.getUniqueId()); + SiegeEngine equipment = SiegeEngines.activeSiegeEngines.get(ent.getUniqueId()); if (equipment != null) { if (player instanceof Player) equipment.ShowFireLocation(player); - if (equipment.RotateStandHead) { + if (equipment.rotateStandHead) { stand.setHeadPose(new EulerAngle(loc.getDirection().getY() * (-1), 0, 0)); } ent.teleport(loc); @@ -631,12 +588,12 @@ public class ClickHandler implements Listener { if (loc.getPitch() == 85 || loc.getPitch() + amount > 85) { return; } - SiegeEquipment equipment = SiegeEngines.equipment.get(ent.getUniqueId()); + SiegeEngine equipment = SiegeEngines.activeSiegeEngines.get(ent.getUniqueId()); if (equipment != null) { if (player instanceof Player) { equipment.ShowFireLocation((Player) player); } - if (equipment.RotateStandHead) { + if (equipment.rotateStandHead) { stand.setHeadPose(new EulerAngle(loc.getDirection().getY() * (-1), 0, 0)); } @@ -646,7 +603,7 @@ public class ClickHandler implements Listener { ent.teleport(loc); } - NamespacedKey key = new NamespacedKey(SiegeEngines.plugin, "cannons"); + NamespacedKey key = new NamespacedKey(SiegeEngines.getInstance(), "cannons"); @EventHandler public void DeathEvent(EntityDeathEvent event) { @@ -657,12 +614,12 @@ public class ClickHandler implements Listener { Entity base = Bukkit.getEntity(UUID.fromString(event.getEntity().getPersistentDataContainer().get(key, PersistentDataType.STRING))); base.remove(); } - if (SiegeEngines.equipment.containsKey(event.getEntity().getUniqueId())) { + if (SiegeEngines.activeSiegeEngines.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())) { + if (SiegeEngines.definedSiegeEngines.containsKey(i.getItemMeta().getCustomModelData())) { removeStands = true; break; } @@ -683,7 +640,7 @@ public class ClickHandler implements Listener { public void AimUp(Entity player, float amount) { - for (Entity ent : SiegeEngines.TrackedStands.get(player.getUniqueId())) { + for (Entity ent : SiegeEngines.trackedStands.get(player.getUniqueId())) { if (ent.isDead()) { continue; } @@ -693,7 +650,7 @@ public class ClickHandler implements Listener { public void AimDown(Entity player, float amount) { - for (Entity ent : SiegeEngines.TrackedStands.get(player.getUniqueId())) { + for (Entity ent : SiegeEngines.trackedStands.get(player.getUniqueId())) { if (ent.isDead()) { continue; } @@ -702,12 +659,12 @@ public class ClickHandler implements Listener { } public void LoadCannonsWithPowder(Entity player) { - for (Entity ent : SiegeEngines.TrackedStands.get(player.getUniqueId())) { + for (Entity ent : SiegeEngines.trackedStands.get(player.getUniqueId())) { if (ent.isDead()) { continue; } - SiegeEquipment equipment = SiegeEngines.equipment.get(ent.getUniqueId()); + SiegeEngine equipment = SiegeEngines.activeSiegeEngines.get(ent.getUniqueId()); if (equipment != null) { equipment.LoadFuel(player); } @@ -715,24 +672,25 @@ public class ClickHandler implements Listener { } public void LoadCannonsWithProjectile(Entity player, ItemStack projectile) { - for (Entity ent : SiegeEngines.TrackedStands.get(player.getUniqueId())) { + for (Entity ent : SiegeEngines.trackedStands.get(player.getUniqueId())) { if (ent.isDead()) { continue; } - SiegeEquipment equipment = SiegeEngines.equipment.get(ent.getUniqueId()); + SiegeEngine equipment = SiegeEngines.activeSiegeEngines.get(ent.getUniqueId()); if (equipment != null) { equipment.LoadProjectile(player, projectile); } } } - @EventHandler + @SuppressWarnings("deprecation") + @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())) { + if (!SiegeEngines.trackedStands.containsKey(player.getUniqueId())) { return; } try { @@ -765,7 +723,7 @@ public class ClickHandler implements Listener { } if (sign.getLine(0).equalsIgnoreCase("[Aim]")) { - if (!SiegeEngines.TrackedStands.containsKey(player.getUniqueId())) { + if (!SiegeEngines.trackedStands.containsKey(player.getUniqueId())) { return; } float amount; @@ -786,7 +744,7 @@ public class ClickHandler implements Listener { if (sign.getLine(0).equalsIgnoreCase("[Cannon]")) { if (event.getAction() == Action.LEFT_CLICK_BLOCK) { - SiegeEngines.TrackedStands.remove(player.getUniqueId()); + SiegeEngines.trackedStands.remove(player.getUniqueId()); player.sendMessage("§eReleasing the equipment!"); return; } @@ -797,9 +755,9 @@ public class ClickHandler implements Listener { return; } - NamespacedKey key = new NamespacedKey(SiegeEngines.plugin, "cannons"); + NamespacedKey key = new NamespacedKey(SiegeEngines.getInstance(), "cannons"); TileState state = (TileState) sign.getBlock().getState(); - SiegeEngines.TrackedStands.remove(player.getUniqueId()); + SiegeEngines.trackedStands.remove(player.getUniqueId()); List temp = new ArrayList(); if (!state.getPersistentDataContainer().has(key, PersistentDataType.STRING)) { return; @@ -809,7 +767,6 @@ public class ClickHandler implements Listener { 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); @@ -834,13 +791,13 @@ public class ClickHandler implements Listener { } if (entity.getType() == EntityType.ARMOR_STAND) { if (itemInHand.getType() != Config.controlItem) { - SiegeEngines.TrackedStands.get(player.getUniqueId()).remove(entity); + 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 (SiegeEngines.activeSiegeEngines.containsKey(entity.getUniqueId())) { if (itemInHand.getType() == Material.CLOCK) { if (player.isSneaking()) { DoAimDown(entity, 1, player); @@ -850,7 +807,7 @@ public class ClickHandler implements Listener { return; } - SiegeEquipment equipment = SiegeEngines.equipment.get(entity.getUniqueId()); + SiegeEngine equipment = SiegeEngines.activeSiegeEngines.get(entity.getUniqueId()); event.setCancelled(true); if (itemInHand == null || itemInHand.getType() == Material.AIR) { @@ -859,32 +816,23 @@ public class ClickHandler implements Listener { stand.setInvisible(false); player.sendMessage("§eEquipment is now breakable"); } else { - if (equipment.AllowInvisibleStand) { + if (equipment.allowInvisibleStand) { stand.setInvisible(true); player.sendMessage("§eEquipment is no longer breakable"); } } return; } - if (itemInHand.isSimilar(equipment.FuelMaterial)) { + if (itemInHand.isSimilar(equipment.fuelItem)) { 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; + 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 Ammunition to Weapon"); itemInHand.setAmount(itemInHand.getAmount() - 1); 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 483478b..1b4488f 100644 --- a/src/main/java/com/github/alathra/siegeengines/listeners/RotationHandler.java +++ b/src/main/java/com/github/alathra/siegeengines/listeners/RotationHandler.java @@ -20,7 +20,7 @@ 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.SiegeEngine; import com.github.alathra.siegeengines.config.Config; public class RotationHandler implements Listener { @@ -28,8 +28,8 @@ 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())) { + NamespacedKey key = new NamespacedKey(SiegeEngines.getInstance(), "cannons"); + if (SiegeEngines.trackedStands.containsKey(player.getUniqueId())) { ItemStack itemInHand = player.getInventory().getItemInMainHand(); if (itemInHand != null) { if (itemInHand.getType() != Config.controlItem) { @@ -37,20 +37,20 @@ public class RotationHandler implements Listener { return; } - final List list = new ArrayList<>(SiegeEngines.TrackedStands.get(player.getUniqueId())); + final List list = new ArrayList<>(SiegeEngines.trackedStands.get(player.getUniqueId())); for (Entity ent : list) { if (ent != null) { - SiegeEquipment equipment = SiegeEngines.equipment.get(ent.getUniqueId()); + SiegeEngine equipment = SiegeEngines.activeSiegeEngines.get(ent.getUniqueId()); if (ent.isDead()) { continue; } double distance = player.getLocation().distance(ent.getLocation()); - if (distance <= 32) { + if (distance <= Config.rotateDistance) { // player.sendMessage("§egot id"); LivingEntity living = (LivingEntity) ent; Location loc = ent.getLocation(); - if (equipment.RotateSideways) { + if (equipment.rotateSideways) { Location direction = player.getLocation().add(player.getLocation().getDirection().multiply(50)); Vector dirBetweenLocations = direction.toVector().subtract(loc.toVector()); @@ -68,7 +68,7 @@ public class RotationHandler implements Listener { ArmorStand stand = (ArmorStand) living; - if (equipment.RotateUpDown) { + if (equipment.rotateUpDown) { loc.setPitch(player.getLocation().getPitch()); if (loc.getPitch() < -85) { loc.setPitch(-85); @@ -76,7 +76,7 @@ public class RotationHandler implements Listener { if (loc.getPitch() > 85) { loc.setPitch(85); } - if (equipment.RotateStandHead) { + if (equipment.rotateStandHead) { stand.setHeadPose(new EulerAngle(loc.getDirection().getY() * (-1), 0, 0)); } } @@ -85,7 +85,7 @@ public class RotationHandler implements Listener { living.teleport(loc); equipment.ShowFireLocation(player); } else { - SiegeEngines.TrackedStands.get(player.getUniqueId()).remove(ent); + SiegeEngines.trackedStands.get(player.getUniqueId()).remove(ent); continue; } } diff --git a/src/main/java/com/github/alathra/siegeengines/projectile/EntityProjectile.java b/src/main/java/com/github/alathra/siegeengines/projectile/EntityProjectile.java index 16958e1..b86159c 100644 --- a/src/main/java/com/github/alathra/siegeengines/projectile/EntityProjectile.java +++ b/src/main/java/com/github/alathra/siegeengines/projectile/EntityProjectile.java @@ -1,6 +1,8 @@ package com.github.alathra.siegeengines.projectile; import com.github.alathra.siegeengines.SiegeEngines; +import com.github.alathra.siegeengines.SiegeEnginesUtil; + import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.Particle; @@ -9,7 +11,6 @@ import org.bukkit.World; import org.bukkit.entity.Arrow; import org.bukkit.entity.Entity; import org.bukkit.entity.EntityType; -import org.bukkit.entity.LivingEntity; import org.bukkit.util.Vector; public class EntityProjectile implements GunnersProjectile { @@ -31,7 +32,7 @@ public class EntityProjectile implements GunnersProjectile { for (int i = 0; i < EntityCount; i++) { if (DelayedFire) { baseDelay += DelayTime; - Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(SiegeEngines.plugin, () -> { + Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(SiegeEngines.getInstance(), () -> { CreateEntity(entity, FireLocation, velocity, player); }, (long) baseDelay); @@ -39,8 +40,6 @@ public class EntityProjectile implements GunnersProjectile { CreateEntity(entity, FireLocation, velocity, player); } - - // } } } @@ -48,8 +47,7 @@ public class EntityProjectile implements GunnersProjectile { private void CreateEntity(Entity entity, Location loc, Float velocity, Entity player) { World world = entity.getLocation().getWorld(); - LivingEntity living = (LivingEntity) entity; - + Entity arrow = world.spawnEntity(loc, EntityTyp); if (Inaccuracy != 0f) { arrow.setVelocity(loc.getDirection().multiply(velocity).add(Randomise()) @@ -57,7 +55,7 @@ public class EntityProjectile implements GunnersProjectile { } else { arrow.setVelocity(loc.getDirection().multiply(velocity)); } - arrow.setMetadata("isEntityProj", SiegeEngines.addMetaDataValue("true")); + arrow.setMetadata("isEntityProj", SiegeEnginesUtil.addMetaDataValue("true")); Bukkit.getServer().getPluginManager().callEvent(new org.bukkit.event.entity.ProjectileLaunchEvent(arrow)); if (arrow instanceof org.bukkit.entity.Projectile) { @@ -69,10 +67,7 @@ public class EntityProjectile implements GunnersProjectile { arr.setDamage(8); if (player instanceof org.bukkit.projectiles.ProjectileSource) arr.setShooter((org.bukkit.projectiles.ProjectileSource) player); - //arr.setBasePotionData(arg0); } - // MiscDisguise miscDisguise = new MiscDisguise(DisguiseType.DROPPED_ITEM, Material.IRON_NUGGET); - // DisguiseAPI.disguiseEntity(arrow, miscDisguise); if (PlaySound) { world.playSound(loc, this.SoundType, 20, 2); world.spawnParticle(this.ParticleType, loc.getX(), loc.getY(), loc.getZ(), 0); diff --git a/src/main/java/com/github/alathra/siegeengines/projectile/ExplosiveProjectile.java b/src/main/java/com/github/alathra/siegeengines/projectile/ExplosiveProjectile.java index 1744c1c..7696d10 100644 --- a/src/main/java/com/github/alathra/siegeengines/projectile/ExplosiveProjectile.java +++ b/src/main/java/com/github/alathra/siegeengines/projectile/ExplosiveProjectile.java @@ -1,6 +1,7 @@ package com.github.alathra.siegeengines.projectile; import com.github.alathra.siegeengines.SiegeEngines; +import com.github.alathra.siegeengines.SiegeEnginesUtil; import com.github.alathra.siegeengines.listeners.ClickHandler; import org.bukkit.Bukkit; import org.bukkit.Location; @@ -10,11 +11,8 @@ import org.bukkit.Sound; import org.bukkit.World; import org.bukkit.entity.Entity; import org.bukkit.entity.EntityType; -import org.bukkit.entity.LivingEntity; import org.bukkit.entity.Snowball; import org.bukkit.util.Vector; -//import me.libraryaddict.disguise.disguisetypes.DisguiseType; -//import me.libraryaddict.disguise.disguisetypes.MiscDisguise; public class ExplosiveProjectile implements GunnersProjectile { @@ -39,19 +37,16 @@ public class ExplosiveProjectile implements GunnersProjectile { for (int i = 0; i < ProjectilesCount; i++) { if (DelayedFire) { baseDelay += DelayTime; - Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(SiegeEngines.plugin, () -> { + Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(SiegeEngines.getInstance(), () -> { CreateEntity(entity, loc, velocity, player); }, (long) baseDelay); } else { CreateEntity(entity, loc, velocity, player); } - - // } } } private void CreateEntity(Entity entity, Location loc, Float velocity, Entity player) { - LivingEntity living = (LivingEntity) entity; World world = entity.getLocation().getWorld(); Entity tnt = world.spawnEntity(loc, EntityType.SNOWBALL); Snowball ball = (Snowball) tnt; @@ -65,7 +60,7 @@ public class ExplosiveProjectile implements GunnersProjectile { } else { tnt.setVelocity(loc.getDirection().multiply(velocity)); } - tnt.setMetadata("isExplosiveProj", SiegeEngines.addMetaDataValue("true")); + tnt.setMetadata("isExplosiveProj", SiegeEnginesUtil.addMetaDataValue("true")); Bukkit.getServer().getPluginManager().callEvent(new org.bukkit.event.entity.ProjectileLaunchEvent(tnt)); if (PlaySound) { @@ -75,9 +70,6 @@ public class ExplosiveProjectile implements GunnersProjectile { PlaySound = false; } } -// MiscDisguise miscDisguise = new MiscDisguise(DisguiseType.ARROW); -// miscDisguise.setEntity(tnt); -// miscDisguise.startDisguise(); } diff --git a/src/main/java/com/github/alathra/siegeengines/projectile/PotionProjectile.java b/src/main/java/com/github/alathra/siegeengines/projectile/PotionProjectile.java index 2d7c475..99f1fd0 100644 --- a/src/main/java/com/github/alathra/siegeengines/projectile/PotionProjectile.java +++ b/src/main/java/com/github/alathra/siegeengines/projectile/PotionProjectile.java @@ -1,6 +1,8 @@ package com.github.alathra.siegeengines.projectile; import com.github.alathra.siegeengines.SiegeEngines; +import com.github.alathra.siegeengines.SiegeEnginesUtil; + import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.Material; @@ -9,7 +11,6 @@ import org.bukkit.Sound; import org.bukkit.World; import org.bukkit.entity.Entity; import org.bukkit.entity.EntityType; -import org.bukkit.entity.LivingEntity; import org.bukkit.entity.ThrownPotion; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.PotionMeta; @@ -33,20 +34,18 @@ public class PotionProjectile implements GunnersProjectile { for (int i = 0; i < EntityCount; i++) { if (DelayedFire) { baseDelay += DelayTime; - Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(SiegeEngines.plugin, () -> { + Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(SiegeEngines.getInstance(), () -> { CreateEntity(entity, loc, velocity); }, (long) baseDelay); } else { CreateEntity(entity, loc, velocity); } - // } } } private void CreateEntity(Entity entity, Location loc, Float velocity) { World world = entity.getLocation().getWorld(); - LivingEntity living = (LivingEntity) entity; Entity arrow = world.spawnEntity(loc, EntityType.SPLASH_POTION); if (Inaccuracy != 0f) { @@ -54,7 +53,7 @@ public class PotionProjectile implements GunnersProjectile { } else { arrow.setVelocity(loc.getDirection().multiply(velocity)); } - arrow.setMetadata("isExplosiveProj", SiegeEngines.addMetaDataValue("true")); + arrow.setMetadata("isExplosiveProj", SiegeEnginesUtil.addMetaDataValue("true")); Bukkit.getServer().getPluginManager().callEvent(new org.bukkit.event.entity.ProjectileLaunchEvent(arrow)); ItemStack itemStack = new ItemStack(Material.SPLASH_POTION); PotionMeta potionMeta = (PotionMeta) itemStack.getItemMeta(); @@ -62,8 +61,6 @@ public class PotionProjectile implements GunnersProjectile { itemStack.setItemMeta(potionMeta); ThrownPotion potion = (ThrownPotion) arrow; potion.setItem(itemStack); - // MiscDisguise miscDisguise = new MiscDisguise(DisguiseType.DROPPED_ITEM, Material.IRON_NUGGET); - // DisguiseAPI.disguiseEntity(arrow, miscDisguise); world.playSound(loc, this.SoundType, 20, 2); world.spawnParticle(this.ParticleType, loc.getX(), loc.getY(), loc.getZ(), 0); } diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 34aff22..15a2a43 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -13,3 +13,9 @@ config-version: 1 # The item used to aim and fire siege engines # Must be a valid material. See https://hub.spigotmc.org/javadocs/bukkit/org/bukkit/Material.html ControlItem: COMPASS + +# The maxmimum distance the player can be away from the siege engine before total control is terminated +ControlDistance: 64 + +# The maximum distance the player can be away from the siege engine before rotation control is terminated +RotateDistance: 32 \ No newline at end of file