2020-05-17 04:52:39 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2019-2020 GeyserMC. http://geysermc.org
|
|
|
|
*
|
|
|
|
* 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:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* @author GeyserMC
|
|
|
|
* @link https://github.com/GeyserMC/Geyser
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
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;
|
|
|
|
import com.nukkitx.nbt.CompoundTagBuilder;
|
|
|
|
import com.nukkitx.nbt.tag.CompoundTag;
|
|
|
|
import com.nukkitx.protocol.bedrock.data.ContainerType;
|
|
|
|
import com.nukkitx.protocol.bedrock.data.EntityData;
|
|
|
|
import com.nukkitx.protocol.bedrock.data.ItemData;
|
|
|
|
import com.nukkitx.protocol.bedrock.packet.UpdateTradePacket;
|
2020-05-19 17:41:44 +00:00
|
|
|
import org.geysermc.connector.entity.Entity;
|
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;
|
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-05-19 17:41:44 +00:00
|
|
|
Entity villager = session.getPlayerEntity();
|
|
|
|
session.setVillagerTrades(packet.getTrades());
|
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-05-17 04:52:39 +00:00
|
|
|
updateTradePacket.setWindowId((short) packet.getWindowId());
|
|
|
|
updateTradePacket.setWindowType((short) ContainerType.TRADING.id());
|
|
|
|
updateTradePacket.setDisplayName("Villager");
|
|
|
|
updateTradePacket.setUnknownInt(0);
|
|
|
|
updateTradePacket.setScreen2(true);
|
|
|
|
updateTradePacket.setWilling(true);
|
|
|
|
updateTradePacket.setPlayerUniqueEntityId(session.getPlayerEntity().getGeyserId());
|
2020-05-19 17:41:44 +00:00
|
|
|
updateTradePacket.setTraderUniqueEntityId(session.getPlayerEntity().getGeyserId());
|
2020-05-17 04:52:39 +00:00
|
|
|
CompoundTagBuilder builder = CompoundTagBuilder.builder();
|
|
|
|
List<CompoundTag> tags = new ArrayList<>();
|
|
|
|
for (VillagerTrade trade : packet.getTrades()) {
|
|
|
|
CompoundTagBuilder recipe = CompoundTagBuilder.builder();
|
|
|
|
recipe.intTag("maxUses", trade.getMaxUses());
|
2020-05-19 05:15:29 +00:00
|
|
|
recipe.intTag("traderExp", trade.getXp());
|
2020-05-17 04:52:39 +00:00
|
|
|
recipe.floatTag("priceMultiplierA", trade.getPriceMultiplier());
|
2020-05-19 05:15:29 +00:00
|
|
|
recipe.tag(getItemTag(session, trade.getOutput(), "sell", 0));
|
2020-05-17 04:52:39 +00:00
|
|
|
recipe.floatTag("priceMultiplierB", 0.0f);
|
2020-05-19 05:15:29 +00:00
|
|
|
recipe.intTag("buyCountB", trade.getSecondInput() != null ? trade.getSecondInput().getAmount() : 0);
|
|
|
|
recipe.intTag("buyCountA", trade.getFirstInput().getAmount());
|
2020-05-17 04:52:39 +00:00
|
|
|
recipe.intTag("demand", trade.getDemand());
|
|
|
|
recipe.intTag("tier", packet.getVillagerLevel() - 1);
|
2020-05-19 05:15:29 +00:00
|
|
|
recipe.tag(getItemTag(session, trade.getFirstInput(), "buyA", trade.getSpecialPrice()));
|
2020-05-17 04:52:39 +00:00
|
|
|
if (trade.getSecondInput() != null) {
|
2020-05-19 05:15:29 +00:00
|
|
|
recipe.tag(getItemTag(session, trade.getSecondInput(), "buyB", 0));
|
2020-05-17 04:52:39 +00:00
|
|
|
}
|
|
|
|
recipe.intTag("uses", trade.getNumUses());
|
2020-05-19 05:15:29 +00:00
|
|
|
recipe.byteTag("rewardExp", (byte) 1);
|
2020-05-17 04:52:39 +00:00
|
|
|
tags.add(recipe.buildRootTag());
|
|
|
|
}
|
2020-05-19 05:15:29 +00:00
|
|
|
|
|
|
|
//Hidden trade to fix visual experience bug
|
2020-05-19 17:41:44 +00:00
|
|
|
if (packet.isRegularVillager() && packet.getVillagerLevel() < 5) {
|
2020-05-19 05:15:29 +00:00
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
2020-05-17 04:52:39 +00:00
|
|
|
builder.listTag("Recipes", CompoundTag.class, tags);
|
|
|
|
List<CompoundTag> expTags = new ArrayList<>();
|
|
|
|
expTags.add(CompoundTagBuilder.builder().intTag("0", 0).buildRootTag());
|
2020-05-19 17:41:44 +00:00
|
|
|
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());
|
2020-05-17 04:52:39 +00:00
|
|
|
builder.listTag("TierExpRequirements", CompoundTag.class, expTags);
|
|
|
|
updateTradePacket.setOffers(builder.buildRootTag());
|
|
|
|
session.sendUpstreamPacket(updateTradePacket);
|
|
|
|
}
|
|
|
|
|
2020-05-19 05:15:29 +00:00
|
|
|
private CompoundTag getItemTag(GeyserSession session, ItemStack stack, String name, int specialPrice) {
|
2020-06-06 02:52:11 +00:00
|
|
|
ItemData itemData = ItemTranslator.translateToBedrock(session, stack);
|
|
|
|
ItemEntry itemEntry = ItemRegistry.getItem(stack);
|
2020-05-17 04:52:39 +00:00
|
|
|
CompoundTagBuilder builder = CompoundTagBuilder.builder();
|
2020-05-19 05:15:29 +00:00
|
|
|
builder.byteTag("Count", (byte) (Math.max(itemData.getCount() + specialPrice, 1)));
|
2020-05-17 04:52:39 +00:00
|
|
|
builder.shortTag("Damage", itemData.getDamage());
|
2020-05-19 05:15:29 +00:00
|
|
|
builder.shortTag("id", (short) itemEntry.getBedrockId());
|
|
|
|
if (itemData.getTag() != null) {
|
|
|
|
CompoundTag tag = itemData.getTag().toBuilder().build("tag");
|
|
|
|
builder.tag(tag);
|
|
|
|
}
|
2020-05-17 04:52:39 +00:00
|
|
|
return builder.build(name);
|
|
|
|
}
|
|
|
|
}
|