Code cleanup

This commit is contained in:
RaphiMC 2024-05-18 00:00:27 +02:00
parent 6c372c2127
commit 30e97c41f4
No known key found for this signature in database
GPG Key ID: 0F6BB0657A03AC94
10 changed files with 84 additions and 86 deletions

View File

@ -25,7 +25,6 @@ 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.version.ProtocolVersion;
import com.viaversion.viaversion.api.type.Type;
import com.viaversion.viaversion.api.type.Types;
import com.viaversion.viaversion.protocols.v1_20_3to1_20_5.packet.ClientboundPackets1_20_5;
import com.viaversion.viaversion.protocols.v1_20_3to1_20_5.packet.ServerboundPackets1_20_5;
@ -61,7 +60,7 @@ public class VFPProtocol extends AbstractSimpleProtocol {
@Override
protected void registerPackets() {
registerClientbound(State.PLAY, getPluginMessagePacket().getId(), getPluginMessagePacket().getId(), wrapper -> {
registerClientbound(State.PLAY, getCustomPayload().getId(), getCustomPayload().getId(), wrapper -> {
final String channel = Key.namespaced(wrapper.passthrough(Types.STRING));
if (!channel.startsWith(Identifier.DEFAULT_NAMESPACE)) {
// Mods might add custom payloads that we don't want to filter, so we check for the namespace.
@ -96,18 +95,18 @@ public class VFPProtocol extends AbstractSimpleProtocol {
payloadDiff.put(id.id().toString(), new Pair<>(version, reader));
}
public static ServerboundPacketType getCreativeInventoryActionPacket() {
public static ServerboundPacketType getSetCreativeModeSlot() {
return ServerboundPackets1_20_5.SET_CREATIVE_MODE_SLOT;
}
public static ClientboundPacketType getPluginMessagePacket() {
public static ClientboundPacketType getCustomPayload() {
return ClientboundPackets1_20_5.CUSTOM_PAYLOAD;
}
@FunctionalInterface
interface PacketReader {
void read(PacketWrapper wrapper) throws Exception;
void read(PacketWrapper wrapper);
}
}

View File

@ -75,16 +75,16 @@ public class ArmorHudEmulation1_8 {
}
previousArmorPoints = armor;
final PacketWrapper entityProperties = PacketWrapper.create(ClientboundPackets1_9.UPDATE_ATTRIBUTES, userConnection);
entityProperties.write(Types.VAR_INT, MinecraftClient.getInstance().player.getId());
entityProperties.write(Types.INT, 1);
entityProperties.write(Types.STRING, "generic.armor");
entityProperties.write(Types.DOUBLE, 0.0D);
entityProperties.write(Types.VAR_INT, 1);
entityProperties.write(Types.UUID, ARMOR_POINTS_UUID);
entityProperties.write(Types.DOUBLE, (double) armor);
entityProperties.write(Types.BYTE, (byte) 0);
entityProperties.scheduleSend(Protocol1_8To1_9.class);
final PacketWrapper updateAttributes = PacketWrapper.create(ClientboundPackets1_9.UPDATE_ATTRIBUTES, userConnection);
updateAttributes.write(Types.VAR_INT, MinecraftClient.getInstance().player.getId());
updateAttributes.write(Types.INT, 1);
updateAttributes.write(Types.STRING, "generic.armor");
updateAttributes.write(Types.DOUBLE, 0.0D);
updateAttributes.write(Types.VAR_INT, 1);
updateAttributes.write(Types.UUID, ARMOR_POINTS_UUID);
updateAttributes.write(Types.DOUBLE, (double) armor);
updateAttributes.write(Types.BYTE, (byte) 0);
updateAttributes.scheduleSend(Protocol1_8To1_9.class);
}
}

View File

@ -120,11 +120,11 @@ public abstract class MixinMinecraftClient {
}
@Inject(method = "handleInputEvents", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/tutorial/TutorialManager;onInventoryOpened()V", shift = At.Shift.AFTER))
private void sendOpenInventoryPacket(CallbackInfo ci) throws Exception {
private void sendOpenInventoryPacket(CallbackInfo ci) {
if (DebugSettings.global().sendOpenInventoryPacket.isEnabled()) {
final PacketWrapper clientStatus = PacketWrapper.create(ServerboundPackets1_9_3.CLIENT_COMMAND, ProtocolTranslator.getPlayNetworkUserConnection());
clientStatus.write(Types.VAR_INT, 2); // Open Inventory Achievement
clientStatus.scheduleSendToServer(Protocol1_11_1To1_12.class);
final PacketWrapper clientCommand = PacketWrapper.create(ServerboundPackets1_9_3.CLIENT_COMMAND, ProtocolTranslator.getPlayNetworkUserConnection());
clientCommand.write(Types.VAR_INT, 2); // Open Inventory Achievement
clientCommand.scheduleSendToServer(Protocol1_11_1To1_12.class);
}
}

View File

@ -219,15 +219,15 @@ public abstract class MixinClientPlayerEntity extends AbstractClientPlayerEntity
@Redirect(method = "tick", slice = @Slice(from = @At(value = "INVOKE", target = "Lnet/minecraft/client/network/ClientPlayerEntity;hasVehicle()Z")), at = @At(value = "INVOKE", target = "Lnet/minecraft/client/network/ClientPlayNetworkHandler;sendPacket(Lnet/minecraft/network/packet/Packet;)V", ordinal = 0))
private void modifyPositionPacket(ClientPlayNetworkHandler instance, Packet<?> packet) {
if (ProtocolTranslator.getTargetVersion().olderThanOrEqualTo(LegacyProtocolVersion.r1_5_2)) {
final PacketWrapper playerPosition = PacketWrapper.create(ServerboundPackets1_5_2.MOVE_PLAYER_POS_ROT, ((IClientConnection) this.networkHandler.getConnection()).viaFabricPlus$getUserConnection());
playerPosition.write(Types.DOUBLE, this.getVelocity().x); // x
playerPosition.write(Types.DOUBLE, -999.0D); // y
playerPosition.write(Types.DOUBLE, -999.0D); // stance
playerPosition.write(Types.DOUBLE, this.getVelocity().z); // z
playerPosition.write(Types.FLOAT, this.getYaw()); // yaw
playerPosition.write(Types.FLOAT, this.getPitch()); // pitch
playerPosition.write(Types.BOOLEAN, this.isOnGround()); // onGround
playerPosition.scheduleSendToServer(Protocolr1_5_2Tor1_6_1.class);
final PacketWrapper movePlayerPosRot = PacketWrapper.create(ServerboundPackets1_5_2.MOVE_PLAYER_POS_ROT, ((IClientConnection) this.networkHandler.getConnection()).viaFabricPlus$getUserConnection());
movePlayerPosRot.write(Types.DOUBLE, this.getVelocity().x); // x
movePlayerPosRot.write(Types.DOUBLE, -999.0D); // y
movePlayerPosRot.write(Types.DOUBLE, -999.0D); // stance
movePlayerPosRot.write(Types.DOUBLE, this.getVelocity().z); // z
movePlayerPosRot.write(Types.FLOAT, this.getYaw()); // yaw
movePlayerPosRot.write(Types.FLOAT, this.getPitch()); // pitch
movePlayerPosRot.write(Types.BOOLEAN, this.isOnGround()); // onGround
movePlayerPosRot.scheduleSendToServer(Protocolr1_5_2Tor1_6_1.class);
return;
}
instance.sendPacket(packet);

View File

@ -153,14 +153,14 @@ public abstract class MixinClientPlayerInteractionManager implements IClientPlay
slotItemBeforeModification = viaFabricPlus$oldItems.get(clickSlot.getSlot());
}
final PacketWrapper clickWindowPacket = PacketWrapper.create(ServerboundPackets1_16_2.CONTAINER_CLICK, ((IClientConnection) networkHandler.getConnection()).viaFabricPlus$getUserConnection());
clickWindowPacket.write(Types.UNSIGNED_BYTE, (short) clickSlot.getSyncId());
clickWindowPacket.write(Types.SHORT, (short) clickSlot.getSlot());
clickWindowPacket.write(Types.BYTE, (byte) clickSlot.getButton());
clickWindowPacket.write(Types.SHORT, ((IScreenHandler) client.player.currentScreenHandler).viaFabricPlus$incrementAndGetActionId());
clickWindowPacket.write(Types.VAR_INT, clickSlot.getActionType().ordinal());
clickWindowPacket.write(Types.ITEM1_13_2, ItemTranslator.mcToVia(slotItemBeforeModification, ProtocolVersion.v1_16_4));
clickWindowPacket.scheduleSendToServer(Protocol1_16_4To1_17.class);
final PacketWrapper containerClick = PacketWrapper.create(ServerboundPackets1_16_2.CONTAINER_CLICK, ((IClientConnection) networkHandler.getConnection()).viaFabricPlus$getUserConnection());
containerClick.write(Types.UNSIGNED_BYTE, (short) clickSlot.getSyncId());
containerClick.write(Types.SHORT, (short) clickSlot.getSlot());
containerClick.write(Types.BYTE, (byte) clickSlot.getButton());
containerClick.write(Types.SHORT, ((IScreenHandler) client.player.currentScreenHandler).viaFabricPlus$incrementAndGetActionId());
containerClick.write(Types.VAR_INT, clickSlot.getActionType().ordinal());
containerClick.write(Types.ITEM1_13_2, ItemTranslator.mcToVia(slotItemBeforeModification, ProtocolVersion.v1_16_4));
containerClick.scheduleSendToServer(Protocol1_16_4To1_17.class);
viaFabricPlus$oldCursorStack = null;
viaFabricPlus$oldItems = null;

View File

@ -60,28 +60,28 @@ public abstract class MixinEntityPacketRewriter1_20_5 extends EntityRewriter<Cli
* @reason Fix interaction range and step height differences
*/
@Overwrite
private void sendRangeAttributes(final UserConnection connection, final boolean creativeMode) throws Exception {
final PacketWrapper wrapper = PacketWrapper.create(ClientboundPackets1_20_5.UPDATE_ATTRIBUTES, connection);
wrapper.write(Types.VAR_INT, this.tracker(connection).clientEntityId());
private void sendRangeAttributes(final UserConnection connection, final boolean creativeMode) {
final PacketWrapper updateAttributes = PacketWrapper.create(ClientboundPackets1_20_5.UPDATE_ATTRIBUTES, connection);
updateAttributes.write(Types.VAR_INT, this.tracker(connection).clientEntityId());
if (connection.getProtocolInfo().serverProtocolVersion().olderThanOrEqualTo(ProtocolVersion.v1_7_6)) {
wrapper.write(Types.VAR_INT, 3); // Number of attributes
this.writeAttribute(wrapper, "generic.step_height", 0.5D, null, 0D);
updateAttributes.write(Types.VAR_INT, 3); // Number of attributes
this.writeAttribute(updateAttributes, "generic.step_height", 0.5D, null, 0D);
} else {
wrapper.write(Types.VAR_INT, 2); // Number of attributes
updateAttributes.write(Types.VAR_INT, 2); // Number of attributes
}
if (connection.getProtocolInfo().serverProtocolVersion().olderThan(LegacyProtocolVersion.r1_0_0tor1_0_1)) {
this.writeAttribute(wrapper, "player.block_interaction_range", 4D, creativeMode ? CREATIVE_BLOCK_INTERACTION_RANGE : null, 1D);
this.writeAttribute(updateAttributes, "player.block_interaction_range", 4D, creativeMode ? CREATIVE_BLOCK_INTERACTION_RANGE : null, 1D);
} else {
this.writeAttribute(wrapper, "player.block_interaction_range", 4.5D, creativeMode ? CREATIVE_BLOCK_INTERACTION_RANGE : null, 0.5D);
this.writeAttribute(updateAttributes, "player.block_interaction_range", 4.5D, creativeMode ? CREATIVE_BLOCK_INTERACTION_RANGE : null, 0.5D);
}
if (connection.getProtocolInfo().serverProtocolVersion().olderThanOrEqualTo(LegacyProtocolVersion.r1_6_4)) {
this.writeAttribute(wrapper, "player.entity_interaction_range", 3D, creativeMode ? CREATIVE_ENTITY_INTERACTION_RANGE : null, 3D);
this.writeAttribute(updateAttributes, "player.entity_interaction_range", 3D, creativeMode ? CREATIVE_ENTITY_INTERACTION_RANGE : null, 3D);
} else if (connection.getProtocolInfo().serverProtocolVersion().olderThanOrEqualTo(ProtocolVersion.v1_13_2)) {
this.writeAttribute(wrapper, "player.entity_interaction_range", 3D, creativeMode ? CREATIVE_ENTITY_INTERACTION_RANGE : null, 1D);
this.writeAttribute(updateAttributes, "player.entity_interaction_range", 3D, creativeMode ? CREATIVE_ENTITY_INTERACTION_RANGE : null, 1D);
} else {
this.writeAttribute(wrapper, "player.entity_interaction_range", 3D, creativeMode ? CREATIVE_ENTITY_INTERACTION_RANGE : null, 2D);
this.writeAttribute(updateAttributes, "player.entity_interaction_range", 3D, creativeMode ? CREATIVE_ENTITY_INTERACTION_RANGE : null, 2D);
}
wrapper.scheduleSend(Protocol1_20_3To1_20_5.class);
updateAttributes.scheduleSend(Protocol1_20_3To1_20_5.class);
}
}

View File

@ -34,21 +34,21 @@ import org.spongepowered.asm.mixin.injection.Redirect;
public abstract class MixinJoinPackets {
@Redirect(method = "lambda$register$8", at = @At(value = "INVOKE", target = "Lcom/viaversion/viaversion/api/protocol/packet/PacketWrapper;read(Lcom/viaversion/viaversion/api/type/Type;)Ljava/lang/Object;", ordinal = 5))
private static Object trackWorldSeed(PacketWrapper instance, Type<LongLEType> tType) throws Exception {
private static Object trackWorldSeed(PacketWrapper instance, Type<LongLEType> tType) {
final Object seed = instance.read(tType);
instance.user().get(JoinGameDataTracker.class).setSeed((long) seed);
return seed;
}
@Redirect(method = "lambda$register$8", at = @At(value = "INVOKE", target = "Lcom/viaversion/viaversion/api/protocol/packet/PacketWrapper;read(Lcom/viaversion/viaversion/api/type/Type;)Ljava/lang/Object;", ordinal = 56))
private static Object trackLevelId(PacketWrapper instance, Type<StringType> tType) throws Exception {
private static Object trackLevelId(PacketWrapper instance, Type<StringType> tType) {
final Object levelId = instance.read(tType);
instance.user().get(JoinGameDataTracker.class).setLevelId((String) levelId);
return levelId;
}
@Redirect(method = "lambda$register$8", at = @At(value = "INVOKE", target = "Lcom/viaversion/viaversion/api/protocol/packet/PacketWrapper;read(Lcom/viaversion/viaversion/api/type/Type;)Ljava/lang/Object;", ordinal = 64))
private static Object trackEnchantmentSeed(PacketWrapper instance, Type<VarIntType> tType) throws Exception {
private static Object trackEnchantmentSeed(PacketWrapper instance, Type<VarIntType> tType) {
final Object enchantmentSeed = instance.read(tType);
instance.user().get(JoinGameDataTracker.class).setEnchantmentSeed((Integer) enchantmentSeed);
return enchantmentSeed;

View File

@ -45,18 +45,18 @@ public class BlockStateTranslator {
*/
public static BlockState via1_18_2toMc(final int blockStateId) {
try {
final PacketWrapper wrapper = PacketWrapper.create(ClientboundPackets1_18.LEVEL_EVENT, DUMMY_USER_CONNECTION);
wrapper.write(Types.INT, 2001); // eventId
wrapper.write(Types.BLOCK_POSITION1_14, new Position(0, 0, 0)); // position
wrapper.write(Types.INT, blockStateId); // data
wrapper.write(Types.BOOLEAN, false); // global
final PacketWrapper levelEvent = PacketWrapper.create(ClientboundPackets1_18.LEVEL_EVENT, DUMMY_USER_CONNECTION);
levelEvent.write(Types.INT, 2001); // eventId
levelEvent.write(Types.BLOCK_POSITION1_14, new Position(0, 0, 0)); // position
levelEvent.write(Types.INT, blockStateId); // data
levelEvent.write(Types.BOOLEAN, false); // global
wrapper.resetReader();
wrapper.user().getProtocolInfo().getPipeline().transform(Direction.CLIENTBOUND, State.PLAY, wrapper);
levelEvent.resetReader();
levelEvent.user().getProtocolInfo().getPipeline().transform(Direction.CLIENTBOUND, State.PLAY, levelEvent);
wrapper.read(Types.INT); // eventId
wrapper.read(Types.BLOCK_POSITION1_14); // position
return Block.getStateFromRawId(wrapper.read(Types.INT)); // data
levelEvent.read(Types.INT); // eventId
levelEvent.read(Types.BLOCK_POSITION1_14); // position
return Block.getStateFromRawId(levelEvent.read(Types.INT)); // data
} catch (Throwable t) {
ViaFabricPlus.global().getLogger().error("Error converting ViaVersion 1.18.2 block state to native block state", t);
return Blocks.AIR.getDefaultState();

View File

@ -60,11 +60,11 @@ public class ItemTranslator {
buf.writeShort(0); // slot
ItemStack.OPTIONAL_PACKET_CODEC.encode(buf, stack); // item
final PacketWrapper wrapper = PacketWrapper.create(VFPProtocol.getCreativeInventoryActionPacket(), buf, user);
user.getProtocolInfo().getPipeline().transform(Direction.SERVERBOUND, State.PLAY, wrapper);
final PacketWrapper setCreativeModeSlot = PacketWrapper.create(VFPProtocol.getSetCreativeModeSlot(), buf, user);
user.getProtocolInfo().getPipeline().transform(Direction.SERVERBOUND, State.PLAY, setCreativeModeSlot);
wrapper.read(Types.SHORT); // slot
return wrapper.read(getItemType(targetVersion)); // item
setCreativeModeSlot.read(Types.SHORT); // slot
return setCreativeModeSlot.read(getItemType(targetVersion)); // item
} catch (Throwable t) {
ViaFabricPlus.global().getLogger().error("Error converting native item stack to ViaVersion {} item stack", targetVersion, t);
return null;
@ -101,18 +101,18 @@ public class ItemTranslator {
*/
public static ItemStack viaB1_8toMc(final Item item) {
try {
final PacketWrapper wrapper = PacketWrapper.create(ClientboundPacketsb1_8.CONTAINER_SET_SLOT, VIA_B1_8_TO_MC_USER_CONNECTION);
wrapper.write(Types.BYTE, (byte) 0); // window id
wrapper.write(Types.SHORT, (short) 0); // slot
wrapper.write(Types1_4_2.NBTLESS_ITEM, item); // item
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
wrapper.resetReader();
wrapper.user().getProtocolInfo().getPipeline().transform(Direction.CLIENTBOUND, State.PLAY, wrapper);
containerSetSlot.resetReader();
containerSetSlot.user().getProtocolInfo().getPipeline().transform(Direction.CLIENTBOUND, State.PLAY, containerSetSlot);
wrapper.read(Types.UNSIGNED_BYTE); // sync id
wrapper.read(Types.VAR_INT); // revision
wrapper.read(Types.SHORT); // slot
final Item viaItem = wrapper.read(getItemType(ProtocolTranslator.NATIVE_VERSION)); // item
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());

View File

@ -19,16 +19,15 @@
package de.florianmichael.viafabricplus.protocoltranslator.translator;
import com.viaversion.nbt.tag.Tag;
import com.viaversion.viaversion.api.connection.UserConnection;
import com.viaversion.viaversion.api.protocol.packet.Direction;
import com.viaversion.viaversion.api.protocol.packet.PacketWrapper;
import com.viaversion.viaversion.api.protocol.packet.State;
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.libs.gson.JsonElement;
import com.viaversion.viaversion.protocols.v1_13_2to1_14.packet.ClientboundPackets1_14;
import com.viaversion.nbt.tag.Tag;
import de.florianmichael.viafabricplus.ViaFabricPlus;
import de.florianmichael.viafabricplus.protocoltranslator.ProtocolTranslator;
@ -44,17 +43,17 @@ public class TextComponentTranslator {
*/
public static Tag via1_14toViaLatest(final JsonElement component) {
try {
final PacketWrapper wrapper = PacketWrapper.create(ClientboundPackets1_14.OPEN_SCREEN, DUMMY_USER_CONNECTION);
wrapper.write(Types.VAR_INT, 1); // window id
wrapper.write(Types.VAR_INT, 0); // type id
wrapper.write(Types.COMPONENT, component); // title
final PacketWrapper openScreen = PacketWrapper.create(ClientboundPackets1_14.OPEN_SCREEN, DUMMY_USER_CONNECTION);
openScreen.write(Types.VAR_INT, 1); // window id
openScreen.write(Types.VAR_INT, 0); // type id
openScreen.write(Types.COMPONENT, component); // title
wrapper.resetReader();
wrapper.user().getProtocolInfo().getPipeline().transform(Direction.CLIENTBOUND, State.PLAY, wrapper);
openScreen.resetReader();
openScreen.user().getProtocolInfo().getPipeline().transform(Direction.CLIENTBOUND, State.PLAY, openScreen);
wrapper.read(Types.VAR_INT); // window id
wrapper.read(Types.VAR_INT); // type id
return wrapper.read(Types.TAG); // title
openScreen.read(Types.VAR_INT); // window id
openScreen.read(Types.VAR_INT); // type id
return openScreen.read(Types.TAG); // title
} catch (Throwable t) {
ViaFabricPlus.global().getLogger().error("Error converting ViaVersion 1.14 text component to native text component", t);
return null;