Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/dev' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianMichael committed Nov 5, 2023
2 parents b1c9616 + 5c4d928 commit 69363c8
Show file tree
Hide file tree
Showing 7 changed files with 237 additions and 126 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.viaversion.viarewind.protocol.protocol1_8to1_9.cooldown;

import com.viaversion.viarewind.protocol.protocol1_8to1_9.Protocol1_8To1_9;
import com.viaversion.viaversion.api.connection.UserConnection;
import com.viaversion.viaversion.api.protocol.packet.PacketWrapper;
import com.viaversion.viaversion.api.type.Type;
import com.viaversion.viaversion.libs.gson.JsonPrimitive;
import com.viaversion.viaversion.protocols.protocol1_8.ClientboundPackets1_8;

public class ActionBarVisualization implements CooldownVisualization {
private final UserConnection user;

public ActionBarVisualization(UserConnection user) {
this.user = user;
}

@Override
public void show(double progress) throws Exception {
sendActionBar(CooldownVisualization.buildProgressText("■", progress));
}

@Override
public void hide() throws Exception {
sendActionBar("§r");
}

private void sendActionBar(String bar) throws Exception {
PacketWrapper actionBarPacket = PacketWrapper.create(ClientboundPackets1_8.CHAT_MESSAGE, user);
actionBarPacket.write(Type.COMPONENT, new JsonPrimitive(bar));
actionBarPacket.write(Type.BYTE, (byte) 2); // Position - above hotbar
actionBarPacket.scheduleSend(Protocol1_8To1_9.class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.viaversion.viarewind.protocol.protocol1_8to1_9.cooldown;

import com.viaversion.viarewind.protocol.protocol1_8to1_9.Protocol1_8To1_9;
import com.viaversion.viaversion.api.connection.UserConnection;
import com.viaversion.viaversion.api.protocol.packet.PacketWrapper;
import com.viaversion.viaversion.api.type.Type;
import com.viaversion.viaversion.libs.gson.JsonPrimitive;
import com.viaversion.viaversion.protocols.protocol1_9to1_8.ClientboundPackets1_9;
import java.util.UUID;

public class BossBarVisualization implements CooldownVisualization {
private final UserConnection user;
private UUID bossUUID;

public BossBarVisualization(UserConnection user) {
this.user = user;
}

@Override
public void show(double progress) throws Exception {
PacketWrapper wrapper = PacketWrapper.create(ClientboundPackets1_9.BOSSBAR, user);
if (bossUUID == null) {
bossUUID = UUID.randomUUID();
wrapper.write(Type.UUID, bossUUID);
wrapper.write(Type.VAR_INT, 0); // Action - add
wrapper.write(Type.COMPONENT, new JsonPrimitive(" ")); // Title
wrapper.write(Type.FLOAT, (float) progress); // Health
wrapper.write(Type.VAR_INT, 0); // Color
wrapper.write(Type.VAR_INT, 0); // Division
wrapper.write(Type.UNSIGNED_BYTE, (short) 0); // Flags
} else {
wrapper.write(Type.UUID, bossUUID);
wrapper.write(Type.VAR_INT, 2); // Action - update health
wrapper.write(Type.FLOAT, (float) progress); // Health
}
wrapper.scheduleSend(Protocol1_8To1_9.class, false);
}

@Override
public void hide() throws Exception {
if (bossUUID == null) {
return;
}
PacketWrapper wrapper = PacketWrapper.create(ClientboundPackets1_9.BOSSBAR, null, user);
wrapper.write(Type.UUID, bossUUID);
wrapper.write(Type.VAR_INT, 1); // Action - remove
wrapper.scheduleSend(Protocol1_8To1_9.class, false);
bossUUID = null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.viaversion.viarewind.protocol.protocol1_8to1_9.cooldown;

import com.viaversion.viarewind.ViaRewind;
import com.viaversion.viarewind.api.ViaRewindConfig.CooldownIndicator;
import com.viaversion.viaversion.api.connection.UserConnection;

public interface CooldownVisualization {
void show(double progress) throws Exception;

void hide() throws Exception;


int MAX_PROGRESS_TEXT_LENGTH = 10;

static String buildProgressText(String symbol, double cooldown) {
int green = (int) Math.floor(((double) MAX_PROGRESS_TEXT_LENGTH) * cooldown);
int grey = MAX_PROGRESS_TEXT_LENGTH - green;
StringBuilder builder = new StringBuilder("§8");
while (green-- > 0) builder.append(symbol);
builder.append("§7");
while (grey-- > 0) builder.append(symbol);
return builder.toString();
}

interface Factory {
CooldownVisualization create(UserConnection user);

static Factory fromConfiguration() {
try {
return fromIndicator(ViaRewind.getConfig().getCooldownIndicator());
} catch (IllegalArgumentException e) {
ViaRewind.getPlatform().getLogger().warning("Invalid cooldown-indicator setting");
return DISABLED;
}
}

static Factory fromIndicator(CooldownIndicator indicator) {
switch (indicator) {
case TITLE:
return TitleCooldownVisualization::new;
case BOSS_BAR:
return BossBarVisualization::new;
case ACTION_BAR:
return ActionBarVisualization::new;
case DISABLED:
return DISABLED;
default:
throw new IllegalArgumentException("Unexpected: " + indicator);
}
}

Factory DISABLED = user -> new DisabledCooldownVisualization();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.viaversion.viarewind.protocol.protocol1_8to1_9.cooldown;

public class DisabledCooldownVisualization implements CooldownVisualization {
@Override
public void show(double progress) {

}

@Override
public void hide() {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.viaversion.viarewind.protocol.protocol1_8to1_9.cooldown;

import com.viaversion.viarewind.protocol.protocol1_8to1_9.Protocol1_8To1_9;
import com.viaversion.viaversion.api.connection.UserConnection;
import com.viaversion.viaversion.api.protocol.packet.PacketWrapper;
import com.viaversion.viaversion.api.type.Type;
import com.viaversion.viaversion.libs.gson.JsonPrimitive;
import com.viaversion.viaversion.protocols.protocol1_8.ClientboundPackets1_8;
import java.util.function.Consumer;

public class TitleCooldownVisualization implements CooldownVisualization {
private final UserConnection user;

public TitleCooldownVisualization(UserConnection user) {
this.user = user;
}

@Override
public void show(double progress) throws Exception {
String text = CooldownVisualization.buildProgressText("˙", progress);
sendTitle("", text, 0, 2, 5);
}

@Override
public void hide() throws Exception {
sendTitlePacket(ACTION_HIDE, wrapper -> {});
}

private static final int ACTION_SET_TITLE = 0;
private static final int ACTION_SET_SUBTITLE = 1;
private static final int ACTION_SET_TIMES_AND_DISPLAY = 2;
private static final int ACTION_HIDE = 3;

private void sendTitle(String titleText, String subTitleText, int fadeIn, int stay, int fadeOut) throws Exception {
sendTitlePacket(
ACTION_SET_TITLE,
packet -> packet.write(Type.COMPONENT, new JsonPrimitive(titleText))
);
sendTitlePacket(
ACTION_SET_SUBTITLE,
packet -> packet.write(Type.COMPONENT, new JsonPrimitive(subTitleText))
);
sendTitlePacket(
ACTION_SET_TIMES_AND_DISPLAY,
packet -> {
packet.write(Type.INT, fadeIn);
packet.write(Type.INT, stay);
packet.write(Type.INT, fadeOut);
}
);
}

private void sendTitlePacket(int action, Consumer<PacketWrapper> writer) throws Exception {
PacketWrapper titlePacket = PacketWrapper.create(ClientboundPackets1_8.TITLE, user);
titlePacket.write(Type.VAR_INT, action);
writer.accept(titlePacket);
titlePacket.scheduleSend(Protocol1_8To1_9.class);
}
}
Loading

0 comments on commit 69363c8

Please sign in to comment.