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;
|
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;
|
|
|
|
import org.geysermc.common.form.Form;
|
2019-12-21 17:35:48 +00:00
|
|
|
import org.geysermc.connector.network.session.GeyserSession;
|
2019-07-23 23:16:25 +00:00
|
|
|
|
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);
|
|
|
|
private final Int2ObjectMap<Form> forms = new Int2ObjectOpenHashMap<>();
|
|
|
|
private final GeyserSession session;
|
2019-07-29 22:20:48 +00:00
|
|
|
|
2020-10-29 23:58:17 +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-10-29 23:58:17 +00:00
|
|
|
public int showForm(Form form) {
|
|
|
|
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());
|
2019-07-23 23:16:25 +00:00
|
|
|
|
2020-10-29 23:58:17 +00:00
|
|
|
session.sendUpstreamPacket(formRequestPacket);
|
|
|
|
return windowId;
|
2019-07-23 23:16:25 +00:00
|
|
|
}
|
|
|
|
|
2020-10-29 23:58:17 +00:00
|
|
|
public void handleResponse(ModalFormResponsePacket response) {
|
|
|
|
Form form = forms.get(response.getFormId());
|
|
|
|
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) {
|
|
|
|
responseConsumer.accept(response.getFormData().trim());
|
|
|
|
}
|
2019-07-23 23:16:25 +00:00
|
|
|
|
2020-10-29 23:58:17 +00:00
|
|
|
removeWindow(response.getFormId());
|
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
|
|
|
}
|
|
|
|
}
|