ViaProxy/src/main/java/net/raphimc/viaproxy/injection/ClassicWorldHeightInjection.java

173 lines
8.7 KiB
Java
Raw Normal View History

2023-01-11 20:46:23 +01:00
/*
* This file is part of ViaProxy - https://github.com/RaphiMC/ViaProxy
2024-01-01 02:38:36 +01:00
* Copyright (C) 2021-2024 RK_01/RaphiMC and contributors
2023-01-11 20:46:23 +01:00
*
* 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/>.
*/
2023-01-04 20:58:09 +01:00
package net.raphimc.viaproxy.injection;
2024-05-17 17:52:38 +02:00
import com.viaversion.nbt.tag.CompoundTag;
2023-01-04 20:58:09 +01:00
import com.viaversion.viaversion.api.Via;
import com.viaversion.viaversion.api.connection.UserConnection;
import com.viaversion.viaversion.api.minecraft.chunks.Chunk;
import com.viaversion.viaversion.api.minecraft.chunks.ChunkSection;
2023-02-12 15:48:36 +01:00
import com.viaversion.viaversion.api.protocol.remapper.PacketHandler;
import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers;
2024-05-17 17:52:38 +02:00
import com.viaversion.viaversion.api.type.Types;
2023-10-19 21:15:32 +02:00
import com.viaversion.viaversion.api.type.types.chunk.ChunkType1_17;
import net.raphimc.vialegacy.api.LegacyProtocolVersion;
2024-05-17 23:41:45 +02:00
import net.raphimc.vialegacy.protocol.classic.c0_28_30toa1_0_15.model.ClassicLevel;
import net.raphimc.vialegacy.protocol.classic.c0_28_30toa1_0_15.provider.ClassicWorldHeightProvider;
import net.raphimc.vialegacy.protocol.classic.c0_28_30toa1_0_15.storage.ClassicLevelStorage;
2023-01-04 20:58:09 +01:00
2023-01-10 21:59:45 +01:00
import java.util.ArrayList;
import java.util.BitSet;
import java.util.List;
2023-01-04 20:58:09 +01:00
public class ClassicWorldHeightInjection {
2023-03-29 23:43:06 +02:00
public static PacketHandler handleJoinGame(final PacketHandler parentHandler) {
2023-02-12 15:48:36 +01:00
return wrapper -> {
parentHandler.handle(wrapper);
if (wrapper.isCancelled()) return;
if (wrapper.user().getProtocolInfo().serverProtocolVersion().olderThanOrEqualTo(LegacyProtocolVersion.c0_28toc0_30)) {
2024-05-17 17:52:38 +02:00
for (CompoundTag dimension : wrapper.get(Types.NAMED_COMPOUND_TAG, 0).getCompoundTag("minecraft:dimension_type").getListTag("value", CompoundTag.class)) {
2024-03-09 15:32:59 +01:00
changeDimensionTagHeight(wrapper.user(), dimension.getCompoundTag("element"));
2023-02-12 15:48:36 +01:00
}
2024-05-17 17:52:38 +02:00
changeDimensionTagHeight(wrapper.user(), wrapper.get(Types.NAMED_COMPOUND_TAG, 1));
2023-01-04 20:58:09 +01:00
}
};
}
2023-03-29 23:43:06 +02:00
public static PacketHandler handleRespawn(final PacketHandler parentHandler) {
2023-02-12 15:48:36 +01:00
return wrapper -> {
parentHandler.handle(wrapper);
if (wrapper.isCancelled()) return;
2023-01-04 20:58:09 +01:00
if (wrapper.user().getProtocolInfo().serverProtocolVersion().olderThanOrEqualTo(LegacyProtocolVersion.c0_28toc0_30)) {
2024-05-17 17:52:38 +02:00
changeDimensionTagHeight(wrapper.user(), wrapper.get(Types.NAMED_COMPOUND_TAG, 0));
2023-01-04 20:58:09 +01:00
}
};
}
2023-03-29 23:43:06 +02:00
public static PacketHandler handleChunkData(final PacketHandler parentHandler) {
2023-02-12 15:48:36 +01:00
return wrapper -> {
parentHandler.handle(wrapper);
if (wrapper.isCancelled()) return;
if (wrapper.user().getProtocolInfo().serverProtocolVersion().olderThanOrEqualTo(LegacyProtocolVersion.c0_28toc0_30)) {
2023-02-12 15:48:36 +01:00
wrapper.resetReader();
2023-10-19 21:15:32 +02:00
final Chunk chunk = wrapper.read(new ChunkType1_17(16));
wrapper.write(new ChunkType1_17(chunk.getSections().length), chunk);
2023-02-12 15:48:36 +01:00
final ClassicWorldHeightProvider heightProvider = Via.getManager().getProviders().get(ClassicWorldHeightProvider.class);
if (chunk.getSections().length < heightProvider.getMaxChunkSectionCount(wrapper.user())) { // Increase available sections to match new world height
final ChunkSection[] newArray = new ChunkSection[heightProvider.getMaxChunkSectionCount(wrapper.user())];
System.arraycopy(chunk.getSections(), 0, newArray, 0, chunk.getSections().length);
chunk.setSections(newArray);
}
final BitSet chunkMask = new BitSet();
for (int i = 0; i < chunk.getSections().length; i++) {
if (chunk.getSections()[i] != null) chunkMask.set(i);
}
chunk.setChunkMask(chunkMask);
final int[] newBiomeData = new int[chunk.getSections().length * 4 * 4 * 4];
System.arraycopy(chunk.getBiomeData(), 0, newBiomeData, 0, chunk.getBiomeData().length);
for (int i = 64; i < chunk.getSections().length * 4; i++) { // copy top layer of old biome data all the way to max world height
System.arraycopy(chunk.getBiomeData(), chunk.getBiomeData().length - 16, newBiomeData, i * 16, 16);
}
chunk.setBiomeData(newBiomeData);
chunk.setHeightMap(new CompoundTag()); // rip heightmap :(
2023-01-04 20:58:09 +01:00
}
};
}
2023-03-29 23:43:06 +02:00
public static PacketHandler handleUpdateLight(final PacketHandler parentHandler) {
2023-02-12 15:48:36 +01:00
final PacketHandler classicLightHandler = new PacketHandlers() {
2023-01-04 20:58:09 +01:00
@Override
2023-02-12 15:48:36 +01:00
public void register() {
2024-05-17 17:52:38 +02:00
map(Types.VAR_INT); // x
map(Types.VAR_INT); // y
map(Types.BOOLEAN); // trust edges
2023-01-04 20:58:09 +01:00
handler(wrapper -> {
2024-05-17 17:52:38 +02:00
wrapper.read(Types.VAR_INT); // sky light mask
wrapper.read(Types.VAR_INT); // block light mask
final int emptySkyLightMask = wrapper.read(Types.VAR_INT); // empty sky light mask
final int emptyBlockLightMask = wrapper.read(Types.VAR_INT); // empty block light mask
2023-01-04 20:58:09 +01:00
final ClassicLevel level = wrapper.user().get(ClassicLevelStorage.class).getClassicLevel();
final ClassicWorldHeightProvider heightProvider = Via.getManager().getProviders().get(ClassicWorldHeightProvider.class);
int sectionYCount = level.getSizeY() >> 4;
if (level.getSizeY() % 16 != 0) sectionYCount++;
if (sectionYCount > heightProvider.getMaxChunkSectionCount(wrapper.user())) {
sectionYCount = heightProvider.getMaxChunkSectionCount(wrapper.user());
}
final List<byte[]> lightArrays = new ArrayList<>();
2024-05-17 17:52:38 +02:00
while (wrapper.isReadable(Types.BYTE_ARRAY_PRIMITIVE, 0)) {
lightArrays.add(wrapper.read(Types.BYTE_ARRAY_PRIMITIVE));
2023-01-04 20:58:09 +01:00
}
int skyLightCount = 16;
int blockLightCount = sectionYCount;
if (lightArrays.size() == 16 + 0 + 2) {
blockLightCount = 0;
} else if (lightArrays.size() == 16 + sectionYCount + 2) {
} else if (lightArrays.size() == sectionYCount + sectionYCount + 2) {
skyLightCount = sectionYCount;
}
skyLightCount += 2; // Chunk below 0 and above 255
final BitSet skyLightMask = new BitSet();
final BitSet blockLightMask = new BitSet();
skyLightMask.set(0, skyLightCount);
blockLightMask.set(0, blockLightCount);
2024-05-17 17:52:38 +02:00
wrapper.write(Types.LONG_ARRAY_PRIMITIVE, skyLightMask.toLongArray());
wrapper.write(Types.LONG_ARRAY_PRIMITIVE, blockLightMask.toLongArray());
wrapper.write(Types.LONG_ARRAY_PRIMITIVE, new long[emptySkyLightMask]);
wrapper.write(Types.LONG_ARRAY_PRIMITIVE, new long[emptyBlockLightMask]);
2023-01-04 20:58:09 +01:00
2024-05-17 17:52:38 +02:00
wrapper.write(Types.VAR_INT, skyLightCount);
2023-01-04 20:58:09 +01:00
for (int i = 0; i < skyLightCount; i++) {
2024-05-17 17:52:38 +02:00
wrapper.write(Types.BYTE_ARRAY_PRIMITIVE, lightArrays.remove(0));
2023-01-04 20:58:09 +01:00
}
2024-05-17 17:52:38 +02:00
wrapper.write(Types.VAR_INT, blockLightCount);
2023-01-04 20:58:09 +01:00
for (int i = 0; i < blockLightCount; i++) {
2024-05-17 17:52:38 +02:00
wrapper.write(Types.BYTE_ARRAY_PRIMITIVE, lightArrays.remove(0));
2023-01-04 20:58:09 +01:00
}
});
}
};
2023-02-12 15:48:36 +01:00
return wrapper -> {
if (wrapper.user().getProtocolInfo().serverProtocolVersion().olderThanOrEqualTo(LegacyProtocolVersion.c0_28toc0_30)) {
2023-02-12 15:48:36 +01:00
classicLightHandler.handle(wrapper);
} else {
parentHandler.handle(wrapper);
2023-01-04 20:58:09 +01:00
}
};
}
private static void changeDimensionTagHeight(final UserConnection user, final CompoundTag tag) {
2024-03-09 15:32:59 +01:00
tag.putInt("height", Via.getManager().getProviders().get(ClassicWorldHeightProvider.class).getMaxChunkSectionCount(user) << 4);
2023-01-04 20:58:09 +01:00
}
}