Skip to content

Commit

Permalink
Restrict game packets to state; Closes #4191 (#4210)
Browse files Browse the repository at this point in the history
Signed-off-by: Joshua Castle <[email protected]>
  • Loading branch information
Kas-tle authored Oct 12, 2023
1 parent 00800ac commit 3fa35b2
Show file tree
Hide file tree
Showing 40 changed files with 132 additions and 103 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public void execute(GeyserSession session, GeyserCommandSource sender, String[]

session.setWaitingForStatistics(true);
ServerboundClientCommandPacket ServerboundClientCommandPacket = new ServerboundClientCommandPacket(ClientCommand.STATS);
session.sendDownstreamPacket(ServerboundClientCommandPacket);
session.sendDownstreamGamePacket(ServerboundClientCommandPacket);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public InteractionResult interact(Hand hand) {
animatePacket.setAction(AnimatePacket.Action.SWING_ARM);
session.sendUpstreamPacket(animatePacket);

session.sendDownstreamPacket(new ServerboundSwingPacket(hand));
session.sendDownstreamGamePacket(new ServerboundSwingPacket(hand));
return InteractionResult.SUCCESS;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,14 @@ public String checkForRename(GeyserSession session, String rename) {
correctRename = plainNewName;
// Java Edition sends a packet every time an item is renamed even slightly in GUI. Fortunately, this works out for us now
ServerboundRenameItemPacket renameItemPacket = new ServerboundRenameItemPacket(plainNewName);
session.sendDownstreamPacket(renameItemPacket);
session.sendDownstreamGamePacket(renameItemPacket);
} else {
// Restore formatting for item since we're not renaming
correctRename = MessageTranslator.convertMessageLenient(originalName);
// Java Edition sends the original custom name when not renaming,
// if there isn't a custom name an empty string is sent
ServerboundRenameItemPacket renameItemPacket = new ServerboundRenameItemPacket(plainOriginalName);
session.sendDownstreamPacket(renameItemPacket);
session.sendDownstreamGamePacket(renameItemPacket);
}

useJavaLevelCost = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public void execute(boolean refresh) {
changedItems
);

session.sendDownstreamPacket(clickPacket);
session.sendDownstreamGamePacket(clickPacket);
}

session.getPlayerInventory().setCursor(simulatedCursor, session);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ private void updateInventoryState(GeyserSession session, AnvilContainer anvilCon
// does not result in a FilterTextPacket
String originalName = MessageTranslator.convertToPlainTextLenient(ItemUtils.getCustomName(input.getNbt()), session.locale());
ServerboundRenameItemPacket renameItemPacket = new ServerboundRenameItemPacket(originalName);
session.sendDownstreamPacket(renameItemPacket);
session.sendDownstreamGamePacket(renameItemPacket);

anvilContainer.setNewName(null);
}
Expand Down
47 changes: 40 additions & 7 deletions core/src/main/java/org/geysermc/geyser/session/GeyserSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -1135,7 +1135,7 @@ protected void tick() {
if (position != null) {
ServerboundMovePlayerPosPacket packet = new ServerboundMovePlayerPosPacket(playerEntity.isOnGround(),
position.getX(), position.getY(), position.getZ());
sendDownstreamPacket(packet);
sendDownstreamGamePacket(packet);
}
lastMovementTimestamp = System.currentTimeMillis();
}
Expand Down Expand Up @@ -1317,7 +1317,7 @@ private boolean attemptToBlock() {
return false;
}

sendDownstreamPacket(useItemPacket);
sendDownstreamGamePacket(useItemPacket);
playerEntity.setFlag(EntityFlag.BLOCKING, true);
// Metadata should be updated later
return true;
Expand Down Expand Up @@ -1351,7 +1351,7 @@ private boolean disableBlocking() {
if (playerEntity.getFlag(EntityFlag.BLOCKING)) {
ServerboundPlayerActionPacket releaseItemPacket = new ServerboundPlayerActionPacket(PlayerAction.RELEASE_USE_ITEM,
Vector3i.ZERO, Direction.DOWN, 0);
sendDownstreamPacket(releaseItemPacket);
sendDownstreamGamePacket(releaseItemPacket);
playerEntity.setFlag(EntityFlag.BLOCKING, false);
return true;
}
Expand All @@ -1361,7 +1361,7 @@ private boolean disableBlocking() {
public void requestOffhandSwap() {
ServerboundPlayerActionPacket swapHandsPacket = new ServerboundPlayerActionPacket(PlayerAction.SWAP_HANDS, Vector3i.ZERO,
Direction.DOWN, 0);
sendDownstreamPacket(swapHandsPacket);
sendDownstreamGamePacket(swapHandsPacket);
}

@Override
Expand Down Expand Up @@ -1396,14 +1396,14 @@ public String locale() {
* Sends a chat message to the Java server.
*/
public void sendChat(String message) {
sendDownstreamPacket(new ServerboundChatPacket(message, Instant.now().toEpochMilli(), 0L, null, 0, new BitSet()));
sendDownstreamGamePacket(new ServerboundChatPacket(message, Instant.now().toEpochMilli(), 0L, null, 0, new BitSet()));
}

/**
* Sends a command to the Java server.
*/
public void sendCommand(String command) {
sendDownstreamPacket(new ServerboundChatCommandPacket(command, Instant.now().toEpochMilli(), 0L, Collections.emptyList(), 0, new BitSet()));
sendDownstreamGamePacket(new ServerboundChatCommandPacket(command, Instant.now().toEpochMilli(), 0L, Collections.emptyList(), 0, new BitSet()));
}

public void setServerRenderDistance(int renderDistance) {
Expand Down Expand Up @@ -1575,6 +1575,39 @@ public void sendUpstreamPacketImmediately(BedrockPacket packet) {
upstream.sendPacketImmediately(packet);
}

/**
* Send a packet to the remote server if in the game state.
*
* @param packet the java edition packet from MCProtocolLib
*/
public void sendDownstreamGamePacket(Packet packet) {
sendDownstreamPacket(packet, ProtocolState.GAME);
}

/**
* Send a packet to the remote server if in the login state.
*
* @param packet the java edition packet from MCProtocolLib
*/
public void sendDownstreamLoginPacket(Packet packet) {
sendDownstreamPacket(packet, ProtocolState.LOGIN);
}

/**
* Send a packet to the remote server if in the specified state.
*
* @param packet the java edition packet from MCProtocolLib
* @param intendedState the state the client should be in
*/
public void sendDownstreamPacket(Packet packet, ProtocolState intendedState) {
if (protocol.getState() != intendedState) {
geyser.getLogger().debug("Tried to send " + packet.getClass().getSimpleName() + " packet while not in " + intendedState.name() + " state");
return;
}

sendDownstreamPacket(packet);
}

/**
* Send a packet to the remote server.
*
Expand Down Expand Up @@ -1718,7 +1751,7 @@ public void sendAdventureSettings() {
// We're "flying locked" in this gamemode
flying = true;
ServerboundPlayerAbilitiesPacket abilitiesPacket = new ServerboundPlayerAbilitiesPacket(true);
sendDownstreamPacket(abilitiesPacket);
sendDownstreamGamePacket(abilitiesPacket);
}
abilities.add(Ability.FLYING);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public void buildAndShowMenuForm() {
} else {
// Send a packet indicating that we intend to open this particular advancement window
ServerboundSeenAdvancementsPacket packet = new ServerboundSeenAdvancementsPacket(id);
session.sendDownstreamPacket(packet);
session.sendDownstreamGamePacket(packet);
// Wait for a response there
}
}
Expand Down Expand Up @@ -137,7 +137,7 @@ public void buildAndShowListForm() {

builder.closedResultHandler(() -> {
// Indicate that we have closed the current advancement tab
session.sendDownstreamPacket(new ServerboundSeenAdvancementsPacket());
session.sendDownstreamGamePacket(new ServerboundSeenAdvancementsPacket());

}).validResultHandler((response) -> {
if (response.getClickedButtonId() < visibleAdvancements.size()) {
Expand All @@ -146,7 +146,7 @@ public void buildAndShowListForm() {
} else {
buildAndShowMenuForm();
// Indicate that we have closed the current advancement tab
session.sendDownstreamPacket(new ServerboundSeenAdvancementsPacket());
session.sendDownstreamGamePacket(new ServerboundSeenAdvancementsPacket());
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public void checkForSend() {
packet = null;
return;
}
session.sendDownstreamPacket(packet);
session.sendDownstreamGamePacket(packet);
packet = null;
lastBookUpdate = System.currentTimeMillis();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public ItemStackResponse translateSpecialRequest(GeyserSession session, Inventor
// Input a beacon payment
BeaconPaymentAction beaconPayment = (BeaconPaymentAction) request.getActions()[0];
ServerboundSetBeaconPacket packet = new ServerboundSetBeaconPacket(toJava(beaconPayment.getPrimaryEffect()), toJava(beaconPayment.getSecondaryEffect()));
session.sendDownstreamPacket(packet);
session.sendDownstreamGamePacket(packet);
return acceptRequest(request, makeContainerEntries(session, inventory, IntSets.emptySet()));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public ItemStackResponse translateSpecialRequest(GeyserSession session, Inventor
return rejectRequest(request);
}
ServerboundContainerButtonClickPacket packet = new ServerboundContainerButtonClickPacket(inventory.getJavaId(), javaSlot);
session.sendDownstreamPacket(packet);
session.sendDownstreamGamePacket(packet);
return acceptRequest(request, makeContainerEntries(session, inventory, IntSets.emptySet()));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ private void updateBook(GeyserSession session, Inventory inventory, GeyserItemSt
if (session.isDroppingLecternBook()) {
// We have to enter the inventory GUI to eject the book
ServerboundContainerButtonClickPacket packet = new ServerboundContainerButtonClickPacket(inventory.getJavaId(), 3);
session.sendDownstreamPacket(packet);
session.sendDownstreamGamePacket(packet);
session.setDroppingLecternBook(false);
InventoryUtils.closeInventory(session, inventory.getJavaId(), false);
} else if (lecternContainer.getBlockEntityTag() == null) {
Expand Down Expand Up @@ -153,7 +153,7 @@ private void updateBook(GeyserSession session, Inventory inventory, GeyserItemSt
session.getLecternCache().add(position);
// Close the window - we will reopen it once the client has this data synced
ServerboundContainerClosePacket closeWindowPacket = new ServerboundContainerClosePacket(lecternContainer.getJavaId());
session.sendDownstreamPacket(closeWindowPacket);
session.sendDownstreamGamePacket(closeWindowPacket);
InventoryUtils.closeInventory(session, inventory.getJavaId(), false);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ public ItemStackResponse translateSpecialRequest(GeyserSession session, Inventor
// And the Java loom window has a fixed row/width of four
// So... Number / 4 = row (so we don't have to bother there), and number % 4 is our column, which leads us back to our index. :)
ServerboundContainerButtonClickPacket packet = new ServerboundContainerButtonClickPacket(inventory.getJavaId(), index);
session.sendDownstreamPacket(packet);
session.sendDownstreamGamePacket(packet);

GeyserItemStack inputCopy = inventory.getItem(0).copy(1);
inputCopy.setNetId(session.getNextItemNetId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public ItemStackResponse translateAutoCraftingRequest(GeyserSession session, Inv

private ItemStackResponse handleTrade(GeyserSession session, Inventory inventory, ItemStackRequest request, int tradeChoice) {
ServerboundSelectTradePacket packet = new ServerboundSelectTradePacket(tradeChoice);
session.sendDownstreamPacket(packet);
session.sendDownstreamGamePacket(packet);

if (session.isEmulatePost1_13Logic()) {
// 1.18 Java cooperates nicer than older versions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ public ItemStackResponse translateRequest(GeyserSession session, Inventory inven
}

ServerboundSetCreativeModeSlotPacket creativeDropPacket = new ServerboundSetCreativeModeSlotPacket(-1, sourceItem.getItemStack(dropAction.getCount()));
session.sendDownstreamPacket(creativeDropPacket);
session.sendDownstreamGamePacket(creativeDropPacket);

sourceItem.sub(dropAction.getCount());
}
Expand Down Expand Up @@ -494,7 +494,7 @@ protected ItemStackResponse translateCreativeRequest(GeyserSession session, Inve
dropStack = new ItemStack(javaCreativeItem.getId(), dropAction.getCount(), javaCreativeItem.getNbt());
}
ServerboundSetCreativeModeSlotPacket creativeDropPacket = new ServerboundSetCreativeModeSlotPacket(-1, dropStack);
session.sendDownstreamPacket(creativeDropPacket);
session.sendDownstreamGamePacket(creativeDropPacket);
break;
}
default:
Expand All @@ -515,7 +515,7 @@ private static void sendCreativeAction(GeyserSession session, Inventory inventor
ItemStack itemStack = item.isEmpty() ? new ItemStack(-1, 0, null) : item.getItemStack();

ServerboundSetCreativeModeSlotPacket creativePacket = new ServerboundSetCreativeModeSlotPacket(slot, itemStack);
session.sendDownstreamPacket(creativePacket);
session.sendDownstreamGamePacket(creativePacket);
}

private static boolean isCraftingGrid(ItemStackRequestSlotData slotInfoData) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ protected ItemStackResponse translateSpecialRequest(GeyserSession session, Inven
if (container.getStonecutterButton() != button) {
// Getting the index of the item in the Java stonecutter list
ServerboundContainerButtonClickPacket packet = new ServerboundContainerButtonClickPacket(inventory.getJavaId(), button);
session.sendDownstreamPacket(packet);
session.sendDownstreamGamePacket(packet);
container.setStonecutterButton(button);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public void translate(GeyserSession session, AnimatePacket packet) {
// and Bedrock 1.19.51.
// Note for the future: we should probably largely ignore this packet and instead replicate
// all actions on our end, and send swings where needed.
session.sendDownstreamPacket(new ServerboundSwingPacket(Hand.MAIN_HAND));
session.sendDownstreamGamePacket(new ServerboundSwingPacket(Hand.MAIN_HAND));
session.activateArmAnimationTicking();
}
},
Expand All @@ -77,12 +77,12 @@ public void translate(GeyserSession session, AnimatePacket packet) {
// Packet value is a float of how long one has been rowing, so we convert that into a boolean
session.setSteeringLeft(packet.getRowingTime() > 0.0);
ServerboundPaddleBoatPacket steerLeftPacket = new ServerboundPaddleBoatPacket(session.isSteeringLeft(), session.isSteeringRight());
session.sendDownstreamPacket(steerLeftPacket);
session.sendDownstreamGamePacket(steerLeftPacket);
}
case ROW_RIGHT -> {
session.setSteeringRight(packet.getRowingTime() > 0.0);
ServerboundPaddleBoatPacket steerRightPacket = new ServerboundPaddleBoatPacket(session.isSteeringLeft(), session.isSteeringRight());
session.sendDownstreamPacket(steerRightPacket);
session.sendDownstreamGamePacket(steerRightPacket);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public void translate(GeyserSession session, BlockEntityDataPacket packet) {
if (iterator < lines.length) lines[iterator] = newMessage.toString();
Vector3i pos = Vector3i.from(tag.getInt("x"), tag.getInt("y"), tag.getInt("z"));
ServerboundSignUpdatePacket signUpdatePacket = new ServerboundSignUpdatePacket(pos, lines, session.getWorldCache().isEditingSignOnFront());
session.sendDownstreamPacket(signUpdatePacket);
session.sendDownstreamGamePacket(signUpdatePacket);

} else if (id.equals("JigsawBlock")) {
// Client has just sent a jigsaw block update
Expand All @@ -120,7 +120,7 @@ public void translate(GeyserSession session, BlockEntityDataPacket packet) {
String joint = tag.getString("joint");
ServerboundSetJigsawBlockPacket jigsawPacket = new ServerboundSetJigsawBlockPacket(pos, name, target, pool,
finalState, joint);
session.sendDownstreamPacket(jigsawPacket);
session.sendDownstreamGamePacket(jigsawPacket);
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,13 @@ public void translate(GeyserSession session, CommandBlockUpdatePacket packet) {
boolean automatic = !packet.isRedstoneMode(); // Automatic = Always Active option in Java
ServerboundSetCommandBlockPacket commandBlockPacket = new ServerboundSetCommandBlockPacket(
packet.getBlockPosition(), command, mode, outputTracked, isConditional, automatic);
session.sendDownstreamPacket(commandBlockPacket);
session.sendDownstreamGamePacket(commandBlockPacket);
} else {
ServerboundSetCommandMinecartPacket commandMinecartPacket = new ServerboundSetCommandMinecartPacket(
session.getEntityCache().getEntityByGeyserId(packet.getMinecartRuntimeEntityId()).getEntityId(),
command, outputTracked
);
session.sendDownstreamPacket(commandMinecartPacket);
session.sendDownstreamGamePacket(commandMinecartPacket);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public void translate(GeyserSession session, ContainerClosePacket packet) {
if (openInventory != null) {
if (bedrockId == openInventory.getBedrockId()) {
ServerboundContainerClosePacket closeWindowPacket = new ServerboundContainerClosePacket(openInventory.getJavaId());
session.sendDownstreamPacket(closeWindowPacket);
session.sendDownstreamGamePacket(closeWindowPacket);
InventoryUtils.closeInventory(session, openInventory.getJavaId(), false);
} else if (openInventory.isPending()) {
InventoryUtils.displayInventory(session, openInventory);
Expand Down
Loading

0 comments on commit 3fa35b2

Please sign in to comment.