Update Upstream

0036e06 Alter the CUI lifecycle to be more consistent and reliable (1633)
This commit is contained in:
NotMyFault
2021-06-05 11:27:27 +02:00
parent d1af6c38e7
commit 75fbe654ee
17 changed files with 326 additions and 118 deletions

View File

@ -107,12 +107,17 @@ import static com.google.common.base.Preconditions.checkNotNull;
*/
public class LocalSession implements TextureHolder {
private static final transient int CUI_VERSION_UNINITIALIZED = -1;
public static transient int MAX_HISTORY_SIZE = 15;
// Non-session related fields
private transient LocalConfiguration config;
private final transient AtomicBoolean dirty = new AtomicBoolean();
// Single-connection lifetime fields
private transient int failedCuiAttempts = 0;
private transient boolean hasCUISupport = false;
private transient int cuiVersion = CUI_VERSION_UNINITIALIZED;
// Session related
private transient RegionSelector selector = new CuboidRegionSelector();
@ -145,8 +150,6 @@ public class LocalSession implements TextureHolder {
private transient boolean useInventory;
private transient com.sk89q.worldedit.world.snapshot.Snapshot snapshot;
private transient Snapshot snapshotExperimental;
private transient boolean hasCUISupport = false;
private transient int cuiVersion = -1;
private transient SideEffectSet sideEffectSet = SideEffectSet.defaults();
private transient Mask mask;
private transient Mask sourceMask;
@ -1398,7 +1401,12 @@ public class LocalSession implements TextureHolder {
*/
public void handleCUIInitializationMessage(String text, Actor actor) {
checkNotNull(text);
if (this.hasCUISupport || this.failedCuiAttempts > 3) {
if (this.hasCUISupport) {
// WECUI is a bit aggressive about re-initializing itself
// the last attempt to touch handshakes didn't go well, so this will do... for now
dispatchCUISelection(actor);
return;
} else if (this.failedCuiAttempts > 3) {
return;
}
@ -1456,6 +1464,10 @@ public class LocalSession implements TextureHolder {
* @param cuiVersion the CUI version
*/
public void setCUIVersion(int cuiVersion) {
if (cuiVersion < 0) {
throw new IllegalArgumentException("CUI protocol version must be non-negative, but '" + cuiVersion + "' was received.");
}
this.cuiVersion = cuiVersion;
}
@ -1687,4 +1699,15 @@ public class LocalSession implements TextureHolder {
this.transform = transform;
}
/**
* Call when this session has become inactive.
*
* <p>This is for internal use only.</p>
*/
public void onIdle() {
this.cuiVersion = CUI_VERSION_UNINITIALIZED;
this.hasCUISupport = false;
this.failedCuiAttempts = 0;
}
}

View File

@ -0,0 +1,45 @@
/*
* WorldEdit, a Minecraft world manipulation toolkit
* Copyright (C) sk89q <http://www.sk89q.com>
* Copyright (C) WorldEdit team and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit.event.platform;
import com.sk89q.worldedit.event.Event;
import com.sk89q.worldedit.session.SessionKey;
/**
* An event fired when a session becomes idle.
*
* <p>This can happen when a player leaves the server.</p>
*/
public final class SessionIdleEvent extends Event {
private final SessionKey key;
public SessionIdleEvent(SessionKey key) {
this.key = key;
}
/**
* Get a key identifying the session that has become idle.
*
* @return the key for the session
*/
public SessionKey getKey() {
return this.key;
}
}

View File

@ -30,6 +30,7 @@ import com.sk89q.worldedit.command.tool.InvalidToolBindException;
import com.sk89q.worldedit.command.tool.Tool;
import com.sk89q.worldedit.entity.Player;
import com.sk89q.worldedit.event.platform.ConfigurationLoadEvent;
import com.sk89q.worldedit.event.platform.SessionIdleEvent;
import com.sk89q.worldedit.extension.platform.Locatable;
import com.sk89q.worldedit.internal.util.LogManagerCompat;
import com.sk89q.worldedit.session.request.Request;
@ -132,6 +133,9 @@ public class SessionManager {
checkNotNull(owner);
SessionHolder stored = sessions.get(getKey(owner));
if (stored != null) {
if (stored.sessionIdle && stored.key.isActive()) {
stored.sessionIdle = false;
}
return stored.session;
} else {
return null;
@ -350,6 +354,18 @@ public class SessionManager {
store = new JsonFileSessionStore(dir);
}
@Subscribe
public void onSessionIdle(final SessionIdleEvent event) {
SessionHolder holder = this.sessions.get(getKey(event.getKey()));
if (holder != null && !holder.sessionIdle) {
holder.sessionIdle = true;
LocalSession session = holder.session;
// Perform any session cleanup for data that should not be persisted.
session.onIdle();
}
}
/**
* Stores the owner of a session, the session, and the last active time.
*/
@ -357,6 +373,7 @@ public class SessionManager {
private final SessionKey key;
private final LocalSession session;
private long lastActive = System.currentTimeMillis();
private boolean sessionIdle = false;
private SessionHolder(SessionKey key, LocalSession session) {
this.key = key;