-
-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/dev' into dev
- Loading branch information
Showing
7 changed files
with
237 additions
and
126 deletions.
There are no files selected for viewing
33 changes: 33 additions & 0 deletions
33
...a/com/viaversion/viarewind/protocol/protocol1_8to1_9/cooldown/ActionBarVisualization.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
...ava/com/viaversion/viarewind/protocol/protocol1_8to1_9/cooldown/BossBarVisualization.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
...va/com/viaversion/viarewind/protocol/protocol1_8to1_9/cooldown/CooldownVisualization.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
...iaversion/viarewind/protocol/protocol1_8to1_9/cooldown/DisabledCooldownVisualization.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() { | ||
|
||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
...m/viaversion/viarewind/protocol/protocol1_8to1_9/cooldown/TitleCooldownVisualization.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Oops, something went wrong.