2020-05-17 04:52:39 +00:00
|
|
|
/*
|
2021-01-01 15:10:36 +00:00
|
|
|
* Copyright (c) 2019-2021 GeyserMC. http://geysermc.org
|
2020-05-17 04:52:39 +00:00
|
|
|
*
|
2020-07-30 20:10:15 +00:00
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
2020-05-17 04:52:39 +00:00
|
|
|
*
|
2020-07-30 20:10:15 +00:00
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
2020-05-17 04:52:39 +00:00
|
|
|
*
|
2020-07-30 20:10:15 +00:00
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
2020-05-17 04:52:39 +00:00
|
|
|
*
|
2020-07-30 20:10:15 +00:00
|
|
|
* @author GeyserMC
|
|
|
|
* @link https://github.com/GeyserMC/Geyser
|
2020-05-17 04:52:39 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
package org.geysermc.connector.network.translators.java.world;
|
|
|
|
|
|
|
|
import com.github.steveice10.mc.protocol.data.game.entity.metadata.ItemStack;
|
|
|
|
import com.github.steveice10.mc.protocol.data.game.window.VillagerTrade;
|
|
|
|
import com.github.steveice10.mc.protocol.packet.ingame.server.window.ServerTradeListPacket;
|
2020-07-05 20:58:43 +00:00
|
|
|
import com.nukkitx.nbt.NbtMap;
|
|
|
|
import com.nukkitx.nbt.NbtMapBuilder;
|
|
|
|
import com.nukkitx.nbt.NbtType;
|
2020-06-23 00:11:09 +00:00
|
|
|
import com.nukkitx.protocol.bedrock.data.entity.EntityData;
|
2021-02-27 00:03:18 +00:00
|
|
|
import com.nukkitx.protocol.bedrock.data.inventory.ContainerType;
|
2020-06-23 00:11:09 +00:00
|
|
|
import com.nukkitx.protocol.bedrock.data.inventory.ItemData;
|
2020-05-17 04:52:39 +00:00
|
|
|
import com.nukkitx.protocol.bedrock.packet.UpdateTradePacket;
|
2020-05-19 17:41:44 +00:00
|
|
|
import org.geysermc.connector.entity.Entity;
|
2020-09-29 17:19:37 +00:00
|
|
|
import org.geysermc.connector.inventory.Inventory;
|
2020-10-16 23:25:05 +00:00
|
|
|
import org.geysermc.connector.inventory.MerchantContainer;
|
2020-05-17 04:52:39 +00:00
|
|
|
import org.geysermc.connector.network.session.GeyserSession;
|
|
|
|
import org.geysermc.connector.network.translators.PacketTranslator;
|
|
|
|
import org.geysermc.connector.network.translators.Translator;
|
|
|
|
import org.geysermc.connector.network.translators.item.ItemEntry;
|
2020-06-06 02:52:11 +00:00
|
|
|
import org.geysermc.connector.network.translators.item.ItemRegistry;
|
|
|
|
import org.geysermc.connector.network.translators.item.ItemTranslator;
|
2021-03-08 20:53:47 +00:00
|
|
|
import org.geysermc.connector.network.translators.world.block.BlockTranslator;
|
2020-05-17 04:52:39 +00:00
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
@Translator(packet = ServerTradeListPacket.class)
|
|
|
|
public class JavaTradeListTranslator extends PacketTranslator<ServerTradeListPacket> {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void translate(ServerTradeListPacket packet, GeyserSession session) {
|
2020-10-16 23:25:05 +00:00
|
|
|
Inventory openInventory = session.getOpenInventory();
|
|
|
|
if (!(openInventory instanceof MerchantContainer && openInventory.getId() == packet.getWindowId())) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
MerchantContainer merchantInventory = (MerchantContainer) openInventory;
|
|
|
|
merchantInventory.setVillagerTrades(packet.getTrades());
|
|
|
|
Entity villager = merchantInventory.getVillager();
|
2020-05-17 04:52:39 +00:00
|
|
|
villager.getMetadata().put(EntityData.TRADE_TIER, packet.getVillagerLevel() - 1);
|
2020-05-19 05:15:29 +00:00
|
|
|
villager.getMetadata().put(EntityData.MAX_TRADE_TIER, 4);
|
|
|
|
villager.getMetadata().put(EntityData.TRADE_XP, packet.getExperience());
|
2020-05-17 04:52:39 +00:00
|
|
|
villager.updateBedrockMetadata(session);
|
|
|
|
|
|
|
|
UpdateTradePacket updateTradePacket = new UpdateTradePacket();
|
2020-05-19 05:15:29 +00:00
|
|
|
updateTradePacket.setTradeTier(packet.getVillagerLevel() - 1);
|
2020-09-29 17:19:37 +00:00
|
|
|
updateTradePacket.setContainerId((short) packet.getWindowId());
|
2020-06-23 00:11:09 +00:00
|
|
|
updateTradePacket.setContainerType(ContainerType.TRADE);
|
2021-02-27 00:03:18 +00:00
|
|
|
updateTradePacket.setDisplayName(openInventory.getTitle());
|
2020-06-29 03:44:38 +00:00
|
|
|
updateTradePacket.setSize(0);
|
|
|
|
updateTradePacket.setNewTradingUi(true);
|
|
|
|
updateTradePacket.setUsingEconomyTrade(true);
|
2020-05-17 04:52:39 +00:00
|
|
|
updateTradePacket.setPlayerUniqueEntityId(session.getPlayerEntity().getGeyserId());
|
2020-10-16 23:25:05 +00:00
|
|
|
updateTradePacket.setTraderUniqueEntityId(villager.getGeyserId());
|
2021-03-08 21:57:31 +00:00
|
|
|
|
2020-07-05 20:58:43 +00:00
|
|
|
NbtMapBuilder builder = NbtMap.builder();
|
2021-03-04 17:26:16 +00:00
|
|
|
boolean addExtraTrade = packet.isRegularVillager() && packet.getVillagerLevel() < 5;
|
|
|
|
List<NbtMap> tags = new ArrayList<>(addExtraTrade ? packet.getTrades().length + 1 : packet.getTrades().length);
|
2020-10-16 23:25:05 +00:00
|
|
|
for (int i = 0; i < packet.getTrades().length; i++) {
|
|
|
|
VillagerTrade trade = packet.getTrades()[i];
|
2020-07-05 20:58:43 +00:00
|
|
|
NbtMapBuilder recipe = NbtMap.builder();
|
2020-10-16 23:25:05 +00:00
|
|
|
recipe.putInt("netId", i + 1);
|
|
|
|
recipe.putInt("maxUses", trade.isTradeDisabled() ? 0 : trade.getMaxUses());
|
2020-07-05 20:58:43 +00:00
|
|
|
recipe.putInt("traderExp", trade.getXp());
|
|
|
|
recipe.putFloat("priceMultiplierA", trade.getPriceMultiplier());
|
|
|
|
recipe.put("sell", getItemTag(session, trade.getOutput(), 0));
|
|
|
|
recipe.putFloat("priceMultiplierB", 0.0f);
|
|
|
|
recipe.putInt("buyCountB", trade.getSecondInput() != null ? trade.getSecondInput().getAmount() : 0);
|
|
|
|
recipe.putInt("buyCountA", trade.getFirstInput().getAmount());
|
|
|
|
recipe.putInt("demand", trade.getDemand());
|
2020-09-29 17:19:37 +00:00
|
|
|
recipe.putInt("tier", packet.getVillagerLevel() > 0 ? packet.getVillagerLevel() - 1 : 0); // -1 crashes client
|
2020-07-05 20:58:43 +00:00
|
|
|
recipe.put("buyA", getItemTag(session, trade.getFirstInput(), trade.getSpecialPrice()));
|
2020-05-17 04:52:39 +00:00
|
|
|
if (trade.getSecondInput() != null) {
|
2020-07-05 20:58:43 +00:00
|
|
|
recipe.put("buyB", getItemTag(session, trade.getSecondInput(), 0));
|
2020-05-17 04:52:39 +00:00
|
|
|
}
|
2020-07-05 20:58:43 +00:00
|
|
|
recipe.putInt("uses", trade.getNumUses());
|
|
|
|
recipe.putByte("rewardExp", (byte) 1);
|
|
|
|
tags.add(recipe.build());
|
2020-05-17 04:52:39 +00:00
|
|
|
}
|
2020-05-19 05:15:29 +00:00
|
|
|
|
|
|
|
//Hidden trade to fix visual experience bug
|
2021-03-04 17:26:16 +00:00
|
|
|
if (addExtraTrade) {
|
2020-07-05 20:58:43 +00:00
|
|
|
tags.add(NbtMap.builder()
|
|
|
|
.putInt("maxUses", 0)
|
|
|
|
.putInt("traderExp", 0)
|
|
|
|
.putFloat("priceMultiplierA", 0.0f)
|
|
|
|
.putFloat("priceMultiplierB", 0.0f)
|
|
|
|
.putInt("buyCountB", 0)
|
|
|
|
.putInt("buyCountA", 0)
|
|
|
|
.putInt("demand", 0)
|
|
|
|
.putInt("tier", 5)
|
|
|
|
.putInt("uses", 0)
|
|
|
|
.putByte("rewardExp", (byte) 0)
|
|
|
|
.build());
|
2020-05-19 05:15:29 +00:00
|
|
|
}
|
|
|
|
|
2020-07-05 20:58:43 +00:00
|
|
|
builder.putList("Recipes", NbtType.COMPOUND, tags);
|
2021-03-08 21:57:31 +00:00
|
|
|
|
2021-03-04 17:26:16 +00:00
|
|
|
List<NbtMap> expTags = new ArrayList<>(5);
|
2020-07-05 20:58:43 +00:00
|
|
|
expTags.add(NbtMap.builder().putInt("0", 0).build());
|
|
|
|
expTags.add(NbtMap.builder().putInt("1", 10).build());
|
|
|
|
expTags.add(NbtMap.builder().putInt("2", 70).build());
|
|
|
|
expTags.add(NbtMap.builder().putInt("3", 150).build());
|
|
|
|
expTags.add(NbtMap.builder().putInt("4", 250).build());
|
|
|
|
builder.putList("TierExpRequirements", NbtType.COMPOUND, expTags);
|
2021-03-08 21:57:31 +00:00
|
|
|
|
2020-07-05 20:58:43 +00:00
|
|
|
updateTradePacket.setOffers(builder.build());
|
2020-05-17 04:52:39 +00:00
|
|
|
session.sendUpstreamPacket(updateTradePacket);
|
|
|
|
}
|
|
|
|
|
2020-07-05 20:58:43 +00:00
|
|
|
private NbtMap getItemTag(GeyserSession session, ItemStack stack, int specialPrice) {
|
2020-06-06 02:52:11 +00:00
|
|
|
ItemData itemData = ItemTranslator.translateToBedrock(session, stack);
|
|
|
|
ItemEntry itemEntry = ItemRegistry.getItem(stack);
|
2021-03-08 21:57:31 +00:00
|
|
|
|
2020-07-05 20:58:43 +00:00
|
|
|
NbtMapBuilder builder = NbtMap.builder();
|
|
|
|
builder.putByte("Count", (byte) (Math.max(itemData.getCount() + specialPrice, 1)));
|
|
|
|
builder.putShort("Damage", itemData.getDamage());
|
2020-11-18 06:10:49 +00:00
|
|
|
builder.putString("Name", itemEntry.getBedrockIdentifier());
|
2020-05-19 05:15:29 +00:00
|
|
|
if (itemData.getTag() != null) {
|
2020-07-05 20:58:43 +00:00
|
|
|
NbtMap tag = itemData.getTag().toBuilder().build();
|
|
|
|
builder.put("tag", tag);
|
2020-05-19 05:15:29 +00:00
|
|
|
}
|
2021-03-08 21:57:31 +00:00
|
|
|
|
2021-03-08 20:53:47 +00:00
|
|
|
NbtMap blockTag = BlockTranslator.getBedrockBlockNbt(itemEntry.getJavaIdentifier());
|
|
|
|
if (blockTag != null) {
|
|
|
|
// This fixes certain blocks being unable to stack after grabbing one
|
|
|
|
builder.putCompound("Block", blockTag);
|
|
|
|
builder.putShort("Damage", (short) 0);
|
|
|
|
}
|
2021-03-08 21:57:31 +00:00
|
|
|
|
2020-07-05 20:58:43 +00:00
|
|
|
return builder.build();
|
2020-05-17 04:52:39 +00:00
|
|
|
}
|
|
|
|
}
|