Attempt to fix auto-start-timer issues.

This commit is contained in:
garbagemule 2013-09-20 23:25:38 +02:00
parent 349172b3b6
commit 42d656863f
5 changed files with 63 additions and 49 deletions

View File

@ -1,7 +1,7 @@
name: MobArena name: MobArena
author: garbagemule author: garbagemule
main: com.garbagemule.MobArena.MobArena main: com.garbagemule.MobArena.MobArena
version: 0.95.5.32 version: 0.95.5.34
softdepend: [Multiverse-Core,Towny,Heroes,MagicSpells,Vault] softdepend: [Multiverse-Core,Towny,Heroes,MagicSpells,Vault]
commands: commands:
ma: ma:

View File

@ -637,11 +637,16 @@ public class ArenaImpl implements Arena
refund(p); refund(p);
} }
ArenaPlayer ap = arenaPlayerMap.get(p);
if (inLobby(p)) { if (inLobby(p)) {
ArenaPlayer ap = arenaPlayerMap.get(p);
if (ap.getArenaClass() != null) { if (ap.getArenaClass() != null) {
limitManager.playerLeftClass(ap.getArenaClass(), ap.getPlayer()); limitManager.playerLeftClass(ap.getArenaClass(), ap.getPlayer());
} }
// Last lobby player leaving? Stop the timer
if (lobbyPlayers.size() == 1) {
autoStartTimer.stop();
}
} }
movePlayerToEntry(p); movePlayerToEntry(p);
@ -1290,6 +1295,11 @@ public class ArenaImpl implements Arena
return arenaPlayerMap.values(); return arenaPlayerMap.values();
} }
@Override
public AutoStartTimer getAutoStartTimer() {
return autoStartTimer;
}
/*@Override /*@Override
public List<ArenaPlayerStatistics> getArenaPlayerStatistics(Comparator<ArenaPlayerStatistics> comparator) public List<ArenaPlayerStatistics> getArenaPlayerStatistics(Comparator<ArenaPlayerStatistics> comparator)
{ {

View File

@ -1,22 +1,25 @@
package com.garbagemule.MobArena.autostart; package com.garbagemule.MobArena.autostart;
import com.garbagemule.MobArena.MobArena;
import com.garbagemule.MobArena.Msg; import com.garbagemule.MobArena.Msg;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import com.garbagemule.MobArena.Messenger; import com.garbagemule.MobArena.Messenger;
import com.garbagemule.MobArena.framework.Arena; import com.garbagemule.MobArena.framework.Arena;
import org.bukkit.scheduler.BukkitRunnable;
public class AutoStartTimer { public class AutoStartTimer {
private MobArena plugin;
private Arena arena; private Arena arena;
private int seconds; private int seconds;
private Timer timer; private Timer timer;
private boolean started;
private boolean useLevels; private boolean useLevels;
public AutoStartTimer(Arena arena, int seconds) { public AutoStartTimer(Arena arena, int seconds) {
this.plugin = arena.getPlugin();
this.arena = arena; this.arena = arena;
this.seconds = seconds; this.seconds = seconds;
this.started = false;
this.useLevels = arena.getSettings().getBoolean("display-timer-as-level", false); this.useLevels = arena.getSettings().getBoolean("display-timer-as-level", false);
} }
@ -26,19 +29,27 @@ public class AutoStartTimer {
* started, nothing happens if the method is called again. * started, nothing happens if the method is called again.
*/ */
public void start() { public void start() {
if (seconds > 5 && !started) { if (seconds > 5 && timer == null) {
timer = new Timer(seconds); timer = new Timer(seconds);
timer.start(); timer.runTaskTimer(plugin, 20, 20);
started = true; }
}
/**
* Stops the timer.
*/
public void stop() {
if (timer != null) {
timer.stop();
} }
} }
public boolean isRunning() { public boolean isRunning() {
return (timer != null && started); return (timer != null);
} }
public int getRemaining() { public int getRemaining() {
return (timer != null ? timer.getRemaining() : -1); return (isRunning() ? timer.getRemaining() : -1);
} }
/** /**
@ -47,7 +58,7 @@ public class AutoStartTimer {
* timer, followed by the creation of a new. Thus, no timers should * timer, followed by the creation of a new. Thus, no timers should
* ever interfere with each other. * ever interfere with each other.
*/ */
private class Timer implements Runnable { private class Timer extends BukkitRunnable {
private int remaining; private int remaining;
private int countdownIndex; private int countdownIndex;
private int[] intervals = new int[]{1, 2, 3, 4, 5, 10, 30}; private int[] intervals = new int[]{1, 2, 3, 4, 5, 10, 30};
@ -65,55 +76,44 @@ public class AutoStartTimer {
} }
} }
/**
* Start the timer
*/
public synchronized void start() {
arena.scheduleTask(this, 20);
}
/** /**
* Get the remaining number of seconds * Get the remaining number of seconds
* @return number of seconds left * @return number of seconds left
*/ */
public synchronized int getRemaining() { public int getRemaining() {
return remaining; return remaining;
} }
public void stop() {
cancel();
AutoStartTimer.this.timer = null;
}
@Override @Override
public void run() { public void run() {
synchronized(this) { // Abort if the arena is running, or if players have left
// Abort if the arena is running, or if players have left if (arena.isRunning() || arena.getPlayersInLobby().isEmpty()) {
if (arena.isRunning() || arena.getPlayersInLobby().isEmpty()) { stop();
started = false; return;
this.notifyAll(); }
return;
// Count down and start if 0
if (--remaining <= 0) {
stop();
arena.forceStart();
return;
}
// If using levels, update 'em
if (useLevels) {
for (Player p : arena.getPlayersInLobby()) {
p.setLevel(remaining);
} }
}
// Count down // Otherwise, warn at x seconds left
remaining--; else if (remaining == intervals[countdownIndex]) {
Messenger.announce(arena, Msg.ARENA_AUTO_START, String.valueOf(remaining));
// Start if 0 countdownIndex--;
if (remaining <= 0) {
arena.forceStart();
started = false;
} else {
// If using levels, update 'em
if (useLevels) {
for (Player p : arena.getPlayersInLobby()) {
p.setLevel(remaining);
}
}
// Otherwise, warn at x seconds left
else if (remaining == intervals[countdownIndex]) {
Messenger.announce(arena, Msg.ARENA_AUTO_START, ""+remaining);
countdownIndex--;
}
// Reschedule
arena.scheduleTask(this, 20);
}
this.notifyAll();
} }
} }
} }

View File

@ -2,6 +2,7 @@ package com.garbagemule.MobArena.framework;
import java.util.*; import java.util.*;
import com.garbagemule.MobArena.autostart.AutoStartTimer;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.World; import org.bukkit.World;
@ -243,4 +244,6 @@ public interface Arena
public boolean hasIsolatedChat(); public boolean hasIsolatedChat();
public Player getLastPlayerStanding(); public Player getLastPlayerStanding();
public AutoStartTimer getAutoStartTimer();
} }

View File

@ -50,6 +50,7 @@ public class InventoryManager
// And clear the inventory // And clear the inventory
clearInventory(p); clearInventory(p);
p.updateInventory();
} }
public void restoreInv(Player p) throws FileNotFoundException, IOException, InvalidConfigurationException { public void restoreInv(Player p) throws FileNotFoundException, IOException, InvalidConfigurationException {