From cbb3581a1235693eb5eadd689ca5d967783f522e Mon Sep 17 00:00:00 2001 From: Sobki Date: Sat, 26 Nov 2016 08:02:54 +1000 Subject: [PATCH] Updated&Fixed HeatControl, added TempBlock delayed reversion (#656) * Fixed all my derps :dancer: * 1 more timeee --- .../projectkorra/projectkorra/PKListener.java | 2 +- .../projectkorra/ProjectKorra.java | 10 +- .../projectkorra/ability/FireAbility.java | 2 - .../firebending/FirebendingManager.java | 1 - .../projectkorra/firebending/HeatControl.java | 139 ++++++------------ .../projectkorra/util/TempBlock.java | 52 ++++++- .../projectkorra/waterbending/Torrent.java | 13 +- 7 files changed, 104 insertions(+), 115 deletions(-) diff --git a/src/com/projectkorra/projectkorra/PKListener.java b/src/com/projectkorra/projectkorra/PKListener.java index 8e4d828d..3b6172dd 100644 --- a/src/com/projectkorra/projectkorra/PKListener.java +++ b/src/com/projectkorra/projectkorra/PKListener.java @@ -1345,7 +1345,7 @@ public class PKListener implements Listener { new FireBlastCharged(player); } else if (abil.equalsIgnoreCase("HeatControl")) { - new HeatControl(player, HeatControlType.SOLIDIFY); + new HeatControl(player, HeatControlType.COOK); } else if (abil.equalsIgnoreCase("FireBurst")) { new FireBurst(player); diff --git a/src/com/projectkorra/projectkorra/ProjectKorra.java b/src/com/projectkorra/projectkorra/ProjectKorra.java index 60e4e184..1f6e1cf8 100644 --- a/src/com/projectkorra/projectkorra/ProjectKorra.java +++ b/src/com/projectkorra/projectkorra/ProjectKorra.java @@ -26,6 +26,7 @@ import com.projectkorra.projectkorra.storage.DBConnection; import com.projectkorra.projectkorra.util.MetricsLite; import com.projectkorra.projectkorra.util.PassiveHandler; import com.projectkorra.projectkorra.util.RevertChecker; +import com.projectkorra.projectkorra.util.TempBlock; import com.projectkorra.projectkorra.util.Updater; import com.projectkorra.projectkorra.util.logging.PKLogHandler; import com.projectkorra.projectkorra.waterbending.WaterbendingManager; @@ -35,8 +36,8 @@ public class ProjectKorra extends JavaPlugin { public static ProjectKorra plugin; public static Logger log; public static PKLogHandler handler; - public static CollisionManager collisionManager; - public static CollisionInitializer collisionInitializer; + public static CollisionManager collisionManager; + public static CollisionInitializer collisionInitializer; public static long time_step = 1; public Updater updater; @@ -61,11 +62,11 @@ public class ProjectKorra extends JavaPlugin { updater = new Updater(this, "http://projectkorra.com/forums/dev-builds.16/index.rss"); new Commands(this); new MultiAbilityManager(); - new ComboManager(); + new ComboManager(); collisionManager = new CollisionManager(); collisionInitializer = new CollisionInitializer(collisionManager); CoreAbility.registerAbilities(); - collisionInitializer.initializeDefaultCollisions(); // must be called after abilities have been registered + collisionInitializer.initializeDefaultCollisions(); // must be called after abilities have been registered collisionManager.startCollisionDetection(); Preset.loadExternalPresets(); @@ -90,6 +91,7 @@ public class ProjectKorra extends JavaPlugin { getServer().getScheduler().scheduleSyncRepeatingTask(this, new ChiblockingManager(this), 0, 1); getServer().getScheduler().scheduleSyncRepeatingTask(this, new PassiveHandler(), 0, 1); getServer().getScheduler().runTaskTimerAsynchronously(this, new RevertChecker(this), 0, 200); + TempBlock.startReversion(); for (Player player : Bukkit.getOnlinePlayers()) { PKListener.getJumpStatistics().put(player, player.getStatistic(Statistic.JUMP)); diff --git a/src/com/projectkorra/projectkorra/ability/FireAbility.java b/src/com/projectkorra/projectkorra/ability/FireAbility.java index dbfcdfde..0a8e89cc 100644 --- a/src/com/projectkorra/projectkorra/ability/FireAbility.java +++ b/src/com/projectkorra/projectkorra/ability/FireAbility.java @@ -22,7 +22,6 @@ import com.projectkorra.projectkorra.GeneralMethods; import com.projectkorra.projectkorra.ability.util.Collision; import com.projectkorra.projectkorra.configuration.ConfigManager; import com.projectkorra.projectkorra.firebending.BlazeArc; -import com.projectkorra.projectkorra.firebending.HeatControl; import com.projectkorra.projectkorra.util.Information; import com.projectkorra.projectkorra.util.ParticleEffect; import com.projectkorra.projectkorra.util.ParticleEffect.ParticleData; @@ -233,7 +232,6 @@ public abstract class FireAbility extends ElementalAbility { public static void stopBending() { BlazeArc.removeAllCleanup(); - HeatControl.revertAllInstances(); for (Location loc : TEMP_FIRE.keySet()) { revertTempFire(loc); } diff --git a/src/com/projectkorra/projectkorra/firebending/FirebendingManager.java b/src/com/projectkorra/projectkorra/firebending/FirebendingManager.java index 0345e7a8..80be973e 100644 --- a/src/com/projectkorra/projectkorra/firebending/FirebendingManager.java +++ b/src/com/projectkorra/projectkorra/firebending/FirebendingManager.java @@ -17,6 +17,5 @@ public class FirebendingManager implements Runnable { FirePassive.handlePassive(); BlazeArc.dissipateAll(); FireAbility.removeFire(); - HeatControl.manageSolidify(); } } diff --git a/src/com/projectkorra/projectkorra/firebending/HeatControl.java b/src/com/projectkorra/projectkorra/firebending/HeatControl.java index afe75e22..d9d7e787 100644 --- a/src/com/projectkorra/projectkorra/firebending/HeatControl.java +++ b/src/com/projectkorra/projectkorra/firebending/HeatControl.java @@ -7,7 +7,6 @@ import java.util.HashSet; import java.util.List; import java.util.Random; import java.util.Set; -import java.util.concurrent.ConcurrentHashMap; import org.bukkit.Effect; import org.bukkit.Location; @@ -16,10 +15,12 @@ import org.bukkit.Sound; import org.bukkit.block.Block; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; +import org.bukkit.scheduler.BukkitRunnable; import com.projectkorra.projectkorra.BendingPlayer; import com.projectkorra.projectkorra.Element; import com.projectkorra.projectkorra.GeneralMethods; +import com.projectkorra.projectkorra.ProjectKorra; import com.projectkorra.projectkorra.ability.EarthAbility; import com.projectkorra.projectkorra.ability.FireAbility; import com.projectkorra.projectkorra.util.ParticleEffect; @@ -37,40 +38,30 @@ public class HeatControl extends FireAbility { private HeatControlType heatControlType; - /* - * HeatControl Cook variables - */ + // HeatControl Cook variables private long cookTime; private long cookInterval; - /* - * HeatControl Extinguish variables - */ + //HeatControl Extinguish variables private long extinguishCooldown; private double extinguishRadius; - /* - * HeatControl Melt variables - */ + //HeatControl Melt variables private double meltRange; private double meltRadius; private Location meltLocation; - /* - * HeatControl Solidify variables - */ + //HeatControl Solidify variables private int solidifyRadius; private long solidifyDelay; private long solidifyLastBlockTime; + private long solidifyRevertTime; private double solidifyMaxRadius; private double solidifyRange; + private boolean solidifyRevert; private boolean solidifying; private Location solidifyLocation; private Random randy; - private ConcurrentHashMap solidifyStone = new ConcurrentHashMap<>(); - private ConcurrentHashMap solidifyRevert = new ConcurrentHashMap<>(); - private List blocks = new ArrayList<>(); - public HeatControl(Player player, HeatControlType heatControlType) { super(player); @@ -79,6 +70,11 @@ public class HeatControl extends FireAbility { setFields(); if (this.heatControlType == HeatControlType.COOK) { + if (!isCookable(player.getInventory().getItemInMainHand().getType())) { + remove(); + new HeatControl(player, HeatControlType.SOLIDIFY); + return; + } start(); } else if (this.heatControlType == HeatControlType.EXTINGUISH) { @@ -103,7 +99,7 @@ public class HeatControl extends FireAbility { return; } else if (EarthAbility.getLavaSourceBlock(player, solidifyRange) == null) { remove(); - new HeatControl(player, HeatControlType.COOK); + new HeatControl(player, HeatControlType.EXTINGUISH); return; } @@ -117,7 +113,6 @@ public class HeatControl extends FireAbility { if (this.heatControlType == HeatControlType.COOK) { this.cookTime = System.currentTimeMillis(); this.cookInterval = getConfig().getLong("Abilities.Fire.HeatControl.Cook.Interval"); - this.heatControlType = HeatControlType.COOK; } else if (this.heatControlType == HeatControlType.EXTINGUISH) { this.extinguishCooldown = getConfig().getLong("Abilities.Fire.HeatControl.Extinguish.Cooldown"); this.extinguishRadius = getConfig().getLong("Abilities.Fire.HeatControl.Extinguish.Radius"); @@ -133,6 +128,8 @@ public class HeatControl extends FireAbility { this.solidifyLastBlockTime = 0; this.solidifyMaxRadius = getConfig().getDouble("Abilities.Fire.HeatControl.Solidify.MaxRadius"); this.solidifyRange = getConfig().getDouble("Abilities.Fire.HeatControl.Solidify.Range"); + this.solidifyRevert = getConfig().getBoolean("Abilities.Fire.HeatControl.Solidify.Revert"); + this.solidifyRevertTime = getConfig().getLong("Abilities.Fire.HeatControl.Solidify.RevertTime"); this.randy = new Random(); } } @@ -147,7 +144,7 @@ public class HeatControl extends FireAbility { } if (this.heatControlType == HeatControlType.COOK) { - + if (!player.isSneaking()) { remove(); return; @@ -155,7 +152,6 @@ public class HeatControl extends FireAbility { if (!isCookable(player.getInventory().getItemInMainHand().getType())) { remove(); - new HeatControl(player, HeatControlType.EXTINGUISH); return; } @@ -206,6 +202,10 @@ public class HeatControl extends FireAbility { } Location targetLocation = GeneralMethods.getTargetedLocation(player, solidifyRange); + //if (isLava(targetLocation.getBlock())) { + // remove(); + // new HeatControl(player, HeatControlType.EXTINGUISH); + //} resetLocation(targetLocation); List area = GeneralMethods.getCircle(solidifyLocation, solidifyRadius, 3, true, true, 0); solidify(area); @@ -308,7 +308,7 @@ public class HeatControl extends FireAbility { Block b = lava.get(randy.nextInt(lava.size())); - TempBlock tempBlock; + final TempBlock tempBlock; if (TempBlock.isTempBlock(b)) { tempBlock = TempBlock.get(b); tempBlock.setType(Material.MAGMA, (byte) 0); @@ -316,39 +316,25 @@ public class HeatControl extends FireAbility { tempBlock = new TempBlock(b, Material.MAGMA, (byte) 0); } - solidifyStone.put(tempBlock, System.currentTimeMillis()); - solidifyRevert.put(tempBlock, System.currentTimeMillis() + 1000); - blocks.add(tempBlock); - } - - @Override - public void remove() { - solidifying = false; - } - - public void removeInstance() { - super.remove(); - } - - public void revert(TempBlock block) { - if (blocks.contains(block)) { - block.revertBlock(); - blocks.remove(block); - } - } - - public void revertAll() { - for (TempBlock tempBlock : blocks) { - tempBlock.revertBlock(); - } - - blocks.clear(); - } - - public static void revertAllInstances() { - for (HeatControl heatControl : getAbilities(HeatControl.class)) { - heatControl.revertAll(); - } + new BukkitRunnable() { + @Override + public void run() { + if (tempBlock != null) { + if (solidifyRevert) { + tempBlock.setType(Material.STONE, (byte) 0); + tempBlock.setRevertTime(solidifyRevertTime); + } else { + tempBlock.revertBlock(); + tempBlock.getBlock().setType(Material.STONE); + } + + ParticleEffect.SMOKE.display(tempBlock.getBlock().getLocation().clone().add(0.5, 1, 0.5), 0.1F, 0.1F, 0.1F, 0.01F, 3); + if (randy.nextInt(3) == 0) { + tempBlock.getBlock().getWorld().playSound(tempBlock.getBlock().getLocation(), Sound.BLOCK_FIRE_EXTINGUISH, 0.5F, 1); + } + } + } + }.runTaskLater(ProjectKorra.plugin, 20); } public void resetLocation(Location loc) { @@ -362,49 +348,6 @@ public class HeatControl extends FireAbility { solidifyLocation = loc; } } - - public static void manageSolidify() { - for (HeatControl heatControl : getAbilities(HeatControl.class)) { - - for (TempBlock tempBlock : heatControl.solidifyStone.keySet()) { - - if (System.currentTimeMillis() - heatControl.solidifyStone.get(tempBlock) > 1000) { - - if (getConfig().getBoolean("Abilities.Fire.HeatControl.Solidify.Revert")) { - - tempBlock.setType(Material.STONE, (byte) 0); - heatControl.solidifyRevert.put(tempBlock, System.currentTimeMillis()); - } else { - - tempBlock.revertBlock(); - tempBlock.getBlock().setType(Material.STONE); - } - - ParticleEffect.SMOKE.display(tempBlock.getBlock().getLocation().clone().add(0.5, 1, 0.5), 0.1F, 0.1F, 0.1F, 0.01F, 3); - - // TODO play the smoke in a line from the block to above the player's head. - - tempBlock.getBlock().getWorld().playSound(tempBlock.getBlock().getLocation(), Sound.BLOCK_FIRE_EXTINGUISH, 1, 1); - heatControl.solidifyStone.remove(tempBlock); - } - } - - for (TempBlock tempBlock : heatControl.solidifyRevert.keySet()) { - - if (System.currentTimeMillis() - heatControl.solidifyRevert.get(tempBlock) > getConfig().getLong("Abilities.Fire.HeatControl.Solidify.RevertTime")) { - - heatControl.revert(tempBlock); - heatControl.solidifyRevert.remove(tempBlock); - } - } - - if (heatControl.solidifyStone.isEmpty() && heatControl.solidifyRevert.isEmpty() && !heatControl.solidifying) { - - heatControl.removeInstance(); - } - } - - } @Override public boolean isSneakAbility() { diff --git a/src/com/projectkorra/projectkorra/util/TempBlock.java b/src/com/projectkorra/projectkorra/util/TempBlock.java index 48ac6119..25a47240 100644 --- a/src/com/projectkorra/projectkorra/util/TempBlock.java +++ b/src/com/projectkorra/projectkorra/util/TempBlock.java @@ -1,24 +1,36 @@ package com.projectkorra.projectkorra.util; -import com.projectkorra.projectkorra.GeneralMethods; +import java.util.Comparator; +import java.util.Map; +import java.util.PriorityQueue; +import java.util.concurrent.ConcurrentHashMap; import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.block.Block; import org.bukkit.block.BlockFace; import org.bukkit.block.BlockState; +import org.bukkit.scheduler.BukkitRunnable; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; +import com.projectkorra.projectkorra.GeneralMethods; +import com.projectkorra.projectkorra.ProjectKorra; public class TempBlock { public static Map instances = new ConcurrentHashMap(); + public static final PriorityQueue REVERT_QUEUE = new PriorityQueue<>(100, new Comparator() { + @Override + public int compare(TempBlock t1, TempBlock t2) { + return (int) (t1.revertTime - t2.revertTime); + } + }); private Block block; private Material newtype; private byte newdata; private BlockState state; + private long revertTime; + private boolean inRevertQueue; @SuppressWarnings("deprecation") public TempBlock(Block block, Material newtype, byte newdata) { @@ -105,6 +117,19 @@ public class TempBlock { public BlockState getState() { return state; } + + public long getRevertTime() { + return revertTime; + } + + public void setRevertTime(long revertTime) { + if (inRevertQueue) { + REVERT_QUEUE.remove(this); + } + this.inRevertQueue = true; + this.revertTime = revertTime + System.currentTimeMillis(); + REVERT_QUEUE.add(this); + } public void revertBlock() { state.update(true); @@ -126,5 +151,26 @@ public class TempBlock { block.setType(material); block.setData(data); } + + public static void startReversion() { + new BukkitRunnable() { + @Override + public void run() { + long currentTime = System.currentTimeMillis(); + while (!REVERT_QUEUE.isEmpty()) { + TempBlock tempBlock = REVERT_QUEUE.peek(); + if (currentTime >= tempBlock.revertTime) { + REVERT_QUEUE.poll(); + tempBlock.revertBlock(); + //long finish = System.currentTimeMillis(); + //Bukkit.broadcastMessage(String.valueOf(finish - currentTime)); + } else { + break; + } + } + } + }.runTaskTimer(ProjectKorra.plugin, 0, 1); + + } } \ No newline at end of file diff --git a/src/com/projectkorra/projectkorra/waterbending/Torrent.java b/src/com/projectkorra/projectkorra/waterbending/Torrent.java index a719bc8f..edf2b9a9 100644 --- a/src/com/projectkorra/projectkorra/waterbending/Torrent.java +++ b/src/com/projectkorra/projectkorra/waterbending/Torrent.java @@ -29,7 +29,6 @@ public class Torrent extends WaterAbility { private static final double CLEANUP_RANGE = 50; private static final Map FROZEN_BLOCKS = new ConcurrentHashMap<>(); - private static final Map FROZEN_BLOCKS_DELAY = new ConcurrentHashMap<>(); private boolean sourceSelected; private boolean settingUp; @@ -38,6 +37,7 @@ public class Torrent extends WaterAbility { private boolean launch; private boolean launching; private boolean freeze; + private boolean revert; private int layer; private int maxLayer; private int maxHits; @@ -45,6 +45,7 @@ public class Torrent extends WaterAbility { private long time; private long interval; private long cooldown; + private long revertTime; private double startAngle; private double angle; private double radius; @@ -80,6 +81,8 @@ public class Torrent extends WaterAbility { this.range = getConfig().getDouble("Abilities.Water.Torrent.Range"); this.selectRange = getConfig().getDouble("Abilities.Water.Torrent.SelectRange"); this.cooldown = getConfig().getLong("Abilities.Water.Torrent.Cooldown"); + this.revert = getConfig().getBoolean("Abilities.Water.Torrent.Revert"); + this.revertTime = getConfig().getLong("Abilities.Water.Torrent.RevertTime"); this.blocks = new ArrayList<>(); this.launchedBlocks = new ArrayList<>(); this.hurtEntities = new ArrayList<>(); @@ -120,7 +123,9 @@ public class Torrent extends WaterAbility { if (isTransparent(player, block) && block.getType() != Material.ICE) { TempBlock tblock = new TempBlock(block, Material.ICE, (byte) 0); FROZEN_BLOCKS.put(tblock, player); - FROZEN_BLOCKS_DELAY.put(tblock, System.currentTimeMillis() + (new Random().nextInt((500 + 500) + 1) - 500)); + if (revert) { + tblock.setRevertTime(revertTime + (new Random().nextInt((500 + 500) + 1) - 500)); + } playIcebendingSound(block.getLocation()); } } @@ -589,10 +594,6 @@ public class Torrent extends WaterAbility { } else if (block.getBlock().getType() != Material.ICE) { FROZEN_BLOCKS.remove(block); continue; - } else if (getConfig().getBoolean("Abilities.Water.Torrent.Revert") && System.currentTimeMillis() - - FROZEN_BLOCKS_DELAY.get(block) > getConfig().getLong("Abilities.Water.Torrent.RevertTime")) { - thaw(block); - continue; } else if (!player.isOnline()) { thaw(block); continue;