Fix auto ready behavior; resolves #435

This commit is contained in:
Daniel Saukel 2018-10-06 17:30:14 +02:00
parent bbf5de85ec
commit 62f3ff065b
4 changed files with 58 additions and 46 deletions

View File

@ -692,9 +692,9 @@ public class DGamePlayer extends DInstancePlayer {
ready(GameTypeDefault.DEFAULT); ready(GameTypeDefault.DEFAULT);
} }
public void ready(GameType gameType) { public boolean ready(GameType gameType) {
if (getDGroup() == null) { if (getDGroup() == null) {
return; return false;
} }
Game game = Game.getByGameWorld(dGroup.getGameWorld()); Game game = Game.getByGameWorld(dGroup.getGameWorld());
@ -708,21 +708,24 @@ public class DGamePlayer extends DInstancePlayer {
if (!checkRequirements(game)) { if (!checkRequirements(game)) {
MessageUtil.sendMessage(player, DMessage.ERROR_REQUIREMENTS.getMessage()); MessageUtil.sendMessage(player, DMessage.ERROR_REQUIREMENTS.getMessage());
return; return false;
} }
ready = true; ready = true;
boolean start = true;
for (DGroup gameGroup : game.getDGroups()) { for (DGroup gameGroup : game.getDGroups()) {
if (!gameGroup.isPlaying()) { if (!gameGroup.isPlaying()) {
gameGroup.startGame(game); if (!gameGroup.startGame(game)) {
start = false;
}
} else { } else {
respawn(); respawn();
} }
} }
game.setStarted(true); game.setStarted(true);
return start;
} }
public void respawn() { public void respawn() {

View File

@ -758,9 +758,9 @@ public class DGroup {
plugin.getGlobalProtectionCache().updateGroupSigns(this); plugin.getGlobalProtectionCache().updateGroupSigns(this);
} }
public void startGame(Game game) { public boolean startGame(Game game) {
if (game == null) { if (game == null) {
return; return false;
} }
game.fetchRules(); game.fetchRules();
GameRuleProvider rules = game.getRules(); GameRuleProvider rules = game.getRules();
@ -788,7 +788,7 @@ public class DGroup {
} }
if (!ready) { if (!ready) {
return; return false;
} }
} }
@ -796,7 +796,7 @@ public class DGroup {
Bukkit.getPluginManager().callEvent(event); Bukkit.getPluginManager().callEvent(event);
if (event.isCancelled()) { if (event.isCancelled()) {
return; return false;
} }
playing = true; playing = true;
@ -872,6 +872,7 @@ public class DGroup {
nextFloor = null; nextFloor = null;
initialLives = rules.getInitialGroupLives(); initialLives = rules.getInitialGroupLives();
lives = initialLives; lives = initialLives;
return true;
} }
public void winGame() { public void winGame() {

View File

@ -34,19 +34,19 @@ import de.erethon.dungeonsxl.world.DGameWorld;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.block.Sign; import org.bukkit.block.Sign;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable;
/** /**
* @author Frank Baumann, Milan Albrecht, Daniel Saukel * @author Frank Baumann, Milan Albrecht, Daniel Saukel
*/ */
public class ReadySign extends DSign { public class ReadySign extends DSign {
private DSignType type = DSignTypeDefault.READY; private DSignType type = DSignTypeDefault.READY;
private GameType gameType; private GameType gameType;
private double autoStart = -1; private double autoStart = -1;
private boolean triggered = false; private boolean triggered = false;
private ProgressBar bar;
public ReadySign(DungeonsXL plugin, Sign sign, String[] lines, DGameWorld gameWorld) { public ReadySign(DungeonsXL plugin, Sign sign, String[] lines, DGameWorld gameWorld) {
super(plugin, sign, lines, gameWorld); super(plugin, sign, lines, gameWorld);
} }
@ -78,101 +78,103 @@ public class ReadySign extends DSign {
public void setTimeToAutoStart(double time) { public void setTimeToAutoStart(double time) {
autoStart = time; autoStart = time;
} }
@Override @Override
public boolean check() { public boolean check() {
return true; return true;
} }
@Override @Override
public void onInit() { public void onInit() {
if (plugin.getGameTypeCache().getBySign(this) != null) { if (plugin.getGameTypeCache().getBySign(this) != null) {
gameType = plugin.getGameTypeCache().getBySign(this); gameType = plugin.getGameTypeCache().getBySign(this);
} else { } else {
gameType = GameTypeDefault.CUSTOM; gameType = GameTypeDefault.CUSTOM;
} }
if (!lines[2].isEmpty()) { if (!lines[2].isEmpty()) {
autoStart = NumberUtil.parseDouble(lines[2], -1); autoStart = NumberUtil.parseDouble(lines[2], -1);
} }
if (!getTriggers().isEmpty()) { if (!getTriggers().isEmpty()) {
getSign().getBlock().setType(VanillaItem.AIR.getMaterial()); getSign().getBlock().setType(VanillaItem.AIR.getMaterial());
return; return;
} }
InteractTrigger trigger = InteractTrigger.getOrCreate(0, getSign().getBlock(), getGameWorld()); InteractTrigger trigger = InteractTrigger.getOrCreate(0, getSign().getBlock(), getGameWorld());
if (trigger != null) { if (trigger != null) {
trigger.addListener(this); trigger.addListener(this);
addTrigger(trigger); addTrigger(trigger);
} }
getSign().setLine(0, ChatColor.DARK_BLUE + "############"); getSign().setLine(0, ChatColor.DARK_BLUE + "############");
getSign().setLine(1, DMessage.SIGN_READY.getMessage()); getSign().setLine(1, DMessage.SIGN_READY.getMessage());
getSign().setLine(2, ChatColor.DARK_RED + gameType.getSignName()); getSign().setLine(2, ChatColor.DARK_RED + gameType.getSignName());
getSign().setLine(3, ChatColor.DARK_BLUE + "############"); getSign().setLine(3, ChatColor.DARK_BLUE + "############");
getSign().update(); getSign().update();
} }
@Override @Override
public boolean onPlayerTrigger(Player player) { public boolean onPlayerTrigger(Player player) {
ready(DGamePlayer.getByPlayer(player)); ready(DGamePlayer.getByPlayer(player));
if (!triggered && autoStart >= 0) { if (!triggered && autoStart >= 0) {
triggered = true; triggered = true;
new BukkitRunnable() {
@Override
public void run() {
onTrigger();
}
}.runTaskLater(plugin, (long) (autoStart * 20));
if (!DGroup.getByPlayer(player).isPlaying()) { if (!DGroup.getByPlayer(player).isPlaying()) {
new ProgressBar(getGame().getPlayers(), (int) Math.ceil(autoStart)).send(plugin); bar = new ProgressBar(getGame().getPlayers(), (int) Math.ceil(autoStart)) {
@Override
public void onFinish() {
onTrigger();
}
};
bar.send(plugin);
} }
} }
return true; return true;
} }
@Override @Override
public void onTrigger() { public void onTrigger() {
if (getGame() == null) { if (getGame() == null) {
return; return;
} }
if (bar != null) {
bar.cancel();
}
for (Player player : getGame().getPlayers()) { for (Player player : getGame().getPlayers()) {
ready(DGamePlayer.getByPlayer(player)); ready(DGamePlayer.getByPlayer(player));
} }
} }
private void ready(DGamePlayer dPlayer) { private void ready(DGamePlayer dPlayer) {
if (dPlayer == null) { if (dPlayer == null || dPlayer.isReady()) {
return; return;
} }
if (dPlayer.isReady()) {
return;
}
if (getGameWorld().getClassesSigns().isEmpty() || dPlayer.getDClass() != null) { if (getGameWorld().getClassesSigns().isEmpty() || dPlayer.getDClass() != null) {
GameType forced = null; GameType forced = null;
if (getGameWorld().getConfig() != null) { if (getGameWorld().getConfig() != null) {
forced = getGameWorld().getConfig().getForcedGameType(); forced = getGameWorld().getConfig().getForcedGameType();
} }
dPlayer.ready(forced == null ? gameType : forced); boolean ready = dPlayer.ready(forced == null ? gameType : forced);
if (ready && bar != null) {
bar.cancel();
}
} }
if (dPlayer.isReady()) { if (dPlayer.isReady()) {
MessageUtil.sendMessage(dPlayer.getPlayer(), (dPlayer.isReady() ? DMessage.PLAYER_READY : DMessage.ERROR_READY).getMessage()); MessageUtil.sendMessage(dPlayer.getPlayer(), (dPlayer.isReady() ? DMessage.PLAYER_READY : DMessage.ERROR_READY).getMessage());
} }
} }
@Override @Override
public DSignType getType() { public DSignType getType() {
return type; return type;
} }
} }

View File

@ -17,7 +17,6 @@
package de.erethon.dungeonsxl.util; package de.erethon.dungeonsxl.util;
import de.erethon.commons.chat.MessageUtil; import de.erethon.commons.chat.MessageUtil;
import de.erethon.commons.javaplugin.DREPlugin;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
@ -83,12 +82,19 @@ public class ProgressBar extends BukkitRunnable {
} }
if (secondsLeft == 0) { if (secondsLeft == 0) {
onFinish();
cancel(); cancel();
} else { } else {
secondsLeft--; secondsLeft--;
} }
} }
/**
* Method to override to set actions when no seconds are left.
*/
public void onFinish() {
}
/** /**
* Sends the progress bar to a player * Sends the progress bar to a player
* *