Rewrote day/night handling

Rewrote day/night handling. Much faster, requires no adding worlds to a concurrenthashmap or looping over them. It's also much easier to understand and modify.
This commit is contained in:
HiddenCloud 2014-07-28 22:16:08 +00:00
parent 0a9983658a
commit 65e0f0bd97

View file

@ -1,7 +1,6 @@
package com.projectkorra.ProjectKorra; package com.projectkorra.ProjectKorra;
import java.util.ArrayList; import java.util.HashMap;
import java.util.concurrent.ConcurrentHashMap;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
@ -67,9 +66,7 @@ public class BendingManager implements Runnable {
long time; long time;
long interval; long interval;
ArrayList<World> worlds = new ArrayList<World>(); private final HashMap<String, Time> dayNight = new HashMap<>();
ConcurrentHashMap<World, Boolean> nights = new ConcurrentHashMap<World, Boolean>();
ConcurrentHashMap<World, Boolean> days = new ConcurrentHashMap<World, Boolean>();
static final String defaultsunrisemessage = "You feel the strength of the rising sun empowering your firebending."; static final String defaultsunrisemessage = "You feel the strength of the rising sun empowering your firebending.";
static final String defaultsunsetmessage = "You feel the empowering of your firebending subside as the sun sets."; static final String defaultsunsetmessage = "You feel the empowering of your firebending subside as the sun sets.";
@ -143,15 +140,15 @@ public class BendingManager implements Runnable {
for (Player player : EarthArmor.instances.keySet()) { for (Player player : EarthArmor.instances.keySet()) {
EarthArmor.moveArmor(player); EarthArmor.moveArmor(player);
} }
for (int ID: AirSwipe.instances.keySet()) { for (int ID : AirSwipe.instances.keySet()) {
AirSwipe.progress(ID); AirSwipe.progress(ID);
} }
for (int ID: Tornado.instances.keySet()) { for (int ID : Tornado.instances.keySet()) {
Tornado.progress(ID); Tornado.progress(ID);
} }
Tremorsense.manage(Bukkit.getServer()); Tremorsense.manage(Bukkit.getServer());
for (int id: FireStream.instances.keySet()) { for (int id : FireStream.instances.keySet()) {
FireStream.progress(id); FireStream.progress(id);
} }
@ -159,29 +156,29 @@ public class BendingManager implements Runnable {
EarthBlast.progress(ID); EarthBlast.progress(ID);
} }
for (Block block: FireStream.ignitedblocks.keySet()) { for (Block block : FireStream.ignitedblocks.keySet()) {
if (block.getType() != Material.FIRE) { if (block.getType() != Material.FIRE) {
FireStream.ignitedblocks.remove(block); FireStream.ignitedblocks.remove(block);
} }
} }
for (int ID: Catapult.instances.keySet()) { for (int ID : Catapult.instances.keySet()) {
Catapult.progress(ID); Catapult.progress(ID);
} }
for (int ID: EarthColumn.instances.keySet()) { for (int ID : EarthColumn.instances.keySet()) {
EarthColumn.progress(ID); EarthColumn.progress(ID);
} }
for (int ID: CompactColumn.instances.keySet()) { for (int ID : CompactColumn.instances.keySet()) {
CompactColumn.progress(ID); CompactColumn.progress(ID);
} }
for (int ID: WaterManipulation.instances.keySet()) { for (int ID : WaterManipulation.instances.keySet()) {
WaterManipulation.progress(ID); WaterManipulation.progress(ID);
} }
for (int ID: WaterWall.instances.keySet()) { for (int ID : WaterWall.instances.keySet()) {
WaterWall.progress(ID); WaterWall.progress(ID);
} }
@ -209,65 +206,104 @@ public class BendingManager implements Runnable {
} }
public void handleDayNight() { public void handleDayNight() {
for (World world: plugin.getServer().getWorlds()) { /**
if (world.getWorldType() == WorldType.NORMAL && !worlds.contains(world)) { * This code is ran on startup, it adds all loaded worlds to the
worlds.add(world); * hashmap.
nights.put(world, false); */
days.put(world, false); if (dayNight.size() < 1) {
for (World world : plugin.getServer().getWorlds()) {
if (world.getWorldType() == WorldType.NORMAL) {
String worldName = world.getName();
if (dayNight.containsKey(worldName))
return;
if (Methods.isDay(world)) {
dayNight.put(worldName, Time.DAY);
} else {
dayNight.put(worldName, Time.NIGHT);
} }
} }
ArrayList<World> removeworlds = new ArrayList<World>();
for (World world: worlds) {
if (!plugin.getServer().getWorlds().contains(world)) {
removeworlds.add(world);
continue;
} }
boolean night = nights.get(world); }
boolean day = days.get(world);
if (Methods.isDay(world) && !day) { for (World world : Bukkit.getWorlds()) {
for (Player player: world.getPlayers()) { final String worldName = world.getName();
if (Methods.isBender(player.getName(), Element.Fire) && player.hasPermission("bending.message.daymessage")) { if (!dayNight.containsKey(worldName))
return;
Time time = dayNight.get(worldName);
if (Methods.isDay(world) && time.equals(Time.NIGHT)) {
final Time newTime = Time.DAY;
sendFirebenderMessage(world, newTime);
dayNight.remove(worldName);
dayNight.put(worldName, newTime);
}
if (!Methods.isDay(world) && time.equals(Time.DAY)) {
final Time newTime = Time.NIGHT;
sendFirebenderMessage(world, newTime);
dayNight.remove(worldName);
dayNight.put(worldName, newTime);
}
if (Methods.isNight(world) && time.equals(Time.DAY)) {
final Time newTime = Time.NIGHT;
sendWaterbenderMessage(world, newTime);
dayNight.remove(worldName);
dayNight.put(worldName, newTime);
}
if (!Methods.isNight(world) && time.equals(Time.NIGHT)) {
final Time newTime = Time.DAY;
sendWaterbenderMessage(world, Time.DAY);
dayNight.remove(worldName);
dayNight.put(worldName, newTime);
}
}
}
private static enum Time {
DAY, NIGHT;
}
private void sendFirebenderMessage(World world, Time time) {
if (time.equals(Time.DAY)) {
for (Player player : world.getPlayers()) {
if (Methods.isBender(player.getName(), Element.Fire)
&& player.hasPermission("bending.message.daymessage")) {
player.sendMessage(ChatColor.RED + defaultsunrisemessage); player.sendMessage(ChatColor.RED + defaultsunrisemessage);
} }
} }
days.replace(world, true); } else {
} for (Player player : world.getPlayers()) {
if (Methods.isBender(player.getName(), Element.Fire)
if (!Methods.isDay(world) && day) { && player.hasPermission("bending.message.daymessage")) {
for (Player player: world.getPlayers()) {
if (Methods.isBender(player.getName(), Element.Fire) && player.hasPermission("bending.message.daymessage")) {
player.sendMessage(ChatColor.RED + defaultsunsetmessage); player.sendMessage(ChatColor.RED + defaultsunsetmessage);
} }
} }
days.replace(world, false); }
} }
if (Methods.isNight(world) && !night) { private void sendWaterbenderMessage(World world, Time time) {
for (Player player: world.getPlayers()) { if (time.equals(Time.NIGHT)) {
if (Methods.isBender(player.getName(), Element.Water) && player.hasPermission("bending.message.nightmessage")) { for (Player player : world.getPlayers()) {
if (Methods.isBender(player.getName(), Element.Water)
&& player.hasPermission("bending.message.nightmessage")) {
if (Methods.isFullMoon(world)) { if (Methods.isFullMoon(world)) {
player.sendMessage(ChatColor.AQUA + defaultfullmoonrisemessage); player.sendMessage(ChatColor.AQUA
+ defaultfullmoonrisemessage);
} else { } else {
player.sendMessage(ChatColor.AQUA + defaultmoonrisemessage); player.sendMessage(ChatColor.AQUA
+ defaultmoonrisemessage);
} }
} }
} }
nights.replace(world, true); } else {
} for (Player player : world.getPlayers()) {
if (Methods.isBender(player.getName(), Element.Water)
if (!Methods.isNight(world) && night) { && player.hasPermission("bending.message.nightmessage")) {
for (Player player: world.getPlayers()) {
if (Methods.isBender(player.getName(), Element.Water) && player.hasPermission("bending.message.nightmessage")) {
player.sendMessage(ChatColor.AQUA + defaultmoonsetmessage); player.sendMessage(ChatColor.AQUA + defaultmoonsetmessage);
} }
} }
nights.replace(world, false);
} }
} }
for (World world: removeworlds) {
worlds.remove(world);
}
}
} }