2020-05-02 06:29:30 +00:00
|
|
|
/*
|
2021-01-01 15:10:36 +00:00
|
|
|
* Copyright (c) 2019-2021 GeyserMC. http://geysermc.org
|
2020-05-02 06:29:30 +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-02 06:29:30 +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-02 06:29:30 +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-02 06:29:30 +00:00
|
|
|
*
|
2020-07-30 20:10:15 +00:00
|
|
|
* @author GeyserMC
|
|
|
|
* @link https://github.com/GeyserMC/Geyser
|
2020-05-02 06:29:30 +00:00
|
|
|
*/
|
|
|
|
|
2020-08-08 22:41:12 +00:00
|
|
|
package org.geysermc.connector.network.translators.bedrock.entity;
|
2020-05-02 06:29:30 +00:00
|
|
|
|
2021-11-14 05:07:24 +00:00
|
|
|
import com.github.steveice10.mc.protocol.data.game.inventory.VillagerTrade;
|
|
|
|
import com.github.steveice10.mc.protocol.packet.ingame.serverbound.inventory.ServerboundSelectTradePacket;
|
2020-05-02 06:29:30 +00:00
|
|
|
import com.nukkitx.protocol.bedrock.packet.EntityEventPacket;
|
2021-11-19 01:44:03 +00:00
|
|
|
import org.geysermc.connector.entity.player.SessionPlayerEntity;
|
2020-10-16 23:25:05 +00:00
|
|
|
import org.geysermc.connector.inventory.GeyserItemStack;
|
2020-05-19 05:15:29 +00:00
|
|
|
import org.geysermc.connector.inventory.Inventory;
|
2020-10-16 23:25:05 +00:00
|
|
|
import org.geysermc.connector.inventory.MerchantContainer;
|
2020-05-02 06:29:30 +00:00
|
|
|
import org.geysermc.connector.network.session.GeyserSession;
|
|
|
|
import org.geysermc.connector.network.translators.PacketTranslator;
|
|
|
|
import org.geysermc.connector.network.translators.Translator;
|
|
|
|
|
2021-08-17 00:39:29 +00:00
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
2020-05-02 06:29:30 +00:00
|
|
|
@Translator(packet = EntityEventPacket.class)
|
|
|
|
public class BedrockEntityEventTranslator extends PacketTranslator<EntityEventPacket> {
|
|
|
|
|
|
|
|
@Override
|
2021-08-30 17:55:01 +00:00
|
|
|
public void translate(GeyserSession session, EntityEventPacket packet) {
|
2020-05-02 06:29:30 +00:00
|
|
|
switch (packet.getType()) {
|
2021-09-10 18:10:56 +00:00
|
|
|
case EATING_ITEM -> {
|
2020-11-30 15:55:35 +00:00
|
|
|
// Resend the packet so we get the eating sounds
|
2020-05-05 15:51:43 +00:00
|
|
|
session.sendUpstreamPacket(packet);
|
2020-05-02 06:29:30 +00:00
|
|
|
return;
|
2021-09-10 18:10:56 +00:00
|
|
|
}
|
|
|
|
case COMPLETE_TRADE -> {
|
2021-11-13 03:44:15 +00:00
|
|
|
ServerboundSelectTradePacket selectTradePacket = new ServerboundSelectTradePacket(packet.getData());
|
2021-08-17 00:39:29 +00:00
|
|
|
session.sendDownstreamPacket(selectTradePacket);
|
2020-05-19 05:15:29 +00:00
|
|
|
|
2021-08-18 00:57:46 +00:00
|
|
|
session.scheduleInEventLoop(() -> {
|
2021-11-19 01:44:03 +00:00
|
|
|
SessionPlayerEntity villager = session.getPlayerEntity();
|
2020-10-16 23:25:05 +00:00
|
|
|
Inventory openInventory = session.getOpenInventory();
|
2021-09-10 18:10:56 +00:00
|
|
|
if (openInventory instanceof MerchantContainer merchantInventory) {
|
2020-10-16 23:25:05 +00:00
|
|
|
VillagerTrade[] trades = merchantInventory.getVillagerTrades();
|
|
|
|
if (trades != null && packet.getData() >= 0 && packet.getData() < trades.length) {
|
|
|
|
VillagerTrade trade = merchantInventory.getVillagerTrades()[packet.getData()];
|
2020-12-30 00:59:22 +00:00
|
|
|
openInventory.setItem(2, GeyserItemStack.from(trade.getOutput()), session);
|
2021-11-19 01:44:03 +00:00
|
|
|
// TODO this logic doesn't add up
|
|
|
|
villager.addFakeTradeExperience(trade.getXp());
|
2021-11-18 03:02:38 +00:00
|
|
|
villager.updateBedrockMetadata();
|
2020-10-16 23:25:05 +00:00
|
|
|
}
|
2020-05-19 05:15:29 +00:00
|
|
|
}
|
2021-08-17 00:39:29 +00:00
|
|
|
}, 100, TimeUnit.MILLISECONDS);
|
2020-05-19 05:15:29 +00:00
|
|
|
return;
|
2021-09-10 18:10:56 +00:00
|
|
|
}
|
2020-05-02 06:29:30 +00:00
|
|
|
}
|
|
|
|
session.getConnector().getLogger().debug("Did not translate incoming EntityEventPacket: " + packet.toString());
|
|
|
|
}
|
|
|
|
}
|