Villager fixes + other stuff

- Consoles trading with villagers should work better
- Villagers closing their inventory work now
This commit is contained in:
Camotoy 2021-02-25 16:12:58 -05:00
parent 255c7ac0d2
commit 0727008881
No known key found for this signature in database
GPG key ID: 7EEFB66FE798081F
3 changed files with 29 additions and 11 deletions

View file

@ -28,10 +28,10 @@ package org.geysermc.connector.network.translators.bedrock;
import com.github.steveice10.mc.protocol.packet.ingame.client.window.ClientCloseWindowPacket;
import com.nukkitx.protocol.bedrock.packet.ContainerClosePacket;
import org.geysermc.connector.inventory.Inventory;
import org.geysermc.connector.inventory.MerchantContainer;
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.inventory.InventoryTranslator;
import org.geysermc.connector.utils.InventoryUtils;
@Translator(packet = ContainerClosePacket.class)
@ -47,6 +47,11 @@ public class BedrockContainerCloseTranslator extends PacketTranslator<ContainerC
session.sendUpstreamPacket(packet);
session.setClosingInventory(false);
if (windowId == -1 && session.getOpenInventory() instanceof MerchantContainer) {
// 1.16.200 - window ID is always -1 sent from Bedrock
windowId = (byte) session.getOpenInventory().getId();
}
Inventory openInventory = session.getOpenInventory();
if (openInventory != null) {
if (windowId == openInventory.getId()) {

View file

@ -39,10 +39,7 @@ import com.nukkitx.protocol.bedrock.data.inventory.stackrequestactions.*;
import com.nukkitx.protocol.bedrock.packet.ItemStackResponsePacket;
import it.unimi.dsi.fastutil.ints.*;
import lombok.AllArgsConstructor;
import org.geysermc.connector.inventory.CartographyContainer;
import org.geysermc.connector.inventory.GeyserItemStack;
import org.geysermc.connector.inventory.Inventory;
import org.geysermc.connector.inventory.PlayerInventory;
import org.geysermc.connector.inventory.*;
import org.geysermc.connector.network.session.GeyserSession;
import org.geysermc.connector.network.translators.inventory.click.Click;
import org.geysermc.connector.network.translators.inventory.click.ClickPlan;
@ -101,7 +98,7 @@ public abstract class InventoryTranslator {
public static final int PLAYER_INVENTORY_SIZE = 36;
public static final int PLAYER_INVENTORY_OFFSET = 9;
private static final int MAX_ITEM_STACK_SIZE = 64;
public final int size;
public abstract void prepareInventory(GeyserSession session, Inventory inventory);
@ -346,8 +343,7 @@ public abstract class InventoryTranslator {
}
break;
}
// The following three tend to be called for UI inventories
case CONSUME: {
case CONSUME: { // Tends to be called for UI inventories
if (inventory instanceof CartographyContainer) {
// TODO add this for more inventories? Only seems to glitch out the cartography table, though.
ConsumeStackRequestActionData consumeData = (ConsumeStackRequestActionData) action;
@ -367,8 +363,9 @@ public abstract class InventoryTranslator {
}
break;
}
case CRAFT_NON_IMPLEMENTED_DEPRECATED:
case CRAFT_RESULTS_DEPRECATED:
case CRAFT_RECIPE_AUTO: // Called by villagers
case CRAFT_NON_IMPLEMENTED_DEPRECATED: // Tends to be called for UI inventories
case CRAFT_RESULTS_DEPRECATED: // Tends to be called for UI inventories
case CRAFT_RECIPE_OPTIONAL: { // Anvils and cartography tables will handle this
break;
}
@ -391,7 +388,6 @@ public abstract class InventoryTranslator {
for (StackRequestActionData action : request.getActions()) {
switch (action.getType()) {
case CRAFT_RECIPE: {
CraftRecipeStackRequestActionData craftAction = (CraftRecipeStackRequestActionData) action;
if (craftState != CraftState.START) {
return rejectRequest(request);
}
@ -716,10 +712,19 @@ public abstract class InventoryTranslator {
return new ItemStackResponsePacket.Response(ItemStackResponsePacket.ResponseStatus.OK, request.getRequestId(), containerEntries);
}
/**
* Reject an incorrect ItemStackRequest.
*/
public static ItemStackResponsePacket.Response rejectRequest(ItemStackRequest request) {
return rejectRequest(request, true);
}
/**
* Reject an incorrect ItemStackRequest.
*
* @param throwError whether this request was truly erroneous (true), or known as an outcome and should not be treated
* as bad (false).
*/
public static ItemStackResponsePacket.Response rejectRequest(ItemStackRequest request, boolean throwError) {
if (throwError) {
// Currently for debugging, but might be worth it to keep in the future if something goes terribly wrong.

View file

@ -31,6 +31,7 @@ import com.nukkitx.protocol.bedrock.data.entity.EntityData;
import com.nukkitx.protocol.bedrock.data.entity.EntityDataMap;
import com.nukkitx.protocol.bedrock.data.entity.EntityLinkData;
import com.nukkitx.protocol.bedrock.data.inventory.*;
import com.nukkitx.protocol.bedrock.packet.ItemStackResponsePacket;
import com.nukkitx.protocol.bedrock.packet.SetEntityLinkPacket;
import org.geysermc.connector.entity.Entity;
import org.geysermc.connector.entity.type.EntityType;
@ -137,6 +138,13 @@ public class MerchantInventoryTranslator extends BaseInventoryTranslator {
}
}
@Override
public ItemStackResponsePacket.Response translateAutoCraftingRequest(GeyserSession session, Inventory inventory, ItemStackRequest request) {
// We're not crafting here
// Called at least by consoles when pressing a trade option button
return translateRequest(session, inventory, request);
}
@Override
public void updateInventory(GeyserSession session, Inventory inventory) {
updater.updateInventory(this, session, inventory);