Skip to content

Commit dfebb91

Browse files
authored
Added cooldown api in 1.7 -> 1.8 (#463)
* Cooldown api * Small cleanup * Remove unnecessary method
1 parent ab4b927 commit dfebb91

File tree

6 files changed

+235
-124
lines changed

6 files changed

+235
-124
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.viaversion.viarewind.protocol.protocol1_8to1_9.cooldown;
2+
3+
import com.viaversion.viarewind.protocol.protocol1_8to1_9.Protocol1_8To1_9;
4+
import com.viaversion.viaversion.api.connection.UserConnection;
5+
import com.viaversion.viaversion.api.protocol.packet.PacketWrapper;
6+
import com.viaversion.viaversion.api.type.Type;
7+
import com.viaversion.viaversion.libs.gson.JsonPrimitive;
8+
import com.viaversion.viaversion.protocols.protocol1_8.ClientboundPackets1_8;
9+
10+
public class ActionBarVisualization implements CooldownVisualization {
11+
private final UserConnection user;
12+
13+
public ActionBarVisualization(UserConnection user) {
14+
this.user = user;
15+
}
16+
17+
@Override
18+
public void show(double progress) throws Exception {
19+
sendActionBar(CooldownVisualization.buildProgressText("■", progress));
20+
}
21+
22+
@Override
23+
public void hide() throws Exception {
24+
sendActionBar("§r");
25+
}
26+
27+
private void sendActionBar(String bar) throws Exception {
28+
PacketWrapper actionBarPacket = PacketWrapper.create(ClientboundPackets1_8.CHAT_MESSAGE, user);
29+
actionBarPacket.write(Type.COMPONENT, new JsonPrimitive(bar));
30+
actionBarPacket.write(Type.BYTE, (byte) 2); // Position - above hotbar
31+
actionBarPacket.scheduleSend(Protocol1_8To1_9.class);
32+
}
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.viaversion.viarewind.protocol.protocol1_8to1_9.cooldown;
2+
3+
import com.viaversion.viarewind.protocol.protocol1_8to1_9.Protocol1_8To1_9;
4+
import com.viaversion.viaversion.api.connection.UserConnection;
5+
import com.viaversion.viaversion.api.protocol.packet.PacketWrapper;
6+
import com.viaversion.viaversion.api.type.Type;
7+
import com.viaversion.viaversion.libs.gson.JsonPrimitive;
8+
import com.viaversion.viaversion.protocols.protocol1_9to1_8.ClientboundPackets1_9;
9+
import java.util.UUID;
10+
11+
public class BossBarVisualization implements CooldownVisualization {
12+
private final UserConnection user;
13+
private UUID bossUUID;
14+
15+
public BossBarVisualization(UserConnection user) {
16+
this.user = user;
17+
}
18+
19+
@Override
20+
public void show(double progress) throws Exception {
21+
PacketWrapper wrapper = PacketWrapper.create(ClientboundPackets1_9.BOSSBAR, user);
22+
if (bossUUID == null) {
23+
bossUUID = UUID.randomUUID();
24+
wrapper.write(Type.UUID, bossUUID);
25+
wrapper.write(Type.VAR_INT, 0); // Action - add
26+
wrapper.write(Type.COMPONENT, new JsonPrimitive(" ")); // Title
27+
wrapper.write(Type.FLOAT, (float) progress); // Health
28+
wrapper.write(Type.VAR_INT, 0); // Color
29+
wrapper.write(Type.VAR_INT, 0); // Division
30+
wrapper.write(Type.UNSIGNED_BYTE, (short) 0); // Flags
31+
} else {
32+
wrapper.write(Type.UUID, bossUUID);
33+
wrapper.write(Type.VAR_INT, 2); // Action - update health
34+
wrapper.write(Type.FLOAT, (float) progress); // Health
35+
}
36+
wrapper.scheduleSend(Protocol1_8To1_9.class, false);
37+
}
38+
39+
@Override
40+
public void hide() throws Exception {
41+
if (bossUUID == null) {
42+
return;
43+
}
44+
PacketWrapper wrapper = PacketWrapper.create(ClientboundPackets1_9.BOSSBAR, null, user);
45+
wrapper.write(Type.UUID, bossUUID);
46+
wrapper.write(Type.VAR_INT, 1); // Action - remove
47+
wrapper.scheduleSend(Protocol1_8To1_9.class, false);
48+
bossUUID = null;
49+
}
50+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.viaversion.viarewind.protocol.protocol1_8to1_9.cooldown;
2+
3+
import com.viaversion.viarewind.ViaRewind;
4+
import com.viaversion.viarewind.api.ViaRewindConfig.CooldownIndicator;
5+
import com.viaversion.viaversion.api.connection.UserConnection;
6+
7+
public interface CooldownVisualization {
8+
void show(double progress) throws Exception;
9+
10+
void hide() throws Exception;
11+
12+
13+
int MAX_PROGRESS_TEXT_LENGTH = 10;
14+
15+
static String buildProgressText(String symbol, double cooldown) {
16+
int green = (int) Math.floor(((double) MAX_PROGRESS_TEXT_LENGTH) * cooldown);
17+
int grey = MAX_PROGRESS_TEXT_LENGTH - green;
18+
StringBuilder builder = new StringBuilder("§8");
19+
while (green-- > 0) builder.append(symbol);
20+
builder.append("§7");
21+
while (grey-- > 0) builder.append(symbol);
22+
return builder.toString();
23+
}
24+
25+
interface Factory {
26+
CooldownVisualization create(UserConnection user);
27+
28+
static Factory fromConfiguration() {
29+
try {
30+
return fromIndicator(ViaRewind.getConfig().getCooldownIndicator());
31+
} catch (IllegalArgumentException e) {
32+
ViaRewind.getPlatform().getLogger().warning("Invalid cooldown-indicator setting");
33+
return DISABLED;
34+
}
35+
}
36+
37+
static Factory fromIndicator(CooldownIndicator indicator) {
38+
switch (indicator) {
39+
case TITLE:
40+
return TitleCooldownVisualization::new;
41+
case BOSS_BAR:
42+
return BossBarVisualization::new;
43+
case ACTION_BAR:
44+
return ActionBarVisualization::new;
45+
case DISABLED:
46+
return DISABLED;
47+
default:
48+
throw new IllegalArgumentException("Unexpected: " + indicator);
49+
}
50+
}
51+
52+
Factory DISABLED = user -> new DisabledCooldownVisualization();
53+
}
54+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.viaversion.viarewind.protocol.protocol1_8to1_9.cooldown;
2+
3+
public class DisabledCooldownVisualization implements CooldownVisualization {
4+
@Override
5+
public void show(double progress) {
6+
7+
}
8+
9+
@Override
10+
public void hide() {
11+
12+
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.viaversion.viarewind.protocol.protocol1_8to1_9.cooldown;
2+
3+
import com.viaversion.viarewind.protocol.protocol1_8to1_9.Protocol1_8To1_9;
4+
import com.viaversion.viaversion.api.connection.UserConnection;
5+
import com.viaversion.viaversion.api.protocol.packet.PacketWrapper;
6+
import com.viaversion.viaversion.api.type.Type;
7+
import com.viaversion.viaversion.libs.gson.JsonPrimitive;
8+
import com.viaversion.viaversion.protocols.protocol1_8.ClientboundPackets1_8;
9+
import java.util.function.Consumer;
10+
11+
public class TitleCooldownVisualization implements CooldownVisualization {
12+
private final UserConnection user;
13+
14+
public TitleCooldownVisualization(UserConnection user) {
15+
this.user = user;
16+
}
17+
18+
@Override
19+
public void show(double progress) throws Exception {
20+
String text = CooldownVisualization.buildProgressText("˙", progress);
21+
sendTitle("", text, 0, 2, 5);
22+
}
23+
24+
@Override
25+
public void hide() throws Exception {
26+
sendTitlePacket(ACTION_HIDE, wrapper -> {});
27+
}
28+
29+
private static final int ACTION_SET_TITLE = 0;
30+
private static final int ACTION_SET_SUBTITLE = 1;
31+
private static final int ACTION_SET_TIMES_AND_DISPLAY = 2;
32+
private static final int ACTION_HIDE = 3;
33+
34+
private void sendTitle(String titleText, String subTitleText, int fadeIn, int stay, int fadeOut) throws Exception {
35+
sendTitlePacket(
36+
ACTION_SET_TITLE,
37+
packet -> packet.write(Type.COMPONENT, new JsonPrimitive(titleText))
38+
);
39+
sendTitlePacket(
40+
ACTION_SET_SUBTITLE,
41+
packet -> packet.write(Type.COMPONENT, new JsonPrimitive(subTitleText))
42+
);
43+
sendTitlePacket(
44+
ACTION_SET_TIMES_AND_DISPLAY,
45+
packet -> {
46+
packet.write(Type.INT, fadeIn);
47+
packet.write(Type.INT, stay);
48+
packet.write(Type.INT, fadeOut);
49+
}
50+
);
51+
}
52+
53+
private void sendTitlePacket(int action, Consumer<PacketWrapper> writer) throws Exception {
54+
PacketWrapper titlePacket = PacketWrapper.create(ClientboundPackets1_8.TITLE, user);
55+
titlePacket.write(Type.VAR_INT, action);
56+
writer.accept(titlePacket);
57+
titlePacket.scheduleSend(Protocol1_8To1_9.class);
58+
}
59+
}

0 commit comments

Comments
 (0)