This commit is contained in:
themode 2020-08-19 16:19:18 +02:00
parent bed09d397a
commit 65db4a48b4
3 changed files with 32 additions and 8 deletions

View File

@ -18,6 +18,7 @@ import net.minestom.server.network.packet.server.play.ChunkDataPacket;
import net.minestom.server.network.packet.server.play.UpdateLightPacket;
import net.minestom.server.network.player.PlayerConnection;
import net.minestom.server.utils.BlockPosition;
import net.minestom.server.utils.MathUtils;
import net.minestom.server.utils.chunk.ChunkUtils;
import net.minestom.server.utils.player.PlayerUtils;
import net.minestom.server.utils.time.CooldownUtils;
@ -40,6 +41,8 @@ public abstract class Chunk implements Viewable {
public static final int CHUNK_SIZE_Z = 16;
public static final int CHUNK_SECTION_SIZE = 16;
public static final int CHUNK_SECTION_COUNT = CHUNK_SIZE_Y / CHUNK_SECTION_SIZE;
public static final int BIOME_COUNT = 1024; // 4x4x4 blocks
protected Biome[] biomes;
@ -176,7 +179,7 @@ public abstract class Chunk implements Viewable {
* <p>
* Use {@link #retrieveDataBuffer(Consumer)} to be sure to get the updated version
*
* @return the current cached data packet, can be null or outdated
* @return the last cached data packet, can be null or outdated
*/
public ByteBuf getFullDataPacket() {
return fullDataPacket;
@ -271,7 +274,13 @@ public abstract class Chunk implements Viewable {
return "Chunk[" + chunkX + ":" + chunkZ + "]";
}
// UNSAFE
/**
* Send the chunk to {@code player} and add it to the player viewing chunks collection
* and send a {@link PlayerChunkLoadEvent}
*
* @param player the viewer to add
* @return true if the player has just been added to the viewer collection
*/
@Override
public boolean addViewer(Player player) {
final boolean result = this.viewers.add(player);
@ -286,7 +295,13 @@ public abstract class Chunk implements Viewable {
return result;
}
// UNSAFE
/**
* Remove the chunk to the player viewing chunks collection
* and send a {@link PlayerChunkUnloadEvent}
*
* @param player the viewer to remove
* @return true if the player has just been removed to the viewer collection
*/
@Override
public boolean removeViewer(Player player) {
final boolean result = this.viewers.remove(player);
@ -382,14 +397,22 @@ public abstract class Chunk implements Viewable {
public void sendChunkSectionUpdate(int section, Player player) {
if (!PlayerUtils.isNettyClient(player))
return;
Check.argCondition(!MathUtils.isBetween(section, 0, CHUNK_SECTION_COUNT),
"The chunk section " + section + " does not exist");
PacketWriterUtils.writeAndSend(player, getChunkSectionUpdatePacket(section));
}
/**
* Get the {@link ChunkDataPacket} to update a single chunk section
*
* @param section the chunk section to update
* @return the {@link ChunkDataPacket} to update a single chunk sectionl
*/
protected ChunkDataPacket getChunkSectionUpdatePacket(int section) {
ChunkDataPacket chunkDataPacket = getFreshPartialDataPacket();
chunkDataPacket.fullChunk = false;
int[] sections = new int[16];
int[] sections = new int[CHUNK_SECTION_COUNT];
sections[section] = 1;
chunkDataPacket.sections = sections;
return chunkDataPacket;

View File

@ -1,4 +1,5 @@
package net.minestom.server.network.packet.server.play;
import net.minestom.server.network.packet.PacketWriter;
import net.minestom.server.network.packet.server.ServerPacket;
import net.minestom.server.network.packet.server.ServerPacketIdentifier;
@ -18,10 +19,10 @@ public class MultiBlockChangePacket implements ServerPacket {
writer.writeLong(ChunkUtils.getChunkIndexWithSection(chunkX, chunkZ, section));
writer.writeBoolean(suppressLightUpdates);
if (blockChanges != null) {
int length = blockChanges.length;
final int length = blockChanges.length;
writer.writeVarInt(length);
for (int i = 0; i < length; i++) {
BlockChange blockChange = blockChanges[i];
final BlockChange blockChange = blockChanges[i];
writer.writeVarInt(blockChange.newBlockId << 12 | ChunkUtils.getLocalBlockPosAsShort(blockChange.positionX, blockChange.positionY, blockChange.positionZ));
}
} else {

View File

@ -17,7 +17,7 @@ public final class MathUtils {
public static double round(double value, int places) {
if (places < 0) throw new IllegalArgumentException();
long factor = (long) Math.pow(10, places);
final long factor = (long) Math.pow(10, places);
value = value * factor;
long tmp = Math.round(value);
return (double) tmp / factor;
@ -26,7 +26,7 @@ public final class MathUtils {
public static float round(float value, int places) {
if (places < 0) throw new IllegalArgumentException();
long factor = (long) Math.pow(10, places);
final long factor = (long) Math.pow(10, places);
value = value * factor;
long tmp = Math.round(value);
return (float) tmp / factor;