mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-22 08:02:31 +01:00
Comments + utils methods for BinaryWriter
This commit is contained in:
parent
74cfc1994d
commit
925c0a89f6
@ -44,7 +44,6 @@ import java.util.UUID;
|
|||||||
|
|
||||||
public class PlayerInit {
|
public class PlayerInit {
|
||||||
private static volatile InstanceContainer instanceContainer;
|
private static volatile InstanceContainer instanceContainer;
|
||||||
private static volatile InstanceContainer netherTest;
|
|
||||||
|
|
||||||
private static volatile Inventory inventory;
|
private static volatile Inventory inventory;
|
||||||
|
|
||||||
@ -52,28 +51,17 @@ public class PlayerInit {
|
|||||||
StorageLocation storageLocation = MinecraftServer.getStorageManager().getLocation("instance_data", new StorageOptions().setCompression(true));
|
StorageLocation storageLocation = MinecraftServer.getStorageManager().getLocation("instance_data", new StorageOptions().setCompression(true));
|
||||||
ChunkGeneratorDemo chunkGeneratorDemo = new ChunkGeneratorDemo();
|
ChunkGeneratorDemo chunkGeneratorDemo = new ChunkGeneratorDemo();
|
||||||
NoiseTestGenerator noiseTestGenerator = new NoiseTestGenerator();
|
NoiseTestGenerator noiseTestGenerator = new NoiseTestGenerator();
|
||||||
instanceContainer = MinecraftServer.getInstanceManager().createInstanceContainer(storageLocation);
|
instanceContainer = MinecraftServer.getInstanceManager().createInstanceContainer(DimensionType.OVERWORLD, storageLocation);
|
||||||
//instanceContainer = MinecraftServer.getInstanceManager().createInstanceContainer(DimensionType.OVERWORLD);
|
|
||||||
instanceContainer.enableAutoChunkLoad(true);
|
instanceContainer.enableAutoChunkLoad(true);
|
||||||
//instanceContainer.setChunkDecider((x,y) -> (pos) -> pos.getY()>40?(short)0:(short)1);
|
//instanceContainer.setChunkDecider((x,y) -> (pos) -> pos.getY()>40?(short)0:(short)1);
|
||||||
instanceContainer.setChunkGenerator(noiseTestGenerator);
|
instanceContainer.setChunkGenerator(noiseTestGenerator);
|
||||||
|
|
||||||
netherTest = MinecraftServer.getInstanceManager().createInstanceContainer(DimensionType.OVERWORLD);
|
|
||||||
netherTest.enableAutoChunkLoad(true);
|
|
||||||
netherTest.setChunkGenerator(noiseTestGenerator);
|
|
||||||
|
|
||||||
InstanceContainer end = MinecraftServer.getInstanceManager().createInstanceContainer(DimensionType.OVERWORLD);
|
|
||||||
end.enableAutoChunkLoad(true);
|
|
||||||
end.setChunkGenerator(noiseTestGenerator);
|
|
||||||
|
|
||||||
// Load some chunks beforehand
|
// Load some chunks beforehand
|
||||||
final int loopStart = -10;
|
final int loopStart = -10;
|
||||||
final int loopEnd = 10;
|
final int loopEnd = 10;
|
||||||
for (int x = loopStart; x < loopEnd; x++)
|
for (int x = loopStart; x < loopEnd; x++)
|
||||||
for (int z = loopStart; z < loopEnd; z++) {
|
for (int z = loopStart; z < loopEnd; z++) {
|
||||||
instanceContainer.loadChunk(x, z);
|
instanceContainer.loadChunk(x, z);
|
||||||
netherTest.loadChunk(x, z);
|
|
||||||
end.loadChunk(x, z);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
inventory = new Inventory(InventoryType.CHEST_1_ROW, "Test inventory");
|
inventory = new Inventory(InventoryType.CHEST_1_ROW, "Test inventory");
|
||||||
|
@ -1,19 +1,8 @@
|
|||||||
package fr.themode.demo.commands;
|
package fr.themode.demo.commands;
|
||||||
|
|
||||||
import net.minestom.server.MinecraftServer;
|
|
||||||
import net.minestom.server.command.CommandProcessor;
|
import net.minestom.server.command.CommandProcessor;
|
||||||
import net.minestom.server.command.CommandSender;
|
import net.minestom.server.command.CommandSender;
|
||||||
import net.minestom.server.entity.Player;
|
import net.minestom.server.entity.Player;
|
||||||
import net.minestom.server.instance.ChunkGenerator;
|
|
||||||
import net.minestom.server.instance.ChunkPopulator;
|
|
||||||
import net.minestom.server.instance.InstanceContainer;
|
|
||||||
import net.minestom.server.instance.InstanceManager;
|
|
||||||
import net.minestom.server.instance.batch.ChunkBatch;
|
|
||||||
import net.minestom.server.world.DimensionType;
|
|
||||||
import net.minestom.server.world.biomes.Biome;
|
|
||||||
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
public class SimpleCommand implements CommandProcessor {
|
public class SimpleCommand implements CommandProcessor {
|
||||||
@Override
|
@Override
|
||||||
@ -31,35 +20,7 @@ public class SimpleCommand implements CommandProcessor {
|
|||||||
if (!sender.isPlayer())
|
if (!sender.isPlayer())
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
final int word = 2;
|
sender.asPlayer().getInstance().saveChunksToStorage(() -> System.out.println("END SAVE"));
|
||||||
ChunkGenerator chunkGeneratorDemo = new ChunkGenerator() {
|
|
||||||
@Override
|
|
||||||
public void generateChunkData(ChunkBatch batch, int chunkX, int chunkZ) {
|
|
||||||
for (int x = 0; x < 16; x++) {
|
|
||||||
for (int z = 0; z < 16; z++) {
|
|
||||||
batch.setBlockStateId(x, 1, z, (short) word);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void fillBiomes(Biome[] biomes, int chunkX, int chunkZ) {
|
|
||||||
Arrays.fill(biomes, Biome.PLAINS);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public List<ChunkPopulator> getPopulators() {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
InstanceManager instanceManager = MinecraftServer.getInstanceManager();
|
|
||||||
InstanceContainer instanceContainer = instanceManager.createInstanceContainer(DimensionType.OVERWORLD);
|
|
||||||
instanceContainer.enableAutoChunkLoad(true);
|
|
||||||
instanceContainer.setChunkGenerator(chunkGeneratorDemo);
|
|
||||||
|
|
||||||
sender.asPlayer().setInstance(instanceContainer);
|
|
||||||
System.out.println(MinecraftServer.getInstanceManager().getInstances().size());
|
|
||||||
|
|
||||||
System.gc();
|
System.gc();
|
||||||
|
|
||||||
|
@ -3,11 +3,15 @@ package net.minestom.server.data;
|
|||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Represent an object which contain key/value based data
|
||||||
|
*/
|
||||||
public interface Data {
|
public interface Data {
|
||||||
|
|
||||||
Data EMPTY = new Data() {
|
Data EMPTY = new Data() {
|
||||||
@Override
|
@Override
|
||||||
public <T> void set(String key, T value, Class<T> type) { }
|
public <T> void set(String key, T value, Class<T> type) {
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <T> T get(String key) {
|
public <T> T get(String key) {
|
||||||
@ -89,7 +93,7 @@ public interface Data {
|
|||||||
*
|
*
|
||||||
* @return true if the data does not contain anything, false otherwise
|
* @return true if the data does not contain anything, false otherwise
|
||||||
*/
|
*/
|
||||||
boolean isEmpty();
|
boolean isEmpty();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Clone this data
|
* Clone this data
|
||||||
|
@ -4,6 +4,9 @@ import java.util.Collections;
|
|||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@link Data} implementation which use a {@link ConcurrentHashMap}
|
||||||
|
*/
|
||||||
public class DataImpl implements Data {
|
public class DataImpl implements Data {
|
||||||
|
|
||||||
protected final ConcurrentHashMap<String, Object> data = new ConcurrentHashMap<>();
|
protected final ConcurrentHashMap<String, Object> data = new ConcurrentHashMap<>();
|
||||||
|
@ -6,6 +6,9 @@ import net.minestom.server.reader.DataReader;
|
|||||||
import net.minestom.server.utils.binary.BinaryReader;
|
import net.minestom.server.utils.binary.BinaryReader;
|
||||||
import net.minestom.server.utils.binary.BinaryWriter;
|
import net.minestom.server.utils.binary.BinaryWriter;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Represent a {@link Data} object which can be serialized and read back by the {@link DataReader}
|
||||||
|
*/
|
||||||
public interface SerializableData extends Data {
|
public interface SerializableData extends Data {
|
||||||
|
|
||||||
DataManager DATA_MANAGER = MinecraftServer.getDataManager();
|
DataManager DATA_MANAGER = MinecraftServer.getDataManager();
|
||||||
|
@ -1,7 +1,5 @@
|
|||||||
package net.minestom.server.data;
|
package net.minestom.server.data;
|
||||||
|
|
||||||
import io.netty.buffer.ByteBuf;
|
|
||||||
import io.netty.buffer.Unpooled;
|
|
||||||
import it.unimi.dsi.fastutil.objects.Object2ShortMap;
|
import it.unimi.dsi.fastutil.objects.Object2ShortMap;
|
||||||
import it.unimi.dsi.fastutil.objects.Object2ShortOpenHashMap;
|
import it.unimi.dsi.fastutil.objects.Object2ShortOpenHashMap;
|
||||||
import net.minestom.server.utils.PrimitiveConversion;
|
import net.minestom.server.utils.PrimitiveConversion;
|
||||||
@ -10,6 +8,9 @@ import net.minestom.server.utils.binary.BinaryWriter;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@link SerializableData} implementation based on {@link DataImpl}
|
||||||
|
*/
|
||||||
public class SerializableDataImpl extends DataImpl implements SerializableData {
|
public class SerializableDataImpl extends DataImpl implements SerializableData {
|
||||||
|
|
||||||
private ConcurrentHashMap<String, Class> dataType = new ConcurrentHashMap<>();
|
private ConcurrentHashMap<String, Class> dataType = new ConcurrentHashMap<>();
|
||||||
@ -91,10 +92,8 @@ public class SerializableDataImpl extends DataImpl implements SerializableData {
|
|||||||
// The buffer containing all the index info (class name to class index)
|
// The buffer containing all the index info (class name to class index)
|
||||||
BinaryWriter indexWriter = new BinaryWriter();
|
BinaryWriter indexWriter = new BinaryWriter();
|
||||||
SerializableData.writeDataIndexHeader(indexWriter, typeToIndexMap);
|
SerializableData.writeDataIndexHeader(indexWriter, typeToIndexMap);
|
||||||
// Merge the index buffer & the main data buffer
|
// Set the header
|
||||||
final ByteBuf finalBuffer = Unpooled.wrappedBuffer(indexWriter.getBuffer(), binaryWriter.getBuffer());
|
binaryWriter.writeAtStart(indexWriter);
|
||||||
// Change the main writer buffer, so it contains both the indexes and the data
|
|
||||||
binaryWriter.setBuffer(finalBuffer);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return binaryWriter.toByteArray();
|
return binaryWriter.toByteArray();
|
||||||
|
@ -1,8 +1,6 @@
|
|||||||
package net.minestom.server.instance;
|
package net.minestom.server.instance;
|
||||||
|
|
||||||
import com.extollit.gaming.ai.path.model.ColumnarOcclusionFieldList;
|
import com.extollit.gaming.ai.path.model.ColumnarOcclusionFieldList;
|
||||||
import io.netty.buffer.ByteBuf;
|
|
||||||
import io.netty.buffer.Unpooled;
|
|
||||||
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
|
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
|
||||||
import it.unimi.dsi.fastutil.objects.Object2ShortMap;
|
import it.unimi.dsi.fastutil.objects.Object2ShortMap;
|
||||||
import it.unimi.dsi.fastutil.objects.Object2ShortOpenHashMap;
|
import it.unimi.dsi.fastutil.objects.Object2ShortOpenHashMap;
|
||||||
@ -204,10 +202,8 @@ public class DynamicChunk extends Chunk {
|
|||||||
SerializableData.writeDataIndexHeader(indexWriter, typeToIndexMap);
|
SerializableData.writeDataIndexHeader(indexWriter, typeToIndexMap);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create the final buffer (data index buffer followed by the chunk buffer)
|
// Add the hasIndex field & the index header if it has it
|
||||||
final ByteBuf finalBuffer = Unpooled.wrappedBuffer(indexWriter.getBuffer(), binaryWriter.getBuffer());
|
binaryWriter.writeAtStart(indexWriter);
|
||||||
// Change the main writer buffer
|
|
||||||
binaryWriter.setBuffer(finalBuffer);
|
|
||||||
|
|
||||||
return binaryWriter.toByteArray();
|
return binaryWriter.toByteArray();
|
||||||
}
|
}
|
||||||
|
@ -72,7 +72,7 @@ public class PacketCompressor extends ByteToMessageCodec<ByteBuf> {
|
|||||||
@Override
|
@Override
|
||||||
protected void decode(ChannelHandlerContext ctx, ByteBuf buf, List<Object> out) throws Exception {
|
protected void decode(ChannelHandlerContext ctx, ByteBuf buf, List<Object> out) throws Exception {
|
||||||
if (buf.readableBytes() != 0) {
|
if (buf.readableBytes() != 0) {
|
||||||
int i = Utils.readVarInt(buf);
|
final int i = Utils.readVarInt(buf);
|
||||||
|
|
||||||
if (i == 0) {
|
if (i == 0) {
|
||||||
out.add(buf.readRetainedSlice(buf.readableBytes()));
|
out.add(buf.readRetainedSlice(buf.readableBytes()));
|
||||||
|
@ -15,6 +15,10 @@ import java.io.IOException;
|
|||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class used to read from a byte array
|
||||||
|
* WARNING: not thread-safe
|
||||||
|
*/
|
||||||
public class BinaryReader extends InputStream {
|
public class BinaryReader extends InputStream {
|
||||||
|
|
||||||
private final ByteBuf buffer;
|
private final ByteBuf buffer;
|
||||||
|
@ -16,6 +16,10 @@ import java.nio.charset.StandardCharsets;
|
|||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class used to write to a byte array
|
||||||
|
* WARNING: not thread-safe
|
||||||
|
*/
|
||||||
public class BinaryWriter extends OutputStream {
|
public class BinaryWriter extends OutputStream {
|
||||||
|
|
||||||
private ByteBuf buffer;
|
private ByteBuf buffer;
|
||||||
@ -252,6 +256,34 @@ public class BinaryWriter extends OutputStream {
|
|||||||
return bytes;
|
return bytes;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add a {@link BinaryWriter}'s {@link ByteBuf} at the beginning of this writer
|
||||||
|
*
|
||||||
|
* @param headerWriter the {@link BinaryWriter} to add at the beginning
|
||||||
|
*/
|
||||||
|
public void writeAtStart(BinaryWriter headerWriter) {
|
||||||
|
// Get the buffer of the header
|
||||||
|
final ByteBuf headerBuf = headerWriter.getBuffer();
|
||||||
|
// Merge both the headerBuf and this buffer
|
||||||
|
final ByteBuf finalBuffer = Unpooled.wrappedBuffer(headerBuf, buffer);
|
||||||
|
// Change the buffer used by this writer
|
||||||
|
setBuffer(finalBuffer);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add a {@link BinaryWriter}'s {@link ByteBuf} at the end of this writer
|
||||||
|
*
|
||||||
|
* @param footerWriter the {@link BinaryWriter} to add at the end
|
||||||
|
*/
|
||||||
|
public void writeAtEnd(BinaryWriter footerWriter) {
|
||||||
|
// Get the buffer of the footer
|
||||||
|
final ByteBuf footerBuf = footerWriter.getBuffer();
|
||||||
|
// Merge both this buffer and the footerBuf
|
||||||
|
final ByteBuf finalBuffer = Unpooled.wrappedBuffer(buffer, footerBuf);
|
||||||
|
// Change the buffer used by this writer
|
||||||
|
setBuffer(finalBuffer);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the raw buffer used by this binary writer
|
* Get the raw buffer used by this binary writer
|
||||||
*
|
*
|
||||||
|
Loading…
Reference in New Issue
Block a user