diff --git a/api/src/main/java/com/viaversion/viaversion/api/protocol/AbstractProtocol.java b/api/src/main/java/com/viaversion/viaversion/api/protocol/AbstractProtocol.java index ede6f7448..e7be61a53 100644 --- a/api/src/main/java/com/viaversion/viaversion/api/protocol/AbstractProtocol.java +++ b/api/src/main/java/com/viaversion/viaversion/api/protocol/AbstractProtocol.java @@ -51,6 +51,7 @@ public abstract class AbstractProtocol newClientboundPacketEnum; protected final Class oldServerboundPacketEnum; protected final Class newServerboundPacketEnum; + private boolean initialized; protected AbstractProtocol() { this(null, null, null, null); @@ -65,15 +66,22 @@ public abstract class AbstractProtocol @@ -257,6 +265,15 @@ public interface Protocol supportedClientVersion, int serverVersion); /** - * Registers a base protocol. Base Protocols registered later have higher priority. + * Registers and initializes a base protocol. Base Protocols registered later have higher priority. * Only base protocol will always be added to pipeline. * * @param baseProtocol base protocol to register diff --git a/api/src/main/java/com/viaversion/viaversion/api/rewriter/EntityRewriter.java b/api/src/main/java/com/viaversion/viaversion/api/rewriter/EntityRewriter.java new file mode 100644 index 000000000..c8e6dfbf3 --- /dev/null +++ b/api/src/main/java/com/viaversion/viaversion/api/rewriter/EntityRewriter.java @@ -0,0 +1,81 @@ +/* + * 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.rewriter; + +import com.viaversion.viaversion.api.connection.UserConnection; +import com.viaversion.viaversion.api.data.entity.EntityTracker; +import com.viaversion.viaversion.api.minecraft.entities.EntityType; +import com.viaversion.viaversion.api.minecraft.metadata.Metadata; +import com.viaversion.viaversion.api.protocol.Protocol; + +import java.util.List; + +public interface EntityRewriter extends Rewriter { + + /** + * Returns the entity type from the given (mapped) type id. + * + * @param type mapped type id + * @return entity type + */ + EntityType typeFromId(int type); + + /** + * Returns the entity type from the given id. + * From 1.14 and onwards, this is the same exact value as {@link #typeFromId(int)}. + * + * @param type entity type id + * @return EntityType from id + */ + default EntityType objectTypeFromId(int type) { + return typeFromId(type); + } + + /** + * Returns the mapped entitiy (or the same if it has not changed). + * + * @param id unmapped entity id + * @return mapped entity id + */ + int newEntityId(int id); + + /** + * Handles and transforms metadata of an entity. + * + * @param entityId entity id + * @param metadataList full, mutable list of metadata + * @param connection user connection + */ + void handleMetadata(int entityId, List metadataList, UserConnection connection); + + /** + * Returns the entity tracker for the current protocol. + * + * @param connection user connection + * @param entity tracker type + * @return entity tracker + */ + default E tracker(UserConnection connection) { + return connection.getEntityTracker(protocol().getClass()); + } +} diff --git a/api/src/main/java/com/viaversion/viaversion/api/rewriter/Rewriter.java b/api/src/main/java/com/viaversion/viaversion/api/rewriter/Rewriter.java new file mode 100644 index 000000000..f83cc4292 --- /dev/null +++ b/api/src/main/java/com/viaversion/viaversion/api/rewriter/Rewriter.java @@ -0,0 +1,40 @@ +/* + * 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.rewriter; + +import com.viaversion.viaversion.api.protocol.Protocol; + +public interface Rewriter { + + /** + * Registers any packet handlers or rewrites needed. + */ + void register(); + + /** + * Returns the {@link Protocol} instance of this rewriter. + * + * @return protocol of the rewriter + */ + T protocol(); +} diff --git a/api/src/main/java/com/viaversion/viaversion/api/rewriter/RewriterBase.java b/api/src/main/java/com/viaversion/viaversion/api/rewriter/RewriterBase.java new file mode 100644 index 000000000..e42402b47 --- /dev/null +++ b/api/src/main/java/com/viaversion/viaversion/api/rewriter/RewriterBase.java @@ -0,0 +1,56 @@ +/* + * 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.rewriter; + +import com.viaversion.viaversion.api.protocol.Protocol; + +public abstract class RewriterBase implements Rewriter { + protected final T protocol; + + protected RewriterBase(T protocol) { + this.protocol = protocol; + } + + @Override + public void register() { + registerPackets(); + registerRewrites(); + } + + /** + * To be overriden. Called when initializing the EntityRewriter. + */ + protected void registerPackets() { + } + + /** + * To be overriden. Called when initializing the EntityRewriter. + */ + protected void registerRewrites() { + } + + @Override + public T protocol() { + return protocol; + } +} diff --git a/common/src/main/java/com/viaversion/viaversion/protocol/ProtocolManagerImpl.java b/common/src/main/java/com/viaversion/viaversion/protocol/ProtocolManagerImpl.java index eded44e2d..e033eddde 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocol/ProtocolManagerImpl.java +++ b/common/src/main/java/com/viaversion/viaversion/protocol/ProtocolManagerImpl.java @@ -169,6 +169,9 @@ public class ProtocolManagerImpl implements ProtocolManager { @Override public void registerProtocol(Protocol protocol, List supportedClientVersion, int serverVersion) { + // Register the protocol's handlers + protocol.initialize(); + // Clear cache as this may make new routes. if (!pathCache.isEmpty()) { pathCache.clear(); @@ -205,6 +208,8 @@ public class ProtocolManagerImpl implements ProtocolManager { @Override public void registerBaseProtocol(Protocol baseProtocol, Range supportedProtocols) { Preconditions.checkArgument(baseProtocol.isBaseProtocol(), "Protocol is not a base protocol"); + baseProtocol.initialize(); + baseProtocols.add(new Pair<>(supportedProtocols, baseProtocol)); if (Via.getPlatform().isPluginEnabled()) { baseProtocol.register(Via.getManager().getProviders()); diff --git a/common/src/main/java/com/viaversion/viaversion/protocol/ProtocolPipelineImpl.java b/common/src/main/java/com/viaversion/viaversion/protocol/ProtocolPipelineImpl.java index 559793507..14d971c26 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocol/ProtocolPipelineImpl.java +++ b/common/src/main/java/com/viaversion/viaversion/protocol/ProtocolPipelineImpl.java @@ -48,6 +48,7 @@ public class ProtocolPipelineImpl extends AbstractSimpleProtocol implements Prot public ProtocolPipelineImpl(UserConnection userConnection) { this.userConnection = userConnection; userConnection.getProtocolInfo().setPipeline(this); + registerPackets(); // Not registered as a standard "protocol", so we have to call the method manually } @Override diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_11to1_10/Protocol1_11To1_10.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_11to1_10/Protocol1_11To1_10.java index 8b1946d83..6e1f5a25d 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_11to1_10/Protocol1_11To1_10.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_11to1_10/Protocol1_11To1_10.java @@ -50,14 +50,15 @@ public class Protocol1_11To1_10 extends AbstractProtocol { + private final EntityRewriter metadataRewriter = new MetadataRewriter1_12To1_11_1(this); + public Protocol1_12To1_11_1() { super(ClientboundPackets1_9_3.class, ClientboundPackets1_12.class, ServerboundPackets1_9_3.class, ServerboundPackets1_12.class); } @Override protected void registerPackets() { - EntityRewriter metadataRewriter = new MetadataRewriter1_12To1_11_1(this); metadataRewriter.register(); InventoryPackets.register(this); @@ -262,4 +263,9 @@ public class Protocol1_12To1_11_1 extends AbstractProtocol { public static final MappingData MAPPINGS = new MappingDataBase("1.13", "1.13.2", true); + private final EntityRewriter entityRewriter = new MetadataRewriter1_13_1To1_13(this); public Protocol1_13_1To1_13() { super(ClientboundPackets1_13.class, ClientboundPackets1_13.class, ServerboundPackets1_13.class, ServerboundPackets1_13.class); @@ -50,8 +52,7 @@ public class Protocol1_13_1To1_13 extends AbstractProtocol SCOREBOARD_TEAM_NAME_REWRITE = new HashMap<>(); private static final Set FORMATTING_CODES = Sets.newHashSet('k', 'l', 'm', 'n', 'o', 'r'); + private final EntityRewriter entityRewriter = new MetadataRewriter1_13To1_12_2(this); static { SCOREBOARD_TEAM_NAME_REWRITE.put('0', 'g'); @@ -155,8 +156,7 @@ public class Protocol1_13To1_12_2 extends AbstractProtocol { + private final EntityRewriter metadataRewriter = new MetadataRewriter1_14_1To1_14(this); + public Protocol1_14_1To1_14() { super(ClientboundPackets1_14.class, ClientboundPackets1_14.class, ServerboundPackets1_14.class, ServerboundPackets1_14.class); } @Override protected void registerPackets() { - EntityRewriter metadataRewriter = new MetadataRewriter1_14_1To1_14(this); metadataRewriter.register(); EntityPackets.register(this); @@ -45,4 +46,9 @@ public class Protocol1_14_1To1_14 extends AbstractProtocol { public static final MappingData MAPPINGS = new MappingData(); + private final EntityRewriter metadataRewriter = new MetadataRewriter1_14To1_13_2(this); public Protocol1_14To1_13_2() { super(ClientboundPackets1_13.class, ClientboundPackets1_14.class, ServerboundPackets1_13.class, ServerboundPackets1_14.class); @@ -50,7 +51,6 @@ public class Protocol1_14To1_13_2 extends AbstractProtocol { public static final MappingData MAPPINGS = new MappingData(); + private final EntityRewriter metadataRewriter = new MetadataRewriter1_15To1_14_4(this); private TagRewriter tagRewriter; public Protocol1_15To1_14_4() { @@ -48,7 +49,6 @@ public class Protocol1_15To1_14_4 extends AbstractProtocol { public static final MappingData MAPPINGS = new MappingData(); + private final EntityRewriter metadataRewriter = new MetadataRewriter1_16_2To1_16_1(this); private TagRewriter tagRewriter; public Protocol1_16_2To1_16_1() { @@ -47,17 +48,16 @@ public class Protocol1_16_2To1_16_1 extends AbstractProtocol { @@ -58,6 +58,7 @@ public class Protocol1_9To1_8 extends AbstractProtocol { } @Override - protected EntityType typeFromId(int type) { + public EntityType typeFromId(int type) { return Entity1_10Types.getTypeFromId(type, false); } @Override - protected EntityType objectTypeFromId(int type) { + public EntityType objectTypeFromId(int type) { return Entity1_10Types.getTypeFromId(type, true); } } diff --git a/common/src/main/java/com/viaversion/viaversion/rewriter/EntityRewriter.java b/common/src/main/java/com/viaversion/viaversion/rewriter/EntityRewriter.java index a7ebb7269..5900af062 100644 --- a/common/src/main/java/com/viaversion/viaversion/rewriter/EntityRewriter.java +++ b/common/src/main/java/com/viaversion/viaversion/rewriter/EntityRewriter.java @@ -32,6 +32,7 @@ import com.viaversion.viaversion.api.protocol.Protocol; import com.viaversion.viaversion.api.protocol.packet.ClientboundPacketType; import com.viaversion.viaversion.api.protocol.remapper.PacketHandler; import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper; +import com.viaversion.viaversion.api.rewriter.RewriterBase; import com.viaversion.viaversion.api.type.Type; import com.viaversion.viaversion.api.type.types.Particle; import com.viaversion.viaversion.rewriter.meta.MetaFilter; @@ -47,7 +48,7 @@ import java.util.List; import java.util.logging.Logger; import java.util.stream.Collectors; -public abstract class EntityRewriter extends RewriterBase { +public abstract class EntityRewriter extends RewriterBase implements com.viaversion.viaversion.api.rewriter.EntityRewriter { private static final Metadata[] EMPTY_ARRAY = new Metadata[0]; protected final List metadataFilters = new ArrayList<>(); protected final boolean trackMappedType; @@ -92,13 +93,7 @@ public abstract class EntityRewriter extends RewriterBase metadataFilters.add(filter); } - /** - * Handles and transforms metadata of an entity. - * - * @param entityId entity id - * @param metadataList full, mutable list of metadata - * @param connection user connection - */ + @Override public void handleMetadata(int entityId, List metadataList, UserConnection connection) { EntityType type = tracker(connection).entityType(entityId); int i = 0; // Count index for fast removal @@ -167,33 +162,9 @@ public abstract class EntityRewriter extends RewriterBase protected void handleMetadata(int entityId, @Nullable EntityType type, Metadata metadata, List metadatas, UserConnection connection) throws Exception { } - /** - * Returns the entity type from the given (mapped) type id. - * - * @param type mapped type id - * @return entity type - */ - protected abstract EntityType typeFromId(int type); - - /** - * Returns the entity type from the given id. - * From 1.14 and onwards, this is the same exact value as {@link #typeFromId(int)}. - * - * @param type entity type id - * @return EntityType from id - */ - protected EntityType objectTypeFromId(int type) { - return typeFromId(type); - } - - /** - * Returns the mapped entitiy (or the same if it has not changed). - * - * @param oldId old entity id - * @return mapped entity id - */ - public int newEntityId(int oldId) { - return typeMappings != null ? typeMappings.getOrDefault(oldId, oldId) : oldId; + @Override + public int newEntityId(int id) { + return typeMappings != null ? typeMappings.getOrDefault(id, id) : id; } /** @@ -497,17 +468,6 @@ public abstract class EntityRewriter extends RewriterBase particle.setId(protocol.getMappingData().getNewParticleId(id)); } - /** - * Returns the entity tracker for the current protocol. - * - * @param connection user connection - * @param entity tracker type - * @return entity tracker - */ - public E tracker(UserConnection connection) { - return connection.getEntityTracker(protocol.getClass()); - } - private void logException(Exception e, @Nullable EntityType type, List metadataList, Metadata metadata) { if (!Via.getConfig().isSuppressMetadataErrors() || Via.getManager().isDebug()) { Logger logger = Via.getPlatform().getLogger(); diff --git a/common/src/main/java/com/viaversion/viaversion/rewriter/Rewriter.java b/common/src/main/java/com/viaversion/viaversion/rewriter/Rewriter.java deleted file mode 100644 index de749d266..000000000 --- a/common/src/main/java/com/viaversion/viaversion/rewriter/Rewriter.java +++ /dev/null @@ -1,35 +0,0 @@ -/* - * This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion - * Copyright (C) 2016-2021 ViaVersion 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 . - */ -package com.viaversion.viaversion.rewriter; - -import com.viaversion.viaversion.api.protocol.Protocol; - -public interface Rewriter { - - /** - * Registers any packet handlers or rewrites needed. - */ - void register(); - - /** - * Returns the {@link Protocol} instance of this rewriter. - * - * @return protocol of the rewriter - */ - T protocol(); -} diff --git a/common/src/main/java/com/viaversion/viaversion/rewriter/RewriterBase.java b/common/src/main/java/com/viaversion/viaversion/rewriter/RewriterBase.java deleted file mode 100644 index 0475edef1..000000000 --- a/common/src/main/java/com/viaversion/viaversion/rewriter/RewriterBase.java +++ /dev/null @@ -1,51 +0,0 @@ -/* - * This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion - * Copyright (C) 2016-2021 ViaVersion 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 . - */ -package com.viaversion.viaversion.rewriter; - -import com.viaversion.viaversion.api.protocol.Protocol; - -public abstract class RewriterBase implements Rewriter { - protected final T protocol; - - protected RewriterBase(T protocol) { - this.protocol = protocol; - } - - @Override - public void register() { - registerPackets(); - registerRewrites(); - } - - /** - * To be overriden. Called when initializing the EntityRewriter. - */ - protected void registerPackets() { - } - - /** - * To be overriden. Called when initializing the EntityRewriter. - */ - protected void registerRewrites() { - } - - @Override - public T protocol() { - return protocol; - } -} diff --git a/common/src/main/java/com/viaversion/viaversion/rewriter/StatisticsRewriter.java b/common/src/main/java/com/viaversion/viaversion/rewriter/StatisticsRewriter.java index 7b6850789..046811a77 100644 --- a/common/src/main/java/com/viaversion/viaversion/rewriter/StatisticsRewriter.java +++ b/common/src/main/java/com/viaversion/viaversion/rewriter/StatisticsRewriter.java @@ -25,12 +25,10 @@ import org.checkerframework.checker.nullness.qual.Nullable; public class StatisticsRewriter { private final Protocol protocol; - private final IdRewriteFunction entityRewriter; private final int customStatsCategory = 8; // Make this changeable if it differs in a future version - public StatisticsRewriter(Protocol protocol, @Nullable IdRewriteFunction entityRewriter) { + public StatisticsRewriter(Protocol protocol) { this.protocol = protocol; - this.entityRewriter = entityRewriter; } public void register(ClientboundPacketType packetType) { @@ -81,7 +79,7 @@ public class StatisticsRewriter { case ITEM: return protocol.getMappingData().getItemMappings() != null ? id -> protocol.getMappingData().getNewItemId(id) : null; case ENTITY: - return entityRewriter; + return protocol.getEntityRewriter() != null ? id -> protocol.getEntityRewriter().newEntityId(id) : null; } throw new IllegalArgumentException("Unknown registry type in statistics packet: " + type); } diff --git a/common/src/main/java/com/viaversion/viaversion/rewriter/TagRewriter.java b/common/src/main/java/com/viaversion/viaversion/rewriter/TagRewriter.java index cb6804c77..f52375f58 100644 --- a/common/src/main/java/com/viaversion/viaversion/rewriter/TagRewriter.java +++ b/common/src/main/java/com/viaversion/viaversion/rewriter/TagRewriter.java @@ -36,12 +36,10 @@ import java.util.Map; public class TagRewriter { private static final int[] EMPTY_ARRAY = {}; private final Protocol protocol; - private final IdRewriteFunction entityRewriter; private final Map> newTags = new EnumMap<>(RegistryType.class); - public TagRewriter(Protocol protocol, @Nullable IdRewriteFunction entityRewriter) { + public TagRewriter(Protocol protocol) { this.protocol = protocol; - this.entityRewriter = entityRewriter; } /** @@ -178,7 +176,7 @@ public class TagRewriter { case ITEM: return mappingData != null && mappingData.getItemMappings() != null ? mappingData::getNewItemId : null; case ENTITY: - return entityRewriter; + return protocol.getEntityRewriter() != null ? id -> protocol.getEntityRewriter().newEntityId(id) : null; case FLUID: case GAME_EVENT: default: