Skip to content
Open
Show file tree
Hide file tree
Changes from 3 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 @@ -26,6 +26,7 @@
public abstract class GenericTitlePacket implements MinecraftPacket {

public enum ActionType {

SET_TITLE(0),
SET_SUBTITLE(1),
SET_ACTION_BAR(2),
Expand All @@ -45,16 +46,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 ActionType getAction();

public ComponentHolder getComponent() {
throw new UnsupportedOperationException("Invalid function for this TitlePacket ActionType");
Expand Down Expand Up @@ -88,7 +80,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 +94,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 @@ -26,23 +26,33 @@

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(null);
Comment thread
WouterGritter marked this conversation as resolved.
Outdated
}

public LegacyTitlePacket(ActionType action) {
this.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 +62,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 ActionType getAction() {
return action;
}

@Override
Expand Down Expand Up @@ -105,7 +114,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 @@ -27,8 +27,9 @@ public class TitleActionbarPacket extends GenericTitlePacket {

private ComponentHolder component;

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

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,22 @@

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 ActionType getAction() {
return action;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ public class TitleSubtitlePacket extends GenericTitlePacket {

private ComponentHolder component;

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

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ public class TitleTextPacket extends GenericTitlePacket {

private ComponentHolder component;

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

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ public class TitleTimesPacket extends GenericTitlePacket {
private int stay;
private int fadeOut;

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

@Override
Expand Down