-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
scale back objects to simply chunk user data. May add a more structur…
…ed object system in the future
- Loading branch information
Showing
11 changed files
with
170 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,38 @@ | ||
package net.hollowcube.polar; | ||
|
||
import net.minestom.server.instance.Chunk; | ||
import net.minestom.server.network.NetworkBuffer; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
/** | ||
* Provides access to user world data for a {@link PolarLoader} to get and set user | ||
* specific world data such as objects, as well as provides some relevant callbacks. | ||
* <br/><br/> | ||
* Usage if world access is completely optional, dependent features will not add | ||
* overhead to the format if unused. | ||
*/ | ||
@SuppressWarnings("UnstableApiUsage") | ||
public interface PolarWorldAccess { | ||
|
||
/** | ||
* Called when a chunk is created, just before it is added to the world. | ||
* <br/><br/> | ||
* Can be used to initialize the chunk based on saved user data in the world. | ||
* | ||
* @param chunk The Minestom chunk being created | ||
* @param userData The saved user data, or null if none is present | ||
*/ | ||
default void loadChunkData(@NotNull Chunk chunk, @Nullable NetworkBuffer userData) {} | ||
|
||
/** | ||
* Called when a chunk is being saved. | ||
* <br/><br/> | ||
* Can be used to save user data in the chunk by writing it to the buffer. | ||
* | ||
* @param chunk The Minestom chunk being saved | ||
* @param userData A buffer to write user data to save | ||
*/ | ||
default void saveChunkData(@NotNull Chunk chunk, @NotNull NetworkBuffer userData) {} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
src/test/java/net/hollowcube/polar/TestUserDataWriteRead.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package net.hollowcube.polar; | ||
|
||
import net.minestom.server.MinecraftServer; | ||
import net.minestom.server.instance.InstanceContainer; | ||
import net.minestom.server.network.NetworkBuffer; | ||
import net.minestom.server.world.DimensionType; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.nio.ByteBuffer; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.UUID; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
class TestUserDataWriteRead { | ||
|
||
static { | ||
MinecraftServer.init(); | ||
} | ||
|
||
@Test | ||
void testWriteRead() { | ||
var world = new PolarWorld(); | ||
|
||
var emptySections = new PolarSection[24]; | ||
Arrays.fill(emptySections, new PolarSection()); | ||
world.updateChunkAt(0, 0, new PolarChunk(0, 0, emptySections, List.of(), new byte[0][0], new byte[0])); | ||
|
||
var wa = new UpdateTimeWorldAccess(); | ||
var loader = new PolarLoader(world).setWorldAccess(wa); | ||
var instance = new InstanceContainer(UUID.randomUUID(), DimensionType.OVERWORLD, loader); | ||
var chunk = loader.loadChunk(instance, 0, 0).join(); | ||
|
||
loader.saveChunk(chunk).join(); | ||
|
||
var newPolarChunk = world.chunkAt(0, 0); | ||
var savedTime = new NetworkBuffer(ByteBuffer.wrap(newPolarChunk.userData())).read(NetworkBuffer.LONG); | ||
assertEquals(wa.saveTime, savedTime); | ||
|
||
loader.loadChunk(instance, 0, 0).join(); | ||
assertEquals(wa.loadTime, savedTime); | ||
} | ||
|
||
} |
31 changes: 31 additions & 0 deletions
31
src/test/java/net/hollowcube/polar/UpdateTimeWorldAccess.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package net.hollowcube.polar; | ||
|
||
import net.minestom.server.instance.Chunk; | ||
import net.minestom.server.network.NetworkBuffer; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
@SuppressWarnings("UnstableApiUsage") | ||
public class UpdateTimeWorldAccess implements PolarWorldAccess { | ||
private static final Logger logger = LoggerFactory.getLogger(UpdateTimeWorldAccess.class); | ||
|
||
public long saveTime = 0; | ||
public long loadTime = 0; | ||
|
||
@Override | ||
public void loadChunkData(@NotNull Chunk chunk, @Nullable NetworkBuffer userData) { | ||
if (userData == null) return; // No saved data, probably first load | ||
|
||
long lastSaveTime = userData.read(NetworkBuffer.LONG); | ||
logger.info("loading chunk {}, {} which was saved at {}.", chunk.getChunkX(), chunk.getChunkZ(), lastSaveTime); | ||
loadTime = lastSaveTime; | ||
} | ||
|
||
@Override | ||
public void saveChunkData(@NotNull Chunk chunk, @NotNull NetworkBuffer userData) { | ||
saveTime = System.currentTimeMillis(); | ||
userData.write(NetworkBuffer.LONG, saveTime); | ||
} | ||
} |