Add new methods to ItemTranslator

This commit is contained in:
RaphiMC 2024-05-27 16:27:55 +02:00
parent 43d5065e1e
commit d0426c82e3
No known key found for this signature in database
GPG Key ID: 0F6BB0657A03AC94
2 changed files with 67 additions and 42 deletions

View File

@ -89,7 +89,7 @@ public class ViaFabricPlusAlphaInventoryProvider extends AlphaInventoryProvider
@Override
public void addToInventory(UserConnection user, Item item) {
getPlayer().getInventory().insertStack(ItemTranslator.viaB1_8toMc(item));
getPlayer().getInventory().insertStack(ItemTranslator.viaToMc(item, LegacyProtocolVersion.b1_8tob1_8_1));
}
protected ClientPlayerEntity getPlayer() {

View File

@ -21,6 +21,7 @@ package de.florianmichael.viafabricplus.protocoltranslator.translator;
import com.viaversion.viaversion.api.connection.UserConnection;
import com.viaversion.viaversion.api.minecraft.item.Item;
import com.viaversion.viaversion.api.protocol.Protocol;
import com.viaversion.viaversion.api.protocol.packet.Direction;
import com.viaversion.viaversion.api.protocol.packet.PacketWrapper;
import com.viaversion.viaversion.api.protocol.packet.State;
@ -28,6 +29,7 @@ import com.viaversion.viaversion.api.protocol.version.ProtocolVersion;
import com.viaversion.viaversion.api.type.Type;
import com.viaversion.viaversion.api.type.Types;
import com.viaversion.viaversion.api.type.types.version.Types1_20_5;
import com.viaversion.viaversion.protocols.v1_12to1_12_1.packet.ClientboundPackets1_12_1;
import de.florianmichael.viafabricplus.ViaFabricPlus;
import de.florianmichael.viafabricplus.fixes.viaversion.ViaFabricPlusProtocol;
import de.florianmichael.viafabricplus.protocoltranslator.ProtocolTranslator;
@ -35,22 +37,20 @@ import io.netty.buffer.Unpooled;
import net.minecraft.client.MinecraftClient;
import net.minecraft.item.ItemStack;
import net.minecraft.network.RegistryByteBuf;
import net.minecraft.registry.Registries;
import net.raphimc.vialegacy.api.LegacyProtocolVersion;
import net.raphimc.vialegacy.protocol.beta.b1_8_0_1tor1_0_0_1.packet.ClientboundPacketsb1_8;
import net.raphimc.vialegacy.protocol.beta.b1_8_0_1tor1_0_0_1.types.Typesb1_8_0_1;
import net.raphimc.vialegacy.protocol.release.r1_2_4_5tor1_3_1_2.types.Types1_2_4;
import net.raphimc.vialegacy.protocol.release.r1_4_2tor1_4_4_5.types.Types1_4_2;
import net.raphimc.vialegacy.protocol.release.r1_7_6_10tor1_8.types.Types1_7_6;
public class ItemTranslator {
private static final UserConnection VIA_B1_8_TO_MC_USER_CONNECTION = ProtocolTranslator.createDummyUserConnection(ProtocolTranslator.NATIVE_VERSION, LegacyProtocolVersion.b1_8tob1_8_1);
/**
* Converts a Minecraft item stack to a ViaVersion item stack
* Converts a Minecraft item stack to a ViaVersion item
*
* @param stack The Minecraft item stack
* @param targetVersion The target version to convert to (e.g. v1.13)
* @return The ViaVersion item stack for the target version
* @return The ViaVersion item for the target version
*/
public static Item mcToVia(final ItemStack stack, final ProtocolVersion targetVersion) {
final UserConnection user = ProtocolTranslator.createDummyUserConnection(ProtocolTranslator.NATIVE_VERSION, targetVersion);
@ -64,7 +64,7 @@ public class ItemTranslator {
user.getProtocolInfo().getPipeline().transform(Direction.SERVERBOUND, State.PLAY, setCreativeModeSlot);
setCreativeModeSlot.read(Types.SHORT); // slot
return setCreativeModeSlot.read(getItemType(targetVersion)); // item
return setCreativeModeSlot.read(getServerboundItemType(targetVersion)); // item
} catch (Throwable t) {
ViaFabricPlus.global().getLogger().error("Error converting native item stack to ViaVersion {} item stack", targetVersion, t);
return null;
@ -72,55 +72,80 @@ public class ItemTranslator {
}
/**
* Gets the ViaVersion item type for the target version
* Converts a ViaVersion item to a Minecraft item stack
*
* @param item The ViaVersion item
* @param sourceVersion The source version of the item (e.g. b1.8)
* @return The Minecraft item stack
*/
public static ItemStack viaToMc(final Item item, final ProtocolVersion sourceVersion) {
final UserConnection user = ProtocolTranslator.createDummyUserConnection(ProtocolTranslator.NATIVE_VERSION, sourceVersion);
try {
final Protocol<?, ?, ?, ?> sourceProtocol = user.getProtocolInfo().getPipeline().reversedPipes().stream().filter(p -> !p.isBaseProtocol()).findFirst().orElseThrow();
final PacketWrapper containerSetSlot = PacketWrapper.create(sourceProtocol.getPacketTypesProvider().unmappedClientboundType(State.PLAY, ClientboundPackets1_12_1.CONTAINER_SET_SLOT.getName()), user);
if (sourceVersion.newerThanOrEqualTo(ProtocolVersion.v1_8)) {
containerSetSlot.write(Types.UNSIGNED_BYTE, (short) 0); // window id
} else {
containerSetSlot.write(Types.BYTE, (byte) 0); // window id
}
containerSetSlot.write(Types.SHORT, (short) 0); // slot
containerSetSlot.write(getClientboundItemType(sourceVersion), item != null ? item.copy() : null); // item
containerSetSlot.resetReader();
containerSetSlot.user().getProtocolInfo().getPipeline().transform(Direction.CLIENTBOUND, State.PLAY, containerSetSlot);
final RegistryByteBuf buf = new RegistryByteBuf(Unpooled.buffer(), MinecraftClient.getInstance().getNetworkHandler().getRegistryManager());
containerSetSlot.setPacketType(null);
containerSetSlot.writeToBuffer(buf);
buf.readUnsignedByte(); // sync id
buf.readVarInt(); // revision
buf.readShort(); // slot
return ItemStack.OPTIONAL_PACKET_CODEC.decode(buf);
} catch (Throwable t) {
ViaFabricPlus.global().getLogger().error("Error converting ViaVersion {} item to native item stack", sourceVersion, t);
return ItemStack.EMPTY;
}
}
/**
* Gets the ViaVersion item type for the target version in the serverbound direction
*
* @param targetVersion The target version
* @return The ViaVersion item type
*/
public static Type<Item> getItemType(final ProtocolVersion targetVersion) {
public static Type<Item> getServerboundItemType(final ProtocolVersion targetVersion) {
if (targetVersion.olderThanOrEqualTo(LegacyProtocolVersion.b1_8tob1_8_1)) {
return Typesb1_8_0_1.CREATIVE_ITEM;
} else {
return getClientboundItemType(targetVersion);
}
}
/**
* Gets the ViaVersion item type for the target version in the clientbound direction
*
* @param targetVersion The target version
* @return The ViaVersion item type
*/
public static Type<Item> getClientboundItemType(final ProtocolVersion targetVersion) {
if (targetVersion.olderThanOrEqualTo(LegacyProtocolVersion.b1_8tob1_8_1)) {
return Types1_4_2.NBTLESS_ITEM;
} else if (targetVersion.olderThanOrEqualTo(LegacyProtocolVersion.r1_2_4tor1_2_5)) {
return Types1_2_4.NBT_ITEM;
} else if (targetVersion.olderThan(ProtocolVersion.v1_8)) {
return Types1_7_6.ITEM;
} else if (targetVersion.olderThan(ProtocolVersion.v1_13)) {
return Types.ITEM1_8;
} else if (targetVersion.olderThan(ProtocolVersion.v1_13_2)) {
return Types.ITEM1_13;
} else if (targetVersion.olderThanOrEqualTo(ProtocolVersion.v1_20_2)) {
} else if (targetVersion.olderThan(ProtocolVersion.v1_20_2)) {
return Types.ITEM1_13_2;
} else if (targetVersion.olderThanOrEqualTo(ProtocolVersion.v1_20_3)) {
} else if (targetVersion.olderThan(ProtocolVersion.v1_20_5)) {
return Types.ITEM1_20_2;
} else {
return Types1_20_5.ITEM;
}
}
/**
* Converts a ViaVersion b1.8 item to a Minecraft item stack
*
* @param item The ViaVersion b1.8 item
* @return The Minecraft item stack
*/
public static ItemStack viaB1_8toMc(final Item item) {
try {
final PacketWrapper containerSetSlot = PacketWrapper.create(ClientboundPacketsb1_8.CONTAINER_SET_SLOT, VIA_B1_8_TO_MC_USER_CONNECTION);
containerSetSlot.write(Types.BYTE, (byte) 0); // window id
containerSetSlot.write(Types.SHORT, (short) 0); // slot
containerSetSlot.write(Types1_4_2.NBTLESS_ITEM, item); // item
containerSetSlot.resetReader();
containerSetSlot.user().getProtocolInfo().getPipeline().transform(Direction.CLIENTBOUND, State.PLAY, containerSetSlot);
containerSetSlot.read(Types.UNSIGNED_BYTE); // sync id
containerSetSlot.read(Types.VAR_INT); // revision
containerSetSlot.read(Types.SHORT); // slot
final Item viaItem = containerSetSlot.read(getItemType(ProtocolTranslator.NATIVE_VERSION)); // item
final ItemStack mcItem = new ItemStack(Registries.ITEM.get(viaItem.identifier()));
mcItem.setCount(viaItem.amount());
mcItem.setDamage(viaItem.data());
return mcItem;
} catch (Throwable t) {
ViaFabricPlus.global().getLogger().error("Error converting ViaVersion b1.8 item to native item stack", t);
return ItemStack.EMPTY;
}
}
}