Skip to content

Commit 4c0bbb7

Browse files
committed
Improved performance and style of action bar timer
1 parent f5cd2bc commit 4c0bbb7

File tree

1 file changed

+60
-140
lines changed
  • src/main/java/de/lars/utilsmanager/features/timer

1 file changed

+60
-140
lines changed

src/main/java/de/lars/utilsmanager/features/timer/Timer.java

Lines changed: 60 additions & 140 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import de.lars.apimanager.apis.languageAPI.LanguageAPI;
44
import de.lars.apimanager.apis.timerAPI.TimerAPI;
55
import de.lars.utilsmanager.UtilsManager;
6+
import de.lars.utilsmanager.util.FormatNumbers;
67
import net.kyori.adventure.text.Component;
78
import net.kyori.adventure.text.format.NamedTextColor;
89
import org.bukkit.Bukkit;
@@ -15,6 +16,38 @@ public Timer() {
1516
run();
1617
}
1718

19+
private void run() {
20+
Bukkit.getScheduler().runTaskTimerAsynchronously(UtilsManager.getInstance(), bukkitTask -> {
21+
for (Player player : Bukkit.getOnlinePlayers()) {
22+
if (TimerAPI.getApi().isOff(player)) {
23+
return;
24+
}
25+
if (TimerAPI.getApi().isPublic(player)) {
26+
sendActionBarPublic(player);
27+
if (TimerAPI.getApi().isRunning(player)) {
28+
if(TimerAPI.getApi().isTimerEnabled(player)) {
29+
TimerAPI.getApi().setTime(player, TimerAPI.getApi().getTime(player) - 1);
30+
} else {
31+
TimerAPI.getApi().setTime(player, TimerAPI.getApi().getTime(player) + 1);
32+
}
33+
}
34+
break;
35+
}
36+
if(!(player.hasPermission("plugin.timer"))) {
37+
return;
38+
}
39+
sendActionBar(player);
40+
if (TimerAPI.getApi().isRunning(player)) {
41+
if(TimerAPI.getApi().isTimerEnabled(player)) {
42+
TimerAPI.getApi().setTime(player, TimerAPI.getApi().getTime(player) - 1);
43+
} else {
44+
TimerAPI.getApi().setTime(player, TimerAPI.getApi().getTime(player) + 1);
45+
}
46+
}
47+
}
48+
}, 20, 20);
49+
}
50+
1851
public void sendActionBar(Player player) {
1952
if (!TimerAPI.getApi().isRunning(player)) {
2053
if (LanguageAPI.getApi().getLanguage(player) == 2) {
@@ -27,29 +60,7 @@ public void sendActionBar(Player player) {
2760

2861
int time = TimerAPI.getApi().getTime(player);
2962
if(!(time < 0)) {
30-
int seconds = time % 60;
31-
int minutes = (time / 60) % 60;
32-
int hours = (time / 3600);
33-
Component minsec = Component.text()
34-
.append(Component.text(String.format("%02d", minutes), NamedTextColor.LIGHT_PURPLE))
35-
.append(Component.text("min ", NamedTextColor.BLUE))
36-
.append(Component.text(String.format("%02d", seconds), NamedTextColor.LIGHT_PURPLE))
37-
.append(Component.text("sec", NamedTextColor.BLUE))
38-
.build();
39-
40-
Component hourminsec = Component.text()
41-
.append(Component.text(String.format("%02d", hours), NamedTextColor.LIGHT_PURPLE))
42-
.append(Component.text("hr ", NamedTextColor.BLUE))
43-
.append(Component.text(String.format("%02d", minutes), NamedTextColor.LIGHT_PURPLE))
44-
.append(Component.text("min ", NamedTextColor.BLUE))
45-
.append(Component.text(String.format("%02d", seconds), NamedTextColor.LIGHT_PURPLE))
46-
.append(Component.text("sec", NamedTextColor.BLUE))
47-
.build();
48-
if ((time / 3600) == 0) {
49-
player.sendActionBar(minsec);
50-
} else {
51-
player.sendActionBar(hourminsec);
52-
}
63+
player.sendActionBar(FormatNumbers.formatTimeComponent(time));
5364

5465
if (TimerAPI.getApi().isTimerEnabled(player)) {
5566
playSound(player, time);
@@ -73,97 +84,47 @@ public void sendActionBar(Player player) {
7384
}
7485

7586
public void sendActionBarPublic(Player player) {
76-
for (Player onlineplayer:Bukkit.getOnlinePlayers()) {
77-
if (!TimerAPI.getApi().isRunning(player)) {
78-
if (LanguageAPI.getApi().getLanguage(player) == 2) {
87+
int time = TimerAPI.getApi().getTime(player);
88+
if (!TimerAPI.getApi().isRunning(player)) {
89+
for (Player onlineplayer:Bukkit.getOnlinePlayers()) {
90+
if (LanguageAPI.getApi().getLanguage(onlineplayer) == 2) {
7991
onlineplayer.sendActionBar(Component.text("Timer ist pausiert", NamedTextColor.RED));
8092
} else {
8193
onlineplayer.sendActionBar(Component.text("Timer is paused", NamedTextColor.RED));
8294
}
83-
return;
8495
}
96+
return;
97+
}
8598

86-
int time = TimerAPI.getApi().getTime(player);
87-
if(!(time < 0)) {
88-
int seconds = time % 60;
89-
int minutes = (time / 60) % 60;
90-
int hours = (time / 3600);
91-
Component minsec = Component.text()
92-
.append(Component.text(String.format("%02d", minutes), NamedTextColor.LIGHT_PURPLE))
93-
.append(Component.text("min ", NamedTextColor.BLUE))
94-
.append(Component.text(String.format("%02d", seconds), NamedTextColor.LIGHT_PURPLE))
95-
.append(Component.text("sec", NamedTextColor.BLUE))
96-
.build();
9799

98-
Component hourminsec = Component.text()
99-
.append(Component.text(String.format("%02d", hours), NamedTextColor.LIGHT_PURPLE))
100-
.append(Component.text("hr ", NamedTextColor.BLUE))
101-
.append(Component.text(String.format("%02d", minutes), NamedTextColor.LIGHT_PURPLE))
102-
.append(Component.text("min ", NamedTextColor.BLUE))
103-
.append(Component.text(String.format("%02d", seconds), NamedTextColor.LIGHT_PURPLE))
104-
.append(Component.text("sec", NamedTextColor.BLUE))
105-
.build();
106-
if ((time / 3600) == 0) {
107-
onlineplayer.sendActionBar(minsec);
108-
} else {
109-
onlineplayer.sendActionBar(hourminsec);
110-
}
100+
if(time >= 0) {
101+
Component formattedTime = FormatNumbers.formatTimeComponent(time);
102+
for (Player onlineplayer:Bukkit.getOnlinePlayers()) {
103+
onlineplayer.sendActionBar(formattedTime);
111104
if (TimerAPI.getApi().isTimerEnabled(player)) {
112105
playSound(onlineplayer, time);
113106
}
114-
} else {
115-
if (TimerAPI.getApi().isTimerEnabled(player)) {
116-
TimerAPI.getApi().setRunning(player, false);
117-
TimerAPI.getApi().setTime(player, 0);
118-
TimerAPI.getApi().setTimer(player, false);
119-
if (LanguageAPI.getApi().getLanguage(player) == 2) {
107+
}
108+
} else {
109+
if (TimerAPI.getApi().isTimerEnabled(player)) {
110+
TimerAPI.getApi().setRunning(player, false);
111+
TimerAPI.getApi().setTime(player, 0);
112+
TimerAPI.getApi().setTimer(player, false);
113+
for (Player onlineplayer:Bukkit.getOnlinePlayers()) {
114+
if (LanguageAPI.getApi().getLanguage(onlineplayer) == 2) {
120115
onlineplayer.sendActionBar(Component.text("Ende", NamedTextColor.GREEN));
121116
} else {
122117
onlineplayer.sendActionBar(Component.text("End", NamedTextColor.GREEN));
123118
}
124119
onlineplayer.playSound(player, Sound.ENTITY_WITHER_DEATH, 0.8F, 1F);
125-
} else {
126-
TimerAPI.getApi().setTime(player, 0);
127-
TimerAPI.getApi().setTimer(player, false);
128120
}
121+
} else {
122+
TimerAPI.getApi().setTime(player, 0);
123+
TimerAPI.getApi().setTimer(player, false);
129124
}
130125
}
131126
}
132127

133-
134-
135-
private void run() {
136-
Bukkit.getScheduler().runTaskTimerAsynchronously(UtilsManager.getInstance(), bukkitTask -> {
137-
for (Player player : Bukkit.getOnlinePlayers()) {
138-
if (TimerAPI.getApi().isOff(player)) {
139-
return;
140-
}
141-
if (TimerAPI.getApi().isPublic(player)) {
142-
sendActionBarPublic(player);
143-
if (TimerAPI.getApi().isRunning(player)) {
144-
if(TimerAPI.getApi().isTimerEnabled(player)) {
145-
TimerAPI.getApi().setTime(player, TimerAPI.getApi().getTime(player) - 1);
146-
} else {
147-
TimerAPI.getApi().setTime(player, TimerAPI.getApi().getTime(player) + 1);
148-
}
149-
}
150-
break;
151-
}
152-
if(!(player.hasPermission("plugin.timer"))) {
153-
return;
154-
}
155-
sendActionBar(player);
156-
if (TimerAPI.getApi().isRunning(player)) {
157-
if(TimerAPI.getApi().isTimerEnabled(player)) {
158-
TimerAPI.getApi().setTime(player, TimerAPI.getApi().getTime(player) - 1);
159-
} else {
160-
TimerAPI.getApi().setTime(player, TimerAPI.getApi().getTime(player) + 1);
161-
}
162-
}
163-
}
164-
}, 20, 20);
165-
}
166-
167128
public void playSound(Player player, Integer time) {
168129
if (time == 3600) {
169130
player.playSound(player, Sound.BLOCK_NOTE_BLOCK_PLING, 0.5F, 0.5F);
@@ -185,51 +146,10 @@ public void playSound(Player player, Integer time) {
185146
player.playSound(player, Sound.BLOCK_NOTE_BLOCK_PLING, 0.5F, 0.8F);
186147
return;
187148
}
188-
if (time < 11) {
189-
if (time == 10) {
190-
player.playSound(player, Sound.BLOCK_NOTE_BLOCK_PLING, 0.5F, 1F);
191-
return;
192-
}
193-
if (time == 9) {
194-
player.playSound(player, Sound.BLOCK_NOTE_BLOCK_PLING, 0.5F, 1.1F);
195-
return;
196-
}
197-
if (time == 8) {
198-
player.playSound(player, Sound.BLOCK_NOTE_BLOCK_PLING, 0.6F, 1.2F);
199-
return;
200-
}
201-
if (time == 7) {
202-
player.playSound(player, Sound.BLOCK_NOTE_BLOCK_PLING, 0.6F, 1.3F);
203-
return;
204-
}
205-
if (time == 6) {
206-
player.playSound(player, Sound.BLOCK_NOTE_BLOCK_PLING, 0.6F, 1.4F);
207-
return;
208-
}
209-
if (time == 5) {
210-
player.playSound(player, Sound.BLOCK_NOTE_BLOCK_PLING, 0.7F, 1.5F);
211-
return;
212-
}
213-
if (time == 4) {
214-
player.playSound(player, Sound.BLOCK_NOTE_BLOCK_PLING, 0.7F, 1.6F);
215-
return;
216-
}
217-
if (time == 3) {
218-
player.playSound(player, Sound.BLOCK_NOTE_BLOCK_PLING, 0.8F, 1.7F);
219-
return;
220-
}
221-
if (time == 2) {
222-
player.playSound(player, Sound.BLOCK_NOTE_BLOCK_PLING, 0.8F, 1.8F);
223-
return;
224-
}
225-
if (time == 1) {
226-
player.playSound(player, Sound.BLOCK_NOTE_BLOCK_PLING, 0.9F, 1.9F);
227-
return;
228-
}
229-
if (time == 0) {
230-
player.playSound(player, Sound.BLOCK_NOTE_BLOCK_PLING, 0.9F, 2F);
231-
}
149+
if (time >= 0 && time <= 10) {
150+
float volume = 0.5F + (10 - time) * 0.05F;
151+
float pitch = 1F + (10 - time) * 0.1F;
152+
player.playSound(player, Sound.BLOCK_NOTE_BLOCK_PLING, volume, pitch);
232153
}
233154
}
234-
}
235-
155+
}

0 commit comments

Comments
 (0)