com.nukkitx.protocol.bedrock.packet.UpdateTradePacket Java Examples

The following examples show how to use com.nukkitx.protocol.bedrock.packet.UpdateTradePacket. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: UpdateTradeSerializer_v354.java    From Protocol with Apache License 2.0 6 votes vote down vote up
@Override
public void serialize(ByteBuf buffer, UpdateTradePacket packet) {
    buffer.writeByte(packet.getWindowId());
    buffer.writeByte(packet.getWindowType());
    VarInts.writeInt(buffer, packet.getUnknownInt());
    VarInts.writeInt(buffer, packet.getTradeTier());
    VarInts.writeLong(buffer, packet.getTraderUniqueEntityId());
    VarInts.writeLong(buffer, packet.getPlayerUniqueEntityId());
    BedrockUtils.writeString(buffer, packet.getDisplayName());
    buffer.writeBoolean(packet.isScreen2());
    buffer.writeBoolean(packet.isWilling());
    try (NBTOutputStream writer = NbtUtils.createNetworkWriter(new ByteBufOutputStream(buffer))) {
        writer.write(packet.getOffers());
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
 
Example #2
Source File: UpdateTradeSerializer_v354.java    From Protocol with Apache License 2.0 6 votes vote down vote up
@Override
public void deserialize(ByteBuf buffer, UpdateTradePacket packet) {
    packet.setWindowId(buffer.readUnsignedByte());
    packet.setWindowType(buffer.readUnsignedByte());
    packet.setUnknownInt(VarInts.readInt(buffer));
    packet.setTradeTier(VarInts.readInt(buffer));
    packet.setWilling(buffer.readBoolean());
    packet.setTraderUniqueEntityId(VarInts.readLong(buffer));
    packet.setPlayerUniqueEntityId(VarInts.readLong(buffer));
    packet.setDisplayName(BedrockUtils.readString(buffer));
    packet.setScreen2(buffer.readBoolean());
    packet.setWilling(buffer.readBoolean());
    try (NBTInputStream reader = NbtUtils.createNetworkReader(new ByteBufInputStream(buffer))) {
        packet.setOffers(reader.readTag());
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
 
Example #3
Source File: UpdateTradeSerializer_v388.java    From Protocol with Apache License 2.0 6 votes vote down vote up
@Override
public void serialize(ByteBuf buffer, UpdateTradePacket packet) {
    buffer.writeByte(packet.getWindowId());
    buffer.writeByte(packet.getWindowType());
    VarInts.writeInt(buffer, packet.getUnknownInt());
    VarInts.writeInt(buffer, packet.getTradeTier());
    VarInts.writeLong(buffer, packet.getTraderUniqueEntityId());
    VarInts.writeLong(buffer, packet.getPlayerUniqueEntityId());
    BedrockUtils.writeString(buffer, packet.getDisplayName());
    buffer.writeBoolean(packet.isScreen2());
    buffer.writeBoolean(packet.isWilling());
    try (NBTOutputStream writer = NbtUtils.createNetworkWriter(new ByteBufOutputStream(buffer))) {
        writer.write(packet.getOffers());
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
 
Example #4
Source File: UpdateTradeSerializer_v388.java    From Protocol with Apache License 2.0 6 votes vote down vote up
@Override
public void deserialize(ByteBuf buffer, UpdateTradePacket packet) {
    packet.setWindowId(buffer.readUnsignedByte());
    packet.setWindowType(buffer.readUnsignedByte());
    packet.setUnknownInt(VarInts.readInt(buffer));
    packet.setTradeTier(VarInts.readInt(buffer));
    packet.setWilling(buffer.readBoolean());
    packet.setTraderUniqueEntityId(VarInts.readLong(buffer));
    packet.setPlayerUniqueEntityId(VarInts.readLong(buffer));
    packet.setDisplayName(BedrockUtils.readString(buffer));
    packet.setScreen2(buffer.readBoolean());
    packet.setWilling(buffer.readBoolean());
    try (NBTInputStream reader = NbtUtils.createNetworkReader(new ByteBufInputStream(buffer))) {
        packet.setOffers(reader.readTag());
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
 
Example #5
Source File: UpdateTradeSerializer_v340.java    From Protocol with Apache License 2.0 6 votes vote down vote up
@Override
public void serialize(ByteBuf buffer, UpdateTradePacket packet) {
    buffer.writeByte(packet.getWindowId());
    buffer.writeByte(packet.getWindowType());
    VarInts.writeInt(buffer, packet.getUnknownInt());
    VarInts.writeInt(buffer, packet.isScreen2() ? 40 : 0);
    VarInts.writeInt(buffer, packet.getTradeTier());
    buffer.writeBoolean(packet.isWilling());
    VarInts.writeLong(buffer, packet.getTraderUniqueEntityId());
    VarInts.writeLong(buffer, packet.getPlayerUniqueEntityId());
    BedrockUtils.writeString(buffer, packet.getDisplayName());
    try (NBTOutputStream writer = NbtUtils.createNetworkWriter(new ByteBufOutputStream(buffer))) {
        writer.write(packet.getOffers());
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
 
Example #6
Source File: UpdateTradeSerializer_v340.java    From Protocol with Apache License 2.0 6 votes vote down vote up
@Override
public void deserialize(ByteBuf buffer, UpdateTradePacket packet) {
    packet.setWindowId(buffer.readUnsignedByte());
    packet.setWindowType(buffer.readUnsignedByte());
    packet.setUnknownInt(VarInts.readInt(buffer));
    packet.setScreen2(VarInts.readInt(buffer) >= 40);
    packet.setTradeTier(VarInts.readInt(buffer));
    packet.setWilling(buffer.readBoolean());
    packet.setTraderUniqueEntityId(VarInts.readLong(buffer));
    packet.setPlayerUniqueEntityId(VarInts.readLong(buffer));
    packet.setDisplayName(BedrockUtils.readString(buffer));
    try (NBTInputStream reader = NbtUtils.createNetworkReader(new ByteBufInputStream(buffer))) {
        packet.setOffers(reader.readTag());
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
 
Example #7
Source File: UpdateTradeSerializer_v313.java    From Protocol with Apache License 2.0 6 votes vote down vote up
@Override
public void serialize(ByteBuf buffer, UpdateTradePacket packet) {
    buffer.writeByte(packet.getWindowId());
    buffer.writeByte(packet.getWindowType());
    VarInts.writeInt(buffer, packet.getUnknownInt());
    VarInts.writeInt(buffer, packet.isScreen2() ? 40 : 0);
    VarInts.writeInt(buffer, packet.getTradeTier());
    buffer.writeBoolean(packet.isWilling());
    VarInts.writeLong(buffer, packet.getTraderUniqueEntityId());
    VarInts.writeLong(buffer, packet.getPlayerUniqueEntityId());
    BedrockUtils.writeString(buffer, packet.getDisplayName());
    try (NBTOutputStream writer = NbtUtils.createNetworkWriter(new ByteBufOutputStream(buffer))) {
        writer.write(packet.getOffers());
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
 
Example #8
Source File: UpdateTradeSerializer_v313.java    From Protocol with Apache License 2.0 6 votes vote down vote up
@Override
public void deserialize(ByteBuf buffer, UpdateTradePacket packet) {
    packet.setWindowId(buffer.readUnsignedByte());
    packet.setWindowType(buffer.readUnsignedByte());
    packet.setUnknownInt(VarInts.readInt(buffer));
    packet.setScreen2(VarInts.readInt(buffer) >= 40);
    packet.setTradeTier(VarInts.readInt(buffer));
    packet.setWilling(buffer.readBoolean());
    packet.setTraderUniqueEntityId(VarInts.readLong(buffer));
    packet.setPlayerUniqueEntityId(VarInts.readLong(buffer));
    packet.setDisplayName(BedrockUtils.readString(buffer));
    try (NBTInputStream reader = NbtUtils.createNetworkReader(new ByteBufInputStream(buffer))) {
        packet.setOffers(reader.readTag());
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
 
Example #9
Source File: UpdateTradeSerializer_v361.java    From Protocol with Apache License 2.0 6 votes vote down vote up
@Override
public void serialize(ByteBuf buffer, UpdateTradePacket packet) {
    buffer.writeByte(packet.getWindowId());
    buffer.writeByte(packet.getWindowType());
    VarInts.writeInt(buffer, packet.getUnknownInt());
    VarInts.writeInt(buffer, packet.getTradeTier());
    VarInts.writeLong(buffer, packet.getTraderUniqueEntityId());
    VarInts.writeLong(buffer, packet.getPlayerUniqueEntityId());
    BedrockUtils.writeString(buffer, packet.getDisplayName());
    buffer.writeBoolean(packet.isScreen2());
    buffer.writeBoolean(packet.isWilling());
    try (NBTOutputStream writer = NbtUtils.createNetworkWriter(new ByteBufOutputStream(buffer))) {
        writer.write(packet.getOffers());
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
 
Example #10
Source File: UpdateTradeSerializer_v361.java    From Protocol with Apache License 2.0 6 votes vote down vote up
@Override
public void deserialize(ByteBuf buffer, UpdateTradePacket packet) {
    packet.setWindowId(buffer.readUnsignedByte());
    packet.setWindowType(buffer.readUnsignedByte());
    packet.setUnknownInt(VarInts.readInt(buffer));
    packet.setTradeTier(VarInts.readInt(buffer));
    packet.setWilling(buffer.readBoolean());
    packet.setTraderUniqueEntityId(VarInts.readLong(buffer));
    packet.setPlayerUniqueEntityId(VarInts.readLong(buffer));
    packet.setDisplayName(BedrockUtils.readString(buffer));
    packet.setScreen2(buffer.readBoolean());
    packet.setWilling(buffer.readBoolean());
    try (NBTInputStream reader = NbtUtils.createNetworkReader(new ByteBufInputStream(buffer))) {
        packet.setOffers(reader.readTag());
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
 
Example #11
Source File: UpdateTradeSerializer_v332.java    From Protocol with Apache License 2.0 6 votes vote down vote up
@Override
public void serialize(ByteBuf buffer, UpdateTradePacket packet) {
    buffer.writeByte(packet.getWindowId());
    buffer.writeByte(packet.getWindowType());
    VarInts.writeInt(buffer, packet.getUnknownInt());
    VarInts.writeInt(buffer, packet.isScreen2() ? 40 : 0);
    VarInts.writeInt(buffer, packet.getTradeTier());
    buffer.writeBoolean(packet.isWilling());
    VarInts.writeLong(buffer, packet.getTraderUniqueEntityId());
    VarInts.writeLong(buffer, packet.getPlayerUniqueEntityId());
    BedrockUtils.writeString(buffer, packet.getDisplayName());
    try (NBTOutputStream writer = NbtUtils.createNetworkWriter(new ByteBufOutputStream(buffer))) {
        writer.write(packet.getOffers());
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
 
Example #12
Source File: UpdateTradeSerializer_v332.java    From Protocol with Apache License 2.0 6 votes vote down vote up
@Override
public void deserialize(ByteBuf buffer, UpdateTradePacket packet) {
    packet.setWindowId(buffer.readUnsignedByte());
    packet.setWindowType(buffer.readUnsignedByte());
    packet.setUnknownInt(VarInts.readInt(buffer));
    packet.setScreen2(VarInts.readInt(buffer) >= 40);
    packet.setTradeTier(VarInts.readInt(buffer));
    packet.setWilling(buffer.readBoolean());
    packet.setTraderUniqueEntityId(VarInts.readLong(buffer));
    packet.setPlayerUniqueEntityId(VarInts.readLong(buffer));
    packet.setDisplayName(BedrockUtils.readString(buffer));
    try (NBTInputStream reader = NbtUtils.createNetworkReader(new ByteBufInputStream(buffer))) {
        packet.setOffers(reader.readTag());
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
 
Example #13
Source File: UpdateTradeSerializer_v291.java    From Protocol with Apache License 2.0 6 votes vote down vote up
@Override
public void serialize(ByteBuf buffer, UpdateTradePacket packet) {
    buffer.writeByte(packet.getWindowId());
    buffer.writeByte(packet.getWindowType());
    VarInts.writeInt(buffer, packet.getUnknownInt());
    VarInts.writeInt(buffer, packet.isScreen2() ? 40 : 0);
    buffer.writeBoolean(packet.isWilling());
    VarInts.writeLong(buffer, packet.getTraderUniqueEntityId());
    VarInts.writeLong(buffer, packet.getPlayerUniqueEntityId());
    BedrockUtils.writeString(buffer, packet.getDisplayName());
    try (NBTOutputStream writer = NbtUtils.createNetworkWriter(new ByteBufOutputStream(buffer))) {
        writer.write(packet.getOffers());
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
 
Example #14
Source File: UpdateTradeSerializer_v291.java    From Protocol with Apache License 2.0 6 votes vote down vote up
@Override
public void deserialize(ByteBuf buffer, UpdateTradePacket packet) {
    packet.setWindowId(buffer.readUnsignedByte());
    packet.setWindowType(buffer.readUnsignedByte());
    packet.setUnknownInt(VarInts.readInt(buffer));
    packet.setScreen2(VarInts.readInt(buffer) >= 40);
    packet.setWilling(buffer.readBoolean());
    packet.setTraderUniqueEntityId(VarInts.readLong(buffer));
    packet.setPlayerUniqueEntityId(VarInts.readLong(buffer));
    packet.setDisplayName(BedrockUtils.readString(buffer));
    try (NBTInputStream reader = NbtUtils.createNetworkReader(new ByteBufInputStream(buffer))) {
        packet.setOffers(reader.readTag());
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
 
Example #15
Source File: JavaTradeListTranslator.java    From Geyser with MIT License 4 votes vote down vote up
@Override
public void translate(ServerTradeListPacket packet, GeyserSession session) {
    Entity villager = session.getPlayerEntity();
    session.setVillagerTrades(packet.getTrades());
    villager.getMetadata().put(EntityData.TRADE_TIER, packet.getVillagerLevel() - 1);
    villager.getMetadata().put(EntityData.MAX_TRADE_TIER, 4);
    villager.getMetadata().put(EntityData.TRADE_XP, packet.getExperience());
    villager.updateBedrockMetadata(session);

    UpdateTradePacket updateTradePacket = new UpdateTradePacket();
    updateTradePacket.setTradeTier(packet.getVillagerLevel() - 1);
    updateTradePacket.setWindowId((short) packet.getWindowId());
    updateTradePacket.setWindowType((short) ContainerType.TRADING.id());
    String displayName;
    Entity realVillager = session.getEntityCache().getEntityByGeyserId(session.getLastInteractedVillagerEid());
    if (realVillager != null && realVillager.getMetadata().containsKey(EntityData.NAMETAG) && realVillager.getMetadata().getString(EntityData.NAMETAG) != null) {
        displayName = realVillager.getMetadata().getString(EntityData.NAMETAG);
    } else {
        displayName = packet.isRegularVillager() ? "Villager" : "Wandering Trader";
    }
    updateTradePacket.setDisplayName(displayName);
    updateTradePacket.setUnknownInt(0);
    updateTradePacket.setScreen2(true);
    updateTradePacket.setWilling(true);
    updateTradePacket.setPlayerUniqueEntityId(session.getPlayerEntity().getGeyserId());
    updateTradePacket.setTraderUniqueEntityId(session.getPlayerEntity().getGeyserId());
    CompoundTagBuilder builder = CompoundTagBuilder.builder();
    List<CompoundTag> tags = new ArrayList<>();
    for (VillagerTrade trade : packet.getTrades()) {
        CompoundTagBuilder recipe = CompoundTagBuilder.builder();
        recipe.intTag("maxUses", trade.getMaxUses());
        recipe.intTag("traderExp", trade.getXp());
        recipe.floatTag("priceMultiplierA", trade.getPriceMultiplier());
        recipe.tag(getItemTag(session, trade.getOutput(), "sell", 0));
        recipe.floatTag("priceMultiplierB", 0.0f);
        recipe.intTag("buyCountB", trade.getSecondInput() != null ? trade.getSecondInput().getAmount() : 0);
        recipe.intTag("buyCountA", trade.getFirstInput().getAmount());
        recipe.intTag("demand", trade.getDemand());
        recipe.intTag("tier", packet.getVillagerLevel() - 1);
        recipe.tag(getItemTag(session, trade.getFirstInput(), "buyA", trade.getSpecialPrice()));
        if (trade.getSecondInput() != null) {
            recipe.tag(getItemTag(session, trade.getSecondInput(), "buyB", 0));
        }
        recipe.intTag("uses", trade.getNumUses());
        recipe.byteTag("rewardExp", (byte) 1);
        tags.add(recipe.buildRootTag());
    }

    //Hidden trade to fix visual experience bug
    if (packet.isRegularVillager() && packet.getVillagerLevel() < 5) {
        tags.add(CompoundTagBuilder.builder()
                .intTag("maxUses", 0)
                .intTag("traderExp", 0)
                .floatTag("priceMultiplierA", 0.0f)
                .floatTag("priceMultiplierB", 0.0f)
                .intTag("buyCountB", 0)
                .intTag("buyCountA", 0)
                .intTag("demand", 0)
                .intTag("tier", 5)
                .intTag("uses", 0)
                .byteTag("rewardExp", (byte) 0)
                .buildRootTag());
    }

    builder.listTag("Recipes", CompoundTag.class, tags);
    List<CompoundTag> expTags = new ArrayList<>();
    expTags.add(CompoundTagBuilder.builder().intTag("0", 0).buildRootTag());
    expTags.add(CompoundTagBuilder.builder().intTag("1", 10).buildRootTag());
    expTags.add(CompoundTagBuilder.builder().intTag("2", 70).buildRootTag());
    expTags.add(CompoundTagBuilder.builder().intTag("3", 150).buildRootTag());
    expTags.add(CompoundTagBuilder.builder().intTag("4", 250).buildRootTag());
    builder.listTag("TierExpRequirements", CompoundTag.class, expTags);
    updateTradePacket.setOffers(builder.buildRootTag());
    session.sendUpstreamPacket(updateTradePacket);
}