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

Cooldown api #463

Merged
merged 3 commits into from
Nov 1, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.viaversion.viarewind.protocol.protocol1_8to1_9.cooldown;

import com.viaversion.viarewind.protocol.protocol1_8to1_9.Protocol1_8To1_9;
import com.viaversion.viarewind.utils.PacketUtil;
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) {
sendActionBar(CooldownVisualization.buildProgressText("■", progress));
}

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

private void sendActionBar(String bar) {
PacketWrapper actionBarPacket = PacketWrapper.create(ClientboundPackets1_8.CHAT_MESSAGE, user);
actionBarPacket.write(Type.COMPONENT, new JsonPrimitive(bar));
actionBarPacket.write(Type.BYTE, (byte) 2);
SplotyCode marked this conversation as resolved.
Show resolved Hide resolved

PacketUtil.sendPacket(actionBarPacket, Protocol1_8To1_9.class);
SplotyCode marked this conversation as resolved.
Show resolved Hide resolved
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.viaversion.viarewind.protocol.protocol1_8to1_9.cooldown;

import com.viaversion.viarewind.protocol.protocol1_8to1_9.Protocol1_8To1_9;
import com.viaversion.viarewind.utils.PacketUtil;
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) {
PacketWrapper wrapper = PacketWrapper.create(ClientboundPackets1_9.BOSSBAR, user);
if (bossUUID == null) {
bossUUID = UUID.randomUUID();
wrapper.write(Type.UUID, bossUUID);
SplotyCode marked this conversation as resolved.
Show resolved Hide resolved
wrapper.write(Type.VAR_INT, 0);
wrapper.write(Type.COMPONENT, new JsonPrimitive(" "));
wrapper.write(Type.FLOAT, (float) progress);
wrapper.write(Type.VAR_INT, 0);
wrapper.write(Type.VAR_INT, 0);
wrapper.write(Type.UNSIGNED_BYTE, (short) 0);
} else {
wrapper.write(Type.UUID, bossUUID);
wrapper.write(Type.VAR_INT, 2);
wrapper.write(Type.FLOAT, (float) progress);
}
PacketUtil.sendPacket(wrapper, Protocol1_8To1_9.class, false, true);
SplotyCode marked this conversation as resolved.
Show resolved Hide resolved
}

@Override
public void hide() {
if (bossUUID == null) return;
PacketWrapper wrapper = PacketWrapper.create(ClientboundPackets1_9.BOSSBAR, null, user);
wrapper.write(Type.UUID, bossUUID);
wrapper.write(Type.VAR_INT, 1);
PacketUtil.sendPacket(wrapper, Protocol1_8To1_9.class, false, true);
SplotyCode marked this conversation as resolved.
Show resolved Hide resolved
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);

void hide();


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,48 @@
package com.viaversion.viarewind.protocol.protocol1_8to1_9.cooldown;

import com.viaversion.viarewind.protocol.protocol1_8to1_9.Protocol1_8To1_9;
import com.viaversion.viarewind.utils.PacketUtil;
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 TitleCooldownVisualization implements CooldownVisualization {
private final UserConnection user;

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

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

@Override
public void hide() {
PacketWrapper hide = PacketWrapper.create(ClientboundPackets1_8.TITLE, user);
hide.write(Type.VAR_INT, 3);
PacketUtil.sendPacket(hide, Protocol1_8To1_9.class);
}

private void sendTitle(String title, String subTitle, int fadeIn, int stay, int fadeOut) {
PacketWrapper timePacket = PacketWrapper.create(ClientboundPackets1_8.TITLE, user);
timePacket.write(Type.VAR_INT, 2);
timePacket.write(Type.INT, fadeIn);
timePacket.write(Type.INT, stay);
timePacket.write(Type.INT, fadeOut);
PacketWrapper titlePacket = PacketWrapper.create(ClientboundPackets1_8.TITLE, user);
titlePacket.write(Type.VAR_INT, 0);
titlePacket.write(Type.COMPONENT, new JsonPrimitive(title));
PacketWrapper subtitlePacket = PacketWrapper.create(ClientboundPackets1_8.TITLE, user);
subtitlePacket.write(Type.VAR_INT, 1);
subtitlePacket.write(Type.COMPONENT, new JsonPrimitive(subTitle));

PacketUtil.sendPacket(titlePacket, Protocol1_8To1_9.class);
SplotyCode marked this conversation as resolved.
Show resolved Hide resolved
PacketUtil.sendPacket(subtitlePacket, Protocol1_8To1_9.class);
PacketUtil.sendPacket(timePacket, Protocol1_8To1_9.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,147 +18,50 @@

package com.viaversion.viarewind.protocol.protocol1_8to1_9.storage;

import com.viaversion.viarewind.ViaRewind;
import com.viaversion.viarewind.api.ViaRewindConfig;
import com.viaversion.viarewind.protocol.protocol1_8to1_9.Protocol1_8To1_9;
import com.viaversion.viarewind.utils.PacketUtil;
import com.viaversion.viarewind.protocol.protocol1_8to1_9.cooldown.CooldownVisualization;
import com.viaversion.viarewind.protocol.protocol1_8to1_9.cooldown.CooldownVisualization.Factory;
import com.viaversion.viarewind.utils.Tickable;
import com.viaversion.viaversion.api.connection.StoredObject;
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 com.viaversion.viaversion.protocols.protocol1_9to1_8.ClientboundPackets1_9;
import com.viaversion.viaversion.util.Pair;

import java.util.ArrayList;
import java.util.UUID;
import java.util.Objects;

public class Cooldown extends StoredObject implements Tickable {

private double attackSpeed = 4.0;
private long lastHit = 0;
private final ViaRewindConfig.CooldownIndicator cooldownIndicator;
private UUID bossUUID;
private boolean lastSend;
private CooldownVisualization.Factory visualizationFactory = CooldownVisualization.Factory.fromConfiguration();
private CooldownVisualization current;

public Cooldown(final UserConnection user) {
super(user);

ViaRewindConfig.CooldownIndicator indicator;
try {
indicator = ViaRewind.getConfig().getCooldownIndicator();
} catch (IllegalArgumentException e) {
ViaRewind.getPlatform().getLogger().warning("Invalid cooldown-indicator setting");
indicator = ViaRewindConfig.CooldownIndicator.DISABLED;
}

this.cooldownIndicator = indicator;
}

@Override
public void tick() {
if (!hasCooldown()) {
if (lastSend) {
hide();
lastSend = false;
}
endCurrentVisualization();
return;
}

BlockPlaceDestroyTracker tracker = getUser().get(BlockPlaceDestroyTracker.class);
if (tracker.isMining()) {
lastHit = 0;
if (lastSend) {
hide();
lastSend = false;
}
endCurrentVisualization();
return;
}

showCooldown();
lastSend = true;
}

private void showCooldown() {
if (cooldownIndicator == ViaRewindConfig.CooldownIndicator.TITLE) {
sendTitle("", getTitle(), 0, 2, 5);
} else if (cooldownIndicator == ViaRewindConfig.CooldownIndicator.ACTION_BAR) {
sendActionBar(getTitle());
} else if (cooldownIndicator == ViaRewindConfig.CooldownIndicator.BOSS_BAR) {
sendBossBar((float) getCooldown());
}
}

private void hide() {
if (cooldownIndicator == ViaRewindConfig.CooldownIndicator.ACTION_BAR) {
sendActionBar("§r");
} else if (cooldownIndicator == ViaRewindConfig.CooldownIndicator.TITLE) {
hideTitle();
} else if (cooldownIndicator == ViaRewindConfig.CooldownIndicator.BOSS_BAR) {
hideBossBar();
if (current == null) {
current = visualizationFactory.create(getUser());
}
current.show(getCooldown());
}

private void hideBossBar() {
if (bossUUID == null) return;
PacketWrapper wrapper = PacketWrapper.create(ClientboundPackets1_9.BOSSBAR, null, getUser());
wrapper.write(Type.UUID, bossUUID);
wrapper.write(Type.VAR_INT, 1);
PacketUtil.sendPacket(wrapper, Protocol1_8To1_9.class, false, true);
bossUUID = null;
}

private void sendBossBar(float cooldown) {
PacketWrapper wrapper = PacketWrapper.create(ClientboundPackets1_9.BOSSBAR, getUser());
if (bossUUID == null) {
bossUUID = UUID.randomUUID();
wrapper.write(Type.UUID, bossUUID);
wrapper.write(Type.VAR_INT, 0);
wrapper.write(Type.COMPONENT, new JsonPrimitive(" "));
wrapper.write(Type.FLOAT, cooldown);
wrapper.write(Type.VAR_INT, 0);
wrapper.write(Type.VAR_INT, 0);
wrapper.write(Type.UNSIGNED_BYTE, (short) 0);
} else {
wrapper.write(Type.UUID, bossUUID);
wrapper.write(Type.VAR_INT, 2);
wrapper.write(Type.FLOAT, cooldown);
private void endCurrentVisualization() {
if (current != null) {
current.hide();
current = null;
}
PacketUtil.sendPacket(wrapper, Protocol1_8To1_9.class, false, true);
}

private void hideTitle() {
PacketWrapper hide = PacketWrapper.create(ClientboundPackets1_8.TITLE, null, getUser());
hide.write(Type.VAR_INT, 3);
PacketUtil.sendPacket(hide, Protocol1_8To1_9.class);
}

private void sendTitle(String title, String subTitle, int fadeIn, int stay, int fadeOut) {
PacketWrapper timePacket = PacketWrapper.create(ClientboundPackets1_8.TITLE, null, getUser());
timePacket.write(Type.VAR_INT, 2);
timePacket.write(Type.INT, fadeIn);
timePacket.write(Type.INT, stay);
timePacket.write(Type.INT, fadeOut);
PacketWrapper titlePacket = PacketWrapper.create(ClientboundPackets1_8.TITLE, getUser());
titlePacket.write(Type.VAR_INT, 0);
titlePacket.write(Type.COMPONENT, new JsonPrimitive(title));
PacketWrapper subtitlePacket = PacketWrapper.create(ClientboundPackets1_8.TITLE, getUser());
subtitlePacket.write(Type.VAR_INT, 1);
subtitlePacket.write(Type.COMPONENT, new JsonPrimitive(subTitle));

PacketUtil.sendPacket(titlePacket, Protocol1_8To1_9.class);
PacketUtil.sendPacket(subtitlePacket, Protocol1_8To1_9.class);
PacketUtil.sendPacket(timePacket, Protocol1_8To1_9.class);
}

private void sendActionBar(String bar) {
PacketWrapper actionBarPacket = PacketWrapper.create(ClientboundPackets1_8.CHAT_MESSAGE, getUser());
actionBarPacket.write(Type.COMPONENT, new JsonPrimitive(bar));
actionBarPacket.write(Type.BYTE, (byte) 2);

PacketUtil.sendPacket(actionBarPacket, Protocol1_8To1_9.class);
}

public boolean hasCooldown() {
Expand All @@ -177,21 +80,6 @@ private double restrain(double x, double a, double b) {
return Math.min(x, b);
}

private final static int max = 10;

private String getTitle() {
String symbol = cooldownIndicator == ViaRewindConfig.CooldownIndicator.ACTION_BAR ? "■" : "˙";

double cooldown = getCooldown();
int green = (int) Math.floor(((double) max) * cooldown);
int grey = max - green;
StringBuilder builder = new StringBuilder("§8");
while (green-- > 0) builder.append(symbol);
builder.append("§7");
while (grey-- > 0) builder.append(symbol);
return builder.toString();
}

public double getAttackSpeed() {
return attackSpeed;
}
Expand Down Expand Up @@ -229,4 +117,8 @@ public void hit() {
public void setLastHit(long lastHit) {
this.lastHit = lastHit;
}

public void setVisualizationFactory(Factory visualizationFactory) {
this.visualizationFactory = Objects.requireNonNull(visualizationFactory, "visualizationFactory");
}
}