Skip to content

Commit

Permalink
Merge pull request #16 from CloudburstMC/1.16
Browse files Browse the repository at this point in the history
1.16
  • Loading branch information
SupremeMortal authored Jul 12, 2020
2 parents 06ae539 + a924aff commit 16c8a4e
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 68 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@
</dependency>
<dependency>
<groupId>com.nukkitx.protocol</groupId>
<artifactId>bedrock-v390</artifactId>
<version>2.5.5</version>
<artifactId>bedrock-v407</artifactId>
<version>2.6.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<dependency>
Expand Down
17 changes: 9 additions & 8 deletions src/main/java/com/nukkitx/proxypass/ProxyPass.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLMapper;
import com.nukkitx.nbt.NBTInputStream;
import com.nukkitx.nbt.NBTOutputStream;
import com.nukkitx.nbt.NbtUtils;
import com.nukkitx.nbt.stream.NBTInputStream;
import com.nukkitx.nbt.stream.NBTOutputStream;
import com.nukkitx.nbt.tag.Tag;
import com.nukkitx.protocol.bedrock.BedrockClient;
import com.nukkitx.protocol.bedrock.BedrockPacketCodec;
import com.nukkitx.protocol.bedrock.BedrockServer;
import com.nukkitx.protocol.bedrock.v390.Bedrock_v390;
import com.nukkitx.protocol.bedrock.v407.Bedrock_v407;
import com.nukkitx.proxypass.network.ProxyBedrockEventHandler;
import io.netty.util.ResourceLeakDetector;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.extern.log4j.Log4j2;
Expand All @@ -37,7 +37,7 @@ public class ProxyPass {
public static final ObjectMapper JSON_MAPPER = new ObjectMapper().disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
public static final YAMLMapper YAML_MAPPER = (YAMLMapper) new YAMLMapper().disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
public static final String MINECRAFT_VERSION;
public static final BedrockPacketCodec CODEC = Bedrock_v390.V390_CODEC;
public static final BedrockPacketCodec CODEC = Bedrock_v407.V407_CODEC;
public static final int PROTOCOL_VERSION = CODEC.getProtocolVersion();
private static final DefaultPrettyPrinter PRETTY_PRINTER = new DefaultPrettyPrinter();

Expand Down Expand Up @@ -69,6 +69,7 @@ public class ProxyPass {
private Path dataDir;

public static void main(String[] args) {
ResourceLeakDetector.setLevel(ResourceLeakDetector.Level.PARANOID);
ProxyPass proxy = new ProxyPass();
try {
proxy.boot();
Expand Down Expand Up @@ -145,17 +146,17 @@ public void shutdown() {
}
}

public void saveNBT(String dataName, Tag<?> dataTag) {
public void saveNBT(String dataName, Object dataTag) {
Path path = dataDir.resolve(dataName + ".dat");
try (OutputStream outputStream = Files.newOutputStream(path, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
NBTOutputStream nbtOutputStream = NbtUtils.createNetworkWriter(outputStream)){
nbtOutputStream.write(dataTag);
nbtOutputStream.writeTag(dataTag);
} catch (IOException e) {
throw new RuntimeException(e);
}
}

public Tag<?> loadNBT(String dataName) {
public Object loadNBT(String dataName) {
Path path = dataDir.resolve(dataName + ".dat");
try (InputStream inputStream = Files.newInputStream(path);
NBTInputStream nbtInputStream = NbtUtils.createNetworkReader(inputStream)){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.nimbusds.jwt.SignedJWT;
import com.nukkitx.nbt.stream.LittleEndianDataOutputStream;
import com.nukkitx.nbt.stream.NBTOutputStream;
import com.nukkitx.nbt.tag.CompoundTag;
import com.nukkitx.nbt.tag.ListTag;
import com.nukkitx.nbt.NBTOutputStream;
import com.nukkitx.nbt.NbtList;
import com.nukkitx.nbt.NbtMap;
import com.nukkitx.nbt.NbtType;
import com.nukkitx.nbt.util.stream.LittleEndianDataOutputStream;
import com.nukkitx.protocol.bedrock.BedrockClientSession;
import com.nukkitx.protocol.bedrock.data.ContainerId;
import com.nukkitx.protocol.bedrock.data.ItemData;
import com.nukkitx.protocol.bedrock.data.inventory.ContainerId;
import com.nukkitx.protocol.bedrock.data.inventory.ItemData;
import com.nukkitx.protocol.bedrock.handler.BedrockPacketHandler;
import com.nukkitx.protocol.bedrock.packet.*;
import com.nukkitx.protocol.bedrock.util.EncryptionUtils;
Expand Down Expand Up @@ -56,25 +57,25 @@ public boolean handle(ServerToClientHandshakePacket packet) {
}

public boolean handle(AvailableEntityIdentifiersPacket packet) {
proxy.saveNBT("entity_identifiers", packet.getTag());
proxy.saveNBT("entity_identifiers", packet.getIdentifiers());
return false;
}

public boolean handle(BiomeDefinitionListPacket packet) {
proxy.saveNBT("biome_definitions", packet.getTag());
proxy.saveNBT("biome_definitions", packet.getDefinitions());
return false;
}

public boolean handle(StartGamePacket packet) {
Map<String, Integer> legacyBlocks = new HashMap<>();
for (CompoundTag entry : packet.getBlockPalette().getValue()) {
for (NbtMap entry : packet.getBlockPalette()) {
legacyBlocks.putIfAbsent(entry.getCompound("block").getString("name"), (int) entry.getShort("id"));
}

proxy.saveJson("legacy_block_ids.json", sortMap(legacyBlocks));
List<CompoundTag> palette = new ArrayList<>(packet.getBlockPalette().getValue());
List<NbtMap> palette = new ArrayList<>(packet.getBlockPalette());
palette.sort(Comparator.comparingInt(value -> value.getShort("id")));
proxy.saveNBT("runtime_block_states", new ListTag<>("", CompoundTag.class, palette));
proxy.saveNBT("runtime_block_states", new NbtList<>(NbtType.COMPOUND, palette));
BlockPaletteUtils.convertToJson(proxy, palette);

List<DataEntry> itemData = new ArrayList<>();
Expand Down Expand Up @@ -107,31 +108,42 @@ public boolean handle(DisconnectPacket packet) {
return false;
}

@Override
public boolean handle(InventoryContentPacket packet) {
if (packet.getContainerId() == ContainerId.CREATIVE) {
List<CreativeItemEntry> entries = new ArrayList<>();
for (ItemData data : packet.getContents()) {
int id = data.getId();
Integer damage = data.getDamage() == 0 ? null : (int) data.getDamage();

CompoundTag tag = data.getTag();
String tagData = null;
if (tag != null) {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
try (NBTOutputStream stream = new NBTOutputStream(new LittleEndianDataOutputStream(byteArrayOutputStream))) {
stream.write(tag);
} catch (IOException e) {
throw new RuntimeException(e);
}
tagData = Base64.getEncoder().encodeToString(byteArrayOutputStream.toByteArray());
private void dumpCreativeItems(ItemData[] contents) {
List<CreativeItemEntry> entries = new ArrayList<>();
for (ItemData data : contents) {
int id = data.getId();
Integer damage = data.getDamage() == 0 ? null : (int) data.getDamage();

NbtMap tag = data.getTag();
String tagData = null;
if (tag != null) {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
try (NBTOutputStream stream = new NBTOutputStream(new LittleEndianDataOutputStream(byteArrayOutputStream))) {
stream.writeTag(tag);
} catch (IOException e) {
throw new RuntimeException(e);
}
entries.add(new CreativeItemEntry(id, damage, tagData));
tagData = Base64.getEncoder().encodeToString(byteArrayOutputStream.toByteArray());
}
entries.add(new CreativeItemEntry(id, damage, tagData));
}

CreativeItems items = new CreativeItems(entries);
CreativeItems items = new CreativeItems(entries);

proxy.saveJson("creative_items.json", items);
proxy.saveJson("creative_items.json", items);
}

@Override
public boolean handle(CreativeContentPacket packet) {
dumpCreativeItems(packet.getEntries().values().toArray(new ItemData[0]));
return false;
}

// Pre 1.16 method of Creative Items
@Override
public boolean handle(InventoryContentPacket packet) {
if (packet.getContainerId() == ContainerId.CREATIVE) {
dumpCreativeItems(packet.getContents());
}
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
import com.nukkitx.protocol.bedrock.BedrockPacket;
import com.nukkitx.protocol.bedrock.BedrockServerSession;
import com.nukkitx.protocol.bedrock.BedrockSession;
import com.nukkitx.protocol.bedrock.exception.PacketSerializeException;
import com.nukkitx.protocol.bedrock.handler.BatchHandler;
import com.nukkitx.protocol.bedrock.handler.BedrockPacketHandler;
import com.nukkitx.protocol.bedrock.util.EncryptionUtils;
import com.nukkitx.proxypass.ProxyPass;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.extern.log4j.Log4j2;
Expand Down Expand Up @@ -131,14 +133,18 @@ public void handle(BedrockSession session, ByteBuf compressed, Collection<Bedroc
}

if (packetTesting) {
ByteBuf buffer = ProxyPass.CODEC.tryEncode(packet);
int packetId = ProxyPass.CODEC.getId(packet.getClass());
ByteBuf buffer = ByteBufAllocator.DEFAULT.ioBuffer();
try {
BedrockPacket packet2 = ProxyPass.CODEC.tryDecode(buffer);
ProxyPass.CODEC.tryEncode(buffer, packet);
BedrockPacket packet2 = ProxyPass.CODEC.tryDecode(buffer, packetId);
if (!Objects.equals(packet, packet2)) {
// Something went wrong in serialization.
log.warn("Packets instances not equal:\n Original : {}\nRe-encoded : {}",
packet, packet2);
}
} catch (PacketSerializeException e) {
//ignore
} finally {
buffer.release();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.nimbusds.jose.JWSObject;
import com.nimbusds.jose.crypto.factories.DefaultJWSVerifierFactory;
import com.nimbusds.jwt.SignedJWT;
import com.nukkitx.protocol.bedrock.BedrockClient;
import com.nukkitx.protocol.bedrock.BedrockServerSession;
import com.nukkitx.protocol.bedrock.handler.BedrockPacketHandler;
import com.nukkitx.protocol.bedrock.packet.LoginPacket;
Expand Down Expand Up @@ -70,9 +71,9 @@ public boolean handle(LoginPacket packet) {
if (protocolVersion != ProxyPass.PROTOCOL_VERSION) {
PlayStatusPacket status = new PlayStatusPacket();
if (protocolVersion > ProxyPass.PROTOCOL_VERSION) {
status.setStatus(PlayStatusPacket.Status.FAILED_SERVER);
status.setStatus(PlayStatusPacket.Status.LOGIN_FAILED_SERVER_OLD);
} else {
status.setStatus(PlayStatusPacket.Status.FAILED_CLIENT);
status.setStatus(PlayStatusPacket.Status.LOGIN_FAILED_CLIENT_OLD);
}
}
session.setPacketCodec(ProxyPass.CODEC);
Expand Down Expand Up @@ -126,7 +127,9 @@ public boolean handle(LoginPacket packet) {

private void initializeProxySession() {
log.debug("Initializing proxy session");
proxy.newClient().connect(proxy.getTargetAddress()).whenComplete((downstream, throwable) -> {
BedrockClient client = proxy.newClient();
client.setRakNetVersion(10);
client.connect(proxy.getTargetAddress()).whenComplete((downstream, throwable) -> {
if (throwable != null) {
log.error("Unable to connect to downstream server " + proxy.getTargetAddress(), throwable);
return;
Expand All @@ -153,8 +156,8 @@ private void initializeProxySession() {
login.setProtocolVersion(ProxyPass.PROTOCOL_VERSION);

downstream.sendPacketImmediately(login);
this.session.setBatchedHandler(proxySession.getUpstreamBatchHandler());
downstream.setBatchedHandler(proxySession.getDownstreamTailHandler());
this.session.setBatchHandler(proxySession.getUpstreamBatchHandler());
downstream.setBatchHandler(proxySession.getDownstreamTailHandler());
downstream.setLogging(true);
downstream.setPacketHandler(new DownstreamPacketHandler(downstream, proxySession, this.proxy));

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.nukkitx.proxypass.network.bedrock.util;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.nukkitx.nbt.TagType;
import com.nukkitx.nbt.tag.CompoundTag;
import com.nukkitx.nbt.NbtMap;
import com.nukkitx.nbt.NbtType;
import com.nukkitx.proxypass.ProxyPass;
import lombok.Value;

Expand All @@ -13,23 +13,23 @@

public class BlockPaletteUtils {

public static void convertToJson(ProxyPass proxy, List<CompoundTag> tags) {
public static void convertToJson(ProxyPass proxy, List<NbtMap> tags) {

List<Entry> palette = new ArrayList<>(tags.size());

for (CompoundTag tag : tags) {
for (NbtMap tag : tags) {
int id = tag.getShort("id");
CompoundTag blockTag = tag.getCompound("block");
NbtMap blockTag = tag.getCompound("block");
String name = blockTag.getString("name");

Map<String, BlockState> states = new LinkedHashMap<>();

blockTag.getCompound("states").getValue().forEach((key, value) -> {
states.put(key, new BlockState(value.getValue(), TagType.byClass(value.getClass()).getId()));
blockTag.getCompound("states").forEach((key, value) -> {
states.put(key, new BlockState(value, NbtType.byClass(value.getClass()).getId()));
});

Integer meta = null;
if (tag.contains("meta")) {
if (tag.containsKey("meta")) {
meta = (int) tag.getShort("meta");
}
palette.add(new Entry(id, meta, name, states));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.nukkitx.proxypass.network.bedrock.util;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.nukkitx.nbt.NBTOutputStream;
import com.nukkitx.nbt.NbtMap;
import com.nukkitx.nbt.NbtUtils;
import com.nukkitx.nbt.stream.NBTOutputStream;
import com.nukkitx.nbt.tag.CompoundTag;
import com.nukkitx.protocol.bedrock.data.*;
import com.nukkitx.protocol.bedrock.data.inventory.*;
import com.nukkitx.protocol.bedrock.packet.CraftingDataPacket;
import com.nukkitx.proxypass.ProxyPass;
import lombok.AllArgsConstructor;
Expand All @@ -27,21 +27,21 @@ public static void writeRecipes(CraftingDataPacket packet, ProxyPass proxy) {
for (CraftingData craftingData : packet.getCraftingData()) {
CraftingDataEntry entry = new CraftingDataEntry();

CraftingType type = craftingData.getType();
CraftingDataType type = craftingData.getType();
entry.type = type.ordinal();

if (type != CraftingType.MULTI) {
if (type != CraftingDataType.MULTI) {
entry.block = craftingData.getCraftingTag();
} else {
entry.uuid = craftingData.getUuid();
}

if (type == CraftingType.SHAPED || type == CraftingType.SHAPELESS || type == CraftingType.SHAPELESS_CHEMISTRY || type == CraftingType.SHULKER_BOX || type == CraftingType.SHAPED_CHEMISTRY) {
if (type == CraftingDataType.SHAPED || type == CraftingDataType.SHAPELESS || type == CraftingDataType.SHAPELESS_CHEMISTRY || type == CraftingDataType.SHULKER_BOX || type == CraftingDataType.SHAPED_CHEMISTRY) {
entry.id = craftingData.getRecipeId();
entry.priority = craftingData.getPriority();
entry.output = writeItemArray(craftingData.getOutputs(), true);
}
if (type == CraftingType.SHAPED || type == CraftingType.SHAPED_CHEMISTRY) {
if (type == CraftingDataType.SHAPED || type == CraftingDataType.SHAPED_CHEMISTRY) {

int charCounter = 0;
ItemData[] inputs = craftingData.getInputs();
Expand Down Expand Up @@ -81,11 +81,11 @@ public static void writeRecipes(CraftingDataPacket packet, ProxyPass proxy) {
}
entry.input = itemMap;
}
if (type == CraftingType.SHAPELESS || type == CraftingType.SHAPELESS_CHEMISTRY || type == CraftingType.SHULKER_BOX) {
if (type == CraftingDataType.SHAPELESS || type == CraftingDataType.SHAPELESS_CHEMISTRY || type == CraftingDataType.SHULKER_BOX) {
entry.input = writeItemArray(craftingData.getInputs(), false);
}

if (type == CraftingType.FURNACE || type == CraftingType.FURNACE_DATA) {
if (type == CraftingDataType.FURNACE || type == CraftingDataType.FURNACE_DATA) {
Integer damage = craftingData.getInputDamage();
if (damage == 0x7fff) damage = -1;
if (damage == 0) damage = null;
Expand All @@ -111,11 +111,11 @@ private static Item[] writeItemArray(ItemData[] inputs, boolean output) {
return outputs.toArray(new Item[0]);
}

private static String nbtToBase64(CompoundTag tag) {
private static String nbtToBase64(NbtMap tag) {
if (tag != null) {
ByteArrayOutputStream tagStream = new ByteArrayOutputStream();
try (NBTOutputStream writer = NbtUtils.createWriterLE(tagStream)) {
writer.write(tag);
writer.writeTag(tag);
} catch (IOException e) {
throw new RuntimeException(e);
}
Expand Down

0 comments on commit 16c8a4e

Please sign in to comment.