Partially implement mapped item handling

This commit is contained in:
Nassim Jahnke 2024-02-29 20:21:18 +01:00
parent 66896d2027
commit 445d798a17
No known key found for this signature in database
GPG Key ID: EF6771C01F6EF02F
7 changed files with 157 additions and 113 deletions

View File

@ -0,0 +1,129 @@
/*
* This file is part of ViaBackwards - https://github.com/ViaVersion/ViaBackwards
* Copyright (C) 2016-2024 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.viabackwards.api.rewriters;
import com.viaversion.viabackwards.api.BackwardsProtocol;
import com.viaversion.viabackwards.api.data.BackwardsMappings;
import com.viaversion.viabackwards.api.data.MappedItem;
import com.viaversion.viaversion.api.minecraft.data.StructuredData;
import com.viaversion.viaversion.api.minecraft.data.StructuredDataContainer;
import com.viaversion.viaversion.api.minecraft.data.StructuredDataKey;
import com.viaversion.viaversion.api.minecraft.item.Item;
import com.viaversion.viaversion.api.protocol.packet.ClientboundPacketType;
import com.viaversion.viaversion.api.protocol.packet.ServerboundPacketType;
import com.viaversion.viaversion.api.type.Type;
import com.viaversion.viaversion.libs.opennbt.tag.builtin.CompoundTag;
import com.viaversion.viaversion.libs.opennbt.tag.builtin.IntTag;
import com.viaversion.viaversion.libs.opennbt.tag.builtin.NumberTag;
import com.viaversion.viaversion.libs.opennbt.tag.builtin.Tag;
import org.checkerframework.checker.nullness.qual.Nullable;
public class BackwardsStructuredItemRewriter<C extends ClientboundPacketType, S extends ServerboundPacketType,
T extends BackwardsProtocol<C, ?, ?, S>> extends ItemRewriter<C, S, T> {
public BackwardsStructuredItemRewriter(final T protocol, final Type<Item> itemType, final Type<Item[]> itemArrayType) {
super(protocol, itemType, itemArrayType);
}
public BackwardsStructuredItemRewriter(final T protocol, final Type<Item> itemType, final Type<Item[]> itemArrayType, final Type<Item> mappedItemType, final Type<Item[]> mappedItemArrayType) {
super(protocol, itemType, itemArrayType, mappedItemType, mappedItemArrayType);
}
@Override
public @Nullable Item handleItemToClient(@Nullable final Item item) {
if (item == null) {
return null;
}
final StructuredDataContainer data = item.structuredData();
data.setIdLookup(protocol, true);
// TODO Translatable rewriter on name and lore
final BackwardsMappings mappingData = protocol.getMappingData();
final MappedItem mappedItem = mappingData != null ? mappingData.getMappedItem(item.identifier()) : null;
if (mappedItem == null) {
// Just rewrite the id
if (mappingData != null && mappingData.getItemMappings() != null) {
item.setIdentifier(mappingData.getNewItemId(item.identifier()));
}
return item;
}
// Save original id, set remapped id
final CompoundTag tag = createCustomTag(item);
tag.putInt(nbtTagName + "|id", item.identifier());
item.setIdentifier(mappedItem.getId());
// Add custom model data
if (mappedItem.customModelData() != null && !data.contains(StructuredDataKey.CUSTOM_MODEL_DATA)) {
data.add(StructuredDataKey.CUSTOM_MODEL_DATA, mappedItem.customModelData());
}
// TODO custom name
return item;
}
@Override
public @Nullable Item handleItemToServer(@Nullable final Item item) {
if (item == null) {
return null;
}
final BackwardsMappings mappingData = protocol.getMappingData();
if (mappingData != null && mappingData.getItemMappings() != null) {
item.setIdentifier(mappingData.getOldItemId(item.identifier()));
}
final StructuredDataContainer data = item.structuredData();
data.setIdLookup(protocol, false);
final CompoundTag tag = customTag(item);
if (tag != null) {
final Tag originalId = tag.remove(nbtTagName + "|id");
if (originalId instanceof IntTag) {
item.setIdentifier(((NumberTag) originalId).asInt());
}
}
restoreDisplayTag(item);
return item;
}
protected @Nullable CompoundTag customTag(final Item item) {
final StructuredData<CompoundTag> customData = item.structuredData().getNonEmpty(StructuredDataKey.CUSTOM_DATA);
return customData != null ? customData.value() : null;
}
protected CompoundTag createCustomTag(final Item item) {
final StructuredDataContainer data = item.structuredData();
final StructuredData<CompoundTag> customData = data.getNonEmpty(StructuredDataKey.CUSTOM_DATA);
if (customData != null) {
return customData.value();
}
final CompoundTag tag = new CompoundTag();
data.add(StructuredDataKey.CUSTOM_DATA, tag);
return tag;
}
@Override
protected void restoreDisplayTag(final Item item) {
// TODO
}
}

View File

@ -155,7 +155,9 @@ public class ItemRewriter<C extends ClientboundPacketType, S extends Serverbound
translatableRewriter.processText(description);
}
handleItemToClient(wrapper.passthrough(getItemType())); // Icon
final Item icon = handleItemToClient(wrapper.read(itemType()));
wrapper.write(mappedItemType(), icon);
wrapper.passthrough(Type.VAR_INT); // Frame type
int flags = wrapper.passthrough(Type.INT); // Flags
if ((flags & 1) != 0) {
@ -201,7 +203,9 @@ public class ItemRewriter<C extends ClientboundPacketType, S extends Serverbound
translatableRewriter.processTag(description);
}
handleItemToClient(wrapper.passthrough(Type.ITEM1_20_2)); // Icon
final Item icon = handleItemToClient(wrapper.read(itemType()));
wrapper.write(mappedItemType(), icon);
wrapper.passthrough(Type.VAR_INT); // Frame type
final int flags = wrapper.passthrough(Type.INT);
if ((flags & 1) != 0) {

View File

@ -1,79 +0,0 @@
/*
* This file is part of ViaBackwards - https://github.com/ViaVersion/ViaBackwards
* Copyright (C) 2016-2024 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.viabackwards.api.rewriters;
import com.viaversion.viabackwards.api.BackwardsProtocol;
import com.viaversion.viabackwards.api.data.MappedItem;
import com.viaversion.viaversion.api.minecraft.item.Item;
import com.viaversion.viaversion.api.protocol.packet.ClientboundPacketType;
import com.viaversion.viaversion.api.protocol.packet.ServerboundPacketType;
import com.viaversion.viaversion.api.type.Type;
import org.checkerframework.checker.nullness.qual.Nullable;
public class StructuredItemRewriter<C extends ClientboundPacketType, S extends ServerboundPacketType,
T extends BackwardsProtocol<C, ?, ?, S>> extends ItemRewriter<C, S, T> {
public StructuredItemRewriter(T protocol, Type<Item> itemType, Type<Item[]> itemArrayType) {
super(protocol, itemType, itemArrayType);
}
public StructuredItemRewriter(T protocol, Type<Item> itemType, Type<Item[]> itemArrayType, Type<Item> mappedItemType, Type<Item[]> mappedItemArrayType) {
super(protocol, itemType, itemArrayType, mappedItemType, mappedItemArrayType);
}
@Override
public @Nullable Item handleItemToClient(@Nullable Item item) {
if (item == null) {
return null;
}
// TODO Translatable rewriter on name and lore
MappedItem data = protocol.getMappingData() != null ? protocol.getMappingData().getMappedItem(item.identifier()) : null;
if (data == null) {
// Just rewrite the id
if (protocol.getMappingData() != null && protocol.getMappingData().getItemMappings() != null) {
item.setIdentifier(protocol.getMappingData().getNewItemId(item.identifier()));
}
}
// TODO Save original id, set remapped id
if (protocol.getMappingData() != null && protocol.getMappingData().getItemMappings() != null) {
item.setIdentifier(protocol.getMappingData().getNewItemId(item.identifier()));
}
// TODO custom model data and custom name
return item;
}
@Override
public @Nullable Item handleItemToServer(@Nullable Item item) {
if (item == null) return null;
if (protocol.getMappingData() != null && protocol.getMappingData().getItemMappings() != null) {
item.setIdentifier(protocol.getMappingData().getOldItemId(item.identifier()));
}
// TODO restore original id and display
return item;
}
@Override
protected void restoreDisplayTag(final Item item) {
// TODO
}
}

View File

@ -256,7 +256,12 @@ public class BlockItemPackets1_13 extends com.viaversion.viabackwards.api.rewrit
map(Type.UNSIGNED_BYTE);
map(Type.ITEM1_13_SHORT_ARRAY, Type.ITEM1_8_SHORT_ARRAY);
handler(itemArrayToClientHandler(Type.ITEM1_8_SHORT_ARRAY));
handler(wrapper -> {
final Item[] items = wrapper.get(Type.ITEM1_8_SHORT_ARRAY, 0);
for (Item item : items) {
handleItemToClient(item);
}
});
}
});

View File

@ -17,9 +17,9 @@
*/
package com.viaversion.viabackwards.protocol.protocol1_20_3to1_20_5.rewriter;
import com.viaversion.viabackwards.api.rewriters.ItemRewriter;
import com.viaversion.viabackwards.api.rewriters.StructuredItemRewriter;
import com.viaversion.viabackwards.api.rewriters.BackwardsStructuredItemRewriter;
import com.viaversion.viabackwards.protocol.protocol1_20_3to1_20_5.Protocol1_20_3To1_20_5;
import com.viaversion.viaversion.api.Via;
import com.viaversion.viaversion.api.minecraft.Particle;
import com.viaversion.viaversion.api.minecraft.item.Item;
import com.viaversion.viaversion.api.type.Type;
@ -29,13 +29,16 @@ import com.viaversion.viaversion.api.type.types.version.Types1_20_5;
import com.viaversion.viaversion.protocols.protocol1_20_3to1_20_2.packet.ServerboundPacket1_20_3;
import com.viaversion.viaversion.protocols.protocol1_20_3to1_20_2.packet.ServerboundPackets1_20_3;
import com.viaversion.viaversion.protocols.protocol1_20_3to1_20_2.rewriter.RecipeRewriter1_20_3;
import com.viaversion.viaversion.protocols.protocol1_20_5to1_20_3.Protocol1_20_5To1_20_3;
import com.viaversion.viaversion.protocols.protocol1_20_5to1_20_3.packet.ClientboundPacket1_20_5;
import com.viaversion.viaversion.protocols.protocol1_20_5to1_20_3.packet.ClientboundPackets1_20_5;
import com.viaversion.viaversion.rewriter.BlockRewriter;
import com.viaversion.viaversion.util.Key;
import org.checkerframework.checker.nullness.qual.Nullable;
public final class BlockItemPacketRewriter1_20_5 extends StructuredItemRewriter<ClientboundPacket1_20_5, ServerboundPacket1_20_3, Protocol1_20_3To1_20_5> {
public final class BlockItemPacketRewriter1_20_5 extends BackwardsStructuredItemRewriter<ClientboundPacket1_20_5, ServerboundPacket1_20_3, Protocol1_20_3To1_20_5> {
private final Protocol1_20_5To1_20_3 vvProtocol = Via.getManager().getProtocolManager().getProtocol(Protocol1_20_5To1_20_3.class);
public BlockItemPacketRewriter1_20_5(final Protocol1_20_3To1_20_5 protocol) {
super(protocol, Types1_20_5.ITEM, Types1_20_5.ITEM_ARRAY, Type.ITEM1_20_2, Type.ITEM1_20_2_ARRAY);
@ -135,27 +138,7 @@ public final class BlockItemPacketRewriter1_20_5 extends StructuredItemRewriter<
}
});
final RecipeRewriter1_20_3<ClientboundPacket1_20_5> recipeRewriter = new RecipeRewriter1_20_3<ClientboundPacket1_20_5>(protocol) {
@Override
protected Type<Item> itemType() {
return Types1_20_5.ITEM;
}
@Override
protected Type<Item[]> itemArrayType() {
return Types1_20_5.ITEM_ARRAY;
}
@Override
protected Type<Item> mappedItemType() {
return Type.ITEM1_20_2;
}
@Override
protected Type<Item[]> mappedItemArrayType() {
return Type.ITEM1_20_2_ARRAY;
}
};
final RecipeRewriter1_20_3<ClientboundPacket1_20_5> recipeRewriter = new RecipeRewriter1_20_3<>(protocol);
protocol.registerClientbound(ClientboundPackets1_20_5.DECLARE_RECIPES, wrapper -> {
final int size = wrapper.passthrough(Type.VAR_INT);
for (int i = 0; i < size; i++) {
@ -175,7 +158,7 @@ public final class BlockItemPacketRewriter1_20_5 extends StructuredItemRewriter<
if (item == null) return null;
super.handleItemToClient(item);
return com.viaversion.viaversion.protocols.protocol1_20_5to1_20_3.rewriter.BlockItemPacketRewriter1_20_5.toOldItem(item);
return vvProtocol.getItemRewriter().toOldItem(item);
}
@Override
@ -183,7 +166,7 @@ public final class BlockItemPacketRewriter1_20_5 extends StructuredItemRewriter<
if (item == null) return null;
// Convert to structured item first
final Item structuredItem = com.viaversion.viaversion.protocols.protocol1_20_5to1_20_3.rewriter.BlockItemPacketRewriter1_20_5.toStructuredItem(item);
final Item structuredItem = vvProtocol.getItemRewriter().toStructuredItem(item);
return super.handleItemToServer(structuredItem);
}
}

View File

@ -26,6 +26,7 @@ import com.viaversion.viaversion.api.data.entity.DimensionData;
import com.viaversion.viaversion.api.minecraft.RegistryEntry;
import com.viaversion.viaversion.api.minecraft.entities.EntityType;
import com.viaversion.viaversion.api.minecraft.entities.EntityTypes1_20_5;
import com.viaversion.viaversion.api.minecraft.item.Item;
import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers;
import com.viaversion.viaversion.api.type.Type;
import com.viaversion.viaversion.api.type.types.version.Types1_20_3;
@ -67,7 +68,9 @@ public final class EntityPacketRewriter1_20_5 extends EntityRewriter<Clientbound
}
wrapper.write(Type.BYTE, slot);
protocol.getItemRewriter().handleItemToClient(wrapper.passthrough(Type.ITEM1_20_2));
final Item item = protocol.getItemRewriter().handleItemToClient(wrapper.read(Types1_20_5.ITEM));
wrapper.write(Type.ITEM1_20_2, item);
} while (slot < 0);
});

View File

@ -17,9 +17,8 @@
*/
package com.viaversion.viabackwards.template.protocol.rewriter;
import com.viaversion.viabackwards.api.rewriters.ItemRewriter;
import com.viaversion.viabackwards.api.rewriters.BackwardsStructuredItemRewriter;
import com.viaversion.viabackwards.template.protocol.Protocol1_98To1_99;
import com.viaversion.viaversion.api.type.Type;
import com.viaversion.viaversion.api.type.types.chunk.ChunkType1_20_2;
import com.viaversion.viaversion.api.type.types.version.Types1_20_5;
import com.viaversion.viaversion.protocols.protocol1_20_3to1_20_2.rewriter.RecipeRewriter1_20_3;
@ -32,10 +31,10 @@ import com.viaversion.viaversion.rewriter.BlockRewriter;
// To replace if needed:
// ChunkType1_20_2
// RecipeRewriter1_20_3
public final class BlockItemPacketRewriter1_99 extends ItemRewriter<ClientboundPacket1_20_5, ServerboundPacket1_20_5, Protocol1_98To1_99> {
public final class BlockItemPacketRewriter1_99 extends BackwardsStructuredItemRewriter<ClientboundPacket1_20_5, ServerboundPacket1_20_5, Protocol1_98To1_99> {
public BlockItemPacketRewriter1_99(final Protocol1_98To1_99 protocol) {
super(protocol, Type.ITEM1_20_2, Type.ITEM1_20_2_ARRAY);
super(protocol, /*old types*/Types1_20_5.ITEM, Types1_20_5.ITEM_ARRAY);
}
@Override