2828import com .viaversion .viaversion .api .protocol .Protocol ;
2929import com .viaversion .viaversion .api .protocol .packet .ClientboundPacketType ;
3030import com .viaversion .viaversion .api .protocol .packet .PacketWrapper ;
31- import com .viaversion .viaversion .api .type .Type ;
31+ import com .viaversion .viaversion .api .protocol .version .ProtocolVersion ;
32+ import com .viaversion .viaversion .api .type .Types ;
3233import com .viaversion .viaversion .libs .gson .JsonElement ;
3334import com .viaversion .viaversion .libs .gson .JsonParser ;
3435import java .text .MessageFormat ;
5758@ SuppressWarnings ({"checkstyle:FilteringWriteTag" , "checkstyle:MissingJavadocType" , "checkstyle:MissingJavadocMethod" })
5859public class ViaFacet <V > extends FacetBase <V > implements Facet .Message <V , String > {
5960 private static final String PACKAGE = "com.viaversion.viaversion" ;
60- private static final int SUPPORTED_VIA_MAJOR_VERSION = 4 ;
61+ private static final int SUPPORTED_VIA_MAJOR_VERSION = 5 ;
6162 private static final boolean SUPPORTED ;
6263
6364 static {
@@ -73,46 +74,48 @@ public class ViaFacet<V> extends FacetBase<V> implements Facet.Message<V, String
7374 }
7475
7576 private final Function <V , UserConnection > connectionFunction ;
76- private final int minProtocol ;
77+ private final ProtocolVersion hexColorProtocol ;
78+ private final ProtocolVersion minProtocol ;
7779
78- public ViaFacet (final @ NotNull Class <? extends V > viewerClass , final @ NotNull Function <V , UserConnection > connectionFunction , final int minProtocol ) {
80+ public ViaFacet (final @ NotNull Class <? extends V > viewerClass , final @ NotNull Function <V , UserConnection > connectionFunction , final String minProtocol ) {
7981 super (viewerClass );
8082 this .connectionFunction = connectionFunction ;
81- this .minProtocol = minProtocol ;
83+ this .hexColorProtocol = ProtocolVersion .getClosest (CLOSEST_RELEASE_VERSION_HEX_COLOR );
84+ this .minProtocol = ProtocolVersion .getClosest (minProtocol );
8285 }
8386
8487 @ Override
8588 public boolean isSupported () {
8689 return super .isSupported ()
8790 && SUPPORTED
8891 && this .connectionFunction != null
89- && this .minProtocol >= 0 ;
92+ && this .minProtocol . isKnown () ;
9093 }
9194
9295 @ Override
9396 public boolean isApplicable (final @ NotNull V viewer ) {
9497 return super .isApplicable (viewer )
95- && this .minProtocol > Via .getAPI ().getServerVersion ().lowestSupportedVersion ( )
96- && this .findProtocol (viewer ) >= this .minProtocol ;
98+ && this .minProtocol . newerThan ( Via .getAPI ().getServerVersion ().lowestSupportedProtocolVersion () )
99+ && this .findProtocol (viewer ). newerThanOrEqualTo ( this .minProtocol ) ;
97100 }
98101
99102 public @ Nullable UserConnection findConnection (final @ NotNull V viewer ) {
100103 return this .connectionFunction .apply (viewer );
101104 }
102105
103- public int findProtocol (final @ NotNull V viewer ) {
106+ public ProtocolVersion findProtocol (final @ NotNull V viewer ) {
104107 final UserConnection connection = this .findConnection (viewer );
105108 if (connection != null ) {
106- return connection .getProtocolInfo ().getProtocolVersion ();
109+ return connection .getProtocolInfo ().protocolVersion ();
107110 }
108- return - 1 ;
111+ return ProtocolVersion . unknown ;
109112 }
110113
111114 @ NotNull
112115 @ Override
113116 public String createMessage (final @ NotNull V viewer , final @ NotNull Component message ) {
114- final int protocol = this .findProtocol (viewer );
115- if (protocol >= PROTOCOL_HEX_COLOR ) {
117+ final ProtocolVersion protocol = this .findProtocol (viewer );
118+ if (protocol . newerThanOrEqualTo ( this . hexColorProtocol ) ) {
116119 return gson ().serialize (message );
117120 } else {
118121 return colorDownsamplingGson ().serialize (message );
@@ -125,11 +128,11 @@ public static class ProtocolBased<V> extends ViaFacet<V> {
125128 private final int packetId ;
126129
127130 @ SuppressWarnings ("unchecked" )
128- protected ProtocolBased (final @ NotNull String fromProtocol , final @ NotNull String toProtocol , final int minProtocol , final @ NotNull String packetName , final @ NotNull Class <? extends V > viewerClass , final @ NotNull Function <V , UserConnection > connectionFunction ) {
131+ protected ProtocolBased (final @ NotNull String fromProtocol , final @ NotNull String toProtocol , final @ NotNull String packetsProtocol , final String minProtocol , final @ NotNull String packetName , final @ NotNull Class <? extends V > viewerClass , final @ NotNull Function <V , UserConnection > connectionFunction ) {
129132 super (viewerClass , connectionFunction , minProtocol );
130133
131- final String protocolClassName = MessageFormat .format ("{0}.protocols.protocol {1}to{2}.Protocol{1}To{2}" , PACKAGE , fromProtocol , toProtocol );
132- final String packetClassName = MessageFormat .format ("{0}.protocols.protocol {1}to{2}.ClientboundPackets{1 }" , PACKAGE , fromProtocol , toProtocol );
134+ final String protocolClassName = MessageFormat .format ("{0}.protocols.v {1}to{2}.Protocol{1}To{2}" , PACKAGE , fromProtocol , toProtocol );
135+ final String packetClassName = MessageFormat .format ("{0}.protocols.v {1}to{2}.packet. ClientboundPackets{3 }" , PACKAGE , fromProtocol , toProtocol , packetsProtocol );
133136
134137 Class <? extends Protocol <?, ?, ?, ?>> protocolClass = null ;
135138 Class <? extends ClientboundPacketType > packetClass = null ;
@@ -180,15 +183,15 @@ public void sendPacket(final @NotNull PacketWrapper packet) {
180183
181184 public static class Chat <V > extends ProtocolBased <V > implements ChatPacket <V , String > {
182185 public Chat (final @ NotNull Class <? extends V > viewerClass , final @ NotNull Function <V , UserConnection > connectionFunction ) {
183- super ("1_16" , "1_15_2 " , PROTOCOL_HEX_COLOR , "CHAT_MESSAGE " , viewerClass , connectionFunction );
186+ super ("1_15_2" , " 1_16" , "1_16 " , CLOSEST_RELEASE_VERSION_HEX_COLOR , "CHAT " , viewerClass , connectionFunction );
184187 }
185188
186189 @ Override
187190 public void sendMessage (final @ NotNull V viewer , final @ NotNull Identity source , final @ NotNull String message , final @ NotNull Object type ) {
188191 final PacketWrapper packet = this .createPacket (viewer );
189- packet .write (Type .COMPONENT , this .parse (message ));
190- packet .write (Type .BYTE , this .createMessageType (type instanceof MessageType ? (MessageType ) type : MessageType .SYSTEM ));
191- packet .write (Type .UUID , source .uuid ());
192+ packet .write (Types .COMPONENT , this .parse (message ));
193+ packet .write (Types .BYTE , this .createMessageType (type instanceof MessageType ? (MessageType ) type : MessageType .SYSTEM ));
194+ packet .write (Types .UUID , source .uuid ());
192195 this .sendPacket (packet );
193196 }
194197 }
@@ -211,25 +214,21 @@ public void sendMessage(final @NotNull V viewer, final @NotNull String message)
211214
212215 public static class ActionBarTitle <V > extends ProtocolBased <V > implements Facet .ActionBar <V , String > {
213216 public ActionBarTitle (final @ NotNull Class <? extends V > viewerClass , final @ NotNull Function <V , UserConnection > connectionFunction ) {
214- super ("1_11" , "1_10 " , TitlePacket .PROTOCOL_ACTION_BAR , "TITLE " , viewerClass , connectionFunction );
217+ super ("1_10" , " 1_11" , "1_9_3 " , TitlePacket .CLOSEST_RELEASE_VERSION_ACTION_BAR , "SET_TITLES " , viewerClass , connectionFunction );
215218 }
216219
217220 @ Override
218221 public void sendMessage (final @ NotNull V viewer , final @ NotNull String message ) {
219222 final PacketWrapper packet = this .createPacket (viewer );
220- packet .write (Type .VAR_INT , TitlePacket .ACTION_ACTIONBAR );
221- packet .write (Type .COMPONENT , this .parse (message ));
223+ packet .write (Types .VAR_INT , TitlePacket .ACTION_ACTIONBAR );
224+ packet .write (Types .COMPONENT , this .parse (message ));
222225 this .sendPacket (packet );
223226 }
224227 }
225228
226229 public static class Title <V > extends ProtocolBased <V > implements Facet .TitlePacket <V , String , List <Consumer <PacketWrapper >>, Consumer <V >> {
227- protected Title (final @ NotNull String fromProtocol , final @ NotNull String toProtocol , final int minProtocol , final @ NotNull Class <? extends V > viewerClass , final @ NotNull Function <V , UserConnection > connectionFunction ) {
228- super (fromProtocol , toProtocol , minProtocol , "TITLE" , viewerClass , connectionFunction );
229- }
230-
231230 public Title (final @ NotNull Class <? extends V > viewerClass , final @ NotNull Function <V , UserConnection > connectionFunction ) {
232- this ( " 1_16" , "1_15_2 " , PROTOCOL_HEX_COLOR , viewerClass , connectionFunction );
231+ super ( "1_15_2" , " 1_16" , "1_16 " , CLOSEST_RELEASE_VERSION_HEX_COLOR , "SET_TITLES" , viewerClass , connectionFunction );
233232 }
234233
235234 @ Override
@@ -240,26 +239,26 @@ public Title(final @NotNull Class<? extends V> viewerClass, final @NotNull Funct
240239 @ Override
241240 public void contributeTitle (final @ NotNull List <Consumer <PacketWrapper >> coll , final @ NotNull String title ) {
242241 coll .add (packet -> {
243- packet .write (Type .VAR_INT , ACTION_TITLE );
244- packet .write (Type .COMPONENT , this .parse (title ));
242+ packet .write (Types .VAR_INT , ACTION_TITLE );
243+ packet .write (Types .COMPONENT , this .parse (title ));
245244 });
246245 }
247246
248247 @ Override
249248 public void contributeSubtitle (final @ NotNull List <Consumer <PacketWrapper >> coll , final @ NotNull String subtitle ) {
250249 coll .add (packet -> {
251- packet .write (Type .VAR_INT , ACTION_SUBTITLE );
252- packet .write (Type .COMPONENT , this .parse (subtitle ));
250+ packet .write (Types .VAR_INT , ACTION_SUBTITLE );
251+ packet .write (Types .COMPONENT , this .parse (subtitle ));
253252 });
254253 }
255254
256255 @ Override
257256 public void contributeTimes (final @ NotNull List <Consumer <PacketWrapper >> coll , final int inTicks , final int stayTicks , final int outTicks ) {
258257 coll .add (packet -> {
259- packet .write (Type .VAR_INT , ACTION_TIMES );
260- packet .write (Type .INT , inTicks );
261- packet .write (Type .INT , stayTicks );
262- packet .write (Type .INT , outTicks );
258+ packet .write (Types .VAR_INT , ACTION_TIMES );
259+ packet .write (Types .INT , inTicks );
260+ packet .write (Types .INT , stayTicks );
261+ packet .write (Types .INT , outTicks );
263262 });
264263 }
265264
@@ -282,14 +281,14 @@ public void showTitle(final @NotNull V viewer, final @NotNull Consumer<V> title)
282281 @ Override
283282 public void clearTitle (final @ NotNull V viewer ) {
284283 final PacketWrapper packet = this .createPacket (viewer );
285- packet .write (Type .VAR_INT , ACTION_CLEAR );
284+ packet .write (Types .VAR_INT , ACTION_CLEAR );
286285 this .sendPacket (packet );
287286 }
288287
289288 @ Override
290289 public void resetTitle (final @ NotNull V viewer ) {
291290 final PacketWrapper packet = this .createPacket (viewer );
292- packet .write (Type .VAR_INT , ACTION_RESET );
291+ packet .write (Types .VAR_INT , ACTION_RESET );
293292 this .sendPacket (packet );
294293 }
295294 }
@@ -303,30 +302,30 @@ public static final class BossBar<V> extends ProtocolBased<V> implements Facet.B
303302 private int overlay ;
304303 private byte flags ;
305304
306- private BossBar (final @ NotNull String fromProtocol , final @ NotNull String toProtocol , final @ NotNull Class <? extends V > viewerClass , final @ NotNull Function <V , UserConnection > connectionFunction , final Collection <V > viewers ) {
307- super (fromProtocol , toProtocol , PROTOCOL_BOSS_BAR , "BOSSBAR " , viewerClass , connectionFunction );
305+ private BossBar (final @ NotNull String fromProtocol , final @ NotNull String toProtocol , final @ NotNull String packetsProtocol , final @ NotNull Class <? extends V > viewerClass , final @ NotNull Function <V , UserConnection > connectionFunction , final Collection <V > viewers ) {
306+ super (fromProtocol , toProtocol , packetsProtocol , CLOSEST_RELEASE_VERSION_BOSS_BAR , "BOSS_EVENT " , viewerClass , connectionFunction );
308307 this .viewers = new CopyOnWriteArraySet <>(viewers );
309308 }
310309
311310 public static class Builder <V > extends ViaFacet <V > implements Facet .BossBar .Builder <V , Facet .BossBar <V >> {
312311 public Builder (final @ NotNull Class <? extends V > viewerClass , final @ NotNull Function <V , UserConnection > connectionFunction ) {
313- super (viewerClass , connectionFunction , PROTOCOL_HEX_COLOR );
312+ super (viewerClass , connectionFunction , CLOSEST_RELEASE_VERSION_HEX_COLOR );
314313 }
315314
316315 @ Override
317316 public Facet .@ NotNull BossBar <V > createBossBar (final @ NotNull Collection <V > viewer ) {
318- return new ViaFacet .BossBar <>("1_16" , "1_15_2 " , this .viewerClass , this ::findConnection , viewer );
317+ return new ViaFacet .BossBar <>("1_15_2" , " 1_16" , "1_16 " , this .viewerClass , this ::findConnection , viewer );
319318 }
320319 }
321320
322321 public static class Builder1_9_To_1_15 <V > extends ViaFacet <V > implements Facet .BossBar .Builder <V , Facet .BossBar <V >> {
323322 public Builder1_9_To_1_15 (final @ NotNull Class <? extends V > viewerClass , final @ NotNull Function <V , UserConnection > connectionFunction ) {
324- super (viewerClass , connectionFunction , PROTOCOL_BOSS_BAR );
323+ super (viewerClass , connectionFunction , CLOSEST_RELEASE_VERSION_BOSS_BAR );
325324 }
326325
327326 @ Override
328327 public Facet .@ NotNull BossBar <V > createBossBar (final @ NotNull Collection <V > viewer ) {
329- return new ViaFacet .BossBar <>("1_9" , "1_8 " , this .viewerClass , this ::findConnection , viewer );
328+ return new ViaFacet .BossBar <>("1_8" , " 1_9" , "1_9 " , this .viewerClass , this ::findConnection , viewer );
330329 }
331330 }
332331
@@ -371,20 +370,20 @@ public void bossBarFlagsChanged(final net.kyori.adventure.bossbar.@NotNull BossB
371370
372371 public void sendPacket (final @ NotNull V viewer , final int action ) {
373372 final PacketWrapper packet = this .createPacket (viewer );
374- packet .write (Type .UUID , this .id );
375- packet .write (Type .VAR_INT , action );
373+ packet .write (Types .UUID , this .id );
374+ packet .write (Types .VAR_INT , action );
376375 if (action == ACTION_ADD || action == ACTION_TITLE ) {
377- packet .write (Type .COMPONENT , this .parse (this .title ));
376+ packet .write (Types .COMPONENT , this .parse (this .title ));
378377 }
379378 if (action == ACTION_ADD || action == ACTION_HEALTH ) {
380- packet .write (Type .FLOAT , this .health );
379+ packet .write (Types .FLOAT , this .health );
381380 }
382381 if (action == ACTION_ADD || action == ACTION_STYLE ) {
383- packet .write (Type .VAR_INT , this .color );
384- packet .write (Type .VAR_INT , this .overlay );
382+ packet .write (Types .VAR_INT , this .color );
383+ packet .write (Types .VAR_INT , this .overlay );
385384 }
386385 if (action == ACTION_ADD || action == ACTION_FLAG ) {
387- packet .write (Type .BYTE , this .flags );
386+ packet .write (Types .BYTE , this .flags );
388387 }
389388 this .sendPacket (packet );
390389 }
@@ -425,14 +424,14 @@ public void close() {
425424 public static final class TabList <V > extends ProtocolBased <V > implements Facet .TabList <V , String > {
426425
427426 public TabList (final @ NotNull Class <? extends V > viewerClass , final @ NotNull Function <V , UserConnection > userConnection ) {
428- super ("1_16" , "1_15_2 " , PROTOCOL_HEX_COLOR , "TAB_LIST" , viewerClass , userConnection );
427+ super ("1_15_2" , " 1_16" , "1_16 " , CLOSEST_RELEASE_VERSION_HEX_COLOR , "TAB_LIST" , viewerClass , userConnection );
429428 }
430429
431430 @ Override
432431 public void send (final V viewer , final @ Nullable String header , final @ Nullable String footer ) {
433432 final PacketWrapper packet = this .createPacket (viewer );
434- packet .write (Type .COMPONENT , this .parse (header ));
435- packet .write (Type .COMPONENT , this .parse (footer ));
433+ packet .write (Types .COMPONENT , this .parse (header ));
434+ packet .write (Types .COMPONENT , this .parse (footer ));
436435 this .sendPacket (packet );
437436 }
438437 }
0 commit comments