Skip to content

Commit

Permalink
A bunch of fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Camotoy committed Feb 21, 2021
1 parent 605201a commit f849a5b
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
public class Inventory {

@Getter
protected int id;
protected final int id;

@Getter
protected final int size;
Expand Down Expand Up @@ -96,4 +96,17 @@ protected static void updateItemNetId(GeyserItemStack oldItem, GeyserItemStack n
public short getNextTransactionId() {
return ++transactionId;
}

@Override
public String toString() {
return "Inventory{" +
"id=" + id +
", size=" + size +
", title='" + title + '\'' +
", items=" + Arrays.toString(items) +
", holderPosition=" + holderPosition +
", holderId=" + holderId +
", transactionId=" + transactionId +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public UpstreamPacketHandler(GeyserConnector connector, GeyserSession session) {
}

private boolean translateAndDefault(BedrockPacket packet) {
System.out.println(packet.toString());
return PacketTranslatorRegistry.BEDROCK_TRANSLATOR.translate(packet.getClass(), packet, session);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public class GeyserSession implements CommandSender {
* Use {@link #getNextItemNetId()} instead for consistency
*/
@Getter(AccessLevel.NONE)
private final AtomicInteger itemNetId = new AtomicInteger(1);
private final AtomicInteger itemNetId = new AtomicInteger(2);

@Getter(AccessLevel.NONE)
private final Object inventoryLock = new Object();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,17 +85,15 @@ public abstract class InventoryTranslator {
put(WindowType.CARTOGRAPHY, new CartographyInventoryTranslator());
put(WindowType.CRAFTING, new CraftingInventoryTranslator());
put(WindowType.ENCHANTMENT, new EnchantingInventoryTranslator());
put(WindowType.HOPPER, new HopperInventoryTranslator());
put(WindowType.GENERIC_3X3, new Generic3X3InventoryTranslator());
put(WindowType.GRINDSTONE, new GrindstoneInventoryTranslator());
put(WindowType.LOOM, new LoomInventoryTranslator());
put(WindowType.MERCHANT, new MerchantInventoryTranslator());
put(WindowType.SHULKER_BOX, new ShulkerInventoryTranslator());
put(WindowType.SMITHING, new SmithingInventoryTranslator());
put(WindowType.STONECUTTER, new StonecutterInventoryTranslator());

/* Generics */
put(WindowType.GENERIC_3X3, new Generic3X3InventoryTranslator());
put(WindowType.HOPPER, new HopperInventoryTranslator());

/* Lectern */
put(WindowType.LECTERN, new LecternInventoryTranslator());
}
Expand Down Expand Up @@ -195,9 +193,11 @@ public ItemStackResponsePacket.Response translateRequest(GeyserSession session,
transferAction.getSource().getSlot() >= 28 && transferAction.getSource().getSlot() <= 31) {
return rejectRequest(request, false);
}
session.getConnector().getLogger().error("DEBUG: About to reject request made by " + session.getName());
session.getConnector().getLogger().error("DEBUG: About to reject TAKE/PLACE request made by " + session.getName());
session.getConnector().getLogger().error("Source: " + transferAction.getSource().toString() + " Result: " + checkNetId(session, inventory, transferAction.getSource()));
session.getConnector().getLogger().error("Destination: " + transferAction.getDestination().toString() + " Result: " + checkNetId(session, inventory, transferAction.getDestination()));
session.getConnector().getLogger().error("Geyser's record of source slot: " + inventory.getItem(bedrockSlotToJava(transferAction.getSource())));
session.getConnector().getLogger().error("Geyser's record of destination slot: " + inventory.getItem(bedrockSlotToJava(transferAction.getDestination())));
return rejectRequest(request);
}

Expand Down Expand Up @@ -278,8 +278,14 @@ public ItemStackResponsePacket.Response translateRequest(GeyserSession session,
}
case SWAP: { //TODO
SwapStackRequestActionData swapAction = (SwapStackRequestActionData) action;
if (!(checkNetId(session, inventory, swapAction.getSource()) && checkNetId(session, inventory, swapAction.getDestination())))
if (!(checkNetId(session, inventory, swapAction.getSource()) && checkNetId(session, inventory, swapAction.getDestination()))) {
session.getConnector().getLogger().error("DEBUG: About to reject SWAP request made by " + session.getName());
session.getConnector().getLogger().error("Source: " + swapAction.getSource().toString() + " Result: " + checkNetId(session, inventory, swapAction.getSource()));
session.getConnector().getLogger().error("Destination: " + swapAction.getDestination().toString() + " Result: " + checkNetId(session, inventory, swapAction.getDestination()));
session.getConnector().getLogger().error("Geyser's record of source slot: " + inventory.getItem(bedrockSlotToJava(swapAction.getSource())));
session.getConnector().getLogger().error("Geyser's record of destination slot: " + inventory.getItem(bedrockSlotToJava(swapAction.getDestination())));
return rejectRequest(request);
}

if (isCursor(swapAction.getSource()) && isCursor(swapAction.getDestination())) { //???
return rejectRequest(request);
Expand Down Expand Up @@ -361,14 +367,9 @@ public ItemStackResponsePacket.Response translateRequest(GeyserSession session,
}
break;
}
case CRAFT_NON_IMPLEMENTED_DEPRECATED: {
break;
}
case CRAFT_RESULTS_DEPRECATED: {
break;
}
case CRAFT_RECIPE_OPTIONAL: {
// Anvils and cartography tables will handle this
case CRAFT_NON_IMPLEMENTED_DEPRECATED:
case CRAFT_RESULTS_DEPRECATED:
case CRAFT_RECIPE_OPTIONAL: { // Anvils and cartography tables will handle this
break;
}
default:
Expand All @@ -381,9 +382,8 @@ public ItemStackResponsePacket.Response translateRequest(GeyserSession session,
}

public ItemStackResponsePacket.Response translateCraftingRequest(GeyserSession session, Inventory inventory, ItemStackRequest request) {
int recipeId = 0;
int resultSize = 0;
int timesCrafted = 0;
int timesCrafted;
CraftState craftState = CraftState.START;

int leftover = 0;
Expand All @@ -396,7 +396,6 @@ public ItemStackResponsePacket.Response translateCraftingRequest(GeyserSession s
return rejectRequest(request);
}
craftState = CraftState.RECIPE_ID;
recipeId = craftAction.getRecipeNetworkId();
break;
}
case CRAFT_RESULTS_DEPRECATED: {
Expand All @@ -417,7 +416,6 @@ public ItemStackResponsePacket.Response translateCraftingRequest(GeyserSession s
break;
}
case CONSUME: {
ConsumeStackRequestActionData consumeAction = (ConsumeStackRequestActionData) action;
if (craftState != CraftState.DEPRECATED && craftState != CraftState.INGREDIENTS) {
return rejectRequest(request);
}
Expand Down Expand Up @@ -507,9 +505,7 @@ public ItemStackResponsePacket.Response translateAutoCraftingRequest(GeyserSessi

Int2IntMap consumedSlots = new Int2IntOpenHashMap();
int prioritySlot = -1;
int secondarySlot = -1;
int tempSlot = -1;
boolean intoCursor = false;
int tempSlot;

int resultSize;
int timesCrafted = 0;
Expand Down Expand Up @@ -626,7 +622,6 @@ public ItemStackResponsePacket.Response translateAutoCraftingRequest(GeyserSessi

int javaSlot = bedrockSlotToJava(transferAction.getDestination());
if (isCursor(transferAction.getDestination())) { //TODO
intoCursor = true;
if (timesCrafted > 1) {
tempSlot = findTempSlot(inventory, GeyserItemStack.from(output), true);
if (tempSlot == -1) {
Expand Down Expand Up @@ -735,6 +730,8 @@ public static ItemStackResponsePacket.Response rejectRequest(ItemStackRequest re

public boolean checkNetId(GeyserSession session, Inventory inventory, StackRequestSlotInfoData slotInfoData) {
int netId = slotInfoData.getStackNetworkId();
// "In my testing, sometimes the client thinks the netId of an item in the crafting grid is 1, even though we never said it was.
// I think it only happens when we manually set the grid but that was my quick fix"
if (netId < 0 || netId == 1)
return true;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import com.nukkitx.nbt.NbtMap;
import com.nukkitx.protocol.bedrock.data.inventory.ContainerType;
import com.nukkitx.protocol.bedrock.packet.BlockEntityDataPacket;
import com.nukkitx.protocol.bedrock.packet.ContainerClosePacket;
import com.nukkitx.protocol.bedrock.packet.ContainerOpenPacket;
import com.nukkitx.protocol.bedrock.packet.UpdateBlockPacket;
import org.geysermc.connector.inventory.Container;
Expand Down Expand Up @@ -76,11 +77,11 @@ public void prepareInventory(InventoryTranslator translator, GeyserSession sessi
if (session.getLastInteractionPlayerPosition().equals(session.getPlayerEntity().getPosition())) {
// Then, check to see if the interacted block is valid for this inventory by ensuring the block state identifier is valid
int javaBlockId = session.getConnector().getWorldManager().getBlockAt(session, session.getLastInteractionBlockPosition());
String javaBlockString = BlockTranslator.getJavaIdBlockMap().inverse().getOrDefault(javaBlockId, "minecraft:air").split("\\[")[0];
if (this.validBlocks.contains(javaBlockString)) {
String[] javaBlockString = BlockTranslator.getJavaIdBlockMap().inverse().getOrDefault(javaBlockId, "minecraft:air").split("\\[");
if (isValidBlock(javaBlockString)) {
// We can safely use this block
inventory.setHolderPosition(session.getLastInteractionBlockPosition());
((Container) inventory).setUsingRealBlock(true, javaBlockString);
((Container) inventory).setUsingRealBlock(true, javaBlockString[0]);
setCustomName(session, session.getLastInteractionBlockPosition(), inventory);
return;
}
Expand All @@ -100,6 +101,13 @@ public void prepareInventory(InventoryTranslator translator, GeyserSession sessi
setCustomName(session, position, inventory);
}

/**
* @return true if this Java block ID can be used for player inventory.
*/
protected boolean isValidBlock(String[] javaBlockString) {
return this.validBlocks.contains(javaBlockString[0]);
}

protected void setCustomName(GeyserSession session, Vector3i position, Inventory inventory) {
NbtMap tag = NbtMap.builder()
.putInt("x", position.getX())
Expand All @@ -126,6 +134,11 @@ public void openInventory(InventoryTranslator translator, GeyserSession session,
public void closeInventory(InventoryTranslator translator, GeyserSession session, Inventory inventory) {
if (((Container) inventory).isUsingRealBlock()) {
// No need to reset a block since we didn't change any blocks
// But send a container close packet because we aren't destroying the original.
ContainerClosePacket packet = new ContainerClosePacket();
packet.setId((byte) inventory.getId());
packet.setUnknownBool0(true); //TODO needs to be changed in Protocol to "server-side" or something
session.sendUpstreamPacket(packet);
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,18 @@ public class SingleChestInventoryTranslator extends ChestInventoryTranslator {
public SingleChestInventoryTranslator(int size) {
super(size, 27);
this.holder = new BlockInventoryHolder("minecraft:chest[facing=north,type=single,waterlogged=false]", ContainerType.CONTAINER,
"minecraft:ender_chest", "minecraft:trapped_chest");
"minecraft:ender_chest", "minecraft:trapped_chest") {
@Override
protected boolean isValidBlock(String[] javaBlockString) {
if (javaBlockString[0].equals("minecraft:ender_chest")) {
// Can't have double ender chests
return true;
}

// Add provision to ensure this isn't a double chest
return super.isValidBlock(javaBlockString) && (javaBlockString.length > 1 && javaBlockString[1].contains("type=single"));
}
};
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ public class JavaCloseWindowTranslator extends PacketTranslator<ServerCloseWindo

@Override
public void translate(ServerCloseWindowPacket packet, GeyserSession session) {
session.addInventoryTask(() -> InventoryUtils.closeInventory(session, packet.getWindowId()));
session.addInventoryTask(() -> {
session.getConnector().getLogger().info("Closing window ID " + packet.getWindowId());
InventoryUtils.closeInventory(session, packet.getWindowId());
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public void translate(ServerOpenWindowPacket packet, GeyserSession session) {
if (packet.getWindowId() == 0) {
return;
}
session.getConnector().getLogger().info("Opening window ID " + packet.getWindowId());

InventoryTranslator newTranslator = InventoryTranslator.INVENTORY_TRANSLATORS.get(packet.getType());
Inventory openInventory = session.getOpenInventory();
Expand All @@ -63,10 +64,7 @@ public void translate(ServerOpenWindowPacket packet, GeyserSession session) {

Inventory newInventory = newTranslator.createInventory(name, packet.getWindowId(), packet.getType(), session.getPlayerInventory());
if (openInventory != null) {
InventoryTranslator openTranslator = session.getInventoryTranslator();
if (!openTranslator.getClass().equals(newTranslator.getClass())) {
InventoryUtils.closeInventory(session, openInventory.getId());
}
InventoryUtils.closeInventory(session, openInventory.getId());
}

session.setInventoryTranslator(newTranslator);
Expand Down

0 comments on commit f849a5b

Please sign in to comment.