From 0180332f4ee25b3a40c27cf8275200100cc7182f Mon Sep 17 00:00:00 2001 From: MistPhizzle Date: Fri, 13 Jun 2014 16:56:28 -0400 Subject: [PATCH] Waterbending Methods Some methods for the Waterbending abilities. --- .classpath | 3 + .../projectkorra/ProjectKorra/Methods.java | 71 ++++++ .../waterbending/WaterReturn.java | 229 ++++++++++++++++++ 3 files changed, 303 insertions(+) create mode 100644 src/com/projectkorra/ProjectKorra/waterbending/WaterReturn.java diff --git a/.classpath b/.classpath index 144906d6..c600c3be 100644 --- a/.classpath +++ b/.classpath @@ -5,5 +5,8 @@ + + + diff --git a/src/com/projectkorra/ProjectKorra/Methods.java b/src/com/projectkorra/ProjectKorra/Methods.java index 75ec5559..3c01ca8b 100644 --- a/src/com/projectkorra/ProjectKorra/Methods.java +++ b/src/com/projectkorra/ProjectKorra/Methods.java @@ -13,6 +13,7 @@ import java.util.concurrent.ConcurrentHashMap; import org.bukkit.Bukkit; import org.bukkit.ChatColor; +import org.bukkit.Effect; import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.World; @@ -31,6 +32,7 @@ import org.bukkit.util.Vector; import com.projectkorra.ProjectKorra.Ability.AbilityModule; import com.projectkorra.ProjectKorra.Ability.AbilityModuleManager; import com.projectkorra.ProjectKorra.earthbending.EarthPassive; +import com.projectkorra.abilities.PhaseChange.FreezeMelt; import com.projectkorra.abilities.RaiseEarth.EarthColumn; public class Methods { @@ -483,6 +485,75 @@ public class Methods { return false; } + public static Block getWaterSourceBlock(Player player, double range, + boolean plantbending) { + Location location = player.getEyeLocation(); + Vector vector = location.getDirection().clone().normalize(); + for (double i = 0; i <= range; i++) { + Block block = location.clone().add(vector.clone().multiply(i)) + .getBlock(); + // if (isRegionProtectedFromBuild(player, Abilities.WaterManipulation, + // location)) + // continue; + if (isWaterbendable(block, player) + && (!isPlant(block) || plantbending)) { + if (TempBlock.isTempBlock(block)) { + TempBlock tb = TempBlock.get(block); + byte full = 0x0; + if (tb.state.getRawData() != full + && (tb.state.getType() != Material.WATER || tb.state + .getType() != Material.STATIONARY_WATER)) { + continue; + } + } + return block; + } + } + return null; + } + + public static boolean isObstructed(Location location1, Location location2) { + Vector loc1 = location1.toVector(); + Vector loc2 = location2.toVector(); + + Vector direction = loc2.subtract(loc1); + direction.normalize(); + + Location loc; + + double max = location1.distance(location2); + + for (double i = 0; i <= max; i++) { + loc = location1.clone().add(direction.clone().multiply(i)); + Material type = loc.getBlock().getType(); + if (type != Material.AIR + && !Arrays.asList(transparentToEarthbending).contains( + type.getId())) + return true; + } + + return false; + } + + public static boolean isAdjacentToFrozenBlock(Block block) { + BlockFace[] faces = { BlockFace.DOWN, BlockFace.UP, BlockFace.NORTH, + BlockFace.EAST, BlockFace.WEST, BlockFace.SOUTH }; + boolean adjacent = false; + if (Methods.isAbilityInstalled("PhaseChange", "orion304")) { + for (BlockFace face : faces) { + if (FreezeMelt.frozenblocks.containsKey((block.getRelative(face)))) + adjacent = true; + } + } + return adjacent; + } + + + + public static void playFocusWaterEffect(Block block) { + block.getWorld().playEffect(block.getLocation(), Effect.SMOKE, 4, 20); + } + public static void removeRevertIndex(Block block) { if (movedearth.containsKey(block)) { Information info = movedearth.get(block); diff --git a/src/com/projectkorra/ProjectKorra/waterbending/WaterReturn.java b/src/com/projectkorra/ProjectKorra/waterbending/WaterReturn.java new file mode 100644 index 00000000..146b7685 --- /dev/null +++ b/src/com/projectkorra/ProjectKorra/waterbending/WaterReturn.java @@ -0,0 +1,229 @@ +package com.projectkorra.ProjectKorra.waterbending; + +import java.util.HashMap; +import java.util.concurrent.ConcurrentHashMap; + +import org.bukkit.Location; +import org.bukkit.Material; +import org.bukkit.block.Block; +import org.bukkit.entity.Player; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.PlayerInventory; +import org.bukkit.util.Vector; + +import com.projectkorra.ProjectKorra.Methods; +import com.projectkorra.ProjectKorra.TempBlock; +import com.projectkorra.abilities.OctopusForm.OctopusFormAbility; +import com.projectkorra.abilities.WaterManipulation.WaterManipulationAbility; + +public class WaterReturn { + + private static ConcurrentHashMap instances = new ConcurrentHashMap(); + // private static int ID = Integer.MIN_VALUE; + private static long interval = 50; + + private static final byte full = 0x0; + private static double range = 30; + + private Player player; + // private int id; + private Location location; + private TempBlock block; + private long time; + + public WaterReturn(Player player, Block block) { + if (instances.containsKey(player)) + return; + this.player = player; + location = block.getLocation(); + // if (!Tools.isRegionProtectedFromBuild(player, + // Abilities.WaterManipulation, location) + // && Tools.canBend(player, Abilities.WaterManipulation)) { + if (Methods.isAbilityInstalled("WaterManipulation", "orion304") && Methods.canBend(player.getName(), "WaterManipulation")) { + if (Methods.isTransparentToEarthbending(player, block) + && !block.isLiquid()) + this.block = new TempBlock(block, Material.WATER, full); + } + // } + // if (ID >= Integer.MAX_VALUE) { + // ID = Integer.MIN_VALUE; + // } + // id = ID++; + instances.put(player, this); + } + + private void progress() { + if (!hasEmptyWaterBottle()) { + remove(); + return; + } + + if (player.isDead() || !player.isOnline()) { + remove(); + return; + } + + if (player.getWorld() != location.getWorld()) { + remove(); + return; + } + + if (System.currentTimeMillis() < time + interval) + return; + + time = System.currentTimeMillis(); + + Vector direction = Methods + .getDirection(location, player.getEyeLocation()).normalize(); + location = location.clone().add(direction); + + if (location == null || block == null) { + remove(); + return; + } + + if (location.getBlock().equals(block.getLocation().getBlock())) + return; + + // if (Tools.isRegionProtectedFromBuild(player, + // Abilities.WaterManipulation, location)) { + // remove(); + // return; + // } + + if (location.distance(player.getEyeLocation()) > Methods + .waterbendingNightAugment(range, player.getWorld())) { + remove(); + return; + } + + if (location.distance(player.getEyeLocation()) <= 1.5) { + fillBottle(); + return; + } + + Block newblock = location.getBlock(); + if (Methods.isTransparentToEarthbending(player, newblock) + && !newblock.isLiquid()) { + block.revertBlock(); + block = new TempBlock(newblock, Material.WATER, full); + } else { + remove(); + return; + } + + } + + private void remove() { + if (block != null) { + block.revertBlock(); + block = null; + } + instances.remove(player); + } + + private boolean hasEmptyWaterBottle() { + PlayerInventory inventory = player.getInventory(); + if (inventory.contains(Material.GLASS_BOTTLE)) { + return true; + } + return false; + } + + private void fillBottle() { + PlayerInventory inventory = player.getInventory(); + if (inventory.contains(Material.GLASS_BOTTLE)) { + int index = inventory.first(Material.GLASS_BOTTLE); + ItemStack item = inventory.getItem(index); + if (item.getAmount() == 1) { + inventory.setItem(index, new ItemStack(Material.POTION)); + } else { + item.setAmount(item.getAmount() - 1); + inventory.setItem(index, item); + HashMap leftover = inventory + .addItem(new ItemStack(Material.POTION)); + for (int left : leftover.keySet()) { + player.getWorld().dropItemNaturally(player.getLocation(), + leftover.get(left)); + } + } + } + + remove(); + } + + private static boolean isBending(Player player) { + if (Methods.isAbilityInstalled("WaterManipulation", "orion304")) { + for (int id : WaterManipulationAbility .instances.keySet()) { + if (WaterManipulationAbility.instances.get(id).player.equals(player)) + return true; + } + } + if (Methods.isAbilityInstalled("OctopusForm", "orion304")) { + if (OctopusFormAbility.instances.containsKey(player)) + return true; + } + if (Methods.isAbilityInstalled("Surge", "orion304")) { + for (int id : Wave.instances.keySet()) { + if (Wave.instances.get(id).player.equals(player)) + return true; + } + + for (int id : WaterWall.instances.keySet()) { + if (WaterWall.instances.get(id).player.equals(player)) + return true; + } + } + + if (Methods.isAbilityInstalled("IceSpike", "orion304")) { + if (IceSpike2.isBending(player)) + return true; + } + return false; + } + + public static boolean hasWaterBottle(Player player) { + if (instances.containsKey(player)) + return false; + if (isBending(player)) + return false; + PlayerInventory inventory = player.getInventory(); + return (inventory.contains(new ItemStack(Material.POTION), 1)); + } + + public static void emptyWaterBottle(Player player) { + PlayerInventory inventory = player.getInventory(); + int index = inventory.first(new ItemStack(Material.POTION)); + if (index != -1) { + ItemStack item = inventory.getItem(index); + if (item.getAmount() == 1) { + inventory.setItem(index, new ItemStack(Material.GLASS_BOTTLE)); + } else { + item.setAmount(item.getAmount() - 1); + inventory.setItem(index, item); + HashMap leftover = inventory + .addItem(new ItemStack(Material.GLASS_BOTTLE)); + for (int left : leftover.keySet()) { + player.getWorld().dropItemNaturally(player.getLocation(), + leftover.get(left)); + } + } + } + } + + public static void progressAll() { + for (Player player : instances.keySet()) { + instances.get(player).progress(); + } + } + + public static void removeAll() { + for (Player player : instances.keySet()) { + WaterReturn wr = instances.get(player); + if (wr.block != null) + wr.block.revertBlock(); + } + instances.clear(); + } + +} \ No newline at end of file