Fixed "Downloading Terrain" screen not closing sometimes

This commit is contained in:
RaphiMC 2023-02-02 18:14:06 +01:00
parent ea186d09aa
commit 06682b5ebe
2 changed files with 113 additions and 0 deletions

View File

@ -0,0 +1,64 @@
/*
* This file is part of ViaProxy - https://github.com/RaphiMC/ViaProxy
* Copyright (C) 2023 RK_01/RaphiMC 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 net.raphimc.viaproxy.injection.transformer;
import com.viaversion.viaversion.api.connection.UserConnection;
import com.viaversion.viaversion.api.protocol.AbstractProtocol;
import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper;
import com.viaversion.viaversion.api.type.Type;
import com.viaversion.viaversion.protocols.protocol1_17to1_16_4.ServerboundPackets1_17;
import com.viaversion.viaversion.protocols.protocol1_18_2to1_18.Protocol1_18_2To1_18;
import com.viaversion.viaversion.protocols.protocol1_18to1_17_1.ClientboundPackets1_18;
import net.lenni0451.classtransform.annotations.CTarget;
import net.lenni0451.classtransform.annotations.CTransformer;
import net.lenni0451.classtransform.annotations.injection.CInject;
import net.raphimc.viaproxy.protocolhack.viaproxy.loading_terrain_fix.SpawnPositionTracker;
@CTransformer(Protocol1_18_2To1_18.class)
public abstract class Protocol1_18_2To1_18Transformer extends AbstractProtocol<ClientboundPackets1_18, ClientboundPackets1_18, ServerboundPackets1_17, ServerboundPackets1_17> {
@CInject(method = "registerPackets", target = @CTarget("RETURN"))
private void fixDownloadingTerrainScreenNotClosing() {
this.registerClientbound(ClientboundPackets1_18.PLAYER_POSITION, new PacketRemapper() {
@Override
public void registerMap() {
handler(wrapper -> {
final SpawnPositionTracker tracker = wrapper.user().get(SpawnPositionTracker.class);
tracker.sendSpawnPosition();
});
}
});
this.registerClientbound(ClientboundPackets1_18.SPAWN_POSITION, new PacketRemapper() {
@Override
public void registerMap() {
map(Type.POSITION1_14); // position
map(Type.FLOAT); // angle
handler(wrapper -> {
final SpawnPositionTracker tracker = wrapper.user().get(SpawnPositionTracker.class);
tracker.setSpawnPosition(wrapper.get(Type.POSITION1_14, 0), wrapper.get(Type.FLOAT, 0));
});
}
});
}
@Override
public void init(UserConnection user) {
user.put(new SpawnPositionTracker(user));
}
}

View File

@ -0,0 +1,49 @@
/*
* This file is part of ViaProxy - https://github.com/RaphiMC/ViaProxy
* Copyright (C) 2023 RK_01/RaphiMC 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 net.raphimc.viaproxy.protocolhack.viaproxy.loading_terrain_fix;
import com.viaversion.viaversion.api.connection.StoredObject;
import com.viaversion.viaversion.api.connection.UserConnection;
import com.viaversion.viaversion.api.minecraft.Position;
import com.viaversion.viaversion.api.protocol.packet.PacketWrapper;
import com.viaversion.viaversion.api.type.Type;
import com.viaversion.viaversion.protocols.protocol1_18_2to1_18.Protocol1_18_2To1_18;
import com.viaversion.viaversion.protocols.protocol1_18to1_17_1.ClientboundPackets1_18;
public class SpawnPositionTracker extends StoredObject {
private Position spawnPosition = new Position(8, 64, 8);
private float angle = 0F;
public SpawnPositionTracker(final UserConnection user) {
super(user);
}
public void setSpawnPosition(final Position spawnPosition, final float angle) {
this.spawnPosition = spawnPosition;
this.angle = angle;
}
public void sendSpawnPosition() throws Exception {
final PacketWrapper spawnPosition = PacketWrapper.create(ClientboundPackets1_18.SPAWN_POSITION, this.getUser());
spawnPosition.write(Type.POSITION1_14, this.spawnPosition); // position
spawnPosition.write(Type.FLOAT, this.angle); // angle
spawnPosition.send(Protocol1_18_2To1_18.class);
}
}