Always create ProtocolInfo, store mappings as int[]

This commit is contained in:
KennyTV 2021-04-27 10:29:30 +02:00
parent 30d122e7fa
commit d4bc31d11e
No known key found for this signature in database
GPG Key ID: 6BE3B555EBC5982B
11 changed files with 43 additions and 54 deletions

View File

@ -44,6 +44,8 @@ public interface ConnectionManager {
/**
* Frontend connections will have the UUID stored. Override this if your platform isn't always frontend.
* UUIDs can't be duplicate between frontend connections.
*
* @return true if the user is a frontend connection
*/
boolean isFrontEnd(UserConnection connection);
@ -55,6 +57,8 @@ public interface ConnectionManager {
* <p>
* Note that connections are removed as soon as their channel is closed,
* so avoid using this method during player quits for example.
*
* @return frontend UserConnection of the player connected to this proxy server
*/
@Nullable UserConnection getConnectedClient(UUID clientIdentifier);
@ -66,6 +70,8 @@ public interface ConnectionManager {
* <p>
* Note that connections are removed as soon as their channel is closed,
* so avoid using this method during player quits for example.
*
* @return UUID of the frontend connection to this proxy server
*/
@Nullable UUID getConnectedClientId(UserConnection connection);
@ -75,6 +81,8 @@ public interface ConnectionManager {
* May contain frontend, backend and/or client-sided connections.
* When ViaVersion is reloaded, this method may not return some players.
* May not contain ProtocolSupport players.
*
* @return connected UserConnections
*/
Set<UserConnection> getConnections();
@ -83,6 +91,8 @@ public interface ConnectionManager {
* Returns empty list when there isn't a server
* When ViaVersion is reloaded, this method may not return some players.
* May not contain ProtocolSupport players.
*
* @return map containing the UUIDs and frontend UserConnections from players connected to this proxy server
*/
Map<UUID, UserConnection> getConnectedClients();

View File

@ -186,9 +186,7 @@ public interface UserConnection {
*
* @return info containing the current protocol state and userdata
*/
@Nullable ProtocolInfo getProtocolInfo();
void setProtocolInfo(@Nullable ProtocolInfo protocolInfo);
ProtocolInfo getProtocolInfo();
/**
* Returns a map of stored objects.

View File

@ -139,11 +139,11 @@ public class MappingDataLoader {
}
}
public static void mapIdentifiers(short[] output, JsonObject oldIdentifiers, JsonObject newIdentifiers) {
public static void mapIdentifiers(int[] output, JsonObject oldIdentifiers, JsonObject newIdentifiers) {
MappingDataLoader.mapIdentifiers(output, oldIdentifiers, newIdentifiers, null);
}
public static void mapIdentifiers(short[] output, JsonObject oldIdentifiers, JsonObject newIdentifiers, @Nullable JsonObject diffIdentifiers) {
public static void mapIdentifiers(int[] output, JsonObject oldIdentifiers, JsonObject newIdentifiers, @Nullable JsonObject diffIdentifiers) {
Object2IntMap newIdentifierMap = MappingDataLoader.indexedObjectToMap(newIdentifiers);
for (Map.Entry<String, JsonElement> entry : oldIdentifiers.entrySet()) {
int value = mapIdentifierEntry(entry, newIdentifierMap, diffIdentifiers);
@ -173,11 +173,11 @@ public class MappingDataLoader {
return value;
}
public static void mapIdentifiers(short[] output, JsonArray oldIdentifiers, JsonArray newIdentifiers, boolean warnOnMissing) {
public static void mapIdentifiers(int[] output, JsonArray oldIdentifiers, JsonArray newIdentifiers, boolean warnOnMissing) {
mapIdentifiers(output, oldIdentifiers, newIdentifiers, null, warnOnMissing);
}
public static void mapIdentifiers(short[] output, JsonArray oldIdentifiers, JsonArray newIdentifiers, @Nullable JsonObject diffIdentifiers, boolean warnOnMissing) {
public static void mapIdentifiers(int[] output, JsonArray oldIdentifiers, JsonArray newIdentifiers, @Nullable JsonObject diffIdentifiers, boolean warnOnMissing) {
Object2IntMap<String> newIdentifierMap = MappingDataLoader.arrayToMap(newIdentifiers);
for (int i = 0; i < oldIdentifiers.size(); i++) {
JsonElement oldIdentifier = oldIdentifiers.get(i);

View File

@ -29,9 +29,9 @@ import org.checkerframework.checker.nullness.qual.Nullable;
import java.util.Arrays;
public class Mappings {
protected final short[] oldToNew;
protected final int[] oldToNew;
public Mappings(short[] oldToNew) {
public Mappings(int[] oldToNew) {
this.oldToNew = oldToNew;
}
@ -45,8 +45,8 @@ public class Mappings {
* @param diffMapping extra mappings that will be used/scanned when an entry cannot be found
*/
public Mappings(int size, JsonObject oldMapping, JsonObject newMapping, @Nullable JsonObject diffMapping) {
oldToNew = new short[size];
Arrays.fill(oldToNew, (short) -1);
oldToNew = new int[size];
Arrays.fill(oldToNew, -1);
MappingDataLoader.mapIdentifiers(oldToNew, oldMapping, newMapping, diffMapping);
}
@ -62,8 +62,8 @@ public class Mappings {
* @param newMapping mappings to map to
*/
public Mappings(int size, JsonObject oldMapping, JsonObject newMapping) {
oldToNew = new short[size];
Arrays.fill(oldToNew, (short) -1);
oldToNew = new int[size];
Arrays.fill(oldToNew, -1);
MappingDataLoader.mapIdentifiers(oldToNew, oldMapping, newMapping);
}
@ -81,8 +81,8 @@ public class Mappings {
* @param warnOnMissing should "No key for x" be printed if there is no matching identifier
*/
public Mappings(int size, JsonArray oldMapping, JsonArray newMapping, JsonObject diffMapping, boolean warnOnMissing) {
oldToNew = new short[size];
Arrays.fill(oldToNew, (short) -1);
oldToNew = new int[size];
Arrays.fill(oldToNew, -1);
MappingDataLoader.mapIdentifiers(oldToNew, oldMapping, newMapping, diffMapping, warnOnMissing);
}
@ -110,7 +110,7 @@ public class Mappings {
return old >= 0 && old < oldToNew.length ? oldToNew[old] : -1;
}
public short[] getOldToNew() {
public int[] getOldToNew() {
return oldToNew;
}
}

View File

@ -76,6 +76,11 @@ public interface ProtocolPipeline extends SimpleProtocol {
*/
boolean filter(Object o, List list) throws Exception;
/**
* Returns the list of protocols this pipeline contains.
*
* @return list of protocols in this pipe
*/
List<Protocol> pipes();
/**

View File

@ -22,8 +22,6 @@
*/
package com.viaversion.viaversion.api.protocol.packet;
import com.viaversion.viaversion.api.protocol.packet.PacketType;
/**
* Interface to be implemented by server outgoing packet type enums,
* representing PLAY state packets, ordered by their packet id.

View File

@ -22,8 +22,6 @@
*/
package com.viaversion.viaversion.api.protocol.packet;
import com.viaversion.viaversion.api.protocol.packet.PacketType;
/**
* Interface to be implemented by server incoming packet type enums,
* representing PLAY state packets, ordered by their packet id.

View File

@ -230,7 +230,6 @@ public class BungeeServerHandler implements Listener {
plMsg.setTag(channel);
}
user.setProtocolInfo(info);
user.put(storage);
user.setActive(protocolPath != null);

View File

@ -22,7 +22,6 @@
*/
package com.viaversion.viaversion.connection;
import com.google.common.base.Preconditions;
import com.google.common.cache.CacheBuilder;
import com.viaversion.viaversion.api.Via;
import com.viaversion.viaversion.api.connection.ProtocolInfo;
@ -58,9 +57,9 @@ public class UserConnectionImpl implements UserConnection {
private final Set<UUID> passthroughTokens = Collections.newSetFromMap(CacheBuilder.newBuilder()
.expireAfterWrite(10, TimeUnit.SECONDS)
.<UUID, Boolean>build().asMap());
private final ProtocolInfo protocolInfo = new ProtocolInfoImpl(this);
private final Channel channel;
private final boolean clientSide;
private ProtocolInfo protocolInfo;
private boolean active = true;
private boolean pendingDisconnect;
@ -73,6 +72,7 @@ public class UserConnectionImpl implements UserConnection {
public UserConnectionImpl(@Nullable Channel channel, boolean clientSide) {
this.channel = channel;
this.clientSide = clientSide;
storedObjects.put(ProtocolInfo.class, (StoredObject) protocolInfo);
}
/**
@ -313,21 +313,10 @@ public class UserConnectionImpl implements UserConnection {
}
@Override
public @Nullable ProtocolInfo getProtocolInfo() {
public ProtocolInfo getProtocolInfo() {
return protocolInfo;
}
@Override
public void setProtocolInfo(@Nullable ProtocolInfo protocolInfo) {
Preconditions.checkArgument(protocolInfo instanceof StoredObject, "ProtocolInfo has to extend StoredObject!");
this.protocolInfo = protocolInfo;
if (protocolInfo != null) {
storedObjects.put(ProtocolInfo.class, (StoredObject) protocolInfo);
} else {
storedObjects.remove(ProtocolInfo.class);
}
}
@Override
public Map<Class<?>, StoredObject> getStoredObjects() {
return storedObjects;

View File

@ -24,7 +24,6 @@ package com.viaversion.viaversion.protocol;
import com.google.common.base.Preconditions;
import com.viaversion.viaversion.api.Via;
import com.viaversion.viaversion.api.connection.ProtocolInfo;
import com.viaversion.viaversion.api.connection.UserConnection;
import com.viaversion.viaversion.api.platform.ViaPlatform;
import com.viaversion.viaversion.api.protocol.AbstractSimpleProtocol;
@ -33,7 +32,6 @@ import com.viaversion.viaversion.api.protocol.base.Protocol;
import com.viaversion.viaversion.api.protocol.packet.Direction;
import com.viaversion.viaversion.api.protocol.packet.PacketWrapper;
import com.viaversion.viaversion.api.protocol.packet.State;
import com.viaversion.viaversion.connection.ProtocolInfoImpl;
import org.checkerframework.checker.nullness.qual.Nullable;
import java.util.ArrayList;
@ -42,14 +40,15 @@ import java.util.concurrent.CopyOnWriteArrayList;
import java.util.logging.Level;
public class ProtocolPipelineImpl extends AbstractSimpleProtocol implements ProtocolPipeline {
private final UserConnection userConnection;
/**
* Protocol list ordered from client to server transforation with the base protocols at the end.
*/
private List<Protocol> protocolList;
private UserConnection userConnection;
public ProtocolPipelineImpl(UserConnection userConnection) {
init(userConnection);
this.userConnection = userConnection;
userConnection.getProtocolInfo().setPipeline(this);
}
@Override
@ -61,17 +60,7 @@ public class ProtocolPipelineImpl extends AbstractSimpleProtocol implements Prot
@Override
public void init(UserConnection userConnection) {
this.userConnection = userConnection;
ProtocolInfo protocolInfo = new ProtocolInfoImpl(userConnection);
protocolInfo.setPipeline(this);
userConnection.setProtocolInfo(protocolInfo);
/* Init through all our pipes */
for (Protocol protocol : protocolList) {
protocol.init(userConnection);
}
throw new UnsupportedOperationException("ProtocolPipeline can only be initialized once");
}
@Override
@ -155,7 +144,9 @@ public class ProtocolPipelineImpl extends AbstractSimpleProtocol implements Prot
@Override
public boolean contains(Class<? extends Protocol> pipeClass) {
for (Protocol protocol : protocolList) {
if (protocol.getClass().equals(pipeClass)) return true;
if (protocol.getClass() == pipeClass) {
return true;
}
}
return false;
}
@ -163,7 +154,9 @@ public class ProtocolPipelineImpl extends AbstractSimpleProtocol implements Prot
@Override
public @Nullable <P extends Protocol> P getProtocol(Class<P> pipeClass) {
for (Protocol protocol : protocolList) {
if (protocol.getClass() == pipeClass) return (P) protocol;
if (protocol.getClass() == pipeClass) {
return (P) protocol;
}
}
return null;
}
@ -187,7 +180,6 @@ public class ProtocolPipelineImpl extends AbstractSimpleProtocol implements Prot
@Override
public void cleanPipes() {
pipes().clear();
registerPackets();
}
}

View File

@ -67,7 +67,7 @@ public class MappingData extends com.viaversion.viaversion.api.data.MappingData
// Remap infested blocks, as they are instantly breakabale in 1.13+ and can't be broken by those clients on older servers
if (Via.getConfig().isInfestedBlocksFix()) {
short[] oldToNew = blockMappings.getOldToNew();
int[] oldToNew = blockMappings.getOldToNew();
oldToNew[1552] = 1; // stone
oldToNew[1553] = 14; // cobblestone
oldToNew[1554] = 3983; // stone bricks