Geyser/connector/src/main/java/org/geysermc/connector/network/session/cache/FormCache.java

90 lines
3.4 KiB
Java
Raw Normal View History

/*
* Copyright (c) 2019-2020 GeyserMC. http://geysermc.org
*
* 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
*/
2019-07-23 23:16:25 +00:00
package org.geysermc.connector.network.session.cache;
import com.nukkitx.protocol.bedrock.packet.ModalFormRequestPacket;
import com.nukkitx.protocol.bedrock.packet.ModalFormResponsePacket;
import com.nukkitx.protocol.bedrock.packet.NetworkStackLatencyPacket;
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
import lombok.RequiredArgsConstructor;
import org.geysermc.connector.network.session.GeyserSession;
2020-12-10 21:57:48 +00:00
import org.geysermc.cumulus.Form;
2019-07-23 23:16:25 +00:00
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Consumer;
2019-07-23 23:16:25 +00:00
@RequiredArgsConstructor
public class FormCache {
private final AtomicInteger formId = new AtomicInteger(0);
2020-12-10 21:57:48 +00:00
private final Int2ObjectMap<Form> forms = new Int2ObjectOpenHashMap<>();
private final GeyserSession session;
2020-12-10 21:57:48 +00:00
public int addForm(Form form) {
int windowId = formId.getAndIncrement();
forms.put(windowId, form);
return windowId;
2019-07-23 23:16:25 +00:00
}
2020-12-10 21:57:48 +00:00
public int showForm(Form form) {
int windowId = addForm(form);
2019-07-23 23:16:25 +00:00
ModalFormRequestPacket formRequestPacket = new ModalFormRequestPacket();
formRequestPacket.setFormId(windowId);
formRequestPacket.setFormData(form.getJsonData());
session.sendUpstreamPacket(formRequestPacket);
// Hack to fix the url image loading bug
NetworkStackLatencyPacket latencyPacket = new NetworkStackLatencyPacket();
latencyPacket.setFromServer(true);
latencyPacket.setTimestamp(-System.currentTimeMillis());
2020-12-10 21:57:48 +00:00
session.getConnector().getGeneralThreadPool().schedule(
() -> session.sendUpstreamPacket(latencyPacket),
500, TimeUnit.MILLISECONDS);
return windowId;
2019-07-23 23:16:25 +00:00
}
public void handleResponse(ModalFormResponsePacket response) {
2020-12-10 21:57:48 +00:00
Form form = forms.get(response.getFormId());
if (form == null) {
2019-07-23 23:16:25 +00:00
return;
}
2019-07-23 23:16:25 +00:00
Consumer<String> responseConsumer = form.getResponseHandler();
if (responseConsumer != null) {
2020-12-12 00:39:09 +00:00
responseConsumer.accept(response.getFormData());
}
2019-07-23 23:16:25 +00:00
removeWindow(response.getFormId());
2019-07-23 23:16:25 +00:00
}
public boolean removeWindow(int id) {
return forms.remove(id) != null;
2019-07-23 23:16:25 +00:00
}
}