mirror of
https://github.com/garbagemule/MobArena.git
synced 2025-02-17 04:51:28 +01:00
Replace the old AutoStartTimer with the new one.
This commit is contained in:
parent
00060ad5a4
commit
066341ba30
@ -22,7 +22,6 @@ import org.bukkit.potion.PotionEffect;
|
||||
import static com.garbagemule.MobArena.util.config.ConfigUtils.makeSection;
|
||||
|
||||
import com.garbagemule.MobArena.ArenaClass.ArmorType;
|
||||
import com.garbagemule.MobArena.autostart.AutoStartTimer;
|
||||
import com.garbagemule.MobArena.events.*;
|
||||
import com.garbagemule.MobArena.framework.Arena;
|
||||
import com.garbagemule.MobArena.leaderboards.Leaderboard;
|
||||
@ -35,6 +34,7 @@ import com.garbagemule.MobArena.time.TimeStrategyNull;
|
||||
import com.garbagemule.MobArena.util.*;
|
||||
import com.garbagemule.MobArena.util.inventory.InventoryManager;
|
||||
import com.garbagemule.MobArena.util.inventory.InventoryUtils;
|
||||
import com.garbagemule.MobArena.util.timer.AutoStartTimer;
|
||||
import com.garbagemule.MobArena.waves.*;
|
||||
import com.garbagemule.MobArena.ScoreboardManager.NullScoreboardManager;
|
||||
|
||||
@ -157,9 +157,8 @@ public class ArenaImpl implements Arena
|
||||
this.entryFee = ItemParser.parseItems(settings.getString("entry-fee", ""));
|
||||
this.allowMonsters = world.getAllowMonsters();
|
||||
this.allowAnimals = world.getAllowAnimals();
|
||||
|
||||
int autoStart = settings.getInt("auto-start-timer", 0);
|
||||
this.autoStartTimer = new AutoStartTimer(this, autoStart);
|
||||
|
||||
this.autoStartTimer = new AutoStartTimer(this);
|
||||
|
||||
this.isolatedChat = settings.getBoolean("isolated-chat", false);
|
||||
|
||||
@ -385,6 +384,9 @@ public class ArenaImpl implements Arena
|
||||
return false;
|
||||
}
|
||||
|
||||
// Stop the auto-start-timer
|
||||
autoStartTimer.stop();
|
||||
|
||||
// Fire the event and check if it's been cancelled.
|
||||
ArenaStartEvent event = new ArenaStartEvent(this);
|
||||
plugin.getServer().getPluginManager().callEvent(event);
|
||||
@ -542,7 +544,7 @@ public class ArenaImpl implements Arena
|
||||
playerLeave(p);
|
||||
Messenger.tell(p, Msg.LEAVE_NOT_READY);
|
||||
}
|
||||
|
||||
|
||||
startArena();
|
||||
}
|
||||
|
||||
@ -588,7 +590,7 @@ public class ArenaImpl implements Arena
|
||||
p.setGameMode(GameMode.SURVIVAL);
|
||||
|
||||
arenaPlayerMap.put(p, new ArenaPlayer(p, this, plugin));
|
||||
|
||||
|
||||
// Start the auto-start-timer
|
||||
autoStartTimer.start();
|
||||
|
||||
@ -597,7 +599,7 @@ public class ArenaImpl implements Arena
|
||||
|
||||
// Notify player of time left
|
||||
if (autoStartTimer.isRunning()) {
|
||||
Messenger.tell(p, Msg.ARENA_AUTO_START, "" + autoStartTimer.getRemaining());
|
||||
Messenger.tell(p, Msg.ARENA_AUTO_START, "" + autoStartTimer.getRemaining() / 20l);
|
||||
}
|
||||
|
||||
return true;
|
||||
|
@ -1,120 +0,0 @@
|
||||
package com.garbagemule.MobArena.autostart;
|
||||
|
||||
import com.garbagemule.MobArena.MobArena;
|
||||
import com.garbagemule.MobArena.Msg;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import com.garbagemule.MobArena.Messenger;
|
||||
import com.garbagemule.MobArena.framework.Arena;
|
||||
import org.bukkit.scheduler.BukkitRunnable;
|
||||
|
||||
public class AutoStartTimer {
|
||||
private MobArena plugin;
|
||||
private Arena arena;
|
||||
private int seconds;
|
||||
private Timer timer;
|
||||
private boolean useLevels;
|
||||
|
||||
public AutoStartTimer(Arena arena, int seconds) {
|
||||
this.plugin = arena.getPlugin();
|
||||
this.arena = arena;
|
||||
this.seconds = seconds;
|
||||
this.useLevels = arena.getSettings().getBoolean("display-timer-as-level", false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts the timer.
|
||||
* The method is idempotent, meaning if the timer was already
|
||||
* started, nothing happens if the method is called again.
|
||||
*/
|
||||
public void start() {
|
||||
if (seconds > 5 && timer == null) {
|
||||
timer = new Timer(seconds);
|
||||
timer.runTaskTimer(plugin, 20, 20);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Stops the timer.
|
||||
*/
|
||||
public void stop() {
|
||||
if (timer != null) {
|
||||
timer.stop();
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isRunning() {
|
||||
return (timer != null);
|
||||
}
|
||||
|
||||
public int getRemaining() {
|
||||
return (isRunning() ? timer.getRemaining() : -1);
|
||||
}
|
||||
|
||||
/**
|
||||
* The internal timer class used for the auto-join-timer setting.
|
||||
* Using an extra internal object allows the interruption of a current
|
||||
* timer, followed by the creation of a new. Thus, no timers should
|
||||
* ever interfere with each other.
|
||||
*/
|
||||
private class Timer extends BukkitRunnable {
|
||||
private int remaining;
|
||||
private int countdownIndex;
|
||||
private int[] intervals = new int[]{1, 2, 3, 4, 5, 10, 30};
|
||||
|
||||
private Timer(int seconds) {
|
||||
this.remaining = seconds;
|
||||
|
||||
// Find the first countdown announcement value
|
||||
for (int i = 0; i < intervals.length; i++) {
|
||||
if (seconds > intervals[i]) {
|
||||
countdownIndex = i;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the remaining number of seconds
|
||||
* @return number of seconds left
|
||||
*/
|
||||
public int getRemaining() {
|
||||
return remaining;
|
||||
}
|
||||
|
||||
public void stop() {
|
||||
cancel();
|
||||
AutoStartTimer.this.timer = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
// Abort if the arena is running, or if players have left
|
||||
if (arena.isRunning() || arena.getPlayersInLobby().isEmpty()) {
|
||||
stop();
|
||||
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);
|
||||
}
|
||||
}
|
||||
// Otherwise, warn at x seconds left
|
||||
else if (remaining == intervals[countdownIndex]) {
|
||||
Messenger.announce(arena, Msg.ARENA_AUTO_START, String.valueOf(remaining));
|
||||
countdownIndex--;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -2,9 +2,7 @@ package com.garbagemule.MobArena.framework;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
import com.garbagemule.MobArena.autostart.AutoStartTimer;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.block.Block;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
@ -25,6 +23,7 @@ import com.garbagemule.MobArena.leaderboards.Leaderboard;
|
||||
import com.garbagemule.MobArena.region.ArenaRegion;
|
||||
import com.garbagemule.MobArena.repairable.Repairable;
|
||||
import com.garbagemule.MobArena.util.inventory.InventoryManager;
|
||||
import com.garbagemule.MobArena.util.timer.AutoStartTimer;
|
||||
import com.garbagemule.MobArena.waves.WaveManager;
|
||||
|
||||
public interface Arena
|
||||
|
Loading…
Reference in New Issue
Block a user