Add optional 1.17 (21w19a) snapshot protocol support

Accessible via the net.md_5.bungee.protocol.snapshot JVM property.
This commit is contained in:
md_5
2021-05-15 09:17:24 +10:00
parent 5fa596fee9
commit f0908b663f
11 changed files with 334 additions and 91 deletions

View File

@@ -1,29 +1,46 @@
package net.md_5.bungee;
import lombok.Data;
import net.md_5.bungee.api.Title;
import net.md_5.bungee.api.chat.BaseComponent;
import net.md_5.bungee.api.connection.ProxiedPlayer;
import net.md_5.bungee.chat.ComponentSerializer;
import net.md_5.bungee.protocol.DefinedPacket;
import net.md_5.bungee.protocol.ProtocolConstants;
import net.md_5.bungee.protocol.packet.ClearTitles;
import net.md_5.bungee.protocol.packet.Subtitle;
import net.md_5.bungee.protocol.packet.Title.Action;
import net.md_5.bungee.protocol.packet.TitleTimes;
public class BungeeTitle implements Title
{
private net.md_5.bungee.protocol.packet.Title title, subtitle, times, clear, reset;
private TitlePacketHolder<net.md_5.bungee.protocol.packet.Title> title;
private TitlePacketHolder<Subtitle> subtitle;
private TitlePacketHolder<TitleTimes> times;
private TitlePacketHolder<ClearTitles> clear;
private TitlePacketHolder<ClearTitles> reset;
private static net.md_5.bungee.protocol.packet.Title createPacket(Action action)
@Data
private static class TitlePacketHolder<T extends DefinedPacket>
{
net.md_5.bungee.protocol.packet.Title title = new net.md_5.bungee.protocol.packet.Title();
title.setAction( action );
if ( action == Action.TIMES )
{
// Set packet to default values first
title.setFadeIn( 20 );
title.setStay( 60 );
title.setFadeOut( 20 );
}
private final net.md_5.bungee.protocol.packet.Title oldPacket;
private final T newPacket;
}
private static TitlePacketHolder<TitleTimes> createAnimationPacket()
{
TitlePacketHolder<TitleTimes> title = new TitlePacketHolder<>( new net.md_5.bungee.protocol.packet.Title( Action.TIMES ), new TitleTimes() );
title.oldPacket.setFadeIn( 20 );
title.oldPacket.setStay( 60 );
title.oldPacket.setFadeOut( 20 );
title.newPacket.setFadeIn( 20 );
title.newPacket.setStay( 60 );
title.newPacket.setFadeOut( 20 );
return title;
}
@@ -32,10 +49,11 @@ public class BungeeTitle implements Title
{
if ( title == null )
{
title = createPacket( Action.TITLE );
net.md_5.bungee.protocol.packet.Title packet = new net.md_5.bungee.protocol.packet.Title( Action.TITLE );
title = new TitlePacketHolder<>( packet, packet );
}
title.setText( ComponentSerializer.toString( text ) );
title.oldPacket.setText( ComponentSerializer.toString( text ) ); // = newPacket
return this;
}
@@ -44,10 +62,11 @@ public class BungeeTitle implements Title
{
if ( title == null )
{
title = createPacket( Action.TITLE );
net.md_5.bungee.protocol.packet.Title packet = new net.md_5.bungee.protocol.packet.Title( Action.TITLE );
title = new TitlePacketHolder<>( packet, packet );
}
title.setText( ComponentSerializer.toString( text ) );
title.oldPacket.setText( ComponentSerializer.toString( text ) ); // = newPacket
return this;
}
@@ -56,10 +75,12 @@ public class BungeeTitle implements Title
{
if ( subtitle == null )
{
subtitle = createPacket( Action.SUBTITLE );
subtitle = new TitlePacketHolder<>( new net.md_5.bungee.protocol.packet.Title( Action.SUBTITLE ), new Subtitle() );
}
subtitle.setText( ComponentSerializer.toString( text ) );
String serialized = ComponentSerializer.toString( text );
subtitle.oldPacket.setText( serialized );
subtitle.newPacket.setText( serialized );
return this;
}
@@ -68,10 +89,12 @@ public class BungeeTitle implements Title
{
if ( subtitle == null )
{
subtitle = createPacket( Action.SUBTITLE );
subtitle = new TitlePacketHolder<>( new net.md_5.bungee.protocol.packet.Title( Action.SUBTITLE ), new Subtitle() );
}
subtitle.setText( ComponentSerializer.toString( text ) );
String serialized = ComponentSerializer.toString( text );
subtitle.oldPacket.setText( serialized );
subtitle.newPacket.setText( serialized );
return this;
}
@@ -80,10 +103,11 @@ public class BungeeTitle implements Title
{
if ( times == null )
{
times = createPacket( Action.TIMES );
times = createAnimationPacket();
}
times.setFadeIn( ticks );
times.oldPacket.setFadeIn( ticks );
times.newPacket.setFadeIn( ticks );
return this;
}
@@ -92,10 +116,11 @@ public class BungeeTitle implements Title
{
if ( times == null )
{
times = createPacket( Action.TIMES );
times = createAnimationPacket();
}
times.setStay( ticks );
times.oldPacket.setStay( ticks );
times.newPacket.setStay( ticks );
return this;
}
@@ -104,10 +129,11 @@ public class BungeeTitle implements Title
{
if ( times == null )
{
times = createPacket( Action.TIMES );
times = createAnimationPacket();
}
times.setFadeOut( ticks );
times.oldPacket.setFadeOut( ticks );
times.newPacket.setFadeOut( ticks );
return this;
}
@@ -116,7 +142,7 @@ public class BungeeTitle implements Title
{
if ( clear == null )
{
clear = createPacket( Action.CLEAR );
clear = new TitlePacketHolder<>( new net.md_5.bungee.protocol.packet.Title( Action.CLEAR ), new ClearTitles() );
}
title = null; // No need to send title if we clear it after that again
@@ -129,7 +155,7 @@ public class BungeeTitle implements Title
{
if ( reset == null )
{
reset = createPacket( Action.RESET );
reset = new TitlePacketHolder<>( new net.md_5.bungee.protocol.packet.Title( Action.RESET ), new ClearTitles( true ) );
}
// No need to send these packets if we reset them later
@@ -140,11 +166,17 @@ public class BungeeTitle implements Title
return this;
}
private static void sendPacket(ProxiedPlayer player, DefinedPacket packet)
private static void sendPacket(ProxiedPlayer player, TitlePacketHolder packet)
{
if ( packet != null )
{
player.unsafe().sendPacket( packet );
if ( player.getPendingConnection().getVersion() >= ProtocolConstants.MINECRAFT_1_17 )
{
player.unsafe().sendPacket( packet.newPacket );
} else
{
player.unsafe().sendPacket( packet.oldPacket );
}
}
}

View File

@@ -68,6 +68,8 @@ public abstract class EntityMap
case ProtocolConstants.MINECRAFT_1_16_3:
case ProtocolConstants.MINECRAFT_1_16_4:
return EntityMap_1_16_2.INSTANCE_1_16_2;
case ProtocolConstants.MINECRAFT_1_17:
return EntityMap_1_16_2.INSTANCE_1_17;
}
throw new RuntimeException( "Version " + version + " has no entity map" );
}

View File

@@ -15,6 +15,7 @@ class EntityMap_1_16_2 extends EntityMap
{
static final EntityMap_1_16_2 INSTANCE_1_16_2 = new EntityMap_1_16_2( 0x04, 0x2D );
static final EntityMap_1_16_2 INSTANCE_1_17 = new EntityMap_1_16_2( 0x04, 0x2D );
//
private final int spawnPlayerId;
private final int spectateId;