Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make protectionTimer publicly accessible #102

Merged
merged 12 commits into from
Jan 11, 2025
46 changes: 28 additions & 18 deletions src/main/java/de/cuuky/varo/game/start/ProtectionTime.java
Original file line number Diff line number Diff line change
@@ -10,6 +10,7 @@
public class ProtectionTime {

private BukkitTask scheduler;
private int protectionTimer;

public ProtectionTime() {
startGeneralTimer(ConfigSetting.STARTPERIOD_PROTECTIONTIME.getValueAsInt());
@@ -20,44 +21,53 @@ public ProtectionTime(int timer) {
}

private void startGeneralTimer(int timer) {
if (timer == 0) {
throw new IllegalArgumentException();
}

this.protectionTimer = timer;
this.scheduler = new BukkitRunnable() {

private int protectionTimer = timer;

@Override
public void run() {
if (!Main.getVaroGame().isRunning()) {
scheduler.cancel();
return;
}

if (this.protectionTimer == 0) {
if (ProtectionTime.this.protectionTimer == 0) {
Main.getLanguageManager().broadcastMessage(ConfigMessages.PROTECTION_TIME_OVER);
Main.getVaroGame().setProtection(null);
scheduler.cancel();
} else if (protectionTimer % ConfigSetting.STARTPERIOD_PROTECTIONTIME_BROADCAST_INTERVAL.getValueAsInt() == 0 && this.protectionTimer != timer)
Main.getLanguageManager().broadcastMessage(ConfigMessages.PROTECTION_TIME_UPDATE).replace("%minutes%", getCountdownMin(protectionTimer)).replace("%seconds%", getCountdownSec(protectionTimer));
} else if (ProtectionTime.this.protectionTimer != timer
&& ProtectionTime.this.protectionTimer % ConfigSetting.STARTPERIOD_PROTECTIONTIME_BROADCAST_INTERVAL.getValueAsInt() == 0) {
Main.getLanguageManager().broadcastMessage(ConfigMessages.PROTECTION_TIME_UPDATE)
.replace("%minutes%", getCountdownMin(ProtectionTime.this.protectionTimer))
.replace("%seconds%", getCountdownSec(ProtectionTime.this.protectionTimer));
}

this.protectionTimer--;
ProtectionTime.this.protectionTimer--;
}
}.runTaskTimer(Main.getInstance(), 1L, 20L);
}

private String getCountdownMin(int sec) {
public String getCountdownMin(int sec) {
int min = sec / 60;

if (min < 10)
return "0" + min;
else
return min + "";
return (min < 10) ? "0" + min : String.valueOf(min);
}

private String getCountdownSec(int sec) {
public String getCountdownSec(int sec) {
sec = sec % 60;
return (sec < 10) ? "0" + sec : String.valueOf(sec);
}

/**
* Returns the protection timer
*
* @return the protection timer
*/
public int getProtectionTimer() {
return this.protectionTimer;

if (sec < 10)
return "0" + sec;
else
return sec + "";
}
}
}