Skip to content

Commit

Permalink
feat(6.2.6): Minecraft 1.20.4 support (closes #71, #64, #69)
Browse files Browse the repository at this point in the history
  • Loading branch information
CatCoderr committed Mar 25, 2024
1 parent 791965d commit e563a73
Show file tree
Hide file tree
Showing 15 changed files with 577 additions and 33 deletions.
25 changes: 17 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<a target="_blank"><img src="https://github.com/CatCoderr/ProtocolSidebar/actions/workflows/build.yaml/badge.svg" alt="Build" /></a>
<a target="_blank"><img src="https://img.shields.io/github/license/CatCoderr/ProtocolSidebar" alt="License" /></a>
<a target="_blank"><img src="https://img.shields.io/nexus/s/me.catcoder/bukkit-sidebar?server=https%3A%2F%2Foss.sonatype.org" alt="Nexus" /></a>
<a target="_blank"><img src="https://img.shields.io/badge/Minecraft%20Versions-1.12.2--1.20.2-blue?style=flat" alt="Minecraft Versions" /></a>
<a target="_blank"><img src="https://img.shields.io/badge/Minecraft%20Versions-1.12.2--1.20.4-blue?style=flat" alt="Minecraft Versions" /></a>
</p>

* [Features](#features)
Expand All @@ -18,6 +18,7 @@
* [Gradle (Kotlin DSL)](#gradle-kotlin-dsl)
* [Basic usage](#basic-usage)
* [Conditional lines](#conditional-lines)
* [Score number formatting](#score-number-formatting)
* [Sidebar title animations](#sidebar-title-animations)
* [Sidebar Pager](#sidebar-pager)

Expand Down Expand Up @@ -65,7 +66,7 @@ or [maven-shade-plugin](https://maven.apache.org/plugins/maven-shade-plugin/) (f
<dependency>
<groupId>me.catcoder</groupId>
<artifactId>bukkit-sidebar</artifactId>
<version>6.2.5-SNAPSHOT</version>
<version>6.2.6-SNAPSHOT</version>
</dependency>
```

Expand All @@ -78,7 +79,7 @@ repositories {
```
```groovy
dependencies {
implementation 'me.catcoder:bukkit-sidebar:6.2.5-SNAPSHOT'
implementation 'me.catcoder:bukkit-sidebar:6.2.6-SNAPSHOT'
}
```

Expand All @@ -91,7 +92,7 @@ repositories {
```
```kotlin
dependencies {
implementation("me.catcoder:bukkit-sidebar:6.2.5-SNAPSHOT")
implementation("me.catcoder:bukkit-sidebar:6.2.6-SNAPSHOT")
}
```

Expand Down Expand Up @@ -145,11 +146,19 @@ The visibility of these lines depends on the condition you set.
If the condition is true, the line will be shown, otherwise it will be hidden.
It's an updatable line, so it will update along with other updatable lines.

## Score number formatting
You can use `scoreNumberFormat` method in both `ScoreboardObjective` and `SidebarLine` classes to format score numbers.

This feature is available starting from 1.20.4 version.
```java
sidebar.addConditionalLine(
player -> Component.text("This line will be shown only for players with health less than 10"),
player -> player.getHealth() <= 10
);
// removes scores completely for all lines
sidebar.getObjective().scoreNumberFormatBlank();
// set's custom fixed text for all lines
sidebar.getObjective().scoreNumberFormatFixed(player -> Component.text("Test").color(NamedTextColor.BLUE));

// set's score number format for specific line (overrides objective's format)
var line = sidebar.addLine(Component.text("Some line").color(NamedTextColor.YELLOW));
line.scoreNumberFormatFixed(player -> Component.text("Test").color(NamedTextColor.BLUE));
```

## Sidebar Title Animations
Expand Down
5 changes: 4 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {
}

group = "me.catcoder"
version = "6.2.5-SNAPSHOT"
version = "6.2.6-SNAPSHOT"
description = "Powerful feature-packed Minecraft scoreboard library"

extra["sonatypeUsername"] = System.getenv("SONATYPE_USERNAME")
Expand All @@ -29,6 +29,7 @@ allprojects {
maven { url = uri("https://oss.sonatype.org/content/groups/public/") }
maven { url = uri("https://repo.viaversion.com") }
maven { url = uri("https://repo.maven.apache.org/maven2/") }
maven { url = uri("https://repo.opencollab.dev/maven-releases/") }
}
dependencies {
testImplementation("junit:junit:4.13.2")
Expand All @@ -39,6 +40,8 @@ allprojects {
compileOnly("io.papermc.paper:paper-api:${paperVersion}")
testCompileOnly("io.papermc.paper:paper-api:${paperVersion}")

implementation("com.github.steveice10:opennbt:1.6")

compileOnly("org.projectlombok:lombok:${lombokVersion}")
annotationProcessor("org.projectlombok:lombok:${lombokVersion}")

Expand Down
5 changes: 4 additions & 1 deletion run_test_server.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
VIA_VERSION=4.7.0
VIA_VERSION=4.9.3

./gradlew clean shadowJar

echo "Copying ProtocolSidebar..."
cp bin/ProtocolSidebar-*.jar server/data/plugins/ProtocolSidebar.jar

Expand Down
2 changes: 1 addition & 1 deletion server/docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ services:
environment:
EULA: "TRUE"
TYPE: "PAPER"
VERSION: "1.20.2"
VERSION: "1.20.4"
JAVA_TOOL_OPTIONS: "-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:5005"
tty: true
stdin_open: true
Expand Down
43 changes: 41 additions & 2 deletions src/main/java/me/catcoder/sidebar/ScoreboardObjective.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package me.catcoder.sidebar;

import com.google.common.base.Function;
import com.google.common.base.Preconditions;
import io.netty.buffer.ByteBuf;
import lombok.Getter;
import lombok.NonNull;
import me.catcoder.sidebar.protocol.ChannelInjector;
import me.catcoder.sidebar.protocol.PacketIds;
import me.catcoder.sidebar.protocol.ProtocolConstants;
import me.catcoder.sidebar.protocol.ScoreNumberFormat;
import me.catcoder.sidebar.text.TextProvider;
import me.catcoder.sidebar.util.buffer.ByteBufNetOutput;
import me.catcoder.sidebar.util.buffer.NetOutput;
Expand All @@ -32,9 +34,15 @@ public class ScoreboardObjective<R> {

private final String name;
private final TextProvider<R> textProvider;

private ScoreNumberFormat numberFormat;
private Function<Player, R> numberFormatter;

private R displayName;

ScoreboardObjective(@NonNull String name, @NonNull R displayName, @NonNull TextProvider<R> textProvider) {
ScoreboardObjective(@NonNull String name,
@NonNull R displayName,
@NonNull TextProvider<R> textProvider) {
Preconditions.checkArgument(
name.length() <= 16, "Objective name exceeds 16 symbols limit");

Expand All @@ -52,6 +60,21 @@ void updateValue(@NonNull Player player) {
sendPacket(player, packet);
}

public void scoreNumberFormatFixed(@NonNull Function<Player, R> numberFormatter) {
this.numberFormat = ScoreNumberFormat.FIXED;
this.numberFormatter = numberFormatter;
}

public void scoreNumberFormatStyled(@NonNull Function<Player, R> numberFormatter) {
this.numberFormat = ScoreNumberFormat.STYLED;
this.numberFormatter = numberFormatter;
}

public void scoreNumberFormatBlank() {
this.numberFormat = ScoreNumberFormat.BLANK;
this.numberFormatter = null;
}

void create(@NonNull Player player) {
ByteBuf packet = getPacket(player, ADD_OBJECTIVE);
sendPacket(player, packet);
Expand Down Expand Up @@ -94,12 +117,28 @@ private ByteBuf getPacket(@NonNull Player player, int mode) {
legacyText = legacyText.substring(0, 32);
}

if (VersionUtil.SERVER_VERSION >= ProtocolConstants.MINECRAFT_1_13) {
if (VersionUtil.SERVER_VERSION >= ProtocolConstants.MINECRAFT_1_20_3) {
// what the heck 1.20.3?
output.writeComponent(textProvider.asJsonMessage(player, displayName));
} else if (VersionUtil.SERVER_VERSION >= ProtocolConstants.MINECRAFT_1_13) {
output.writeString(textProvider.asJsonMessage(player, displayName));
} else {
output.writeString(legacyText);
}

if (VersionUtil.SERVER_VERSION >= ProtocolConstants.MINECRAFT_1_20_3) {
output.writeVarInt(0);
output.writeBoolean(numberFormat != null); // has number format

if (numberFormat != null) {
numberFormat.accept(output, numberFormatter == null ?
null : textProvider.asJsonMessage(player, numberFormatter.apply(player))
);
}

return buf;
}

if (VersionUtil.SERVER_VERSION >= ProtocolConstants.MINECRAFT_1_13) {
output.writeVarInt(0); // Health display
} else {
Expand Down
1 change: 1 addition & 0 deletions src/main/java/me/catcoder/sidebar/Sidebar.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public class Sidebar<R> {

private final Set<UUID> viewers = Collections.synchronizedSet(new HashSet<>());
private final List<SidebarLine<R>> lines = new ArrayList<>();
@Getter
private final ScoreboardObjective<R> objective;

private TextIterator titleText;
Expand Down
37 changes: 32 additions & 5 deletions src/main/java/me/catcoder/sidebar/SidebarLine.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package me.catcoder.sidebar;

import com.google.common.base.Function;
import com.google.common.base.Preconditions;
import io.netty.buffer.ByteBuf;
import lombok.*;
import me.catcoder.sidebar.protocol.ChannelInjector;
import me.catcoder.sidebar.protocol.ScoreNumberFormat;
import me.catcoder.sidebar.protocol.ScoreboardPackets;
import me.catcoder.sidebar.text.TextProvider;
import me.catcoder.sidebar.util.lang.ThrowingFunction;
Expand Down Expand Up @@ -31,6 +33,8 @@ public class SidebarLine<R> {
private ThrowingFunction<Player, R, Throwable> updater;
private ThrowingPredicate<Player, Throwable> displayCondition;
private final TextProvider<R> textProvider;
private ScoreNumberFormat scoreNumberFormat;
private Function<Player, R> scoreNumberFormatter;

SidebarLine(@NonNull ThrowingFunction<Player, R, Throwable> updater,
@NonNull String teamName,
Expand All @@ -46,6 +50,24 @@ public class SidebarLine<R> {
this.textProvider = textProvider;
}

public SidebarLine<R> scoreNumberFormatBlank() {
this.scoreNumberFormat = null;
this.scoreNumberFormatter = null;
return this;
}

public SidebarLine<R> scoreNumberFormatFixed(@NonNull Function<Player, R> scoreNumberFormatter) {
this.scoreNumberFormat = ScoreNumberFormat.FIXED;
this.scoreNumberFormatter = scoreNumberFormatter;
return this;
}

public SidebarLine<R> scoreNumberFormatStyled(@NonNull Function<Player, R> scoreNumberFormatter) {
this.scoreNumberFormat = ScoreNumberFormat.STYLED;
this.scoreNumberFormatter = scoreNumberFormatter;
return this;
}

public BukkitTask updatePeriodically(long delay, long period, @NonNull Sidebar<R> sidebar) {
Preconditions.checkState(!isStaticText(), "Cannot set updater for static text line");

Expand Down Expand Up @@ -102,21 +124,25 @@ void updateTeam(@NonNull Player player, @NonNull String objective) throws Throwa

if (!isStaticText() && visible) {
R text = updater.apply(player);
sendPacket(player, ScoreboardPackets.createTeamPacket(ScoreboardPackets.TEAM_UPDATED, index, teamName,
sendPacket(player, ScoreboardPackets.createTeamPacket(
ScoreboardPackets.TEAM_UPDATED, index, teamName,
player, text, textProvider));
}

if (!visible) {
// if player doesn't meet display condition, remove score
sendPacket(player, ScoreboardPackets.createScorePacket(player, 1, objective, score, index));
sendPacket(player, ScoreboardPackets.createScorePacket(
player, 1, objective, score, index, textProvider, scoreNumberFormat, null));
return;
}

sendPacket(player, ScoreboardPackets.createScorePacket(player, 0, objective, score, index));
sendPacket(player, ScoreboardPackets.createScorePacket(
player, 0, objective, score, index, textProvider, scoreNumberFormat, scoreNumberFormatter));
}

void removeTeam(@NonNull Player player, @NonNull String objective) {
sendPacket(player, ScoreboardPackets.createScorePacket(player, 1, objective, score, index));
sendPacket(player, ScoreboardPackets.createScorePacket(
player, 1, objective, score, index, textProvider, null, null));

sendPacket(player, ScoreboardPackets.createTeamPacket(ScoreboardPackets.TEAM_REMOVED, index, teamName,
player, null, textProvider));
Expand All @@ -131,7 +157,8 @@ void createTeam(@NonNull Player player, @NonNull String objective) throws Throwa
player, text, textProvider));

if (visible) {
sendPacket(player, ScoreboardPackets.createScorePacket(player, 0, objective, score, index));
sendPacket(player, ScoreboardPackets.createScorePacket(
player, 0, objective, score, index, textProvider, scoreNumberFormat, scoreNumberFormatter));
}
}

Expand Down
16 changes: 12 additions & 4 deletions src/main/java/me/catcoder/sidebar/protocol/PacketIds.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ public enum PacketIds {
map(ProtocolConstants.MINECRAFT_1_19_1, 0x58),
map(ProtocolConstants.MINECRAFT_1_19_3, 0x56),
map(ProtocolConstants.MINECRAFT_1_19_4, 0x5A),
map(ProtocolConstants.MINECRAFT_1_20_2, 0x5C)
map(ProtocolConstants.MINECRAFT_1_20_2, 0x5C),
map(ProtocolConstants.MINECRAFT_1_20_4, 0x5E)
),
UPDATE_SCORE(
map(ProtocolConstants.MINECRAFT_1_12_2, 0x45),
Expand All @@ -27,8 +28,13 @@ public enum PacketIds {
map(ProtocolConstants.MINECRAFT_1_19_1, 0x59),
map(ProtocolConstants.MINECRAFT_1_19_3, 0x57),
map(ProtocolConstants.MINECRAFT_1_19_4, 0x5B),
map(ProtocolConstants.MINECRAFT_1_20_2, 0x5D)
map(ProtocolConstants.MINECRAFT_1_20_2, 0x5D),
map(ProtocolConstants.MINECRAFT_1_20_4, 0x5F)


),
RESET_SCORE(
map(ProtocolConstants.MINECRAFT_1_20_3, 0x42)
),
OBJECTIVE_DISPLAY(
map(ProtocolConstants.MINECRAFT_1_12_2, 0x3B),
Expand All @@ -39,7 +45,8 @@ public enum PacketIds {
map(ProtocolConstants.MINECRAFT_1_19_1, 0x4F),
map(ProtocolConstants.MINECRAFT_1_19_3, 0x4D),
map(ProtocolConstants.MINECRAFT_1_19_4, 0x51),
map(ProtocolConstants.MINECRAFT_1_20_2, 0x53)
map(ProtocolConstants.MINECRAFT_1_20_2, 0x53),
map(ProtocolConstants.MINECRAFT_1_20_4, 0x55)

),
OBJECTIVE(
Expand All @@ -51,7 +58,8 @@ public enum PacketIds {
map(ProtocolConstants.MINECRAFT_1_19_1, 0x56),
map(ProtocolConstants.MINECRAFT_1_19_3, 0x54),
map(ProtocolConstants.MINECRAFT_1_19_4, 0x58),
map(ProtocolConstants.MINECRAFT_1_20_2, 0x5A)
map(ProtocolConstants.MINECRAFT_1_20_2, 0x5A),
map(ProtocolConstants.MINECRAFT_1_20_4, 0x5C)
);

private final ProtocolConstants.ProtocolMapping[] mappings;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,11 @@ public class ProtocolConstants {
public static final int MINECRAFT_1_20 = 763;
public static final int MINECRAFT_1_20_1 = 763;
public static final int MINECRAFT_1_20_2 = 764;
public static final int MINECRAFT_1_20_3 = 765;
public static final int MINECRAFT_1_20_4 = 765;

public static final int MINIMUM_SUPPORTED_VERSION = MINECRAFT_1_12_2;
public static final int MAXIMUM_SUPPORTED_VERSION = MINECRAFT_1_20_2;
public static final int MAXIMUM_SUPPORTED_VERSION = MINECRAFT_1_20_4;

@Getter
@RequiredArgsConstructor
Expand Down
32 changes: 32 additions & 0 deletions src/main/java/me/catcoder/sidebar/protocol/ScoreNumberFormat.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package me.catcoder.sidebar.protocol;

import com.google.common.base.Preconditions;
import me.catcoder.sidebar.util.buffer.NetOutput;

import java.util.function.BiConsumer;

public enum ScoreNumberFormat implements BiConsumer<NetOutput, String> {

BLANK {
@Override
public void accept(NetOutput out, String value) {
out.writeVarInt(0);
}
},
STYLED {
@Override
public void accept(NetOutput netOutput, String value) {
netOutput.writeVarInt(1);
Preconditions.checkArgument(value != null, "Value cannot be null for STYLED format");
netOutput.writeComponent(value);
}
},
FIXED {
@Override
public void accept(NetOutput out, String value) {
out.writeVarInt(2);
Preconditions.checkArgument(value != null, "Value cannot be null for FIXED format");
out.writeComponent(value);
}
}
}
Loading

0 comments on commit e563a73

Please sign in to comment.