2021-01-08 00:40:34 +00:00
|
|
|
/*
|
2022-01-01 19:03:05 +00:00
|
|
|
* Copyright (c) 2019-2022 GeyserMC. http://geysermc.org
|
2021-01-08 00:40:34 +00:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*
|
|
|
|
* @author GeyserMC
|
|
|
|
* @link https://github.com/GeyserMC/Geyser
|
|
|
|
*/
|
|
|
|
|
2021-11-20 23:29:46 +00:00
|
|
|
package org.geysermc.geyser.session.cache;
|
2021-01-08 00:40:34 +00:00
|
|
|
|
2021-11-14 05:07:24 +00:00
|
|
|
import com.github.steveice10.mc.protocol.packet.ingame.serverbound.inventory.ServerboundEditBookPacket;
|
2021-01-08 00:40:34 +00:00
|
|
|
import lombok.Setter;
|
2021-11-20 21:34:30 +00:00
|
|
|
import org.geysermc.geyser.inventory.GeyserItemStack;
|
2022-12-29 20:10:40 +00:00
|
|
|
import org.geysermc.geyser.item.Items;
|
2021-11-22 19:52:26 +00:00
|
|
|
import org.geysermc.geyser.session.GeyserSession;
|
2021-01-08 00:40:34 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Manages updating the current writable book.
|
|
|
|
*
|
|
|
|
* Java sends book updates less frequently than Bedrock, and this can cause issues with servers that rate limit
|
|
|
|
* book packets. Because of this, we need to ensure packets are only send every second or so at maximum.
|
|
|
|
*/
|
|
|
|
public class BookEditCache {
|
2021-11-22 19:52:26 +00:00
|
|
|
private final GeyserSession session;
|
2021-01-08 00:40:34 +00:00
|
|
|
@Setter
|
2021-11-13 03:44:15 +00:00
|
|
|
private ServerboundEditBookPacket packet;
|
2021-01-08 00:40:34 +00:00
|
|
|
/**
|
|
|
|
* Stores the last time a book update packet was sent to the server.
|
|
|
|
*/
|
|
|
|
private long lastBookUpdate;
|
|
|
|
|
2021-11-22 19:52:26 +00:00
|
|
|
public BookEditCache(GeyserSession session) {
|
2021-01-08 00:40:34 +00:00
|
|
|
this.session = session;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check to see if there is a book edit update to send, and if so, send it.
|
|
|
|
*/
|
|
|
|
public void checkForSend() {
|
|
|
|
if (packet == null) {
|
|
|
|
// No new packet has to be sent
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Prevent kicks due to rate limiting - specifically on Spigot servers
|
|
|
|
if ((System.currentTimeMillis() - lastBookUpdate) < 1000) {
|
|
|
|
return;
|
|
|
|
}
|
2023-06-19 00:02:27 +00:00
|
|
|
// Don't send the update if the player is not holding a book, shouldn't happen if we catch all interactions
|
2021-01-12 00:46:39 +00:00
|
|
|
GeyserItemStack itemStack = session.getPlayerInventory().getItemInHand();
|
2022-12-29 20:10:40 +00:00
|
|
|
if (itemStack == null || itemStack.asItem() != Items.WRITABLE_BOOK) {
|
2021-01-08 00:40:34 +00:00
|
|
|
packet = null;
|
|
|
|
return;
|
|
|
|
}
|
2021-05-23 19:55:01 +00:00
|
|
|
session.sendDownstreamPacket(packet);
|
2021-01-08 00:40:34 +00:00
|
|
|
packet = null;
|
|
|
|
lastBookUpdate = System.currentTimeMillis();
|
|
|
|
}
|
|
|
|
}
|