Skip to content

Commit 426f666

Browse files
committed
Endless Timer and Teams
+ EndlessGameTimer + TeamManager
1 parent 11e35d3 commit 426f666

File tree

9 files changed

+191
-37
lines changed

9 files changed

+191
-37
lines changed

.idea/workspace.xml

+7-24
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
<groupId>com.github.teraprath.tinylib</groupId>
66
<artifactId>TinyLib</artifactId>
7-
<version>2.2.0</version>
7+
<version>2.2.1</version>
88

99
<name>TinyLib</name>
1010

src/main/java/com/github/teraprath/tinylib/game/api/GameAPI.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import com.github.teraprath.tinylib.game.api.listener.PlayerJoinListener;
77
import com.github.teraprath.tinylib.game.api.listener.PlayerQuitListener;
88
import com.github.teraprath.tinylib.game.api.listener.WaitingListener;
9-
import com.github.teraprath.tinylib.game.api.team.GameTeamManager;
9+
import com.github.teraprath.tinylib.game.team.GameTeamManager;
1010
import com.github.teraprath.tinylib.game.api.timer.RunningTimer;
1111
import com.github.teraprath.tinylib.game.api.timer.ShutdownTimer;
1212
import com.github.teraprath.tinylib.game.api.timer.WaitingTimer;
@@ -39,7 +39,6 @@ public GameAPI(@Nonnull GameSettings settings, @Nonnull JavaPlugin plugin) {
3939
this.runningTimer = new RunningTimer(this);
4040
this.shutdownTimer = new ShutdownTimer(this);
4141
this.alive = new ArrayList<>();
42-
4342
}
4443

4544
public GameAPI init() {

src/main/java/com/github/teraprath/tinylib/game/api/team/GameTeam.java

-4
This file was deleted.

src/main/java/com/github/teraprath/tinylib/game/api/team/GameTeamManager.java

-4
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.github.teraprath.tinylib.game.team;
2+
3+
import javax.annotation.Nonnegative;
4+
import javax.annotation.Nonnull;
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
import java.util.UUID;
8+
9+
public class GameTeam {
10+
11+
private final String name;
12+
private final List<UUID> members;
13+
14+
public GameTeam(@Nonnull String name, @Nonnegative int maxMembers) {
15+
this.name = name;
16+
this.members = new ArrayList<>(maxMembers);
17+
}
18+
19+
public String getName() {
20+
return name;
21+
}
22+
23+
public List<UUID> getMembers() {
24+
return members;
25+
}
26+
27+
public int getMaxMembers() {
28+
return this.members.size();
29+
}
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.github.teraprath.tinylib.game.team;
2+
3+
import org.bukkit.entity.Player;
4+
5+
import javax.annotation.Nonnull;
6+
import java.util.ArrayList;
7+
import java.util.HashMap;
8+
import java.util.List;
9+
import java.util.Map;
10+
11+
public class GameTeamManager {
12+
13+
private final Map<String, GameTeam> teams;
14+
15+
public GameTeamManager() {
16+
this.teams = new HashMap<>();
17+
}
18+
19+
public void register(@Nonnull GameTeam team) {
20+
this.teams.put(team.getName(), team);
21+
}
22+
23+
public GameTeam getTeam(@Nonnull String name) {
24+
return this.teams.get(name);
25+
}
26+
27+
public GameTeam getTeam(@Nonnull Player player) {
28+
for (GameTeam team : teams.values()) {
29+
if (team.getMembers().contains(player.getUniqueId())) {
30+
return team;
31+
}
32+
}
33+
return null;
34+
}
35+
36+
public List<GameTeam> getTeams() {
37+
return new ArrayList<>(teams.values());
38+
}
39+
40+
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
package com.github.teraprath.tinylib.game.timer;
2+
3+
import org.bukkit.plugin.java.JavaPlugin;
4+
import org.bukkit.scheduler.BukkitRunnable;
5+
6+
import javax.annotation.Nonnull;
7+
8+
public abstract class EndlessGameTimer {
9+
10+
private final JavaPlugin plugin;
11+
private long elapsedTime = 0;
12+
private BukkitRunnable timerTask;
13+
private boolean isPaused = false;
14+
private boolean isRunning = false;
15+
16+
public EndlessGameTimer(@Nonnull JavaPlugin plugin) {
17+
this.plugin = plugin;
18+
}
19+
20+
public void start() {
21+
if (!isRunning) {
22+
isRunning = true;
23+
runTimerTask();
24+
}
25+
}
26+
27+
private void runTimerTask() {
28+
timerTask = new BukkitRunnable() {
29+
@Override
30+
public void run() {
31+
if (!isPaused) {
32+
onTick();
33+
elapsedTime += 1000;
34+
}
35+
}
36+
};
37+
timerTask.runTaskTimer(plugin, 0, 20); // 20 Ticks = 1 Sekunde
38+
}
39+
40+
public void pause() {
41+
if (!isPaused && isRunning) {
42+
isPaused = true;
43+
if (timerTask != null) {
44+
timerTask.cancel();
45+
}
46+
}
47+
}
48+
49+
public void resume() {
50+
if (isPaused && isRunning) {
51+
isPaused = false;
52+
runTimerTask();
53+
}
54+
}
55+
56+
public void cancel() {
57+
if (timerTask != null && !timerTask.isCancelled()) {
58+
timerTask.cancel();
59+
isRunning = false;
60+
isPaused = false;
61+
elapsedTime = 0;
62+
}
63+
}
64+
65+
public void stop() {
66+
if (isRunning) {
67+
isRunning = false;
68+
isPaused = false;
69+
if (timerTask != null) {
70+
timerTask.cancel();
71+
}
72+
onStop();
73+
}
74+
}
75+
76+
public void reset() {
77+
if (isRunning) {
78+
cancel();
79+
runTimerTask();
80+
}
81+
}
82+
83+
public boolean isRunning() {
84+
return isRunning && !isPaused;
85+
}
86+
87+
public int getElapsedTimeInSeconds() {
88+
return (int) (elapsedTime / 1000);
89+
}
90+
91+
public long getElapsedTime() {
92+
return elapsedTime;
93+
}
94+
95+
public String getFormattedElapsedTime() {
96+
long totalSeconds = getElapsedTimeInSeconds();
97+
long hours = totalSeconds / 3600;
98+
long minutes = (totalSeconds / 60) % 60;
99+
long seconds = totalSeconds % 60;
100+
101+
if (hours > 0) {
102+
return String.format("%02d:%02d:%02d", hours, minutes, seconds);
103+
} else {
104+
return String.format("%02d:%02d", minutes, seconds);
105+
}
106+
}
107+
108+
protected abstract void onTick();
109+
protected abstract void onStop();
110+
}

src/main/java/com/github/teraprath/tinylib/game/timer/GameTimer.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ public void run() {
3636
onTick();
3737
remainingTime -= 1000;
3838
} else if (remainingTime <= 0) {
39-
onComplete();
4039
stop();
4140
}
4241
}
@@ -72,12 +71,12 @@ public void cancel() {
7271

7372
public void stop() {
7473
if (isRunning) {
75-
remainingTime = 0;
7674
isRunning = false;
7775
isPaused = false;
7876
if (timerTask != null) {
7977
timerTask.cancel();
8078
}
79+
onComplete();
8180
}
8281
}
8382

0 commit comments

Comments
 (0)