Improved classic chunk loading system

This commit is contained in:
RaphiMC 2023-08-27 00:14:20 +02:00
parent a3060f1bb1
commit 08cee46788
No known key found for this signature in database
GPG Key ID: 0F6BB0657A03AC94
6 changed files with 72 additions and 38 deletions

View File

@ -35,7 +35,6 @@ public class ViaLegacyConfig extends Config implements net.raphimc.vialegacy.pla
private boolean oldBiomes;
private boolean remapBasedOnColor;
private int classicChunkRange;
private int chunksPerTick;
private boolean enableClassicFly;
public ViaLegacyConfig(final File configFile) {
@ -57,7 +56,6 @@ public class ViaLegacyConfig extends Config implements net.raphimc.vialegacy.pla
this.oldBiomes = this.getBoolean("old-biomes", true);
this.remapBasedOnColor = this.getBoolean("remap-based-on-color", true);
this.classicChunkRange = this.getInt("classic-chunk-range", 10);
this.chunksPerTick = this.getInt("chunks-per-tick", -1);
this.enableClassicFly = this.getBoolean("enable-classic-fly", false);
}
@ -115,11 +113,6 @@ public class ViaLegacyConfig extends Config implements net.raphimc.vialegacy.pla
return this.classicChunkRange;
}
@Override
public int getChunksPerTick() {
return this.chunksPerTick;
}
@Override
public boolean enableClassicFly() {
return this.enableClassicFly;

View File

@ -35,8 +35,6 @@ public interface ViaLegacyConfig {
int getClassicChunkRange();
int getChunksPerTick();
boolean enableClassicFly();
}

View File

@ -53,6 +53,7 @@ import net.raphimc.vialegacy.protocols.classic.protocola1_0_15toc0_28_30.provide
import net.raphimc.vialegacy.protocols.classic.protocola1_0_15toc0_28_30.providers.ClassicMPPassProvider;
import net.raphimc.vialegacy.protocols.classic.protocola1_0_15toc0_28_30.providers.ClassicWorldHeightProvider;
import net.raphimc.vialegacy.protocols.classic.protocola1_0_15toc0_28_30.storage.*;
import net.raphimc.vialegacy.protocols.classic.protocola1_0_15toc0_28_30.task.ClassicLevelStorageTickTask;
import net.raphimc.vialegacy.protocols.classic.protocola1_0_15toc0_28_30.types.Typesc0_30;
import net.raphimc.vialegacy.protocols.classic.protocolc0_28_30toc0_28_30cpe.Protocolc0_30toc0_30cpe;
import net.raphimc.vialegacy.protocols.classic.protocolc0_28_30toc0_28_30cpe.storage.ExtBlockPermissionsStorage;
@ -397,12 +398,7 @@ public class Protocola1_0_15toc0_30 extends AbstractProtocol<ClientboundPacketsc
@Override
public void register() {
read(Type.BOOLEAN); // onGround
handler(wrapper -> {
final ClassicPositionTracker positionTracker = wrapper.user().get(ClassicPositionTracker.class);
final ClassicLevelStorage levelStorage = wrapper.user().get(ClassicLevelStorage.class);
positionTracker.writeToPacket(wrapper);
if (levelStorage != null) levelStorage.tickChunks(positionTracker.getChunkPosition());
});
handler(wrapper -> wrapper.user().get(ClassicPositionTracker.class).writeToPacket(wrapper));
}
});
this.registerServerbound(ServerboundPacketsa1_0_15.PLAYER_POSITION, ServerboundPacketsc0_28.PLAYER_POSITION_AND_ROTATION, new PacketHandlers() {
@ -410,7 +406,6 @@ public class Protocola1_0_15toc0_30 extends AbstractProtocol<ClientboundPacketsc
public void register() {
handler(wrapper -> {
final ClassicPositionTracker positionTracker = wrapper.user().get(ClassicPositionTracker.class);
final ClassicLevelStorage levelStorage = wrapper.user().get(ClassicLevelStorage.class);
positionTracker.posX = wrapper.read(Type.DOUBLE); // x
wrapper.read(Type.DOUBLE); // y
positionTracker.stance = wrapper.read(Type.DOUBLE); // stance
@ -418,7 +413,6 @@ public class Protocola1_0_15toc0_30 extends AbstractProtocol<ClientboundPacketsc
wrapper.read(Type.BOOLEAN); // onGround
positionTracker.writeToPacket(wrapper);
if (levelStorage != null) levelStorage.tickChunks(positionTracker.getChunkPosition());
});
}
});
@ -427,13 +421,11 @@ public class Protocola1_0_15toc0_30 extends AbstractProtocol<ClientboundPacketsc
public void register() {
handler(wrapper -> {
final ClassicPositionTracker positionTracker = wrapper.user().get(ClassicPositionTracker.class);
final ClassicLevelStorage levelStorage = wrapper.user().get(ClassicLevelStorage.class);
positionTracker.yaw = wrapper.read(Type.FLOAT); // yaw
positionTracker.pitch = wrapper.read(Type.FLOAT); // pitch
wrapper.read(Type.BOOLEAN); // onGround
positionTracker.writeToPacket(wrapper);
if (levelStorage != null) levelStorage.tickChunks(positionTracker.getChunkPosition());
});
}
});
@ -442,7 +434,6 @@ public class Protocola1_0_15toc0_30 extends AbstractProtocol<ClientboundPacketsc
public void register() {
handler(wrapper -> {
final ClassicPositionTracker positionTracker = wrapper.user().get(ClassicPositionTracker.class);
final ClassicLevelStorage levelStorage = wrapper.user().get(ClassicLevelStorage.class);
positionTracker.posX = wrapper.read(Type.DOUBLE); // x
wrapper.read(Type.DOUBLE); // y
positionTracker.stance = wrapper.read(Type.DOUBLE); // stance
@ -452,7 +443,6 @@ public class Protocola1_0_15toc0_30 extends AbstractProtocol<ClientboundPacketsc
wrapper.read(Type.BOOLEAN); // onGround
positionTracker.writeToPacket(wrapper);
if (levelStorage != null) levelStorage.tickChunks(positionTracker.getChunkPosition());
});
}
});
@ -557,6 +547,8 @@ public class Protocola1_0_15toc0_30 extends AbstractProtocol<ClientboundPacketsc
providers.register(ClassicWorldHeightProvider.class, new ClassicWorldHeightProvider());
providers.register(ClassicMPPassProvider.class, new ClassicMPPassProvider());
providers.register(ClassicCustomCommandProvider.class, new ClassicCustomCommandProvider());
Via.getPlatform().runRepeatingSync(new ClassicLevelStorageTickTask(), 2L);
}
@Override

View File

@ -23,7 +23,6 @@ import com.viaversion.viaversion.api.connection.UserConnection;
import com.viaversion.viaversion.api.minecraft.Position;
import com.viaversion.viaversion.api.minecraft.chunks.*;
import com.viaversion.viaversion.api.protocol.packet.PacketWrapper;
import com.viaversion.viaversion.util.MathUtil;
import net.raphimc.vialegacy.ViaLegacy;
import net.raphimc.vialegacy.api.model.ChunkCoord;
import net.raphimc.vialegacy.api.util.ChunkCoordSpiral;
@ -58,10 +57,8 @@ public class ClassicLevelStorage extends StoredObject {
private int sectionBitmask;
private int chunksPerTick = ViaLegacy.getConfig().getChunksPerTick();
private final Set<ChunkCoord> loadedChunks = new HashSet<>();
private long lastPosPacket;
private long eventLoopPing = 0;
public ClassicLevelStorage(final UserConnection user) {
super(user);
@ -98,19 +95,31 @@ public class ClassicLevelStorage extends StoredObject {
this.subChunkZLength = Math.min(16, sizeZ);
this.sectionBitmask = 0;
for (int i = 0; i < this.sectionYCount; i++) this.sectionBitmask = (this.sectionBitmask << 1) | 1;
if (this.chunksPerTick <= 0) {
this.chunksPerTick = MathUtil.clamp(32 / this.sectionYCount, 1, 8);
}
}
public void tickChunks(final ChunkCoord center) throws Exception {
if (!this.getUser().get(ClassicPositionTracker.class).spawned) return;
if (System.currentTimeMillis() - this.lastPosPacket < 50) {
return;
public void tick() throws Exception {
final ClassicPositionTracker positionTracker = this.getUser().get(ClassicPositionTracker.class);
if (!positionTracker.spawned) return;
final long start = System.currentTimeMillis();
this.getUser().getChannel().eventLoop().submit(() -> {
ClassicLevelStorage.this.eventLoopPing = System.currentTimeMillis() - start;
});
System.out.println(this.eventLoopPing);
int limit = 0;
if (this.eventLoopPing < 50) {
limit = 12;
} else if (this.eventLoopPing < 100) {
limit = 6;
} else if (this.eventLoopPing < 250) {
limit = 3;
} else if (this.eventLoopPing < 400) {
limit = 1;
}
if (limit != 0) {
this.sendChunks(positionTracker.getChunkPosition(), ViaLegacy.getConfig().getClassicChunkRange(), limit);
}
this.lastPosPacket = System.currentTimeMillis();
this.sendChunks(center, ViaLegacy.getConfig().getClassicChunkRange(), this.chunksPerTick);
}
public void sendChunks(final ChunkCoord center, final int radius) throws Exception {

View File

@ -0,0 +1,45 @@
/*
* This file is part of ViaLegacy - https://github.com/RaphiMC/ViaLegacy
* 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.vialegacy.protocols.classic.protocola1_0_15toc0_28_30.task;
import com.viaversion.viaversion.api.Via;
import com.viaversion.viaversion.api.connection.UserConnection;
import net.raphimc.vialegacy.ViaLegacy;
import net.raphimc.vialegacy.protocols.classic.protocola1_0_15toc0_28_30.storage.ClassicLevelStorage;
import java.util.logging.Level;
public class ClassicLevelStorageTickTask implements Runnable {
@Override
public void run() {
for (UserConnection info : Via.getManager().getConnectionManager().getConnections()) {
final ClassicLevelStorage classicLevelStorage = info.get(ClassicLevelStorage.class);
if (classicLevelStorage != null) {
info.getChannel().eventLoop().submit(() -> {
try {
classicLevelStorage.tick();
} catch (Throwable e) {
ViaLegacy.getPlatform().getLogger().log(Level.WARNING, "Error while ticking ClassicLevelStorage", e);
}
});
}
}
}
}

View File

@ -24,8 +24,5 @@ remap-based-on-color: true
# Classic chunk loading range
classic-chunk-range: 10
#
# How many chunks should be sent to the client per movement tick (-1 for auto)
chunks-per-tick: -1
#
# Enable fly on regular (non CPE) classic servers
enable-classic-fly: false