mirror of
https://github.com/GeyserMC/Geyser.git
synced 2024-08-14 23:57:35 +00:00
Against possible leaks memory
This commit is contained in:
parent
a9973e7c1f
commit
be4a8e6906
1 changed files with 19 additions and 3 deletions
|
|
@ -25,18 +25,22 @@
|
|||
|
||||
package org.geysermc.geyser.session.cache;
|
||||
|
||||
import org.cloudburstmc.protocol.bedrock.packet.ModalFormRequestPacket;
|
||||
import org.cloudburstmc.protocol.bedrock.packet.ModalFormResponsePacket;
|
||||
import org.cloudburstmc.protocol.bedrock.packet.NetworkStackLatencyPacket;
|
||||
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
|
||||
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.cloudburstmc.protocol.bedrock.data.ModalFormCancelReason;
|
||||
import org.cloudburstmc.protocol.bedrock.packet.ModalFormRequestPacket;
|
||||
import org.cloudburstmc.protocol.bedrock.packet.ModalFormResponsePacket;
|
||||
import org.cloudburstmc.protocol.bedrock.packet.NetworkStackLatencyPacket;
|
||||
import org.geysermc.cumulus.form.Form;
|
||||
import org.geysermc.cumulus.form.SimpleForm;
|
||||
import org.geysermc.cumulus.form.impl.FormDefinitions;
|
||||
import org.geysermc.geyser.GeyserImpl;
|
||||
import org.geysermc.geyser.session.GeyserSession;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
|
|
@ -52,11 +56,22 @@ public class FormCache {
|
|||
private final FormDefinitions formDefinitions = FormDefinitions.instance();
|
||||
private final AtomicInteger formIdCounter = new AtomicInteger(0);
|
||||
private final Int2ObjectMap<Form> forms = new Int2ObjectOpenHashMap<>();
|
||||
List<Integer> formsOrderList = new ArrayList<>();
|
||||
private final GeyserSession session;
|
||||
|
||||
public int addForm(Form form) {
|
||||
int formId = formIdCounter.getAndIncrement();
|
||||
forms.put(formId, form);
|
||||
formsOrderList.add(formId);
|
||||
|
||||
if (formsOrderList.size() > 50) {
|
||||
int removeFormId = formsOrderList.getFirst();
|
||||
ModalFormResponsePacket packet = new ModalFormResponsePacket();
|
||||
packet.setFormId(removeFormId);
|
||||
packet.setCancelReason(Optional.of(ModalFormCancelReason.USER_CLOSED));
|
||||
packet.setFormData(null);
|
||||
this.handleResponse(packet);
|
||||
}
|
||||
return formId;
|
||||
}
|
||||
|
||||
|
|
@ -96,6 +111,7 @@ public class FormCache {
|
|||
|
||||
public void handleResponse(ModalFormResponsePacket response) {
|
||||
Form form = forms.remove(response.getFormId());
|
||||
formsOrderList.remove((Integer) response.getFormId());
|
||||
if (form == null) {
|
||||
return;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue