Cleaned up ItemTranslator base

This commit is contained in:
FlorianMichael 2023-07-18 06:07:10 +02:00
parent 884e05f3d6
commit cd34aa7d7a
No known key found for this signature in database
GPG Key ID: C2FB87E71C425126
4 changed files with 15 additions and 14 deletions

View File

@ -135,7 +135,7 @@ public abstract class MixinClientPlayerInteractionManager {
final byte button = (byte) clickSlot.getButton();
final short lastActionId = ((IScreenHandler) client.player.currentScreenHandler).viafabricplus_getAndIncrementLastActionId();
final int actionType = clickSlot.getActionType().ordinal();
final Item item = ItemTranslator.minecraftToViaVersion(slotItemBeforeModification, VersionEnum.r1_16.getVersion());
final Item item = ItemTranslator.MC_TO_VIA_LATEST_TO_TARGET(slotItemBeforeModification, VersionEnum.r1_16);
userConnection.getChannel().eventLoop().submit(() -> {
final PacketWrapper clickSlotPacket = PacketWrapper.create(ServerboundPackets1_16_2.CLICK_WINDOW, userConnection);

View File

@ -24,7 +24,6 @@ import net.minecraft.client.MinecraftClient;
import net.minecraft.client.network.ClientPlayerEntity;
import net.minecraft.item.ItemStack;
import net.raphimc.vialegacy.protocols.alpha.protocolb1_0_1_1_1toa1_2_3_5_1_2_6.providers.AlphaInventoryProvider;
import net.raphimc.vialegacy.protocols.beta.protocol1_0_0_1tob1_8_0_1.types.Typesb1_8_0_1;
import net.raphimc.vialoader.util.VersionEnum;
import java.util.List;
@ -45,7 +44,7 @@ public class ViaFabricPlusAlphaInventoryProvider extends AlphaInventoryProvider
final var alphaItem = trackingItems.get(i);
if (alphaItem.isEmpty()) continue;
items[i] = ItemTranslator.minecraftToViaVersion(alphaItem, Typesb1_8_0_1.CREATIVE_ITEM, VersionEnum.b1_8tob1_8_1.getVersion());
items[i] = ItemTranslator.MC_TO_VIA_LATEST_TO_TARGET(alphaItem, VersionEnum.b1_8tob1_8_1);
}
return copyItems(items);
}
@ -80,7 +79,7 @@ public class ViaFabricPlusAlphaInventoryProvider extends AlphaInventoryProvider
@Override
public void addToInventory(UserConnection user, Item item) {
getPlayer().getInventory().insertStack(ItemTranslator.viaVersionToMinecraft(item, VersionEnum.b1_8tob1_8_1.getVersion()));
getPlayer().getInventory().insertStack(ItemTranslator.VIA_TO_MC_B1_8_TO_LATEST(item));
}
protected ClientPlayerEntity getPlayer() {

View File

@ -32,6 +32,6 @@ public class ViaFabricPlusHandItemProvider extends HandItemProvider {
if (lastUsedItem == null) {
return null;
}
return ItemTranslator.minecraftToViaVersion(lastUsedItem, VersionEnum.r1_8.getVersion());
return ItemTranslator.MC_TO_VIA_LATEST_TO_TARGET(lastUsedItem, VersionEnum.r1_8);
}
}

View File

@ -36,8 +36,10 @@ import net.minecraft.network.PacketByteBuf;
import net.minecraft.network.packet.c2s.play.CreativeInventoryActionC2SPacket;
import net.minecraft.registry.Registries;
import net.raphimc.vialegacy.protocols.beta.protocol1_0_0_1tob1_8_0_1.ClientboundPacketsb1_8;
import net.raphimc.vialegacy.protocols.beta.protocol1_0_0_1tob1_8_0_1.types.Typesb1_8_0_1;
import net.raphimc.vialegacy.protocols.release.protocol1_4_4_5to1_4_2.types.Types1_4_2;
import net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.storage.WindowTracker;
import net.raphimc.vialoader.util.VersionEnum;
import java.util.List;
import java.util.stream.Collectors;
@ -45,12 +47,8 @@ import java.util.stream.Collectors;
public class ItemTranslator {
private final static UserConnection DUMMY_USER_CONNECTION = new UserConnectionImpl(null, false);
public static Item minecraftToViaVersion(final ItemStack stack, final int targetVersion) {
return minecraftToViaVersion(stack, Type.ITEM, targetVersion);
}
public static Item minecraftToViaVersion(final ItemStack stack, final Type<Item> item, final int targetVersion) {
final List<ProtocolPathEntry> protocolPath = Via.getManager().getProtocolManager().getProtocolPath(SharedConstants.getProtocolVersion(), targetVersion);
public static Item MC_TO_VIA_LATEST_TO_TARGET(final ItemStack stack, final VersionEnum targetVersion) {
final List<ProtocolPathEntry> protocolPath = Via.getManager().getProtocolManager().getProtocolPath(SharedConstants.getProtocolVersion(), targetVersion.getVersion());
if (protocolPath == null) return null;
final var dummyPacket = new CreativeInventoryActionC2SPacket(36, stack);
@ -64,15 +62,19 @@ public class ItemTranslator {
wrapper.apply(Direction.SERVERBOUND, State.PLAY, 0, protocolPath.stream().map(ProtocolPathEntry::protocol).collect(Collectors.toList()));
wrapper.read(Type.SHORT);
return wrapper.read(item);
if (targetVersion.isOlderThanOrEqualTo(VersionEnum.b1_8tob1_8_1)) {
return wrapper.read(Typesb1_8_0_1.CREATIVE_ITEM);
} else {
return wrapper.read(Type.ITEM);
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static ItemStack viaVersionToMinecraft(final Item item, final int targetVersion) {
final List<ProtocolPathEntry> protocolPath = Via.getManager().getProtocolManager().getProtocolPath(SharedConstants.getProtocolVersion(), targetVersion);
public static ItemStack VIA_TO_MC_B1_8_TO_LATEST(final Item item) {
final List<ProtocolPathEntry> protocolPath = Via.getManager().getProtocolManager().getProtocolPath(SharedConstants.getProtocolVersion(), VersionEnum.b1_8tob1_8_1.getVersion());
if (protocolPath == null) return null;
DUMMY_USER_CONNECTION.put(new WindowTracker(DUMMY_USER_CONNECTION));