From 08cee4678895007b42188511f52d97c2b8ce88d8 Mon Sep 17 00:00:00 2001 From: RaphiMC <50594595+RaphiMC@users.noreply.github.com> Date: Sun, 27 Aug 2023 00:14:20 +0200 Subject: [PATCH] Improved classic chunk loading system --- .../raphimc/vialegacy/ViaLegacyConfig.java | 7 --- .../vialegacy/platform/ViaLegacyConfig.java | 2 - .../Protocola1_0_15toc0_30.java | 16 ++----- .../storage/ClassicLevelStorage.java | 37 +++++++++------ .../task/ClassicLevelStorageTickTask.java | 45 +++++++++++++++++++ .../resources/assets/vialegacy/vialegacy.yml | 3 -- 6 files changed, 72 insertions(+), 38 deletions(-) create mode 100644 src/main/java/net/raphimc/vialegacy/protocols/classic/protocola1_0_15toc0_28_30/task/ClassicLevelStorageTickTask.java diff --git a/src/main/java/net/raphimc/vialegacy/ViaLegacyConfig.java b/src/main/java/net/raphimc/vialegacy/ViaLegacyConfig.java index c266c2b..bad2c2d 100644 --- a/src/main/java/net/raphimc/vialegacy/ViaLegacyConfig.java +++ b/src/main/java/net/raphimc/vialegacy/ViaLegacyConfig.java @@ -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; diff --git a/src/main/java/net/raphimc/vialegacy/platform/ViaLegacyConfig.java b/src/main/java/net/raphimc/vialegacy/platform/ViaLegacyConfig.java index ddc707e..28faddd 100644 --- a/src/main/java/net/raphimc/vialegacy/platform/ViaLegacyConfig.java +++ b/src/main/java/net/raphimc/vialegacy/platform/ViaLegacyConfig.java @@ -35,8 +35,6 @@ public interface ViaLegacyConfig { int getClassicChunkRange(); - int getChunksPerTick(); - boolean enableClassicFly(); } diff --git a/src/main/java/net/raphimc/vialegacy/protocols/classic/protocola1_0_15toc0_28_30/Protocola1_0_15toc0_30.java b/src/main/java/net/raphimc/vialegacy/protocols/classic/protocola1_0_15toc0_28_30/Protocola1_0_15toc0_30.java index ce45515..8bb4d6e 100644 --- a/src/main/java/net/raphimc/vialegacy/protocols/classic/protocola1_0_15toc0_28_30/Protocola1_0_15toc0_30.java +++ b/src/main/java/net/raphimc/vialegacy/protocols/classic/protocola1_0_15toc0_28_30/Protocola1_0_15toc0_30.java @@ -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 { - 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 { 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 { 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 { 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 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 { diff --git a/src/main/java/net/raphimc/vialegacy/protocols/classic/protocola1_0_15toc0_28_30/task/ClassicLevelStorageTickTask.java b/src/main/java/net/raphimc/vialegacy/protocols/classic/protocola1_0_15toc0_28_30/task/ClassicLevelStorageTickTask.java new file mode 100644 index 0000000..42df291 --- /dev/null +++ b/src/main/java/net/raphimc/vialegacy/protocols/classic/protocola1_0_15toc0_28_30/task/ClassicLevelStorageTickTask.java @@ -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 . + */ +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); + } + }); + } + } + } + +} diff --git a/src/main/resources/assets/vialegacy/vialegacy.yml b/src/main/resources/assets/vialegacy/vialegacy.yml index 6b80c7e..c211487 100644 --- a/src/main/resources/assets/vialegacy/vialegacy.yml +++ b/src/main/resources/assets/vialegacy/vialegacy.yml @@ -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