Slightly more OOP, move registerPackets out of Protocol constructor

registerPackets being called within the constructor made it impossible to create instance objects then used in registerPackets (vs. having to then create the objects in registerPackets).
This commit is contained in:
KennyTV 2021-06-03 17:15:24 +02:00
parent e1072a8dc7
commit fc1450fe38
No known key found for this signature in database
GPG Key ID: 6BE3B555EBC5982B
35 changed files with 342 additions and 205 deletions

View File

@ -51,6 +51,7 @@ public abstract class AbstractProtocol<C1 extends ClientboundPacketType, C2 exte
protected final Class<C2> newClientboundPacketEnum;
protected final Class<S1> oldServerboundPacketEnum;
protected final Class<S2> newServerboundPacketEnum;
private boolean initialized;
protected AbstractProtocol() {
this(null, null, null, null);
@ -65,15 +66,22 @@ public abstract class AbstractProtocol<C1 extends ClientboundPacketType, C2 exte
this.newClientboundPacketEnum = clientboundPacketEnum;
this.oldServerboundPacketEnum = oldServerboundPacketEnum;
this.newServerboundPacketEnum = serverboundPacketEnum;
}
@Override
public final void initialize() {
Preconditions.checkArgument(!initialized);
initialized = true;
registerPackets();
// Register the rest of the ids with no handlers if necessary
if (oldClientboundPacketEnum != null && clientboundPacketEnum != null
&& oldClientboundPacketEnum != clientboundPacketEnum) {
if (oldClientboundPacketEnum != null && newClientboundPacketEnum != null
&& oldClientboundPacketEnum != newClientboundPacketEnum) {
registerClientboundChannelIdChanges();
}
if (oldServerboundPacketEnum != null && serverboundPacketEnum != null
&& oldServerboundPacketEnum != serverboundPacketEnum) {
if (oldServerboundPacketEnum != null && newServerboundPacketEnum != null
&& oldServerboundPacketEnum != newServerboundPacketEnum) {
registerServerboundChannelIdChanges();
}
}
@ -146,7 +154,7 @@ public abstract class AbstractProtocol<C1 extends ClientboundPacketType, C2 exte
protected void onMappingDataLoaded() {
}
public void addEntityTracker(UserConnection connection, EntityTracker tracker) {
protected void addEntityTracker(UserConnection connection, EntityTracker tracker) {
connection.addEntityTracker(this.getClass(), tracker);
}

View File

@ -31,6 +31,7 @@ import com.viaversion.viaversion.api.protocol.packet.PacketWrapper;
import com.viaversion.viaversion.api.protocol.packet.ServerboundPacketType;
import com.viaversion.viaversion.api.protocol.packet.State;
import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper;
import com.viaversion.viaversion.api.rewriter.EntityRewriter;
import org.checkerframework.checker.nullness.qual.Nullable;
/**
@ -210,6 +211,13 @@ public interface Protocol<C1 extends ClientboundPacketType, C2 extends Clientbou
*/
void put(Object object);
/**
* Called with {@link ProtocolManager#registerProtocol} to register packet handlers and automatic packet id remapping.
*
* @throws IllegalArgumentException if this method has already been called
*/
void initialize();
/**
* Returns true if this Protocol's {@link #loadMappingData()} method should be called.
* <p>
@ -257,6 +265,15 @@ public interface Protocol<C1 extends ClientboundPacketType, C2 extends Clientbou
return null;
}
/**
* Returns the protocol's entity rewriter if present.
*
* @return entity rewriter
*/
default @Nullable EntityRewriter getEntityRewriter() {
return null;
}
/**
* Returns whether this protocol is a base protocol.
*

View File

@ -104,7 +104,7 @@ public interface ProtocolManager {
}
/**
* Register a protocol.
* Register and initializes a protocol.
*
* @param protocol protocol to register
* @param clientVersion supported client protocol versions
@ -114,7 +114,7 @@ public interface ProtocolManager {
void registerProtocol(Protocol protocol, ProtocolVersion clientVersion, ProtocolVersion serverVersion);
/**
* Register a protocol.
* Register and initializes protocol.
*
* @param protocol protocol to register
* @param supportedClientVersion supported client protocol versions
@ -124,7 +124,7 @@ public interface ProtocolManager {
void registerProtocol(Protocol protocol, List<Integer> 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

View File

@ -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<T extends Protocol> extends Rewriter<T> {
/**
* 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<Metadata> metadataList, UserConnection connection);
/**
* Returns the entity tracker for the current protocol.
*
* @param connection user connection
* @param <E> entity tracker type
* @return entity tracker
*/
default <E extends EntityTracker> E tracker(UserConnection connection) {
return connection.getEntityTracker(protocol().getClass());
}
}

View File

@ -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<T extends Protocol> {
/**
* Registers any packet handlers or rewrites needed.
*/
void register();
/**
* Returns the {@link Protocol} instance of this rewriter.
*
* @return protocol of the rewriter
*/
T protocol();
}

View File

@ -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<T extends Protocol> implements Rewriter<T> {
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;
}
}

View File

@ -169,6 +169,9 @@ public class ProtocolManagerImpl implements ProtocolManager {
@Override
public void registerProtocol(Protocol protocol, List<Integer> 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<Integer> 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());

View File

@ -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

View File

@ -50,14 +50,15 @@ public class Protocol1_11To1_10 extends AbstractProtocol<ClientboundPackets1_9_3
}
};
private final EntityRewriter entityRewriter = new MetadataRewriter1_11To1_10(this);
public Protocol1_11To1_10() {
super(ClientboundPackets1_9_3.class, ClientboundPackets1_9_3.class, ServerboundPackets1_9_3.class, ServerboundPackets1_9_3.class);
}
@Override
protected void registerPackets() {
EntityRewriter metadataRewriter = new MetadataRewriter1_11To1_10(this);
metadataRewriter.register();
entityRewriter.register();
InventoryPackets.register(this);
@ -69,7 +70,7 @@ public class Protocol1_11To1_10 extends AbstractProtocol<ClientboundPackets1_9_3
map(Type.BYTE); // 2 - Type
// Track Entity
handler(metadataRewriter.objectTrackerHandler());
handler(entityRewriter.objectTrackerHandler());
}
});
@ -103,7 +104,7 @@ public class Protocol1_11To1_10 extends AbstractProtocol<ClientboundPackets1_9_3
// Register Type ID
wrapper.user().getEntityTracker(Protocol1_11To1_10.class).addEntity(entityId, entType);
metadataRewriter.handleMetadata(entityId, wrapper.get(Types1_9.METADATA_LIST, 0), wrapper.user());
entityRewriter.handleMetadata(entityId, wrapper.get(Types1_9.METADATA_LIST, 0), wrapper.user());
}
}
});
@ -127,7 +128,7 @@ public class Protocol1_11To1_10 extends AbstractProtocol<ClientboundPackets1_9_3
}
});
metadataRewriter.registerMetadataRewriter(ClientboundPackets1_9_3.ENTITY_METADATA, Types1_9.METADATA_LIST);
entityRewriter.registerMetadataRewriter(ClientboundPackets1_9_3.ENTITY_METADATA, Types1_9.METADATA_LIST);
registerClientbound(ClientboundPackets1_9_3.ENTITY_TELEPORT, new PacketRemapper() {
@Override
@ -157,7 +158,7 @@ public class Protocol1_11To1_10 extends AbstractProtocol<ClientboundPackets1_9_3
}
});
metadataRewriter.registerRemoveEntities(ClientboundPackets1_9_3.DESTROY_ENTITIES);
entityRewriter.registerRemoveEntities(ClientboundPackets1_9_3.DESTROY_ENTITIES);
registerClientbound(ClientboundPackets1_9_3.TITLE, new PacketRemapper() {
@Override
@ -382,4 +383,9 @@ public class Protocol1_11To1_10 extends AbstractProtocol<ClientboundPackets1_9_3
if (!userConnection.has(ClientWorld.class))
userConnection.put(new ClientWorld(userConnection));
}
@Override
public EntityRewriter getEntityRewriter() {
return entityRewriter;
}
}

View File

@ -146,12 +146,12 @@ public class MetadataRewriter1_11To1_10 extends EntityRewriter<Protocol1_11To1_1
}
@Override
protected com.viaversion.viaversion.api.minecraft.entities.EntityType typeFromId(int type) {
public com.viaversion.viaversion.api.minecraft.entities.EntityType typeFromId(int type) {
return Entity1_11Types.getTypeFromId(type, false);
}
@Override
protected com.viaversion.viaversion.api.minecraft.entities.EntityType objectTypeFromId(int type) {
public com.viaversion.viaversion.api.minecraft.entities.EntityType objectTypeFromId(int type) {
return Entity1_11Types.getTypeFromId(type, true);
}

View File

@ -47,13 +47,14 @@ import com.viaversion.viaversion.rewriter.SoundRewriter;
public class Protocol1_12To1_11_1 extends AbstractProtocol<ClientboundPackets1_9_3, ClientboundPackets1_12, ServerboundPackets1_9_3, ServerboundPackets1_12> {
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<ClientboundPackets1_9
userConnection.put(new ClientWorld(userConnection));
}
}
@Override
public EntityRewriter getEntityRewriter() {
return metadataRewriter;
}
}

View File

@ -51,12 +51,12 @@ public class MetadataRewriter1_12To1_11_1 extends EntityRewriter<Protocol1_12To1
}
@Override
protected EntityType typeFromId(int type) {
public EntityType typeFromId(int type) {
return Entity1_12Types.getTypeFromId(type, false);
}
@Override
protected EntityType objectTypeFromId(int type) {
public EntityType objectTypeFromId(int type) {
return Entity1_12Types.getTypeFromId(type, true);
}
}

View File

@ -27,6 +27,7 @@ import com.viaversion.viaversion.api.protocol.packet.PacketWrapper;
import com.viaversion.viaversion.api.protocol.remapper.PacketHandler;
import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper;
import com.viaversion.viaversion.api.protocol.remapper.ValueTransformer;
import com.viaversion.viaversion.api.rewriter.EntityRewriter;
import com.viaversion.viaversion.api.type.Type;
import com.viaversion.viaversion.data.entity.EntityTrackerBase;
import com.viaversion.viaversion.protocols.protocol1_13_1to1_13.metadata.MetadataRewriter1_13_1To1_13;
@ -43,6 +44,7 @@ import com.viaversion.viaversion.rewriter.TagRewriter;
public class Protocol1_13_1To1_13 extends AbstractProtocol<ClientboundPackets1_13, ClientboundPackets1_13, ServerboundPackets1_13, ServerboundPackets1_13> {
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<ClientboundPackets1_1
@Override
protected void registerPackets() {
MetadataRewriter1_13_1To1_13 metadataRewriter = new MetadataRewriter1_13_1To1_13(this);
metadataRewriter.register();
entityRewriter.register();
EntityPackets.register(this);
InventoryPackets.register(this);
@ -144,8 +145,8 @@ public class Protocol1_13_1To1_13 extends AbstractProtocol<ClientboundPackets1_1
}
});
new TagRewriter(this, null).register(ClientboundPackets1_13.TAGS, RegistryType.ITEM);
new StatisticsRewriter(this, null).register(ClientboundPackets1_13.STATISTICS);
new TagRewriter(this).register(ClientboundPackets1_13.TAGS, RegistryType.ITEM);
new StatisticsRewriter(this).register(ClientboundPackets1_13.STATISTICS);
}
@Override
@ -160,4 +161,9 @@ public class Protocol1_13_1To1_13 extends AbstractProtocol<ClientboundPackets1_1
public MappingData getMappingData() {
return MAPPINGS;
}
@Override
public EntityRewriter getEntityRewriter() {
return entityRewriter;
}
}

View File

@ -61,12 +61,12 @@ public class MetadataRewriter1_13_1To1_13 extends EntityRewriter<Protocol1_13_1T
}
@Override
protected EntityType typeFromId(int type) {
public EntityType typeFromId(int type) {
return Entity1_13Types.getTypeFromId(type, false);
}
@Override
protected EntityType objectTypeFromId(int type) {
public EntityType objectTypeFromId(int type) {
return Entity1_13Types.getTypeFromId(type, true);
}
}

View File

@ -33,6 +33,7 @@ import com.viaversion.viaversion.api.protocol.packet.State;
import com.viaversion.viaversion.api.protocol.remapper.PacketHandler;
import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper;
import com.viaversion.viaversion.api.protocol.remapper.ValueTransformer;
import com.viaversion.viaversion.api.rewriter.EntityRewriter;
import com.viaversion.viaversion.api.type.Type;
import com.viaversion.viaversion.data.entity.EntityTrackerBase;
import com.viaversion.viaversion.protocols.protocol1_12_1to1_12.ClientboundPackets1_12_1;
@ -55,7 +56,6 @@ import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.storage.BlockCon
import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.storage.BlockStorage;
import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.storage.TabCompleteTracker;
import com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld;
import com.viaversion.viaversion.rewriter.EntityRewriter;
import com.viaversion.viaversion.rewriter.SoundRewriter;
import com.viaversion.viaversion.util.ChatColorUtil;
import com.viaversion.viaversion.util.GsonUtil;
@ -72,6 +72,7 @@ public class Protocol1_13To1_12_2 extends AbstractProtocol<ClientboundPackets1_1
// These are arbitrary rewrite values, it just needs an invalid color code character.
private static final Map<Character, Character> SCOREBOARD_TEAM_NAME_REWRITE = new HashMap<>();
private static final Set<Character> 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<ClientboundPackets1_1
@Override
protected void registerPackets() {
EntityRewriter metadataRewriter = new MetadataRewriter1_13To1_12_2(this);
metadataRewriter.register();
entityRewriter.register();
// Register grouped packet changes
EntityPackets.register(this);
@ -1103,4 +1103,9 @@ public class Protocol1_13To1_12_2 extends AbstractProtocol<ClientboundPackets1_1
public MappingData getMappingData() {
return MAPPINGS;
}
@Override
public EntityRewriter getEntityRewriter() {
return entityRewriter;
}
}

View File

@ -125,17 +125,17 @@ public class MetadataRewriter1_13To1_12_2 extends EntityRewriter<Protocol1_13To1
}
@Override
public int newEntityId(final int oldId) {
return EntityTypeRewriter.getNewId(oldId);
public int newEntityId(final int id) {
return EntityTypeRewriter.getNewId(id);
}
@Override
protected EntityType typeFromId(int type) {
public EntityType typeFromId(int type) {
return Entity1_13Types.getTypeFromId(type, false);
}
@Override
protected EntityType objectTypeFromId(int type) {
public EntityType objectTypeFromId(int type) {
return Entity1_13Types.getTypeFromId(type, true);
}
}

View File

@ -20,22 +20,23 @@ package com.viaversion.viaversion.protocols.protocol1_14_1to1_14;
import com.viaversion.viaversion.api.connection.UserConnection;
import com.viaversion.viaversion.api.minecraft.entities.Entity1_14Types;
import com.viaversion.viaversion.api.protocol.AbstractProtocol;
import com.viaversion.viaversion.api.rewriter.EntityRewriter;
import com.viaversion.viaversion.data.entity.EntityTrackerBase;
import com.viaversion.viaversion.protocols.protocol1_14_1to1_14.metadata.MetadataRewriter1_14_1To1_14;
import com.viaversion.viaversion.protocols.protocol1_14_1to1_14.packets.EntityPackets;
import com.viaversion.viaversion.protocols.protocol1_14to1_13_2.ClientboundPackets1_14;
import com.viaversion.viaversion.protocols.protocol1_14to1_13_2.ServerboundPackets1_14;
import com.viaversion.viaversion.rewriter.EntityRewriter;
public class Protocol1_14_1To1_14 extends AbstractProtocol<ClientboundPackets1_14, ClientboundPackets1_14, ServerboundPackets1_14, ServerboundPackets1_14> {
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<ClientboundPackets1_1
public void init(UserConnection userConnection) {
userConnection.addEntityTracker(this.getClass(), new EntityTrackerBase(userConnection, Entity1_14Types.PLAYER));
}
@Override
public EntityRewriter getEntityRewriter() {
return metadataRewriter;
}
}

View File

@ -44,7 +44,7 @@ public class MetadataRewriter1_14_1To1_14 extends EntityRewriter<Protocol1_14_1T
}
@Override
protected EntityType typeFromId(int type) {
public EntityType typeFromId(int type) {
return Entity1_14Types.getTypeFromId(type);
}
}

View File

@ -22,6 +22,7 @@ import com.viaversion.viaversion.api.protocol.AbstractProtocol;
import com.viaversion.viaversion.api.protocol.packet.PacketWrapper;
import com.viaversion.viaversion.api.protocol.remapper.PacketHandler;
import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper;
import com.viaversion.viaversion.api.rewriter.EntityRewriter;
import com.viaversion.viaversion.api.type.Type;
import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ClientboundPackets1_13;
import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ServerboundPackets1_13;
@ -36,13 +37,13 @@ import com.viaversion.viaversion.protocols.protocol1_14to1_13_2.packets.WorldPac
import com.viaversion.viaversion.protocols.protocol1_14to1_13_2.storage.EntityTracker1_14;
import com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld;
import com.viaversion.viaversion.rewriter.ComponentRewriter;
import com.viaversion.viaversion.rewriter.EntityRewriter;
import com.viaversion.viaversion.rewriter.SoundRewriter;
import com.viaversion.viaversion.rewriter.StatisticsRewriter;
public class Protocol1_14To1_13_2 extends AbstractProtocol<ClientboundPackets1_13, ClientboundPackets1_14, ServerboundPackets1_13, ServerboundPackets1_14> {
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<ClientboundPackets1_1
@Override
protected void registerPackets() {
EntityRewriter metadataRewriter = new MetadataRewriter1_14To1_13_2(this);
metadataRewriter.register();
InventoryPackets.register(this);
@ -59,7 +59,7 @@ public class Protocol1_14To1_13_2 extends AbstractProtocol<ClientboundPackets1_1
PlayerPackets.register(this);
new SoundRewriter(this).registerSound(ClientboundPackets1_13.SOUND);
new StatisticsRewriter(this, metadataRewriter::newEntityId).register(ClientboundPackets1_13.STATISTICS);
new StatisticsRewriter(this).register(ClientboundPackets1_13.STATISTICS);
ComponentRewriter componentRewriter = new ComponentRewriter1_14(this);
componentRewriter.registerChatMessage(ClientboundPackets1_13.CHAT_MESSAGE);
@ -157,4 +157,9 @@ public class Protocol1_14To1_13_2 extends AbstractProtocol<ClientboundPackets1_1
public MappingData getMappingData() {
return MAPPINGS;
}
@Override
public EntityRewriter getEntityRewriter() {
return metadataRewriter;
}
}

View File

@ -184,7 +184,7 @@ public class MetadataRewriter1_14To1_13_2 extends EntityRewriter<Protocol1_14To1
}
@Override
protected EntityType typeFromId(int type) {
public EntityType typeFromId(int type) {
return Entity1_14Types.getTypeFromId(type);
}

View File

@ -21,6 +21,7 @@ import com.viaversion.viaversion.api.connection.UserConnection;
import com.viaversion.viaversion.api.minecraft.entities.Entity1_15Types;
import com.viaversion.viaversion.api.protocol.AbstractProtocol;
import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper;
import com.viaversion.viaversion.api.rewriter.EntityRewriter;
import com.viaversion.viaversion.api.type.Type;
import com.viaversion.viaversion.data.entity.EntityTrackerBase;
import com.viaversion.viaversion.protocols.protocol1_14to1_13_2.ClientboundPackets1_14;
@ -31,7 +32,6 @@ import com.viaversion.viaversion.protocols.protocol1_15to1_14_4.packets.EntityPa
import com.viaversion.viaversion.protocols.protocol1_15to1_14_4.packets.InventoryPackets;
import com.viaversion.viaversion.protocols.protocol1_15to1_14_4.packets.PlayerPackets;
import com.viaversion.viaversion.protocols.protocol1_15to1_14_4.packets.WorldPackets;
import com.viaversion.viaversion.rewriter.EntityRewriter;
import com.viaversion.viaversion.rewriter.RegistryType;
import com.viaversion.viaversion.rewriter.SoundRewriter;
import com.viaversion.viaversion.rewriter.StatisticsRewriter;
@ -40,6 +40,7 @@ import com.viaversion.viaversion.rewriter.TagRewriter;
public class Protocol1_15To1_14_4 extends AbstractProtocol<ClientboundPackets1_14, ClientboundPackets1_15, ServerboundPackets1_14, ServerboundPackets1_14> {
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<ClientboundPackets1_1
@Override
protected void registerPackets() {
EntityRewriter metadataRewriter = new MetadataRewriter1_15To1_14_4(this);
metadataRewriter.register();
EntityPackets.register(this);
@ -60,7 +60,7 @@ public class Protocol1_15To1_14_4 extends AbstractProtocol<ClientboundPackets1_1
soundRewriter.registerSound(ClientboundPackets1_14.ENTITY_SOUND); // Entity Sound Effect (added somewhere in 1.14)
soundRewriter.registerSound(ClientboundPackets1_14.SOUND);
new StatisticsRewriter(this, metadataRewriter::newEntityId).register(ClientboundPackets1_14.STATISTICS);
new StatisticsRewriter(this).register(ClientboundPackets1_14.STATISTICS);
registerServerbound(ServerboundPackets1_14.EDIT_BOOK, new PacketRemapper() {
@Override
@ -69,7 +69,7 @@ public class Protocol1_15To1_14_4 extends AbstractProtocol<ClientboundPackets1_1
}
});
tagRewriter = new TagRewriter(this, EntityPackets::getNewEntityId);
tagRewriter = new TagRewriter(this);
tagRewriter.register(ClientboundPackets1_14.TAGS, RegistryType.ENTITY);
}
@ -92,4 +92,9 @@ public class Protocol1_15To1_14_4 extends AbstractProtocol<ClientboundPackets1_1
public MappingData getMappingData() {
return MAPPINGS;
}
@Override
public EntityRewriter getEntityRewriter() {
return metadataRewriter;
}
}

View File

@ -78,12 +78,12 @@ public class MetadataRewriter1_15To1_14_4 extends EntityRewriter<Protocol1_15To1
}
@Override
public int newEntityId(final int oldId) {
return EntityPackets.getNewEntityId(oldId);
public int newEntityId(final int id) {
return EntityPackets.getNewEntityId(id);
}
@Override
protected EntityType typeFromId(int type) {
public EntityType typeFromId(int type) {
return Entity1_15Types.getTypeFromId(type);
}
}

View File

@ -21,6 +21,7 @@ import com.viaversion.viaversion.api.connection.UserConnection;
import com.viaversion.viaversion.api.minecraft.entities.Entity1_16_2Types;
import com.viaversion.viaversion.api.protocol.AbstractProtocol;
import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper;
import com.viaversion.viaversion.api.rewriter.EntityRewriter;
import com.viaversion.viaversion.api.type.Type;
import com.viaversion.viaversion.data.entity.EntityTrackerBase;
import com.viaversion.viaversion.protocols.protocol1_16_2to1_16_1.data.MappingData;
@ -30,7 +31,6 @@ import com.viaversion.viaversion.protocols.protocol1_16_2to1_16_1.packets.Invent
import com.viaversion.viaversion.protocols.protocol1_16_2to1_16_1.packets.WorldPackets;
import com.viaversion.viaversion.protocols.protocol1_16to1_15_2.ClientboundPackets1_16;
import com.viaversion.viaversion.protocols.protocol1_16to1_15_2.ServerboundPackets1_16;
import com.viaversion.viaversion.rewriter.EntityRewriter;
import com.viaversion.viaversion.rewriter.RegistryType;
import com.viaversion.viaversion.rewriter.SoundRewriter;
import com.viaversion.viaversion.rewriter.StatisticsRewriter;
@ -39,6 +39,7 @@ import com.viaversion.viaversion.rewriter.TagRewriter;
public class Protocol1_16_2To1_16_1 extends AbstractProtocol<ClientboundPackets1_16, ClientboundPackets1_16_2, ServerboundPackets1_16, ServerboundPackets1_16_2> {
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<ClientboundPackets1
@Override
protected void registerPackets() {
EntityRewriter metadataRewriter = new MetadataRewriter1_16_2To1_16_1(this);
metadataRewriter.register();
EntityPackets.register(this);
WorldPackets.register(this);
InventoryPackets.register(this);
tagRewriter = new TagRewriter(this, metadataRewriter::newEntityId);
tagRewriter = new TagRewriter(this);
tagRewriter.register(ClientboundPackets1_16.TAGS, RegistryType.ENTITY);
new StatisticsRewriter(this, metadataRewriter::newEntityId).register(ClientboundPackets1_16.STATISTICS);
new StatisticsRewriter(this).register(ClientboundPackets1_16.STATISTICS);
SoundRewriter soundRewriter = new SoundRewriter(this);
soundRewriter.registerSound(ClientboundPackets1_16.SOUND);
@ -121,4 +121,9 @@ public class Protocol1_16_2To1_16_1 extends AbstractProtocol<ClientboundPackets1
public MappingData getMappingData() {
return MAPPINGS;
}
@Override
public EntityRewriter getEntityRewriter() {
return metadataRewriter;
}
}

View File

@ -68,7 +68,7 @@ public class MetadataRewriter1_16_2To1_16_1 extends EntityRewriter<Protocol1_16_
}
@Override
protected EntityType typeFromId(int type) {
public EntityType typeFromId(int type) {
return Entity1_16_2Types.getTypeFromId(type);
}
}

View File

@ -27,6 +27,7 @@ import com.viaversion.viaversion.api.minecraft.entities.Entity1_16Types;
import com.viaversion.viaversion.api.protocol.AbstractProtocol;
import com.viaversion.viaversion.api.protocol.packet.State;
import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper;
import com.viaversion.viaversion.api.rewriter.EntityRewriter;
import com.viaversion.viaversion.api.type.Type;
import com.viaversion.viaversion.data.entity.EntityTrackerBase;
import com.viaversion.viaversion.protocols.protocol1_14to1_13_2.ServerboundPackets1_14;
@ -39,7 +40,6 @@ import com.viaversion.viaversion.protocols.protocol1_16to1_15_2.packets.Inventor
import com.viaversion.viaversion.protocols.protocol1_16to1_15_2.packets.WorldPackets;
import com.viaversion.viaversion.protocols.protocol1_16to1_15_2.storage.InventoryTracker1_16;
import com.viaversion.viaversion.rewriter.ComponentRewriter;
import com.viaversion.viaversion.rewriter.EntityRewriter;
import com.viaversion.viaversion.rewriter.RegistryType;
import com.viaversion.viaversion.rewriter.SoundRewriter;
import com.viaversion.viaversion.rewriter.StatisticsRewriter;
@ -55,6 +55,7 @@ public class Protocol1_16To1_15_2 extends AbstractProtocol<ClientboundPackets1_1
private static final UUID ZERO_UUID = new UUID(0, 0);
public static final MappingData MAPPINGS = new MappingData();
private final EntityRewriter metadataRewriter = new MetadataRewriter1_16To1_15_2(this);
private TagRewriter tagRewriter;
public Protocol1_16To1_15_2() {
@ -63,17 +64,16 @@ public class Protocol1_16To1_15_2 extends AbstractProtocol<ClientboundPackets1_1
@Override
protected void registerPackets() {
EntityRewriter metadataRewriter = new MetadataRewriter1_16To1_15_2(this);
metadataRewriter.register();
EntityPackets.register(this);
WorldPackets.register(this);
InventoryPackets.register(this);
tagRewriter = new TagRewriter(this, metadataRewriter::newEntityId);
tagRewriter = new TagRewriter(this);
tagRewriter.register(ClientboundPackets1_15.TAGS, RegistryType.ENTITY);
new StatisticsRewriter(this, metadataRewriter::newEntityId).register(ClientboundPackets1_15.STATISTICS);
new StatisticsRewriter(this).register(ClientboundPackets1_15.STATISTICS);
// Login Success
registerClientbound(State.LOGIN, 0x02, 0x02, new PacketRemapper() {
@ -281,4 +281,9 @@ public class Protocol1_16To1_15_2 extends AbstractProtocol<ClientboundPackets1_1
public MappingData getMappingData() {
return MAPPINGS;
}
@Override
public EntityRewriter getEntityRewriter() {
return metadataRewriter;
}
}

View File

@ -69,7 +69,7 @@ public class MetadataRewriter1_16To1_15_2 extends EntityRewriter<Protocol1_16To1
}
@Override
protected EntityType typeFromId(int type) {
public EntityType typeFromId(int type) {
return Entity1_16Types.getTypeFromId(type);
}
}

View File

@ -25,6 +25,7 @@ import com.viaversion.viaversion.api.minecraft.entities.Entity1_17Types;
import com.viaversion.viaversion.api.protocol.AbstractProtocol;
import com.viaversion.viaversion.api.protocol.packet.ClientboundPacketType;
import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper;
import com.viaversion.viaversion.api.rewriter.EntityRewriter;
import com.viaversion.viaversion.api.type.Type;
import com.viaversion.viaversion.data.entity.EntityTrackerBase;
import com.viaversion.viaversion.protocols.protocol1_16_2to1_16_1.ClientboundPackets1_16_2;
@ -34,7 +35,6 @@ import com.viaversion.viaversion.protocols.protocol1_17to1_16_4.packets.EntityPa
import com.viaversion.viaversion.protocols.protocol1_17to1_16_4.packets.InventoryPackets;
import com.viaversion.viaversion.protocols.protocol1_17to1_16_4.packets.WorldPackets;
import com.viaversion.viaversion.protocols.protocol1_17to1_16_4.storage.InventoryAcknowledgements;
import com.viaversion.viaversion.rewriter.EntityRewriter;
import com.viaversion.viaversion.rewriter.RegistryType;
import com.viaversion.viaversion.rewriter.SoundRewriter;
import com.viaversion.viaversion.rewriter.StatisticsRewriter;
@ -44,6 +44,7 @@ public class Protocol1_17To1_16_4 extends AbstractProtocol<ClientboundPackets1_1
public static final MappingData MAPPINGS = new MappingDataBase("1.16.2", "1.17", true);
private static final String[] NEW_GAME_EVENT_TAGS = {"minecraft:ignore_vibrations_sneaking", "minecraft:vibrations"};
private final EntityRewriter metadataRewriter = new MetadataRewriter1_17To1_16_4(this);
private TagRewriter tagRewriter;
public Protocol1_17To1_16_4() {
@ -52,14 +53,13 @@ public class Protocol1_17To1_16_4 extends AbstractProtocol<ClientboundPackets1_1
@Override
protected void registerPackets() {
EntityRewriter metadataRewriter = new MetadataRewriter1_17To1_16_4(this);
metadataRewriter.register();
EntityPackets.register(this);
InventoryPackets.register(this);
WorldPackets.register(this);
tagRewriter = new TagRewriter(this, null);
tagRewriter = new TagRewriter(this);
registerClientbound(ClientboundPackets1_16_2.TAGS, new PacketRemapper() {
@Override
public void registerMap() {
@ -90,7 +90,7 @@ public class Protocol1_17To1_16_4 extends AbstractProtocol<ClientboundPackets1_1
}
});
new StatisticsRewriter(this, metadataRewriter::newEntityId).register(ClientboundPackets1_16_2.STATISTICS);
new StatisticsRewriter(this).register(ClientboundPackets1_16_2.STATISTICS);
SoundRewriter soundRewriter = new SoundRewriter(this);
soundRewriter.registerSound(ClientboundPackets1_16_2.SOUND);
@ -245,4 +245,9 @@ public class Protocol1_17To1_16_4 extends AbstractProtocol<ClientboundPackets1_1
public MappingData getMappingData() {
return MAPPINGS;
}
@Override
public EntityRewriter getEntityRewriter() {
return metadataRewriter;
}
}

View File

@ -61,7 +61,7 @@ public class MetadataRewriter1_17To1_16_4 extends EntityRewriter {
}
@Override
protected EntityType typeFromId(int type) {
public EntityType typeFromId(int type) {
return Entity1_17Types.getTypeFromId(type);
}
}

View File

@ -28,6 +28,7 @@ import com.viaversion.viaversion.api.protocol.packet.PacketWrapper;
import com.viaversion.viaversion.api.protocol.packet.State;
import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper;
import com.viaversion.viaversion.api.protocol.remapper.ValueTransformer;
import com.viaversion.viaversion.api.rewriter.EntityRewriter;
import com.viaversion.viaversion.api.type.Type;
import com.viaversion.viaversion.protocols.protocol1_8.ClientboundPackets1_8;
import com.viaversion.viaversion.protocols.protocol1_8.ServerboundPackets1_8;
@ -48,7 +49,6 @@ import com.viaversion.viaversion.protocols.protocol1_9to1_8.storage.CommandBlock
import com.viaversion.viaversion.protocols.protocol1_9to1_8.storage.EntityTracker1_9;
import com.viaversion.viaversion.protocols.protocol1_9to1_8.storage.InventoryTracker;
import com.viaversion.viaversion.protocols.protocol1_9to1_8.storage.MovementTracker;
import com.viaversion.viaversion.rewriter.EntityRewriter;
import com.viaversion.viaversion.util.GsonUtil;
public class Protocol1_9To1_8 extends AbstractProtocol<ClientboundPackets1_8, ClientboundPackets1_9, ServerboundPackets1_8, ServerboundPackets1_9> {
@ -58,6 +58,7 @@ public class Protocol1_9To1_8 extends AbstractProtocol<ClientboundPackets1_8, Cl
return fixJson(line);
}
};
private final EntityRewriter metadataRewriter = new MetadataRewriter1_9To1_8(this);
public Protocol1_9To1_8() {
super(ClientboundPackets1_8.class, ClientboundPackets1_9.class, ServerboundPackets1_8.class, ServerboundPackets1_9.class);
@ -108,7 +109,6 @@ public class Protocol1_9To1_8 extends AbstractProtocol<ClientboundPackets1_8, Cl
@Override
protected void registerPackets() {
EntityRewriter metadataRewriter = new MetadataRewriter1_9To1_8(this);
metadataRewriter.register();
// Disconnect workaround (JSON!)
@ -157,4 +157,9 @@ public class Protocol1_9To1_8 extends AbstractProtocol<ClientboundPackets1_8, Cl
// CommandBlock storage
userConnection.put(new CommandBlockStorage());
}
@Override
public EntityRewriter getEntityRewriter() {
return metadataRewriter;
}
}

View File

@ -137,12 +137,12 @@ public class MetadataRewriter1_9To1_8 extends EntityRewriter<Protocol1_9To1_8> {
}
@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);
}
}

View File

@ -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<T extends Protocol> extends RewriterBase<T> {
public abstract class EntityRewriter<T extends Protocol> extends RewriterBase<T> implements com.viaversion.viaversion.api.rewriter.EntityRewriter<T> {
private static final Metadata[] EMPTY_ARRAY = new Metadata[0];
protected final List<MetaFilter> metadataFilters = new ArrayList<>();
protected final boolean trackMappedType;
@ -92,13 +93,7 @@ public abstract class EntityRewriter<T extends Protocol> extends RewriterBase<T>
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<Metadata> 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<T extends Protocol> extends RewriterBase<T>
protected void handleMetadata(int entityId, @Nullable EntityType type, Metadata metadata, List<Metadata> 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<T extends Protocol> extends RewriterBase<T>
particle.setId(protocol.getMappingData().getNewParticleId(id));
}
/**
* Returns the entity tracker for the current protocol.
*
* @param connection user connection
* @param <E> entity tracker type
* @return entity tracker
*/
public <E extends EntityTracker> E tracker(UserConnection connection) {
return connection.getEntityTracker(protocol.getClass());
}
private void logException(Exception e, @Nullable EntityType type, List<Metadata> metadataList, Metadata metadata) {
if (!Via.getConfig().isSuppressMetadataErrors() || Via.getManager().isDebug()) {
Logger logger = Via.getPlatform().getLogger();

View File

@ -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 <http://www.gnu.org/licenses/>.
*/
package com.viaversion.viaversion.rewriter;
import com.viaversion.viaversion.api.protocol.Protocol;
public interface Rewriter<T extends Protocol> {
/**
* Registers any packet handlers or rewrites needed.
*/
void register();
/**
* Returns the {@link Protocol} instance of this rewriter.
*
* @return protocol of the rewriter
*/
T protocol();
}

View File

@ -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 <http://www.gnu.org/licenses/>.
*/
package com.viaversion.viaversion.rewriter;
import com.viaversion.viaversion.api.protocol.Protocol;
public abstract class RewriterBase<T extends Protocol> implements Rewriter<T> {
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;
}
}

View File

@ -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);
}

View File

@ -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<RegistryType, List<TagData>> 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: