2019-07-29 22:20:48 +00:00
|
|
|
/*
|
2020-01-09 03:05:42 +00:00
|
|
|
* Copyright (c) 2019-2020 GeyserMC. http://geysermc.org
|
2019-07-29 22:20:48 +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
|
|
|
|
*/
|
|
|
|
|
2019-07-23 23:16:25 +00:00
|
|
|
package org.geysermc.connector.network.session.cache;
|
|
|
|
|
|
|
|
import com.nukkitx.protocol.bedrock.packet.ModalFormRequestPacket;
|
2020-10-29 23:58:17 +00:00
|
|
|
import com.nukkitx.protocol.bedrock.packet.ModalFormResponsePacket;
|
2020-11-26 22:00:43 +00:00
|
|
|
import com.nukkitx.protocol.bedrock.packet.NetworkStackLatencyPacket;
|
2019-11-30 19:26:51 +00:00
|
|
|
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
|
|
|
|
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
|
2020-10-29 23:58:17 +00:00
|
|
|
import lombok.RequiredArgsConstructor;
|
2021-06-27 23:57:53 +00:00
|
|
|
import org.geysermc.connector.GeyserConnector;
|
2019-12-21 17:35:48 +00:00
|
|
|
import org.geysermc.connector.network.session.GeyserSession;
|
2020-12-10 21:57:48 +00:00
|
|
|
import org.geysermc.cumulus.Form;
|
2021-06-05 22:53:58 +00:00
|
|
|
import org.geysermc.cumulus.SimpleForm;
|
2019-07-23 23:16:25 +00:00
|
|
|
|
2020-11-26 22:00:43 +00:00
|
|
|
import java.util.concurrent.TimeUnit;
|
2020-10-29 23:58:17 +00:00
|
|
|
import java.util.concurrent.atomic.AtomicInteger;
|
|
|
|
import java.util.function.Consumer;
|
2019-07-23 23:16:25 +00:00
|
|
|
|
2020-10-29 23:58:17 +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<>();
|
2020-10-29 23:58:17 +00:00
|
|
|
private final GeyserSession session;
|
2019-07-29 22:20:48 +00:00
|
|
|
|
2020-12-10 21:57:48 +00:00
|
|
|
public int addForm(Form form) {
|
2020-10-29 23:58:17 +00:00
|
|
|
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) {
|
2020-10-29 23:58:17 +00:00
|
|
|
int windowId = addForm(form);
|
2019-07-23 23:16:25 +00:00
|
|
|
|
2020-10-29 23:58:17 +00:00
|
|
|
ModalFormRequestPacket formRequestPacket = new ModalFormRequestPacket();
|
|
|
|
formRequestPacket.setFormId(windowId);
|
|
|
|
formRequestPacket.setFormData(form.getJsonData());
|
|
|
|
session.sendUpstreamPacket(formRequestPacket);
|
2020-11-26 22:00:43 +00:00
|
|
|
|
2021-06-05 22:53:58 +00:00
|
|
|
// Hack to fix the (url) image loading bug
|
|
|
|
if (form instanceof SimpleForm) {
|
|
|
|
NetworkStackLatencyPacket latencyPacket = new NetworkStackLatencyPacket();
|
|
|
|
latencyPacket.setFromServer(true);
|
|
|
|
latencyPacket.setTimestamp(-System.currentTimeMillis());
|
2021-11-13 16:03:55 +00:00
|
|
|
session.scheduleInEventLoop(() -> session.sendUpstreamPacket(latencyPacket),
|
2021-06-05 22:53:58 +00:00
|
|
|
500, TimeUnit.MILLISECONDS);
|
|
|
|
}
|
2020-11-26 22:00:43 +00:00
|
|
|
|
2020-10-29 23:58:17 +00:00
|
|
|
return windowId;
|
2019-07-23 23:16:25 +00:00
|
|
|
}
|
|
|
|
|
2020-10-29 23:58:17 +00:00
|
|
|
public void handleResponse(ModalFormResponsePacket response) {
|
2021-06-27 23:57:53 +00:00
|
|
|
Form form = forms.remove(response.getFormId());
|
2020-10-29 23:58:17 +00:00
|
|
|
if (form == null) {
|
2019-07-23 23:16:25 +00:00
|
|
|
return;
|
2020-10-29 23:58:17 +00:00
|
|
|
}
|
2019-07-23 23:16:25 +00:00
|
|
|
|
2020-10-29 23:58:17 +00:00
|
|
|
Consumer<String> responseConsumer = form.getResponseHandler();
|
|
|
|
if (responseConsumer != null) {
|
2021-06-27 23:57:53 +00:00
|
|
|
try {
|
|
|
|
responseConsumer.accept(response.getFormData());
|
|
|
|
} catch (Exception e) {
|
|
|
|
GeyserConnector.getInstance().getLogger().error("Error while processing form response!", e);
|
|
|
|
}
|
2020-10-29 23:58:17 +00:00
|
|
|
}
|
2019-07-23 23:16:25 +00:00
|
|
|
}
|
|
|
|
|
2020-10-29 23:58:17 +00:00
|
|
|
public boolean removeWindow(int id) {
|
|
|
|
return forms.remove(id) != null;
|
2019-07-23 23:16:25 +00:00
|
|
|
}
|
|
|
|
}
|