Don't require UserConnection holding class in stored objects

This commit is contained in:
KennyTV 2021-05-26 21:35:28 +02:00
parent 7195319eb5
commit 703978e227
No known key found for this signature in database
GPG Key ID: 6BE3B555EBC5982B
25 changed files with 90 additions and 98 deletions

View File

@ -0,0 +1,32 @@
/*
* This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion
* Copyright (C) 2016-2021 ViaVersion and contributors
*
* 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.
*/
package com.viaversion.viaversion.api.connection;
/**
* Dummy interface used to explicitly identify objects storable in user connections.
*
* @see UserConnection#get(Class)
* @see UserConnection#put(StorableObject)
*/
public interface StorableObject {
}

View File

@ -22,7 +22,7 @@
*/
package com.viaversion.viaversion.api.connection;
public abstract class StoredObject {
public abstract class StoredObject implements StorableObject {
private final UserConnection user;
protected StoredObject(UserConnection user) {

View File

@ -48,7 +48,7 @@ public interface UserConnection {
* @param <T> The type of the class you want to get.
* @return The requested object
*/
@Nullable <T extends StoredObject> T get(Class<T> objectClass);
@Nullable <T extends StorableObject> T get(Class<T> objectClass);
/**
* Check if the storage has an object.
@ -56,14 +56,14 @@ public interface UserConnection {
* @param objectClass The object class to check
* @return True if the object is in the storage
*/
boolean has(Class<? extends StoredObject> objectClass);
boolean has(Class<? extends StorableObject> objectClass);
/**
* Put an object into the stored objects based on class.
*
* @param object The object to store.
*/
void put(StoredObject object);
void put(StorableObject object);
/**
* Returns a collection of entity trackers currently registered.
@ -266,9 +266,9 @@ public interface UserConnection {
* @return map of stored objects
* @see #has(Class)
* @see #get(Class)
* @see #put(StoredObject)
* @see #put(StorableObject)
*/
Map<Class<?>, StoredObject> getStoredObjects();
Map<Class<?>, StorableObject> getStoredObjects();
/**
* Returns whether the connection has protocols other than the base protocol applied.

View File

@ -70,7 +70,7 @@ public final class Metadata {
this.metaType = metaType;
}
public <T> T value() {
public @Nullable <T> T value() {
return (T) value;
}

View File

@ -47,6 +47,7 @@ public enum MetaType1_17 implements MetaType {
OPT_VAR_INT(17, Type.OPTIONAL_VAR_INT),
POSE(18, Type.VAR_INT);
private static final MetaType1_17[] VALUES = values();
private final int typeId;
private final Type type;
@ -56,7 +57,7 @@ public enum MetaType1_17 implements MetaType {
}
public static MetaType1_17 byId(int id) {
return values()[id];
return VALUES[id];
}
@Override

View File

@ -19,7 +19,7 @@ package com.viaversion.viaversion.bungee.handlers;
import com.viaversion.viaversion.api.Via;
import com.viaversion.viaversion.api.connection.ProtocolInfo;
import com.viaversion.viaversion.api.connection.StoredObject;
import com.viaversion.viaversion.api.connection.StorableObject;
import com.viaversion.viaversion.api.connection.UserConnection;
import com.viaversion.viaversion.api.data.entity.ClientEntityIdChangeListener;
import com.viaversion.viaversion.api.data.entity.EntityTracker;
@ -90,7 +90,7 @@ public class BungeeServerHandler implements Listener {
UserConnection user = Via.getManager().getConnectionManager().getConnectedClient(e.getPlayer().getUniqueId());
if (user == null) return;
if (!user.has(BungeeStorage.class)) {
user.put(new BungeeStorage(user, e.getPlayer()));
user.put(new BungeeStorage(e.getPlayer()));
}
int protocolId = ProtocolDetectorService.getProtocolId(e.getTarget().getName());
@ -132,7 +132,7 @@ public class BungeeServerHandler implements Listener {
}
// For ViaRewind
for (StoredObject object : userConnection.getStoredObjects().values()) {
for (StorableObject object : userConnection.getStoredObjects().values()) {
if (object instanceof ClientEntityIdChangeListener) {
((ClientEntityIdChangeListener) object).setClientEntityId(playerId);
}

View File

@ -17,8 +17,7 @@
*/
package com.viaversion.viaversion.bungee.storage;
import com.viaversion.viaversion.api.connection.StoredObject;
import com.viaversion.viaversion.api.connection.UserConnection;
import com.viaversion.viaversion.api.connection.StorableObject;
import net.md_5.bungee.api.connection.ProxiedPlayer;
import java.lang.reflect.Field;
@ -26,7 +25,7 @@ import java.util.Objects;
import java.util.Set;
import java.util.UUID;
public class BungeeStorage extends StoredObject {
public class BungeeStorage implements StorableObject {
private static Field bossField;
static {
@ -45,8 +44,7 @@ public class BungeeStorage extends StoredObject {
private String currentServer;
private Set<UUID> bossbar;
public BungeeStorage(UserConnection user, ProxiedPlayer player) {
super(user);
public BungeeStorage(ProxiedPlayer player) {
this.player = player;
this.currentServer = "";

View File

@ -20,7 +20,7 @@ package com.viaversion.viaversion.connection;
import com.google.common.cache.CacheBuilder;
import com.viaversion.viaversion.api.Via;
import com.viaversion.viaversion.api.connection.ProtocolInfo;
import com.viaversion.viaversion.api.connection.StoredObject;
import com.viaversion.viaversion.api.connection.StorableObject;
import com.viaversion.viaversion.api.connection.UserConnection;
import com.viaversion.viaversion.api.data.entity.EntityTracker;
import com.viaversion.viaversion.api.protocol.Protocol;
@ -51,7 +51,7 @@ import java.util.function.Function;
public class UserConnectionImpl implements UserConnection {
private static final AtomicLong IDS = new AtomicLong();
private final long id = IDS.incrementAndGet();
private final Map<Class<?>, StoredObject> storedObjects = new ConcurrentHashMap<>();
private final Map<Class<?>, StorableObject> storedObjects = new ConcurrentHashMap<>();
private final Map<Class<? extends Protocol>, EntityTracker> entityTrackers = new HashMap<>();
private final PacketTracker packetTracker = new PacketTracker(this);
private final Set<UUID> passthroughTokens = Collections.newSetFromMap(CacheBuilder.newBuilder()
@ -82,17 +82,17 @@ public class UserConnectionImpl implements UserConnection {
}
@Override
public @Nullable <T extends StoredObject> T get(Class<T> objectClass) {
public @Nullable <T extends StorableObject> T get(Class<T> objectClass) {
return (T) storedObjects.get(objectClass);
}
@Override
public boolean has(Class<? extends StoredObject> objectClass) {
public boolean has(Class<? extends StorableObject> objectClass) {
return storedObjects.containsKey(objectClass);
}
@Override
public void put(StoredObject object) {
public void put(StorableObject object) {
storedObjects.put(object.getClass(), object);
}
@ -306,7 +306,7 @@ public class UserConnectionImpl implements UserConnection {
}
@Override
public Map<Class<?>, StoredObject> getStoredObjects() {
public Map<Class<?>, StorableObject> getStoredObjects() {
return storedObjects;
}

View File

@ -194,6 +194,6 @@ public class Protocol1_10To1_9_3_4 extends AbstractProtocol<ClientboundPackets1_
@Override
public void init(UserConnection userConnection) {
userConnection.put(new ResourcePackTracker(userConnection));
userConnection.put(new ResourcePackTracker());
}
}

View File

@ -17,16 +17,11 @@
*/
package com.viaversion.viaversion.protocols.protocol1_10to1_9_3.storage;
import com.viaversion.viaversion.api.connection.StoredObject;
import com.viaversion.viaversion.api.connection.UserConnection;
import com.viaversion.viaversion.api.connection.StorableObject;
public class ResourcePackTracker extends StoredObject {
public class ResourcePackTracker implements StorableObject {
private String lastHash = "";
public ResourcePackTracker(UserConnection user) {
super(user);
}
public String getLastHash() {
return lastHash;
}

View File

@ -1047,13 +1047,13 @@ public class Protocol1_13To1_12_2 extends AbstractProtocol<ClientboundPackets1_1
@Override
public void init(UserConnection userConnection) {
userConnection.addEntityTracker(this.getClass(), new EntityTrackerBase(userConnection, Entity1_13Types.EntityType.PLAYER));
userConnection.put(new TabCompleteTracker(userConnection));
userConnection.put(new TabCompleteTracker());
if (!userConnection.has(ClientWorld.class))
userConnection.put(new ClientWorld(userConnection));
userConnection.put(new BlockStorage(userConnection));
userConnection.put(new BlockStorage());
if (Via.getConfig().isServersideBlockConnections()) {
if (Via.getManager().getProviders().get(BlockConnectionProvider.class) instanceof PacketBlockConnectionProvider) {
userConnection.put(new BlockConnectionStorage(userConnection));
userConnection.put(new BlockConnectionStorage());
}
}
}

View File

@ -28,7 +28,7 @@ public class TabCompleteThread implements Runnable {
if (info.getProtocolInfo() == null) continue;
if (info.getProtocolInfo().getPipeline().contains(Protocol1_13To1_12_2.class)) {
if (info.getChannel().isOpen()) {
info.get(TabCompleteTracker.class).sendPacketToServer();
info.get(TabCompleteTracker.class).sendPacketToServer(info);
}
}
}

View File

@ -18,8 +18,7 @@
package com.viaversion.viaversion.protocols.protocol1_13to1_12_2.storage;
import com.viaversion.viaversion.api.Via;
import com.viaversion.viaversion.api.connection.StoredObject;
import com.viaversion.viaversion.api.connection.UserConnection;
import com.viaversion.viaversion.api.connection.StorableObject;
import com.viaversion.viaversion.api.minecraft.Position;
import com.viaversion.viaversion.api.minecraft.chunks.NibbleArray;
import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.Protocol1_13To1_12_2;
@ -32,7 +31,7 @@ import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
public class BlockConnectionStorage extends StoredObject {
public class BlockConnectionStorage implements StorableObject {
private static final short[] REVERSE_BLOCK_MAPPINGS = new short[8582];
private static Constructor<?> fastUtilLongObjectHashMap;
@ -56,10 +55,6 @@ public class BlockConnectionStorage extends StoredObject {
}
}
public BlockConnectionStorage(UserConnection user) {
super(user);
}
public void store(int x, int y, int z, int blockState) {
short mapping = REVERSE_BLOCK_MAPPINGS[blockState];
if (mapping == -1) return;

View File

@ -17,8 +17,7 @@
*/
package com.viaversion.viaversion.protocols.protocol1_13to1_12_2.storage;
import com.viaversion.viaversion.api.connection.StoredObject;
import com.viaversion.viaversion.api.connection.UserConnection;
import com.viaversion.viaversion.api.connection.StorableObject;
import com.viaversion.viaversion.api.minecraft.Position;
import it.unimi.dsi.fastutil.ints.IntOpenHashSet;
import it.unimi.dsi.fastutil.ints.IntSet;
@ -26,7 +25,7 @@ import it.unimi.dsi.fastutil.ints.IntSet;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
public class BlockStorage extends StoredObject {
public class BlockStorage implements StorableObject {
private static final IntSet WHITELIST = new IntOpenHashSet(46, 1F);
private final Map<Position, ReplacementData> blocks = new ConcurrentHashMap<>();
@ -55,10 +54,6 @@ public class BlockStorage extends StoredObject {
}
}
public BlockStorage(UserConnection user) {
super(user);
}
public void store(Position position, int block) {
store(position, block, -1);
}

View File

@ -17,25 +17,21 @@
*/
package com.viaversion.viaversion.protocols.protocol1_13to1_12_2.storage;
import com.viaversion.viaversion.api.connection.StoredObject;
import com.viaversion.viaversion.api.connection.StorableObject;
import com.viaversion.viaversion.api.connection.UserConnection;
import com.viaversion.viaversion.api.protocol.packet.PacketWrapper;
import com.viaversion.viaversion.api.type.Type;
import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.Protocol1_13To1_12_2;
public class TabCompleteTracker extends StoredObject {
public class TabCompleteTracker implements StorableObject {
private int transactionId;
private String input;
private String lastTabComplete;
private long timeToSend;
public TabCompleteTracker(UserConnection user) {
super(user);
}
public void sendPacketToServer() {
public void sendPacketToServer(UserConnection connection) {
if (lastTabComplete == null || timeToSend > System.currentTimeMillis()) return;
PacketWrapper wrapper = PacketWrapper.create(0x01, null, getUser());
PacketWrapper wrapper = PacketWrapper.create(0x01, null, connection);
wrapper.write(Type.STRING, lastTabComplete);
wrapper.write(Type.BOOLEAN, false);
wrapper.write(Type.OPTIONAL_POSITION, null);

View File

@ -274,7 +274,7 @@ public class Protocol1_16To1_15_2 extends AbstractProtocol<ClientboundPackets1_1
@Override
public void init(UserConnection userConnection) {
userConnection.addEntityTracker(this.getClass(), new EntityTrackerBase(userConnection, Entity1_16Types.PLAYER));
userConnection.put(new InventoryTracker1_16(userConnection));
userConnection.put(new InventoryTracker1_16());
}
@Override

View File

@ -17,16 +17,12 @@
*/
package com.viaversion.viaversion.protocols.protocol1_16to1_15_2.storage;
import com.viaversion.viaversion.api.connection.StoredObject;
import com.viaversion.viaversion.api.connection.StorableObject;
import com.viaversion.viaversion.api.connection.UserConnection;
public class InventoryTracker1_16 extends StoredObject {
public class InventoryTracker1_16 implements StorableObject {
private short inventory = -1;
public InventoryTracker1_16(UserConnection user) {
super(user);
}
public short getInventory() {
return this.inventory;
}

View File

@ -27,8 +27,8 @@ import com.viaversion.viaversion.api.minecraft.Environment;
public class ClientWorld extends StoredObject {
private Environment environment;
public ClientWorld(UserConnection user) {
super(user);
public ClientWorld(final UserConnection connection) {
super(connection);
}
public Environment getEnvironment() {

View File

@ -151,10 +151,10 @@ public class Protocol1_9To1_8 extends AbstractProtocol<ClientboundPackets1_8, Cl
// Chunk tracker
userConnection.put(new ClientChunks(userConnection));
// Movement tracker
userConnection.put(new MovementTracker(userConnection));
userConnection.put(new MovementTracker());
// Inventory tracker
userConnection.put(new InventoryTracker(userConnection));
userConnection.put(new InventoryTracker());
// CommandBlock storage
userConnection.put(new CommandBlockStorage(userConnection));
userConnection.put(new CommandBlockStorage());
}
}

View File

@ -326,7 +326,7 @@ public class InventoryPackets {
// Move items in inventory to track the sword location
InventoryTracker inventoryTracker = wrapper.user().get(InventoryTracker.class);
inventoryTracker.handleWindowClick(windowId, mode, hoverSlot, button);
inventoryTracker.handleWindowClick(wrapper.user(), windowId, mode, hoverSlot, button);
}
ItemRewriter.toServer(stack);

View File

@ -27,8 +27,8 @@ public class ClientChunks extends StoredObject {
private final Set<Long> loadedChunks = Sets.newConcurrentHashSet();
private final Set<Long> bulkChunks = Sets.newConcurrentHashSet();
public ClientChunks(UserConnection user) {
super(user);
public ClientChunks(UserConnection connection) {
super(connection);
}
public static long toLong(int msw, int lsw) {

View File

@ -19,8 +19,7 @@ package com.viaversion.viaversion.protocols.protocol1_9to1_8.storage;
import com.github.steveice10.opennbt.tag.builtin.ByteTag;
import com.github.steveice10.opennbt.tag.builtin.CompoundTag;
import com.viaversion.viaversion.api.connection.StoredObject;
import com.viaversion.viaversion.api.connection.UserConnection;
import com.viaversion.viaversion.api.connection.StorableObject;
import com.viaversion.viaversion.api.minecraft.Position;
import com.viaversion.viaversion.util.Pair;
@ -28,14 +27,10 @@ import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
public class CommandBlockStorage extends StoredObject {
public class CommandBlockStorage implements StorableObject {
private final Map<Pair<Integer, Integer>, Map<Position, CompoundTag>> storedCommandBlocks = new ConcurrentHashMap<>();
private boolean permissions = false;
public CommandBlockStorage(UserConnection user) {
super(user);
}
public void unloadChunk(int x, int z) {
Pair<Integer, Integer> chunkPos = new Pair<>(x, z);
storedCommandBlocks.remove(chunkPos);

View File

@ -17,24 +17,20 @@
*/
package com.viaversion.viaversion.protocols.protocol1_9to1_8.storage;
import com.viaversion.viaversion.api.connection.StoredObject;
import com.viaversion.viaversion.api.connection.StorableObject;
import com.viaversion.viaversion.api.connection.UserConnection;
import com.viaversion.viaversion.protocols.protocol1_9to1_8.Protocol1_9To1_8;
import java.util.HashMap;
import java.util.Map;
public class InventoryTracker extends StoredObject {
public class InventoryTracker implements StorableObject {
private String inventory;
private final Map<Short, Map<Short, Integer>> windowItemCache = new HashMap<>();
private int itemIdInCursor = 0;
private boolean dragging = false;
public InventoryTracker(UserConnection user) {
super(user);
}
public String getInventory() {
return inventory;
}
@ -83,8 +79,8 @@ public class InventoryTracker extends StoredObject {
* @param hoverSlot The slot number of the current mouse position
* @param button The button to use in the click
*/
public void handleWindowClick(short windowId, byte mode, short hoverSlot, byte button) {
EntityTracker1_9 entityTracker = getUser().getEntityTracker(Protocol1_9To1_8.class);
public void handleWindowClick(UserConnection user, short windowId, byte mode, short hoverSlot, byte button) {
EntityTracker1_9 entityTracker = user.getEntityTracker(Protocol1_9To1_8.class);
// Skip inventory background clicks
if (hoverSlot == -1) {

View File

@ -17,19 +17,14 @@
*/
package com.viaversion.viaversion.protocols.protocol1_9to1_8.storage;
import com.viaversion.viaversion.api.connection.StoredObject;
import com.viaversion.viaversion.api.connection.UserConnection;
import com.viaversion.viaversion.api.connection.StorableObject;
public class MovementTracker extends StoredObject {
public class MovementTracker implements StorableObject {
private static final long IDLE_PACKET_DELAY = 50L; // Update every 50ms (20tps)
private static final long IDLE_PACKET_LIMIT = 20; // Max 20 ticks behind
private long nextIdlePacket = 0L;
private boolean ground = true;
public MovementTracker(UserConnection user) {
super(user);
}
public void incrementIdlePacket() {
// Notify of next update
// Allow a maximum lag spike of 1 second (20 ticks/updates)

View File

@ -18,8 +18,7 @@
package com.viaversion.viaversion.velocity.storage;
import com.velocitypowered.api.proxy.Player;
import com.viaversion.viaversion.api.connection.StoredObject;
import com.viaversion.viaversion.api.connection.UserConnection;
import com.viaversion.viaversion.api.connection.StorableObject;
import com.viaversion.viaversion.util.ReflectionUtil;
import java.lang.reflect.InvocationTargetException;
@ -28,7 +27,7 @@ import java.util.List;
import java.util.Objects;
import java.util.UUID;
public class VelocityStorage extends StoredObject {
public class VelocityStorage implements StorableObject {
private final Player player;
private String currentServer;
private List<UUID> cachedBossbar;
@ -48,8 +47,7 @@ public class VelocityStorage extends StoredObject {
}
}
public VelocityStorage(UserConnection user, Player player) {
super(user);
public VelocityStorage(Player player) {
this.player = player;
this.currentServer = "";
}