Skip to content

Commit

Permalink
Minecraft 23w43b support
Browse files Browse the repository at this point in the history
  • Loading branch information
md-5 committed Oct 28, 2023
1 parent e5c80d0 commit 0f5f09b
Show file tree
Hide file tree
Showing 31 changed files with 520 additions and 199 deletions.
22 changes: 22 additions & 0 deletions chat/src/main/java/net/md_5/bungee/api/chat/TextComponent.java
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,28 @@ public static BaseComponent[] fromLegacyText(String message, ChatColor defaultCo
return components.toArray( new BaseComponent[ 0 ] );
}

/**
* Internal compatibility method to transform an array of components to a
* single component.
*
* @param components array
* @return single component
*/
public static BaseComponent fromArray(BaseComponent... components)
{
if ( components == null )
{
return null;
}

if ( components.length == 1 )
{
return components[0];
}

return new TextComponent( components );
}

/**
* The text of the component that will be displayed to the client
*/
Expand Down
32 changes: 31 additions & 1 deletion chat/src/main/java/net/md_5/bungee/chat/ComponentSerializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.JsonParser;
import com.google.gson.JsonPrimitive;
import java.lang.reflect.Type;
import java.util.Set;
import net.md_5.bungee.api.chat.BaseComponent;
Expand Down Expand Up @@ -87,14 +88,43 @@ public static BaseComponent[] parse(String json)
public static BaseComponent deserialize(String json)
{
JsonElement jsonElement = JsonParser.parseString( json );

return deserialize( jsonElement );
}

/**
* Deserialize a JSON element as a single component. The input is expected
* to be a JSON object that represents only one component.
*
* @param jsonElement the component json to parse
* @return the deserialized component
* @throws IllegalArgumentException if anything other than a JSON object is
* passed as input
*/
public static BaseComponent deserialize(JsonElement jsonElement)
{
if ( jsonElement instanceof JsonPrimitive )
{
JsonPrimitive primitive = (JsonPrimitive) jsonElement;
if ( primitive.isString() )
{
return new TextComponent( primitive.getAsString() );
}
}

if ( !jsonElement.isJsonObject() )
{
throw new IllegalArgumentException( "Malformatted JSON. Expected object, got array for input \"" + json + "\"." );
throw new IllegalArgumentException( "Malformatted JSON. Expected object, got array for input \"" + jsonElement + "\"." );
}

return gson.fromJson( jsonElement, BaseComponent.class );
}

public static JsonElement toJson(BaseComponent component)
{
return gson.toJsonTree( component );
}

public static String toString(Object object)
{
return gson.toJson( object );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,10 @@ public TextComponent deserialize(JsonElement json, Type typeOfT, JsonDeserializa
{
TextComponent component = new TextComponent();
JsonObject object = json.getAsJsonObject();
if ( !object.has( "text" ) )
if ( object.has( "text" ) )
{
throw new JsonParseException( "Could not parse JSON: missing 'text' property" );
component.setText( object.get( "text" ).getAsString() );
}
component.setText( object.get( "text" ).getAsString() );
deserialize( object, component, context );
return component;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import java.util.function.ObjIntConsumer;
import java.util.function.Supplier;
import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.chat.hover.content.Item;
import net.md_5.bungee.api.chat.hover.content.Text;
import net.md_5.bungee.chat.ComponentSerializer;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -72,6 +71,8 @@ public void testItemParse()
assertEquals( hoverVal, ComponentSerializer.toString( (BaseComponent[]) contentText.getValue() ) );
testDissembleReassemble( components );
//////////
// TODO: now ambiguous since "text" to distinguish Text from Item is not required
/*
TextComponent component1 = new TextComponent( "HoverableText" );
String nbt = "{display:{Name:{text:Hello},Lore:[{text:Line_1},{text:Line_2}]},ench:[{id:49,lvl:5}],Unbreakable:1}}";
Item contentItem = new Item( "minecraft:wood", 1, ItemTag.ofNbt( nbt ) );
Expand All @@ -84,6 +85,7 @@ public void testItemParse()
assertEquals( contentItem.getCount(), parsedContentItem.getCount() );
assertEquals( contentItem.getId(), parsedContentItem.getId() );
assertEquals( nbt, parsedContentItem.getTag().getNbt() );
*/
}

@Test
Expand Down
18 changes: 6 additions & 12 deletions protocol/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,20 @@
<name>BungeeCord-Protocol</name>
<description>Minimal implementation of the Minecraft protocol for use in BungeeCord</description>

<!-- We really shouldn't depend on external repositories, but at least this is the Central staging one -->
<!-- We really shouldn't depend on external repositories -->
<repositories>
<repository>
<id>sonatype-nexus-snapshots</id>
<name>Sonatype Nexus Snapshots</name>
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
<releases>
<enabled>false</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
<id>minecraft-libraries</id>
<name>Minecraft Libraries</name>
<url>https://libraries.minecraft.net/</url>
</repository>
</repositories>

<dependencies>
<dependency>
<groupId>net.md-5</groupId>
<groupId>com.mojang</groupId>
<artifactId>brigadier</artifactId>
<version>1.0.16-SNAPSHOT</version>
<version>1.2.9</version>
<scope>compile</scope>
</dependency>
<dependency>
Expand Down
45 changes: 45 additions & 0 deletions protocol/src/main/java/net/md_5/bungee/protocol/DefinedPacket.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.google.common.base.Charsets;
import com.google.common.base.Preconditions;
import com.google.gson.JsonElement;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufInputStream;
import io.netty.buffer.ByteBufOutputStream;
Expand All @@ -15,6 +16,8 @@
import java.util.List;
import java.util.UUID;
import lombok.RequiredArgsConstructor;
import net.md_5.bungee.api.chat.BaseComponent;
import net.md_5.bungee.chat.ComponentSerializer;
import se.llbit.nbt.ErrorTag;
import se.llbit.nbt.NamedTag;
import se.llbit.nbt.SpecificTag;
Expand Down Expand Up @@ -70,6 +73,38 @@ public static String readString(ByteBuf buf, int maxLen)
return s;
}

public static BaseComponent readBaseComponent(ByteBuf buf, int protocolVersion)
{
if ( protocolVersion >= ProtocolConstants.MINECRAFT_1_20_3 )
{
SpecificTag nbt = (SpecificTag) readTag( buf, protocolVersion );
JsonElement json = TagUtil.toJson( nbt );

return ComponentSerializer.deserialize( json );
} else
{
String string = readString( buf );

return ComponentSerializer.deserialize( string );
}
}

public static void writeBaseComponent(BaseComponent message, ByteBuf buf, int protocolVersion)
{
if ( protocolVersion >= ProtocolConstants.MINECRAFT_1_20_3 )
{
JsonElement json = ComponentSerializer.toJson( message );
SpecificTag nbt = TagUtil.fromJson( json );

writeTag( nbt, buf, protocolVersion );
} else
{
String string = ComponentSerializer.toString( message );

writeString( string, buf );
}
}

public static void writeArray(byte[] b, ByteBuf buf)
{
if ( b.length > Short.MAX_VALUE )
Expand Down Expand Up @@ -395,6 +430,11 @@ public void read(ByteBuf buf)
throw new UnsupportedOperationException( "Packet must implement read method" );
}

public void read(ByteBuf buf, Protocol protocol, ProtocolConstants.Direction direction, int protocolVersion)
{
read( buf, direction, protocolVersion );
}

public void read(ByteBuf buf, ProtocolConstants.Direction direction, int protocolVersion)
{
read( buf );
Expand All @@ -405,6 +445,11 @@ public void write(ByteBuf buf)
throw new UnsupportedOperationException( "Packet must implement write method" );
}

public void write(ByteBuf buf, Protocol protocol, ProtocolConstants.Direction direction, int protocolVersion)
{
write( buf, direction, protocolVersion );
}

public void write(ByteBuf buf, ProtocolConstants.Direction direction, int protocolVersion)
{
write( buf );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) t
DefinedPacket packet = prot.createPacket( packetId, protocolVersion );
if ( packet != null )
{
packet.read( in, prot.getDirection(), protocolVersion );
packet.read( in, protocol, prot.getDirection(), protocolVersion );

if ( in.isReadable() )
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ protected void encode(ChannelHandlerContext ctx, DefinedPacket msg, ByteBuf out)
{
Protocol.DirectionData prot = ( server ) ? protocol.TO_CLIENT : protocol.TO_SERVER;
DefinedPacket.writeVarInt( prot.getId( msg.getClass(), protocolVersion ), out );
msg.write( out, prot.getDirection(), protocolVersion );
msg.write( out, protocol, prot.getDirection(), protocolVersion );
}
}
6 changes: 4 additions & 2 deletions protocol/src/main/java/net/md_5/bungee/protocol/Protocol.java
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,8 @@ public enum Protocol
map( ProtocolConstants.MINECRAFT_1_19_1, 0x12 ),
map( ProtocolConstants.MINECRAFT_1_19_3, 0x11 ),
map( ProtocolConstants.MINECRAFT_1_19_4, 0x12 ),
map( ProtocolConstants.MINECRAFT_1_20_2, 0x14 )
map( ProtocolConstants.MINECRAFT_1_20_2, 0x14 ),
map( ProtocolConstants.MINECRAFT_1_20_3, 0x15 )
);
TO_SERVER.registerPacket( Chat.class,
Chat::new,
Expand Down Expand Up @@ -517,7 +518,8 @@ public enum Protocol
map( ProtocolConstants.MINECRAFT_1_19_1, 0x0D ),
map( ProtocolConstants.MINECRAFT_1_19_3, 0x0C ),
map( ProtocolConstants.MINECRAFT_1_19_4, 0x0D ),
map( ProtocolConstants.MINECRAFT_1_20_2, 0x0F )
map( ProtocolConstants.MINECRAFT_1_20_2, 0x0F ),
map( ProtocolConstants.MINECRAFT_1_20_3, 0x10 )
);
TO_SERVER.registerPacket(
StartConfiguration.class,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public class ProtocolConstants
public static final int MINECRAFT_1_19_4 = 762;
public static final int MINECRAFT_1_20 = 763;
public static final int MINECRAFT_1_20_2 = 764;
public static final int MINECRAFT_1_20_3 = 1073741984;
public static final List<String> SUPPORTED_VERSIONS;
public static final List<Integer> SUPPORTED_VERSION_IDS;

Expand Down Expand Up @@ -107,7 +108,7 @@ public class ProtocolConstants
if ( SNAPSHOT_SUPPORT )
{
// supportedVersions.add( "1.20.x" );
// supportedVersionIds.add( ProtocolConstants.MINECRAFT_1_20_2 );
supportedVersionIds.add( ProtocolConstants.MINECRAFT_1_20_3 );
}

SUPPORTED_VERSIONS = supportedVersions.build();
Expand Down
Loading

0 comments on commit 0f5f09b

Please sign in to comment.