Fix level handling for real time updates

This commit is contained in:
s809
2024-07-12 23:11:36 +05:00
parent 2617cd294d
commit 778c2f894e
4 changed files with 36 additions and 14 deletions
@@ -133,7 +133,7 @@ public class DhClientLevel extends AbstractDhLevel implements IDhClientLevel
{
try
{
if (msg.dataSourceDto == null)
if (!msg.isSameLevelAs(this.levelWrapper))
{
return;
}
@@ -288,6 +288,12 @@ public class DhClientLevel extends AbstractDhLevel implements IDhClientLevel
{
this.worldGenModule.close();
}
if (this.eventSource != null)
{
this.eventSource.close();
}
this.clientside.close();
super.close();
this.dataFileHandler.close();
@@ -61,14 +61,19 @@ public class DhServerLevel extends AbstractDhLevel implements IDhServerLevel
public final ServerLevelModule serverside;
private final IServerLevelWrapper serverLevelWrapper;
private final RemotePlayerConnectionHandler remotePlayerConnectionHandler;
private final ConcurrentLinkedQueue<IServerPlayerWrapper> worldGenLoopingQueue = new ConcurrentLinkedQueue<>();
/**
* This queue is used for ensuring fair generation speed for each player. <br>
* Every tick the first player gets used for centering generation, and then is immediately moved into the back of the queue. <br>
* TODO only add players that actually have something to generate
*/
private final ConcurrentLinkedQueue<IServerPlayerWrapper> worldGenPlayerCenteringQueue = new ConcurrentLinkedQueue<>();
private final ConcurrentMap<Long, DataSourceRequestGroup> requestGroupsByPos = new ConcurrentHashMap<>();
private final ConcurrentMap<Long, DataSourceRequestGroup> requestGroupsByFutureId = new ConcurrentHashMap<>();
public DhServerLevel(AbstractSaveStructure saveStructure, IServerLevelWrapper serverLevelWrapper, RemotePlayerConnectionHandler remotePlayerConnectionHandler)
{
if (saveStructure.getFullDataFolder(serverLevelWrapper).mkdirs())
@@ -233,12 +238,12 @@ public class DhServerLevel extends AbstractDhLevel implements IDhServerLevel
public void addPlayer(IServerPlayerWrapper serverPlayer)
{
this.worldGenLoopingQueue.add(serverPlayer);
this.worldGenPlayerCenteringQueue.add(serverPlayer);
}
public void removePlayer(IServerPlayerWrapper serverPlayer)
{
this.worldGenLoopingQueue.remove(serverPlayer);
this.worldGenPlayerCenteringQueue.remove(serverPlayer);
}
@Override
@@ -307,6 +312,11 @@ public class DhServerLevel extends AbstractDhLevel implements IDhServerLevel
FullDataPartialUpdateMessage updateMessage = new FullDataPartialUpdateMessage(this.serverLevelWrapper, data);
for (ServerPlayerState serverPlayerState : this.remotePlayerConnectionHandler.getConnectedPlayers())
{
if (serverPlayerState.serverPlayer().getLevel() != this.serverLevelWrapper)
{
continue;
}
if (!serverPlayerState.config.isRealTimeUpdatesEnabled())
{
continue;
@@ -358,7 +368,7 @@ public class DhServerLevel extends AbstractDhLevel implements IDhServerLevel
if (this.serverside.worldGenModule.isWorldGenRunning())
{
IServerPlayerWrapper firstPlayer = this.worldGenLoopingQueue.peek();
IServerPlayerWrapper firstPlayer = this.worldGenPlayerCenteringQueue.peek();
if (firstPlayer == null)
{
return;
@@ -366,8 +376,8 @@ public class DhServerLevel extends AbstractDhLevel implements IDhServerLevel
// Put first player in back before removing from front, so it can be removed by other thread without blocking
// - if it gets removed, remove() below will remove the item we just put instead
this.worldGenLoopingQueue.add(firstPlayer);
this.worldGenLoopingQueue.remove(firstPlayer);
this.worldGenPlayerCenteringQueue.add(firstPlayer);
this.worldGenPlayerCenteringQueue.remove(firstPlayer);
Vec3d position = firstPlayer.getPosition();
this.serverside.worldGenModule.worldGenTick(new DhBlockPos2D((int) position.x, (int) position.z));
@@ -1,5 +1,6 @@
package com.seibel.distanthorizons.core.network.messages;
import com.seibel.distanthorizons.core.wrapperInterfaces.world.ILevelWrapper;
import com.seibel.distanthorizons.core.wrapperInterfaces.world.IServerLevelWrapper;
public interface ILevelRelatedMessage
@@ -10,9 +11,14 @@ public interface ILevelRelatedMessage
* Checks whether the message's level matches the given level.
*/
@SuppressWarnings("BooleanMethodIsAlwaysInverted")
default boolean isSameLevelAs(IServerLevelWrapper levelWrapper)
default boolean isSameLevelAs(ILevelWrapper levelWrapper)
{
return this.getLevelName().equals(levelWrapper.getKeyedLevelDimensionName());
if (levelWrapper instanceof IServerLevelWrapper)
{
return this.getLevelName().equals(((IServerLevelWrapper) levelWrapper).getKeyedLevelDimensionName());
}
return this.getLevelName().equals(levelWrapper.getDimensionName());
}
}
@@ -27,7 +27,7 @@ import com.seibel.distanthorizons.core.network.messages.ILevelRelatedMessage;
import com.seibel.distanthorizons.core.network.messages.NetworkMessage;
import com.seibel.distanthorizons.core.network.INetworkObject;
import com.seibel.distanthorizons.core.sql.dto.FullDataSourceV2DTO;
import com.seibel.distanthorizons.core.wrapperInterfaces.world.ILevelWrapper;
import com.seibel.distanthorizons.core.wrapperInterfaces.world.IServerLevelWrapper;
import io.netty.buffer.ByteBuf;
import java.io.IOException;
@@ -42,9 +42,9 @@ public class FullDataPartialUpdateMessage extends NetworkMessage implements ILev
public FullDataPartialUpdateMessage() { }
public FullDataPartialUpdateMessage(ILevelWrapper level, FullDataSourceV2 fullDataSource)
public FullDataPartialUpdateMessage(IServerLevelWrapper level, FullDataSourceV2 fullDataSource)
{
this.levelName = level.getDimensionName();
this.levelName = level.getKeyedLevelDimensionName();
try
{