Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
Expand Up @@ -22,10 +22,12 @@
import com.velocitypowered.proxy.protocol.ProtocolUtils;
import com.velocitypowered.proxy.protocol.packet.chat.ComponentHolder;
import io.netty.buffer.ByteBuf;
import org.jetbrains.annotations.NotNull;

public abstract class GenericTitlePacket implements MinecraftPacket {

public enum ActionType {

SET_TITLE(0),
SET_SUBTITLE(1),
SET_ACTION_BAR(2),
Expand All @@ -45,16 +47,7 @@ public int getAction(ProtocolVersion version) {
}
}


private ActionType action;

protected void setAction(ActionType action) {
this.action = action;
}

public final ActionType getAction() {
return action;
}
public abstract @NotNull ActionType getAction();

public ComponentHolder getComponent() {
throw new UnsupportedOperationException("Invalid function for this TitlePacket ActionType");
Expand Down Expand Up @@ -88,7 +81,6 @@ public void setFadeOut(int fadeOut) {
throw new UnsupportedOperationException("Invalid function for this TitlePacket ActionType");
}


@Override
public final void decode(ByteBuf buf, ProtocolUtils.Direction direction,
ProtocolVersion version) {
Expand All @@ -103,21 +95,17 @@ public final void decode(ByteBuf buf, ProtocolUtils.Direction direction,
* @return GenericTitlePacket instance that follows the invoker type/version
*/
public static GenericTitlePacket constructTitlePacket(ActionType type, ProtocolVersion version) {
GenericTitlePacket packet = null;
if (version.noLessThan(ProtocolVersion.MINECRAFT_1_17)) {
packet = switch (type) {
return switch (type) {
case SET_ACTION_BAR -> new TitleActionbarPacket();
case SET_SUBTITLE -> new TitleSubtitlePacket();
case SET_TIMES -> new TitleTimesPacket();
case SET_TITLE -> new TitleTextPacket();
case HIDE, RESET -> new TitleClearPacket();
case HIDE, RESET -> new TitleClearPacket(type);
default -> throw new IllegalArgumentException("Invalid ActionType");
};
} else {
packet = new LegacyTitlePacket();
return new LegacyTitlePacket(type);
}
packet.setAction(type);
return packet;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,27 +22,40 @@
import com.velocitypowered.proxy.protocol.ProtocolUtils;
import com.velocitypowered.proxy.protocol.packet.chat.ComponentHolder;
import io.netty.buffer.ByteBuf;
import java.util.Objects;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.jetbrains.annotations.NotNull;

public class LegacyTitlePacket extends GenericTitlePacket {

private final ActionType action;

private @Nullable ComponentHolder component;
private int fadeIn;
private int stay;
private int fadeOut;

public LegacyTitlePacket() {
// This constructor only exists to keep StateRegistry happy (all mappings are encode-only, the constructor isn't stored or used).
throw new AssertionError("A bare LegacyTitlePacket should never be instantiated");
}

public LegacyTitlePacket(ActionType action) {
this.action = Objects.requireNonNull(action, "action");
}

@Override
public void encode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion version) {
if (version.lessThan(ProtocolVersion.MINECRAFT_1_11)
&& getAction() == ActionType.SET_ACTION_BAR) {
&& this.action == ActionType.SET_ACTION_BAR) {
throw new IllegalStateException("Action bars are only supported on 1.11 and newer");
}
ProtocolUtils.writeVarInt(buf, getAction().getAction(version));
ProtocolUtils.writeVarInt(buf, this.action.getAction(version));

switch (getAction()) {
switch (this.action) {
case SET_TITLE, SET_SUBTITLE, SET_ACTION_BAR -> {
if (component == null) {
throw new IllegalStateException("No component found for " + getAction());
throw new IllegalStateException("No component found for " + this.action);
}
component.write(buf);
}
Expand All @@ -52,14 +65,13 @@ && getAction() == ActionType.SET_ACTION_BAR) {
buf.writeInt(fadeOut);
}
case HIDE, RESET -> {}
default -> throw new UnsupportedOperationException("Unknown action " + getAction());
default -> throw new UnsupportedOperationException("Unknown action " + this.action);
}

}

@Override
public void setAction(ActionType action) {
super.setAction(action);
public @NotNull ActionType getAction() {
return action;
}

@Override
Expand Down Expand Up @@ -105,7 +117,7 @@ public void setFadeOut(int fadeOut) {
@Override
public String toString() {
return "GenericTitlePacket{"
+ "action=" + getAction()
+ "action=" + action
+ ", component='" + component + '\''
+ ", fadeIn=" + fadeIn
+ ", stay=" + stay
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@
import com.velocitypowered.proxy.protocol.ProtocolUtils;
import com.velocitypowered.proxy.protocol.packet.chat.ComponentHolder;
import io.netty.buffer.ByteBuf;
import org.jetbrains.annotations.NotNull;

public class TitleActionbarPacket extends GenericTitlePacket {

private ComponentHolder component;

public TitleActionbarPacket() {
setAction(ActionType.SET_TITLE);
@Override
public @NotNull ActionType getAction() {
return ActionType.SET_ACTION_BAR;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,30 +21,37 @@
import com.velocitypowered.proxy.connection.MinecraftSessionHandler;
import com.velocitypowered.proxy.protocol.ProtocolUtils;
import io.netty.buffer.ByteBuf;
import org.jetbrains.annotations.NotNull;

public class TitleClearPacket extends GenericTitlePacket {

private final ActionType action;

public TitleClearPacket() {
setAction(ActionType.HIDE);
this.action = ActionType.HIDE;
}

@Override
public void setAction(ActionType action) {
public TitleClearPacket(ActionType action) {
if (action != ActionType.HIDE && action != ActionType.RESET) {
throw new IllegalArgumentException("TitleClearPacket only accepts CLEAR and RESET actions");
throw new IllegalArgumentException("TitleClearPacket only accepts the CLEAR and RESET actions.");
}
super.setAction(action);
this.action = action;
}

@Override
public @NotNull ActionType getAction() {
return action;
}

@Override
public void encode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion version) {
buf.writeBoolean(getAction() == ActionType.RESET);
buf.writeBoolean(this.action == ActionType.RESET);
}

@Override
public String toString() {
return "TitleClearPacket{"
+ ", resetTimes=" + (getAction() == ActionType.RESET)
+ ", resetTimes=" + (this.action == ActionType.RESET)
+ '}';
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@
import com.velocitypowered.proxy.protocol.ProtocolUtils;
import com.velocitypowered.proxy.protocol.packet.chat.ComponentHolder;
import io.netty.buffer.ByteBuf;
import org.jetbrains.annotations.NotNull;

public class TitleSubtitlePacket extends GenericTitlePacket {

private ComponentHolder component;

public TitleSubtitlePacket() {
setAction(ActionType.SET_SUBTITLE);
@Override
public @NotNull ActionType getAction() {
return ActionType.SET_SUBTITLE;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@
import com.velocitypowered.proxy.protocol.ProtocolUtils;
import com.velocitypowered.proxy.protocol.packet.chat.ComponentHolder;
import io.netty.buffer.ByteBuf;
import org.jetbrains.annotations.NotNull;

public class TitleTextPacket extends GenericTitlePacket {

private ComponentHolder component;

public TitleTextPacket() {
setAction(ActionType.SET_TITLE);
@Override
public @NotNull ActionType getAction() {
return ActionType.SET_TITLE;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,17 @@
import com.velocitypowered.proxy.connection.MinecraftSessionHandler;
import com.velocitypowered.proxy.protocol.ProtocolUtils;
import io.netty.buffer.ByteBuf;
import org.jetbrains.annotations.NotNull;

public class TitleTimesPacket extends GenericTitlePacket {

private int fadeIn;
private int stay;
private int fadeOut;

public TitleTimesPacket() {
setAction(ActionType.SET_TIMES);
@Override
public @NotNull ActionType getAction() {
return ActionType.SET_TIMES;
}

@Override
Expand Down