Small cleanup of a few rewriters

This commit is contained in:
Nassim Jahnke 2024-08-25 18:42:21 +02:00
parent a993a08b8d
commit 2a71612aea
No known key found for this signature in database
GPG Key ID: EF6771C01F6EF02F
10 changed files with 73 additions and 83 deletions

View File

@ -199,6 +199,10 @@ public class PacketWrapperImpl implements PacketWrapper {
@Override
public <T> T passthroughAndMap(Type<?> type, Type<T> mappedType) throws InformativeException {
if (type == mappedType) {
return passthrough(mappedType);
}
final Object value = read(type);
final T mappedValue = attemptTransform(mappedType, value);
write(mappedType, mappedValue);

View File

@ -68,7 +68,7 @@ public final class Protocol1_16_4To1_17 extends AbstractProtocol<ClientboundPack
wrapper.write(Types.STRING, type.resourceLocation());
// Id conversion
tagRewriter.handle(wrapper, tagRewriter.getRewriter(type), tagRewriter.getNewTags(type));
tagRewriter.handle(wrapper, type);
// Stop iterating after entity types
if (type == RegistryType.ENTITY) {
@ -214,4 +214,4 @@ public final class Protocol1_16_4To1_17 extends AbstractProtocol<ClientboundPack
public TagRewriter<ClientboundPackets1_16_2> getTagRewriter() {
return tagRewriter;
}
}
}

View File

@ -29,18 +29,11 @@ import com.viaversion.viaversion.api.protocol.packet.provider.PacketTypesProvide
import com.viaversion.viaversion.api.protocol.packet.provider.SimplePacketTypesProvider;
import com.viaversion.viaversion.api.protocol.remapper.PacketHandler;
import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers;
import com.viaversion.viaversion.api.type.Type;
import com.viaversion.viaversion.api.type.Types;
import com.viaversion.viaversion.api.type.types.misc.ParticleType;
import com.viaversion.viaversion.api.type.types.version.Types1_20_3;
import com.viaversion.viaversion.data.entity.EntityTrackerBase;
import com.viaversion.viaversion.protocols.v1_19_3to1_19_4.rewriter.CommandRewriter1_19_4;
import com.viaversion.viaversion.protocols.v1_20to1_20_2.packet.ClientboundConfigurationPackets1_20_2;
import com.viaversion.viaversion.protocols.v1_20to1_20_2.packet.ClientboundPacket1_20_2;
import com.viaversion.viaversion.protocols.v1_20to1_20_2.packet.ClientboundPackets1_20_2;
import com.viaversion.viaversion.protocols.v1_20to1_20_2.packet.ServerboundConfigurationPackets1_20_2;
import com.viaversion.viaversion.protocols.v1_20to1_20_2.packet.ServerboundPacket1_20_2;
import com.viaversion.viaversion.protocols.v1_20to1_20_2.packet.ServerboundPackets1_20_2;
import com.viaversion.viaversion.protocols.v1_20_2to1_20_3.packet.ClientboundConfigurationPackets1_20_3;
import com.viaversion.viaversion.protocols.v1_20_2to1_20_3.packet.ClientboundPacket1_20_3;
import com.viaversion.viaversion.protocols.v1_20_2to1_20_3.packet.ClientboundPackets1_20_3;
@ -48,6 +41,12 @@ import com.viaversion.viaversion.protocols.v1_20_2to1_20_3.packet.ServerboundPac
import com.viaversion.viaversion.protocols.v1_20_2to1_20_3.packet.ServerboundPackets1_20_3;
import com.viaversion.viaversion.protocols.v1_20_2to1_20_3.rewriter.BlockItemPacketRewriter1_20_3;
import com.viaversion.viaversion.protocols.v1_20_2to1_20_3.rewriter.EntityPacketRewriter1_20_3;
import com.viaversion.viaversion.protocols.v1_20to1_20_2.packet.ClientboundConfigurationPackets1_20_2;
import com.viaversion.viaversion.protocols.v1_20to1_20_2.packet.ClientboundPacket1_20_2;
import com.viaversion.viaversion.protocols.v1_20to1_20_2.packet.ClientboundPackets1_20_2;
import com.viaversion.viaversion.protocols.v1_20to1_20_2.packet.ServerboundConfigurationPackets1_20_2;
import com.viaversion.viaversion.protocols.v1_20to1_20_2.packet.ServerboundPacket1_20_2;
import com.viaversion.viaversion.protocols.v1_20to1_20_2.packet.ServerboundPackets1_20_2;
import com.viaversion.viaversion.rewriter.SoundRewriter;
import com.viaversion.viaversion.rewriter.StatisticsRewriter;
import com.viaversion.viaversion.rewriter.TagRewriter;
@ -306,7 +305,7 @@ public final class Protocol1_20_2To1_20_3 extends AbstractProtocol<ClientboundPa
registerServerbound(ServerboundConfigurationPackets1_20_2.RESOURCE_PACK, resourcePackStatusHandler());
registerClientbound(ClientboundConfigurationPackets1_20_2.RESOURCE_PACK, ClientboundConfigurationPackets1_20_3.RESOURCE_PACK_PUSH, resourcePackHandler(ClientboundConfigurationPackets1_20_3.RESOURCE_PACK_POP));
registerClientbound(ClientboundConfigurationPackets1_20_2.UPDATE_TAGS, tagRewriter.getGenericHandler());
tagRewriter.registerGeneric(ClientboundConfigurationPackets1_20_2.UPDATE_TAGS);
}
private PacketHandler resourcePackStatusHandler() {

View File

@ -304,8 +304,10 @@ public final class BlockItemPacketRewriter1_20_5 extends ItemRewriter<Clientboun
wrapper.passthrough(Types.FLOAT); // Knockback Z
wrapper.passthrough(Types.VAR_INT); // Block interaction type
protocol.getEntityRewriter().rewriteParticle(wrapper, Types1_20_3.PARTICLE, Types1_20_5.PARTICLE); // Small explosion particle
protocol.getEntityRewriter().rewriteParticle(wrapper, Types1_20_3.PARTICLE, Types1_20_5.PARTICLE); // Large explosion particle
final Particle smallExplosionParticle = wrapper.passthroughAndMap(Types1_20_3.PARTICLE, Types1_20_5.PARTICLE);
final Particle largeExplosionParticle = wrapper.passthroughAndMap(Types1_20_3.PARTICLE, Types1_20_5.PARTICLE);
rewriteParticle(wrapper.user(), smallExplosionParticle);
rewriteParticle(wrapper.user(), largeExplosionParticle);
final String sound = wrapper.read(Types.STRING);
final Float range = wrapper.read(Types.OPTIONAL_FLOAT);

View File

@ -116,12 +116,12 @@ public final class Protocol1_20To1_20_2 extends AbstractProtocol<ClientboundPack
});
registerClientbound(ClientboundPackets1_19_4.UPDATE_TAGS, wrapper -> {
tagRewriter.getGenericHandler().handle(wrapper);
tagRewriter.handleGeneric(wrapper);
wrapper.resetReader();
wrapper.user().put(new LastTags(wrapper));
});
registerClientbound(State.CONFIGURATION, ClientboundConfigurationPackets1_20_2.UPDATE_TAGS.getId(), ClientboundConfigurationPackets1_20_2.UPDATE_TAGS.getId(), wrapper -> {
tagRewriter.getGenericHandler().handle(wrapper);
tagRewriter.handleGeneric(wrapper);
wrapper.resetReader();
wrapper.user().put(new LastTags(wrapper));
});

View File

@ -19,6 +19,7 @@ package com.viaversion.viaversion.protocols.v1_20to1_20_2.storage;
import com.viaversion.viaversion.api.connection.StorableObject;
import com.viaversion.viaversion.api.connection.UserConnection;
import com.viaversion.viaversion.api.minecraft.TagData;
import com.viaversion.viaversion.api.protocol.packet.PacketWrapper;
import com.viaversion.viaversion.api.type.Types;
import com.viaversion.viaversion.protocols.v1_20to1_20_2.Protocol1_20To1_20_2;
@ -34,15 +35,14 @@ public class LastTags implements StorableObject {
public LastTags(final PacketWrapper wrapper) {
final int length = wrapper.passthrough(Types.VAR_INT);
for (int i = 0; i < length; i++) {
final List<Tag> tags = new ArrayList<>();
final List<TagData> tags = new ArrayList<>();
final String registryKey = wrapper.passthrough(Types.STRING);
final int tagsSize = wrapper.passthrough(Types.VAR_INT);
for (int j = 0; j < tagsSize; j++) {
final String key = wrapper.passthrough(Types.STRING);
final int[] ids = wrapper.passthrough(Types.VAR_INT_ARRAY_PRIMITIVE);
tags.add(new Tag(key, ids));
tags.add(new TagData(key, ids));
}
this.registryTags.add(new RegistryTags(registryKey, tags));
}
}
@ -57,17 +57,14 @@ public class LastTags implements StorableObject {
for (final RegistryTags registryTag : registryTags) {
packet.write(Types.STRING, registryTag.registryKey);
packet.write(Types.VAR_INT, registryTag.tags.size());
for (final Tag tag : registryTag.tags) {
packet.write(Types.STRING, tag.key);
packet.write(Types.VAR_INT_ARRAY_PRIMITIVE, Arrays.copyOf(tag.ids, tag.ids.length));
for (final TagData tag : registryTag.tags) {
packet.write(Types.STRING, tag.identifier());
packet.write(Types.VAR_INT_ARRAY_PRIMITIVE, Arrays.copyOf(tag.entries(), tag.entries().length));
}
}
packet.send(Protocol1_20To1_20_2.class);
}
private record RegistryTags(String registryKey, List<Tag> tags) {
}
private record Tag(String key, int[] ids) {
private record RegistryTags(String registryKey, List<TagData> tags) {
}
}

View File

@ -38,7 +38,6 @@ import com.viaversion.viaversion.api.minecraft.entitydata.EntityDataType;
import com.viaversion.viaversion.api.minecraft.item.Item;
import com.viaversion.viaversion.api.protocol.Protocol;
import com.viaversion.viaversion.api.protocol.packet.ClientboundPacketType;
import com.viaversion.viaversion.api.protocol.packet.PacketWrapper;
import com.viaversion.viaversion.api.protocol.remapper.PacketHandler;
import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers;
import com.viaversion.viaversion.api.rewriter.ItemRewriter;
@ -602,12 +601,6 @@ public abstract class EntityRewriter<C extends ClientboundPacketType, T extends
particle.setId(protocol.getMappingData().getNewParticleId(id));
}
public void rewriteParticle(PacketWrapper wrapper, Type<Particle> from, Type<Particle> to) {
final Particle particle = wrapper.read(from);
rewriteParticle(wrapper.user(), particle);
wrapper.write(to, particle);
}
private void logException(Exception e, @Nullable EntityType type, List<EntityData> entityDataList, EntityData entityData) {
if (!Via.getConfig().isSuppressMetadataErrors() || Via.getManager().isDebug()) {
protocol.getLogger().severe("An error occurred in entity data handler " + this.getClass().getSimpleName()

View File

@ -95,17 +95,11 @@ public class ItemRewriter<C extends ClientboundPacketType, S extends Serverbound
}
public void registerSetContent(C packetType) {
protocol.registerClientbound(packetType, new PacketHandlers() {
@Override
public void register() {
map(Types.UNSIGNED_BYTE); // Window id
handler(wrapper -> {
Item[] items = wrapper.read(itemArrayType);
wrapper.write(mappedItemArrayType, items);
for (int i = 0; i < items.length; i++) {
items[i] = handleItemToClient(wrapper.user(), items[i]);
}
});
protocol.registerClientbound(packetType, wrapper -> {
wrapper.passthrough(Types.UNSIGNED_BYTE); // Container id
Item[] items = wrapper.passthroughAndMap(itemArrayType, mappedItemArrayType);
for (int i = 0; i < items.length; i++) {
items[i] = handleItemToClient(wrapper.user(), items[i]);
}
});
}
@ -117,8 +111,7 @@ public class ItemRewriter<C extends ClientboundPacketType, S extends Serverbound
map(Types.UNSIGNED_BYTE); // Window id
map(Types.VAR_INT); // State id
handler(wrapper -> {
Item[] items = wrapper.read(itemArrayType);
wrapper.write(mappedItemArrayType, items);
Item[] items = wrapper.passthroughAndMap(itemArrayType, mappedItemArrayType);
for (int i = 0; i < items.length; i++) {
items[i] = handleItemToClient(wrapper.user(), items[i]);
}
@ -522,9 +515,8 @@ public class ItemRewriter<C extends ClientboundPacketType, S extends Serverbound
map(Types.FLOAT); // Particle Data
map(Types.INT); // Particle Count
handler(wrapper -> {
final Particle particle = wrapper.read(particleType);
final Particle particle = wrapper.passthroughAndMap(particleType, mappedParticleType);
rewriteParticle(wrapper.user(), particle);
wrapper.write(mappedParticleType, particle);
});
}
});
@ -548,10 +540,8 @@ public class ItemRewriter<C extends ClientboundPacketType, S extends Serverbound
wrapper.passthrough(Types.FLOAT); // Knockback Z
wrapper.passthrough(Types.VAR_INT); // Block interaction type
final Particle smallExplosionParticle = wrapper.read(particleType);
final Particle largeExplosionParticle = wrapper.read(particleType);
wrapper.write(mappedParticleType, smallExplosionParticle);
wrapper.write(mappedParticleType, largeExplosionParticle);
final Particle smallExplosionParticle = wrapper.passthroughAndMap(particleType, mappedParticleType);
final Particle largeExplosionParticle = wrapper.passthroughAndMap(particleType, mappedParticleType);
rewriteParticle(wrapper.user(), smallExplosionParticle);
rewriteParticle(wrapper.user(), largeExplosionParticle);

View File

@ -158,8 +158,7 @@ public class RecipeRewriter<C extends ClientboundPacketType> {
}
protected void handleIngredient(final PacketWrapper wrapper) {
final Item[] items = wrapper.read(itemArrayType());
wrapper.write(mappedItemArrayType(), items);
final Item[] items = wrapper.passthroughAndMap(itemArrayType(), mappedItemArrayType());
for (int i = 0; i < items.length; i++) {
Item item = items[i];
items[i] = rewrite(wrapper.user(), item);

View File

@ -125,13 +125,13 @@ public class TagRewriter<C extends ClientboundPacketType> implements com.viavers
}
public void registerGeneric(C packetType) {
protocol.registerClientbound(packetType, getGenericHandler());
protocol.registerClientbound(packetType, this::handleGeneric);
}
public PacketHandler getHandler(@Nullable RegistryType readUntilType) {
return wrapper -> {
for (RegistryType type : RegistryType.getValues()) {
handle(wrapper, getRewriter(type), getNewTags(type), toRename.get(type), toRemove.get(type));
handle(wrapper, type);
// Stop iterating
if (type == readUntilType) {
@ -141,40 +141,46 @@ public class TagRewriter<C extends ClientboundPacketType> implements com.viavers
};
}
public PacketHandler getGenericHandler() {
return wrapper -> {
final int length = wrapper.passthrough(Types.VAR_INT);
int editedLength = length;
for (int i = 0; i < length; i++) {
String registryKey = wrapper.read(Types.STRING);
if (toRemoveRegistries.contains(Key.stripMinecraftNamespace(registryKey))) {
wrapper.set(Types.VAR_INT, 0, --editedLength);
int tagsSize = wrapper.read(Types.VAR_INT);
for (int j = 0; j < tagsSize; j++) {
wrapper.read(Types.STRING);
wrapper.read(Types.VAR_INT_ARRAY_PRIMITIVE);
}
continue;
}
wrapper.write(Types.STRING, registryKey);
registryKey = Key.stripMinecraftNamespace(registryKey);
RegistryType type = RegistryType.getByKey(registryKey);
if (type != null) {
handle(wrapper, getRewriter(type), getNewTags(type), toRename.get(type), toRemove.get(type));
} else {
handle(wrapper, null, null, null, null);
public void handleGeneric(final PacketWrapper wrapper) {
final int length = wrapper.passthrough(Types.VAR_INT);
int editedLength = length;
for (int i = 0; i < length; i++) {
final String registryKey = wrapper.read(Types.STRING);
if (toRemoveRegistries.contains(Key.stripMinecraftNamespace(registryKey))) {
wrapper.set(Types.VAR_INT, 0, --editedLength);
final int tagsSize = wrapper.read(Types.VAR_INT);
for (int j = 0; j < tagsSize; j++) {
wrapper.read(Types.STRING);
wrapper.read(Types.VAR_INT_ARRAY_PRIMITIVE);
}
continue;
}
};
wrapper.write(Types.STRING, registryKey);
handle(wrapper, Key.stripMinecraftNamespace(registryKey));
}
}
public void handle(PacketWrapper wrapper, @Nullable IdRewriteFunction rewriteFunction, @Nullable List<TagData> newTags) {
handle(wrapper, rewriteFunction, newTags, null, null);
public void handle(final PacketWrapper wrapper, final String registryKey) {
final RegistryType type = RegistryType.getByKey(registryKey);
if (type != null) {
handle(wrapper, type);
} else {
handle(wrapper, null, null, null, null);
}
}
public void handle(PacketWrapper wrapper, @Nullable IdRewriteFunction rewriteFunction, @Nullable List<TagData> newTags, @Nullable Map<String, String> tagsToRename, @Nullable Set<String> tagsToRemove) {
public void handle(PacketWrapper wrapper, RegistryType registryType) {
handle(wrapper, getRewriter(registryType), getNewTags(registryType), toRename.get(registryType), toRemove.get(registryType));
}
protected void handle(
PacketWrapper wrapper,
@Nullable IdRewriteFunction rewriteFunction,
@Nullable List<TagData> newTags,
@Nullable Map<String, String> tagsToRename,
@Nullable Set<String> tagsToRemove
) {
final int tagsSize = wrapper.read(Types.VAR_INT);
final List<TagData> tags = new ArrayList<>(newTags != null ? tagsSize + newTags.size() : tagsSize);
final Set<String> currentTags = new HashSet<>(tagsSize);
@ -260,4 +266,4 @@ public class TagRewriter<C extends ClientboundPacketType> implements com.viavers
case FLUID, GAME_EVENT -> null;
};
}
}
}