Skip to content

Commit

Permalink
wip support old crafting
Browse files Browse the repository at this point in the history
  • Loading branch information
AJ-Ferguson committed Jan 9, 2021
1 parent c801664 commit 9118ec6
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ public class GeyserSession implements CommandSender {
@Setter
private Int2ObjectMap<Recipe> craftingRecipes;
private final Set<String> unlockedRecipes;
private AtomicInteger lastRecipeNetId;

/**
* Saves a list of all stonecutter recipes, for use in a stonecutter inventory.
Expand Down Expand Up @@ -385,6 +386,7 @@ public GeyserSession(GeyserConnector connector, BedrockServerSession bedrockServ
this.inventoryFuture = CompletableFuture.completedFuture(null);
this.craftingRecipes = new Int2ObjectOpenHashMap<>();
this.unlockedRecipes = new ObjectOpenHashSet<>();
this.lastRecipeNetId = new AtomicInteger(1);

this.spawned = false;
this.loggedIn = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -805,13 +805,12 @@ public static ItemStackResponsePacket.Response rejectRequest(ItemStackRequestPac
}

public boolean checkNetId(GeyserSession session, Inventory inventory, StackRequestSlotInfoData slotInfoData) {
if (slotInfoData.getStackNetworkId() < 0)
int netId = slotInfoData.getStackNetworkId();
if (netId < 0 || netId == 1)
return true;
// if (slotInfoData.getContainer() == ContainerSlotType.CURSOR) //TODO: temporary
// return true;

GeyserItemStack currentItem = isCursor(slotInfoData) ? session.getPlayerInventory().getCursor() : inventory.getItem(bedrockSlotToJava(slotInfoData));
return currentItem.getNetId() == slotInfoData.getStackNetworkId();
return currentItem.getNetId() == netId;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,10 +186,11 @@ public void translate(ServerDeclareRecipesPacket packet, GeyserSession session)
}
}

session.sendUpstreamPacket(craftingDataPacket);
//session.sendUpstreamPacket(craftingDataPacket); //commented out for testing
session.setCraftingRecipes(recipeMap);
session.getUnlockedRecipes().clear();
session.setStonecutterRecipes(stonecutterRecipeMap);
session.getLastRecipeNetId().set(netId);
}

//TODO: rewrite
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,26 @@
package org.geysermc.connector.network.translators.java.window;

import com.github.steveice10.mc.protocol.packet.ingame.server.window.ServerSetSlotPacket;
import com.nukkitx.protocol.bedrock.data.inventory.ContainerId;
import com.nukkitx.protocol.bedrock.data.inventory.CraftingData;
import com.nukkitx.protocol.bedrock.data.inventory.ItemData;
import com.nukkitx.protocol.bedrock.packet.CraftingDataPacket;
import com.nukkitx.protocol.bedrock.packet.InventorySlotPacket;
import org.geysermc.connector.inventory.GeyserItemStack;
import org.geysermc.connector.inventory.Inventory;
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.network.translators.inventory.translators.CraftingInventoryTranslator;
import org.geysermc.connector.network.translators.inventory.translators.PlayerInventoryTranslator;
import org.geysermc.connector.network.translators.item.ItemTranslator;
import org.geysermc.connector.utils.InventoryUtils;

import java.util.Arrays;
import java.util.Collections;
import java.util.UUID;

@Translator(packet = ServerSetSlotPacket.class)
public class JavaSetSlotTranslator extends PacketTranslator<ServerSetSlotPacket> {

Expand All @@ -55,6 +67,56 @@ public void translate(ServerSetSlotPacket packet, GeyserSession session) {

InventoryTranslator translator = session.getInventoryTranslator();
if (translator != null) {
if (packet.getSlot() == 0) {
int gridSize = -1;
if (translator instanceof PlayerInventoryTranslator) {
gridSize = 4;
}
if (translator instanceof CraftingInventoryTranslator) {
gridSize = 9;
}
if (gridSize != -1) {
int offset = gridSize == 4 ? 28 : 32;
int gridWidth = gridSize == 4 ? 2 : 3;
ItemData[] ingredients = new ItemData[gridSize];
//construct ingredient list and clear slots on client
for (int i = 0; i < gridSize; i++) {
ingredients[i] = inventory.getItem(i + 1).getItemData(session);

InventorySlotPacket slotPacket = new InventorySlotPacket();
slotPacket.setContainerId(ContainerId.UI);
slotPacket.setSlot(i + offset);
slotPacket.setItem(ItemData.AIR);
session.sendUpstreamPacket(slotPacket);
}

CraftingDataPacket craftPacket = new CraftingDataPacket();
UUID uuid = UUID.fromString("e0a4971a-698c-40fb-95dd-afc8ed16e108");
craftPacket.getCraftingData().add(CraftingData.fromShaped(
uuid.toString(),
gridWidth,
gridWidth,
Arrays.asList(ingredients),
Collections.singletonList(ItemTranslator.translateToBedrock(session, packet.getItem())),
uuid,
"crafting_table",
0,
session.getLastRecipeNetId().incrementAndGet()
));
craftPacket.setCleanRecipes(false);
session.sendUpstreamPacket(craftPacket);

//restore cleared slots
for (int i = 0; i < gridSize; i++) {
InventorySlotPacket slotPacket = new InventorySlotPacket();
slotPacket.setContainerId(ContainerId.UI);
slotPacket.setSlot(i + offset);
slotPacket.setItem(ingredients[i]);
session.sendUpstreamPacket(slotPacket);
}
}
}

GeyserItemStack newItem = GeyserItemStack.from(packet.getItem());
inventory.setItem(packet.getSlot(), newItem, session);
translator.updateSlot(session, inventory, packet.getSlot());
Expand Down

0 comments on commit 9118ec6

Please sign in to comment.