Skip to content

Commit

Permalink
Fix auto ready behavior; resolves #435
Browse files Browse the repository at this point in the history
  • Loading branch information
Sataniel98 committed Oct 6, 2018
1 parent bbf5de8 commit 62f3ff0
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 46 deletions.
13 changes: 8 additions & 5 deletions src/main/java/de/erethon/dungeonsxl/player/DGamePlayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -692,9 +692,9 @@ public void ready() {
ready(GameTypeDefault.DEFAULT);
}

public void ready(GameType gameType) {
public boolean ready(GameType gameType) {
if (getDGroup() == null) {
return;
return false;
}

Game game = Game.getByGameWorld(dGroup.getGameWorld());
Expand All @@ -708,21 +708,24 @@ public void ready(GameType gameType) {

if (!checkRequirements(game)) {
MessageUtil.sendMessage(player, DMessage.ERROR_REQUIREMENTS.getMessage());
return;
return false;
}

ready = true;

boolean start = true;
for (DGroup gameGroup : game.getDGroups()) {
if (!gameGroup.isPlaying()) {
gameGroup.startGame(game);

if (!gameGroup.startGame(game)) {
start = false;
}
} else {
respawn();
}
}

game.setStarted(true);
return start;
}

public void respawn() {
Expand Down
9 changes: 5 additions & 4 deletions src/main/java/de/erethon/dungeonsxl/player/DGroup.java
Original file line number Diff line number Diff line change
Expand Up @@ -758,9 +758,9 @@ public void delete() {
plugin.getGlobalProtectionCache().updateGroupSigns(this);
}

public void startGame(Game game) {
public boolean startGame(Game game) {
if (game == null) {
return;
return false;
}
game.fetchRules();
GameRuleProvider rules = game.getRules();
Expand Down Expand Up @@ -788,15 +788,15 @@ public void startGame(Game game) {
}

if (!ready) {
return;
return false;
}
}

DGroupStartFloorEvent event = new DGroupStartFloorEvent(this, gameWorld);
Bukkit.getPluginManager().callEvent(event);

if (event.isCancelled()) {
return;
return false;
}

playing = true;
Expand Down Expand Up @@ -872,6 +872,7 @@ public void startGame(Game game) {
nextFloor = null;
initialLives = rules.getInitialGroupLives();
lives = initialLives;
return true;
}

public void winGame() {
Expand Down
74 changes: 38 additions & 36 deletions src/main/java/de/erethon/dungeonsxl/sign/lobby/ReadySign.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,19 @@
import org.bukkit.ChatColor;
import org.bukkit.block.Sign;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable;

/**
* @author Frank Baumann, Milan Albrecht, Daniel Saukel
*/
public class ReadySign extends DSign {

private DSignType type = DSignTypeDefault.READY;

private GameType gameType;
private double autoStart = -1;
private boolean triggered = false;

private ProgressBar bar;

public ReadySign(DungeonsXL plugin, Sign sign, String[] lines, DGameWorld gameWorld) {
super(plugin, sign, lines, gameWorld);
}
Expand Down Expand Up @@ -78,101 +78,103 @@ public double getTimeToAutoStart() {
public void setTimeToAutoStart(double time) {
autoStart = time;
}

@Override
public boolean check() {
return true;
}

@Override
public void onInit() {
if (plugin.getGameTypeCache().getBySign(this) != null) {
gameType = plugin.getGameTypeCache().getBySign(this);

} else {
gameType = GameTypeDefault.CUSTOM;
}

if (!lines[2].isEmpty()) {
autoStart = NumberUtil.parseDouble(lines[2], -1);
}

if (!getTriggers().isEmpty()) {
getSign().getBlock().setType(VanillaItem.AIR.getMaterial());
return;
}

InteractTrigger trigger = InteractTrigger.getOrCreate(0, getSign().getBlock(), getGameWorld());
if (trigger != null) {
trigger.addListener(this);
addTrigger(trigger);
}

getSign().setLine(0, ChatColor.DARK_BLUE + "############");
getSign().setLine(1, DMessage.SIGN_READY.getMessage());
getSign().setLine(2, ChatColor.DARK_RED + gameType.getSignName());
getSign().setLine(3, ChatColor.DARK_BLUE + "############");
getSign().update();
}

@Override
public boolean onPlayerTrigger(Player player) {
ready(DGamePlayer.getByPlayer(player));

if (!triggered && autoStart >= 0) {
triggered = true;

new BukkitRunnable() {
@Override
public void run() {
onTrigger();
}
}.runTaskLater(plugin, (long) (autoStart * 20));


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;
}

@Override
public void onTrigger() {
if (getGame() == null) {
return;
}


if (bar != null) {
bar.cancel();
}

for (Player player : getGame().getPlayers()) {
ready(DGamePlayer.getByPlayer(player));
}
}

private void ready(DGamePlayer dPlayer) {
if (dPlayer == null) {
if (dPlayer == null || dPlayer.isReady()) {
return;
}

if (dPlayer.isReady()) {
return;
}


if (getGameWorld().getClassesSigns().isEmpty() || dPlayer.getDClass() != null) {
GameType forced = null;
if (getGameWorld().getConfig() != null) {
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()) {
MessageUtil.sendMessage(dPlayer.getPlayer(), (dPlayer.isReady() ? DMessage.PLAYER_READY : DMessage.ERROR_READY).getMessage());
}
}

@Override
public DSignType getType() {
return type;
}

}
8 changes: 7 additions & 1 deletion src/main/java/de/erethon/dungeonsxl/util/ProgressBar.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package de.erethon.dungeonsxl.util;

import de.erethon.commons.chat.MessageUtil;
import de.erethon.commons.javaplugin.DREPlugin;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
Expand Down Expand Up @@ -83,12 +82,19 @@ public void run() {
}

if (secondsLeft == 0) {
onFinish();
cancel();
} else {
secondsLeft--;
}
}

/**
* Method to override to set actions when no seconds are left.
*/
public void onFinish() {
}

/**
* Sends the progress bar to a player
*
Expand Down

0 comments on commit 62f3ff0

Please sign in to comment.