Skip to content

Commit

Permalink
More review fixes and make beacon more reliable
Browse files Browse the repository at this point in the history
  • Loading branch information
Camotoy committed Mar 12, 2021
1 parent d4c21bb commit 23ab697
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public void prepareInventory(InventoryTranslator translator, GeyserSession sessi
// Check to see if there is an existing block we can use that the player just selected.
// First, verify that the player's position has not changed, so we don't try to select a block wildly out of range.
// (This could be a virtual inventory that the player is opening)
if (session.getLastInteractionPlayerPosition().equals(session.getPlayerEntity().getPosition())) {
if (checkInteractionPosition(session)) {
// 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("\\[");
Expand All @@ -101,6 +101,16 @@ public void prepareInventory(InventoryTranslator translator, GeyserSession sessi
setCustomName(session, position, inventory, defaultJavaBlockState);
}

/**
* Will be overwritten in the beacon inventory translator to remove the check, since virtual inventories can't exist.
*
* @return if the player's last interaction position and current position match. Used to ensure that we don't select
* a block to hold the inventory that's wildly out of range.
*/
protected boolean checkInteractionPosition(GeyserSession session) {
return session.getLastInteractionPlayerPosition().equals(session.getPlayerEntity().getPosition());
}

/**
* @return true if this Java block ID can be used for player inventory.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,40 @@
import org.geysermc.connector.inventory.PlayerInventory;
import org.geysermc.connector.network.session.GeyserSession;
import org.geysermc.connector.network.translators.inventory.BedrockContainerSlot;
import org.geysermc.connector.network.translators.inventory.InventoryTranslator;
import org.geysermc.connector.network.translators.inventory.holder.BlockInventoryHolder;
import org.geysermc.connector.network.translators.inventory.updater.UIInventoryUpdater;
import org.geysermc.connector.utils.InventoryUtils;

import java.util.Collections;

public class BeaconInventoryTranslator extends AbstractBlockInventoryTranslator {
public BeaconInventoryTranslator() {
super(1, "minecraft:beacon", ContainerType.BEACON, UIInventoryUpdater.INSTANCE);
super(1, new BlockInventoryHolder("minecraft:beacon", ContainerType.BEACON) {
@Override
public void prepareInventory(InventoryTranslator translator, GeyserSession session, Inventory inventory) {
if (!session.getConnector().getConfig().isCacheChunks()) {
// Beacons cannot work without knowing their physical location
return;
}
super.prepareInventory(translator, session, inventory);
}

@Override
protected boolean checkInteractionPosition(GeyserSession session) {
// Since we can't fall back to a virtual inventory, let's make opening one easier
return true;
}

@Override
public void openInventory(InventoryTranslator translator, GeyserSession session, Inventory inventory) {
if (!session.getConnector().getConfig().isCacheChunks() || !((BeaconContainer) inventory).isUsingRealBlock()) {
InventoryUtils.closeInventory(session, inventory.getId(), false);
return;
}
super.openInventory(translator, session, inventory);
}
}, UIInventoryUpdater.INSTANCE);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,14 @@ public boolean shouldRejectItemPlace(GeyserSession session, Inventory inventory,

@Override
public int bedrockSlotToJava(StackRequestSlotInfoData slotInfoData) {
if (slotInfoData.getContainer() == ContainerSlotType.CARTOGRAPHY_INPUT) {
return 0;
}
if (slotInfoData.getContainer() == ContainerSlotType.CARTOGRAPHY_ADDITIONAL) {
return 1;
}
if (slotInfoData.getContainer() == ContainerSlotType.CARTOGRAPHY_RESULT || slotInfoData.getContainer() == ContainerSlotType.CREATIVE_OUTPUT) {
return 2;
switch (slotInfoData.getContainer()) {
case CARTOGRAPHY_INPUT:
return 0;
case CARTOGRAPHY_ADDITIONAL:
return 1;
case CARTOGRAPHY_RESULT:
case CREATIVE_OUTPUT:
return 2;
}
return super.bedrockSlotToJava(slotInfoData);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public void translate(ServerTradeListPacket packet, GeyserSession session) {
return;
}

// Retrieve the fake villager involved in the trade, and update its metadata to match with the window information
MerchantContainer merchantInventory = (MerchantContainer) openInventory;
merchantInventory.setVillagerTrades(packet.getTrades());
Entity villager = merchantInventory.getVillager();
Expand All @@ -66,6 +67,7 @@ public void translate(ServerTradeListPacket packet, GeyserSession session) {
villager.getMetadata().put(EntityData.TRADE_XP, packet.getExperience());
villager.updateBedrockMetadata(session);

// Construct the packet that opens the trading window
UpdateTradePacket updateTradePacket = new UpdateTradePacket();
updateTradePacket.setTradeTier(packet.getVillagerLevel() - 1);
updateTradePacket.setContainerId((short) packet.getWindowId());
Expand Down

0 comments on commit 23ab697

Please sign in to comment.