From 409cf3407c2a28226936ccec4289314c821cb9fe Mon Sep 17 00:00:00 2001 From: daoge_cmd <3523206925@qq.com> Date: Tue, 1 Oct 2024 21:30:59 +0800 Subject: [PATCH] fix: fix issue#419 --- .../player/EntityPlayerBaseComponent.java | 29 +++++-------------- .../player/EntityPlayerBaseComponentImpl.java | 23 +++++++-------- .../ModalFormResponsePacketProcessor.java | 5 ++-- .../ServerSettingsRequestProcessor.java | 11 ++++--- .../server/plugin/AllayPluginManager.java | 6 ++-- 5 files changed, 30 insertions(+), 44 deletions(-) diff --git a/api/src/main/java/org/allaymc/api/entity/component/player/EntityPlayerBaseComponent.java b/api/src/main/java/org/allaymc/api/entity/component/player/EntityPlayerBaseComponent.java index 33bdb90b2..cf3743f37 100644 --- a/api/src/main/java/org/allaymc/api/entity/component/player/EntityPlayerBaseComponent.java +++ b/api/src/main/java/org/allaymc/api/entity/component/player/EntityPlayerBaseComponent.java @@ -1,5 +1,6 @@ package org.allaymc.api.entity.component.player; +import it.unimi.dsi.fastutil.Pair; import org.allaymc.api.client.data.Abilities; import org.allaymc.api.client.data.AdventureSettings; import org.allaymc.api.client.skin.Skin; @@ -338,37 +339,23 @@ default void setFlying(boolean flying) { Form removeForm(int id); /** - * Get the server setting forms of the player. + * Get the server setting form and its id. * - * @return The server setting forms of the player. + * @return The server setting form and its id. */ - @UnmodifiableView - Map getServerSettingForms(); + Pair getServerSettingForm(); /** - * Add a server setting form to the player. + * Set a server setting form to the player. * * @param form The form to add. */ - void addServerSettingForm(CustomForm form); - - /** - * Get a server setting form by its ID. - * - * @param id The ID of the form. - * - * @return The form. - */ - CustomForm getServerSettingForm(int id); + void setServerSettingForm(CustomForm form); /** - * Remove a server setting form by its ID. - * - * @param id The ID of the form. - * - * @return The removed form. + * Remove the server setting form. */ - CustomForm removeServerSettingForm(int id); + void removeServerSettingForm(); /** * Show a form to the player. diff --git a/server/src/main/java/org/allaymc/server/entity/component/player/EntityPlayerBaseComponentImpl.java b/server/src/main/java/org/allaymc/server/entity/component/player/EntityPlayerBaseComponentImpl.java index 860f7cf91..f524f28f2 100644 --- a/server/src/main/java/org/allaymc/server/entity/component/player/EntityPlayerBaseComponentImpl.java +++ b/server/src/main/java/org/allaymc/server/entity/component/player/EntityPlayerBaseComponentImpl.java @@ -1,6 +1,7 @@ package org.allaymc.server.entity.component.player; import com.google.common.base.Preconditions; +import it.unimi.dsi.fastutil.Pair; import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap; import lombok.Getter; import lombok.Setter; @@ -119,7 +120,8 @@ public class EntityPlayerBaseComponentImpl extends EntityBaseComponentImpl imple protected long startUsingItemInAirTime = -1; protected AtomicInteger formIdCounter = new AtomicInteger(0); protected Map forms = new Int2ObjectOpenHashMap<>(); - protected Map serverSettingForms = new Int2ObjectOpenHashMap<>(); + protected CustomForm serverSettingForm; + protected int serverSettingFormId = -1; @Getter protected float movementSpeed = DEFAULT_MOVEMENT_SPEED; @ComponentedObject @@ -688,23 +690,20 @@ public Form removeForm(int id) { } @Override - public void addServerSettingForm(CustomForm form) { - serverSettingForms.put(assignFormId(), form); + public void setServerSettingForm(CustomForm form) { + serverSettingFormId = assignFormId(); + serverSettingForm = form; } @Override - public Map getServerSettingForms() { - return Collections.unmodifiableMap(serverSettingForms); + public Pair getServerSettingForm() { + return Pair.of(serverSettingFormId, serverSettingForm); } @Override - public CustomForm getServerSettingForm(int id) { - return serverSettingForms.get(id); - } - - @Override - public CustomForm removeServerSettingForm(int id) { - return serverSettingForms.remove(id); + public void removeServerSettingForm() { + serverSettingForm = null; + serverSettingFormId = -1; } @Override diff --git a/server/src/main/java/org/allaymc/server/network/processor/ModalFormResponsePacketProcessor.java b/server/src/main/java/org/allaymc/server/network/processor/ModalFormResponsePacketProcessor.java index 56ae329be..221ec5f7a 100644 --- a/server/src/main/java/org/allaymc/server/network/processor/ModalFormResponsePacketProcessor.java +++ b/server/src/main/java/org/allaymc/server/network/processor/ModalFormResponsePacketProcessor.java @@ -18,12 +18,13 @@ public void handleSync(EntityPlayer player, ModalFormResponsePacket packet, long var form = player.removeForm(id); var isServerSettingsForm = false; if (form == null) { - form = player.getServerSettingForm(id); - if (form == null) { + var serverSettingForm = player.getServerSettingForm(); + if (id != serverSettingForm.left()) { log.warn("Received response for unknown form from player {}: {}", player.getOriginName(), packet.getFormId()); return; } + form = serverSettingForm.right(); isServerSettingsForm = true; } diff --git a/server/src/main/java/org/allaymc/server/network/processor/ServerSettingsRequestProcessor.java b/server/src/main/java/org/allaymc/server/network/processor/ServerSettingsRequestProcessor.java index e6c98940a..eaf12dfb4 100644 --- a/server/src/main/java/org/allaymc/server/network/processor/ServerSettingsRequestProcessor.java +++ b/server/src/main/java/org/allaymc/server/network/processor/ServerSettingsRequestProcessor.java @@ -16,12 +16,11 @@ public class ServerSettingsRequestProcessor extends PacketProcessor { - player.getServerSettingForms().forEach((id, form) -> { - var pk = new ServerSettingsResponsePacket(); - pk.setFormId(id); - pk.setFormData(form.toJson()); - player.sendPacket(pk); - }); + var serverSettingForm = player.getServerSettingForm(); + var pk = new ServerSettingsResponsePacket(); + pk.setFormId(serverSettingForm.left()); + pk.setFormData(serverSettingForm.right().toJson()); + player.sendPacket(pk); return true; }, 20); return PacketSignal.HANDLED; diff --git a/server/src/main/java/org/allaymc/server/plugin/AllayPluginManager.java b/server/src/main/java/org/allaymc/server/plugin/AllayPluginManager.java index 69ca3cf79..5ef767a39 100644 --- a/server/src/main/java/org/allaymc/server/plugin/AllayPluginManager.java +++ b/server/src/main/java/org/allaymc/server/plugin/AllayPluginManager.java @@ -78,11 +78,11 @@ protected void onLoad(Map descriptors, Map