Merge branch 'dev/sonar-fix' into development
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
package com.flowpowered.nbt;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public enum TagType {
|
||||
|
||||
@@ -7,7 +7,6 @@ import mc.core.world.chunk.Chunk;
|
||||
import mc.core.world.chunk.ChunkProvider;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
|
||||
@@ -20,7 +19,7 @@ public class AnvilChunkProvider implements ChunkProvider {
|
||||
|
||||
public AnvilChunkProvider(String mapPath) {
|
||||
Path pathMap = Paths.get(mapPath);
|
||||
if (Files.exists(pathMap)) {
|
||||
if (pathMap.toFile().exists()) {
|
||||
log.info("Use Anvil map from \"{}\"", pathMap);
|
||||
this.setRegionManager(new RegionManager(pathMap.resolve("region")));
|
||||
} else {
|
||||
|
||||
@@ -2,7 +2,6 @@ package mc.world.anvil;
|
||||
|
||||
import gnu.trove.list.TByteList;
|
||||
import gnu.trove.list.array.TByteArrayList;
|
||||
import gnu.trove.list.linked.TByteLinkedList;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import mc.core.utils.NibbleArray;
|
||||
|
||||
@@ -7,7 +7,6 @@ import mc.core.utils.CompactedCoords;
|
||||
import org.springframework.lang.Nullable;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
|
||||
@@ -32,7 +31,7 @@ public class RegionManager {
|
||||
return regions.get(xz);
|
||||
} else {
|
||||
Path regionFilePath = regionFilesPath.resolve("r." + x + "." + z + ".mca");
|
||||
if (Files.exists(regionFilePath)) {
|
||||
if (regionFilePath.toFile().exists()) {
|
||||
try {
|
||||
Region region = new Region(regionFilePath.toFile());
|
||||
regions.put(xz, region);
|
||||
|
||||
@@ -19,6 +19,40 @@ public class CoreEventListener {
|
||||
EventBus.getInstance().registerSubscribes(this);
|
||||
}
|
||||
|
||||
private void processLoadChunks(CS_PlayerMoveEvent event, int cMinX, int cMaxX, int cMinZ, int cMaxZ) {
|
||||
SC_ChunkLoadEvent eventChunkLoad = new SC_ChunkLoadEvent(event.getPlayer());
|
||||
for (int cZ = cMinZ; cZ <= cMaxZ; cZ++) {
|
||||
for (int cX = cMinX; cX <= cMaxX; cX++) {
|
||||
int compressXZ = CompactedCoords.compressXZ(cX, cZ);
|
||||
if (!event.getPlayer().getLoadedChunks().contains(compressXZ)) {
|
||||
eventChunkLoad.getNeedLoadChunks().add(compressXZ);
|
||||
event.getPlayer().getLoadedChunks().add(compressXZ);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!eventChunkLoad.getNeedLoadChunks().isEmpty()) {
|
||||
EventBus.getInstance().post(eventChunkLoad);
|
||||
}
|
||||
}
|
||||
|
||||
private void processUnloadChunks(CS_PlayerMoveEvent event, int cMinX, int cMaxX, int cMinZ, int cMaxZ) {
|
||||
SC_ChunkUnloadEvent eventChunkUnload = new SC_ChunkUnloadEvent(event.getPlayer());
|
||||
Iterator<Integer> itr = event.getPlayer().getLoadedChunks().iterator();
|
||||
while(itr.hasNext()) {
|
||||
int compressXZ = itr.next();
|
||||
int[] xz = CompactedCoords.uncompressXZ(compressXZ);
|
||||
if (xz[0] > cMaxX || xz[0] < cMinX || xz[1] > cMaxZ || xz[1] < cMinZ) {
|
||||
eventChunkUnload.getNeedUnloadChunks().add(compressXZ);
|
||||
itr.remove();
|
||||
}
|
||||
}
|
||||
|
||||
if (!eventChunkUnload.getNeedUnloadChunks().isEmpty()) {
|
||||
EventBus.getInstance().post(eventChunkUnload);
|
||||
}
|
||||
}
|
||||
|
||||
@Subscriber
|
||||
public void handlerPlayerMoveEvent(CS_PlayerMoveEvent event) {
|
||||
Chunk chunk;
|
||||
@@ -33,42 +67,14 @@ public class CoreEventListener {
|
||||
|
||||
if (event.isRecalcChunk() || (ncX != ccX || ncZ != ccZ)) {
|
||||
final int viewDistance = event.getPlayer().getSettings().getViewDistance() + 1;
|
||||
int cMinX = chunk.getX() - viewDistance;
|
||||
int cMaxX = chunk.getX() + viewDistance;
|
||||
int cMinZ = chunk.getZ() - viewDistance;
|
||||
int cMaxZ = chunk.getZ() + viewDistance;
|
||||
final int cMinX = chunk.getX() - viewDistance;
|
||||
final int cMaxX = chunk.getX() + viewDistance;
|
||||
final int cMinZ = chunk.getZ() - viewDistance;
|
||||
final int cMaxZ = chunk.getZ() + viewDistance;
|
||||
|
||||
SC_ChunkLoadEvent eventChunkLoad = new SC_ChunkLoadEvent(event.getPlayer());
|
||||
for (int cZ = cMinZ; cZ <= cMaxZ; cZ++) {
|
||||
for (int cX = cMinX; cX <= cMaxX; cX++) {
|
||||
int compressXZ = CompactedCoords.compressXZ(cX, cZ);
|
||||
if (!event.getPlayer().getLoadedChunks().contains(compressXZ)) {
|
||||
if (!event.getPlayer().getLoadedChunks().contains(compressXZ)) {
|
||||
eventChunkLoad.getNeedLoadChunks().add(compressXZ);
|
||||
event.getPlayer().getLoadedChunks().add(compressXZ);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
processLoadChunks(event, cMinX, cMaxX, cMinZ, cMaxZ);
|
||||
processUnloadChunks(event, cMinX, cMaxX, cMinZ, cMaxZ);
|
||||
|
||||
if (!eventChunkLoad.getNeedLoadChunks().isEmpty()) {
|
||||
EventBus.getInstance().post(eventChunkLoad);
|
||||
}
|
||||
|
||||
SC_ChunkUnloadEvent eventChunkUnload = new SC_ChunkUnloadEvent(event.getPlayer());
|
||||
Iterator<Integer> itr = event.getPlayer().getLoadedChunks().iterator();
|
||||
while(itr.hasNext()) {
|
||||
int compressXZ = itr.next();
|
||||
int[] xz = CompactedCoords.uncompressXZ(compressXZ);
|
||||
if (xz[0] > cMaxX || xz[0] < cMinX || xz[1] > cMaxZ || xz[1] < cMinZ) {
|
||||
eventChunkUnload.getNeedUnloadChunks().add(compressXZ);
|
||||
itr.remove();
|
||||
}
|
||||
}
|
||||
|
||||
if (!eventChunkUnload.getNeedUnloadChunks().isEmpty()) {
|
||||
EventBus.getInstance().post(eventChunkUnload);
|
||||
}
|
||||
}
|
||||
|
||||
event.getPlayer().getLocation().setXYZ(
|
||||
|
||||
@@ -3,12 +3,14 @@ package mc.core;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import mc.core.world.block.BlockLocation;
|
||||
|
||||
@Slf4j
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@Data
|
||||
public class EntityLocation implements Cloneable {
|
||||
public class EntityLocation {
|
||||
private double x, y, z;
|
||||
private float yaw, pitch;
|
||||
|
||||
@@ -48,13 +50,7 @@ public class EntityLocation implements Cloneable {
|
||||
return new BlockLocation(getBlockX(), getBlockY(), getBlockZ());
|
||||
}
|
||||
|
||||
@Override
|
||||
public EntityLocation clone() {
|
||||
try {
|
||||
return (EntityLocation) super.clone();
|
||||
} catch (CloneNotSupportedException e) {
|
||||
e.printStackTrace();
|
||||
return ZERO();
|
||||
}
|
||||
public EntityLocation copy() {
|
||||
return new EntityLocation(x, y, z, yaw, pitch);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,17 +7,16 @@ import org.apache.commons.io.IOUtils;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.support.FileSystemXmlApplicationContext;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
|
||||
@Slf4j
|
||||
public class Main {
|
||||
private static ApplicationContext createContext() {
|
||||
final String springXml = System.getProperty("springConfig", "./spring.xml");
|
||||
|
||||
if (!Files.exists(Paths.get(springXml))) {
|
||||
if (!(new File(springXml)).exists()) {
|
||||
log.info("File \"{}\" not found. Get default config.", springXml);
|
||||
try (FileOutputStream fos = new FileOutputStream(springXml)) {
|
||||
IOUtils.copy(Main.class.getResourceAsStream("/spring.xml"), fos);
|
||||
|
||||
@@ -2,7 +2,6 @@ package mc.core.embedded;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import mc.core.network.Server;
|
||||
import mc.core.network.StartServerException;
|
||||
|
||||
@Slf4j
|
||||
public class FakeServer implements Server {
|
||||
|
||||
@@ -31,7 +31,7 @@ public abstract class NetInputStream extends InputStream {
|
||||
public abstract double readDouble();
|
||||
public abstract String readString();
|
||||
public abstract UUID readUUID();
|
||||
public abstract Tag<?> readNBT();
|
||||
public abstract Tag readNBT();
|
||||
|
||||
public abstract void skipBytes(int count);
|
||||
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
package mc.core.utils;
|
||||
|
||||
import lombok.AccessLevel;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
@Slf4j
|
||||
@NoArgsConstructor(access = AccessLevel.PRIVATE)
|
||||
public class CompactedCoords {
|
||||
public static int compressXZ(int x, int z) {
|
||||
if (x < Short.MIN_VALUE || x > Short.MAX_VALUE ||
|
||||
|
||||
@@ -2,7 +2,6 @@ package mc.core.world;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
|
||||
@@ -3,11 +3,13 @@ package mc.core.world.block;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
@Slf4j
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@Data
|
||||
public class BlockLocation implements Cloneable {
|
||||
public class BlockLocation {
|
||||
private int x, y, z;
|
||||
|
||||
public static BlockLocation ZERO() {
|
||||
@@ -20,13 +22,7 @@ public class BlockLocation implements Cloneable {
|
||||
this.z = z;
|
||||
}
|
||||
|
||||
@Override
|
||||
public BlockLocation clone() {
|
||||
try {
|
||||
return (BlockLocation) super.clone();
|
||||
} catch (CloneNotSupportedException e) {
|
||||
e.printStackTrace();
|
||||
return ZERO();
|
||||
}
|
||||
public BlockLocation copy() {
|
||||
return new BlockLocation(x, y, z);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -37,9 +37,9 @@ class EntityLocationTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
void clone_() {
|
||||
void copy() {
|
||||
EntityLocation locOrig = new EntityLocation(x, y, z, yaw, pitch);
|
||||
EntityLocation locClone = locOrig.clone();
|
||||
EntityLocation locClone = locOrig.copy();
|
||||
assertEquals(locOrig, locClone);
|
||||
assertNotSame(locOrig, locClone);
|
||||
}
|
||||
|
||||
@@ -26,7 +26,7 @@ class ImmutableEntityLocationTest {
|
||||
@Test
|
||||
void clone_() {
|
||||
EntityLocation locOrig = new ImmutableEntityLocation(1d, 2d, 3d, 4f, 5f);
|
||||
EntityLocation locClone = locOrig.clone();
|
||||
EntityLocation locClone = locOrig.copy();
|
||||
|
||||
assertEquals(locOrig, locClone);
|
||||
}
|
||||
|
||||
@@ -31,9 +31,9 @@ class BlockLocationTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
void clone_() {
|
||||
void copy() {
|
||||
BlockLocation locOrig = new BlockLocation(x, y, z);
|
||||
BlockLocation locClone = locOrig.clone();
|
||||
BlockLocation locClone = locOrig.copy();
|
||||
assertEquals(locOrig, locClone);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,7 +12,6 @@ import mc.core.player.PlayerManager;
|
||||
import mc.core.player.PlayerSettings;
|
||||
import mc.core.world.World;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.*;
|
||||
@@ -30,7 +29,7 @@ public class H2PlayerManager implements PlayerManager {
|
||||
H2Player h2Player = new H2Player();
|
||||
h2Player.setName(name);
|
||||
h2Player.setUuid(UUID.randomUUID());
|
||||
h2Player.setLocation(location.clone());
|
||||
h2Player.setLocation(location.copy());
|
||||
h2Player.setLoadedChunks(new ArrayList<>());
|
||||
h2Player.setWorld(world);
|
||||
h2Player.setSettings(new PlayerSettings());
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package mc.core.network.proto_1_12_2;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
public class ByteArrayOutputNetStream extends NetOutputStream_p340 {
|
||||
private ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
@@ -61,6 +62,11 @@ public class ByteArrayOutputNetStream extends NetOutputStream_p340 {
|
||||
writeLong(Double.doubleToLongBits(value));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
baos.close();
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return baos.size();
|
||||
}
|
||||
|
||||
@@ -62,7 +62,7 @@ public abstract class NetInputStream_p340 extends NetInputStream {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Tag<?> readNBT() {
|
||||
public Tag readNBT() {
|
||||
if (nbtInputStream == null) {
|
||||
try {
|
||||
nbtInputStream = new NBTInputStream(this, false);
|
||||
|
||||
@@ -78,7 +78,6 @@ public enum State {
|
||||
.put(0x0E, PlayerPositionAndLookPacket.class)
|
||||
.put(0x0F, PlayerLookPacket.class)
|
||||
.put(0x13, PlayerAbilitiesPacket.class)
|
||||
.put(0x1A, HeldItemChangePacket.class)
|
||||
.build(),
|
||||
ImmutableMap.<Class<? extends SCPacket>, Integer>builder()
|
||||
.put(BossBarPacket.class, 0x0C)
|
||||
|
||||
@@ -17,8 +17,8 @@ public class TeleportManager {
|
||||
|
||||
@AllArgsConstructor
|
||||
private class TpData {
|
||||
public Player player;
|
||||
public EntityLocation newLocation;
|
||||
Player player;
|
||||
EntityLocation newLocation;
|
||||
// TODO необходимо добавить TimeStamp, что бы понимать, когда клиент отвергнул телепортацию
|
||||
// т.е. идея такова: долгое молчание клиента знак отвержения телепортации.
|
||||
}
|
||||
@@ -34,7 +34,7 @@ public class TeleportManager {
|
||||
teleportId = RAND.nextInt(9999);
|
||||
} while (teleportMap.containsKey(teleportId));
|
||||
|
||||
teleportMap.put(teleportId, new TpData(player, location.clone()));
|
||||
teleportMap.put(teleportId, new TpData(player, location.copy()));
|
||||
return teleportId;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,13 +0,0 @@
|
||||
package mc.core.network.proto_1_12_2.packets.clientside;
|
||||
|
||||
import mc.core.network.CSPacket;
|
||||
import mc.core.network.NetInputStream;
|
||||
|
||||
public class HeldItemChangePacket implements CSPacket {
|
||||
private int slot;
|
||||
|
||||
@Override
|
||||
public void readSelf(NetInputStream netStream) {
|
||||
this.slot = netStream.readShort();
|
||||
}
|
||||
}
|
||||
@@ -13,9 +13,11 @@ import mc.core.world.block.BlockType;
|
||||
import mc.core.world.chunk.Chunk;
|
||||
import mc.core.world.chunk.ChunkSection;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
/*
|
||||
Packet structure
|
||||
@@ -88,19 +90,13 @@ public class ChunkDataPacket implements SCPacket {
|
||||
this.sectionList = sectionList;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeSelf(NetOutputStream netStream) {
|
||||
if (sectionList == null && chunk == null) {
|
||||
log.warn("Empty chunk data!"); //TODO для такого нужна заглушка
|
||||
return;
|
||||
}
|
||||
|
||||
netStream.writeInt(x); // Chunk X
|
||||
netStream.writeInt(z); // Chunk Y
|
||||
netStream.writeBoolean(initChunk); // Init Chunk
|
||||
|
||||
/**
|
||||
* @return max height chunk
|
||||
*/
|
||||
private int calcAndWriteBitMask(NetOutputStream netStream) {
|
||||
int maxH = 0;
|
||||
int bitMask = 0;
|
||||
|
||||
if (sectionList == null && chunk != null) {
|
||||
for (int h = 15; h >= 0; h--) {
|
||||
bitMask = bitMask << 1;
|
||||
@@ -125,74 +121,106 @@ public class ChunkDataPacket implements SCPacket {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
netStream.writeVarInt(bitMask); // Primary Bit Mask
|
||||
|
||||
final ByteArrayOutputNetStream data = new ByteArrayOutputNetStream();
|
||||
return maxH;
|
||||
}
|
||||
|
||||
final ByteArrayOutputNetStream biomes = new ByteArrayOutputNetStream();
|
||||
boolean biomeWrite = true;
|
||||
private ChunkSection getChunkSection(int h) {
|
||||
if (chunk != null) {
|
||||
return chunk.getChunkSection(h);
|
||||
} else if (sectionList != null) {
|
||||
return sectionList.remove(0);
|
||||
}
|
||||
|
||||
List<CompoundTag> nbtList = new ArrayList<>();
|
||||
return null;
|
||||
}
|
||||
|
||||
for (int h = 0; h < maxH; h++) {
|
||||
ChunkSection chunkSection = null;
|
||||
private PalettedChunkSection createPalette(final ChunkSection chunkSection,
|
||||
final List<CompoundTag> nbtList,
|
||||
final AtomicBoolean biomeWrite,
|
||||
final ByteArrayOutputNetStream biomes) {
|
||||
|
||||
if (chunk != null) {
|
||||
chunkSection = chunk.getChunkSection(h);
|
||||
} else if (sectionList != null) {
|
||||
chunkSection = sectionList.remove(0);
|
||||
}
|
||||
final PalettedChunkSection palettedChunkSection = new PalettedChunkSection();
|
||||
|
||||
if (chunkSection == null) {
|
||||
continue;
|
||||
}
|
||||
for (int y = 0; y < 16; y++) {
|
||||
for (int z = 0; z < 16; z++) {
|
||||
for (int x = 0; x < 16; x++) {
|
||||
Block block = chunkSection.getBlock(x, y, z);
|
||||
|
||||
final PalettedChunkSection palettedChunkSection = new PalettedChunkSection();
|
||||
palettedChunkSection.addBlock(
|
||||
block,
|
||||
chunkSection.getSkyLight(x, y, z)
|
||||
);
|
||||
|
||||
for (int y = 0; y < 16; y++) {
|
||||
for (int z = 0; z < 16; z++) {
|
||||
for (int x = 0; x < 16; x++) {
|
||||
Block block = chunkSection.getBlock(x, y, z);
|
||||
CompoundTag nbt = block.getNBTData();
|
||||
if (nbt != null) {
|
||||
nbtList.add(nbt);
|
||||
}
|
||||
|
||||
palettedChunkSection.addBlock(
|
||||
block,
|
||||
chunkSection.getSkyLight(x, y, z)
|
||||
);
|
||||
|
||||
CompoundTag nbt = block.getNBTData();
|
||||
if (nbt != null) {
|
||||
nbtList.add(nbt);
|
||||
}
|
||||
|
||||
if (biomeWrite) {
|
||||
biomes.writeByte(chunk.getBiome(
|
||||
(chunk.getX() << 4) + x,
|
||||
(chunk.getZ() << 4) + z
|
||||
).getId());
|
||||
if (x == 15 && z == 15) {
|
||||
biomeWrite = false;
|
||||
}
|
||||
if (biomeWrite.get()) {
|
||||
biomes.writeByte(chunk.getBiome(
|
||||
(chunk.getX() << 4) + x,
|
||||
(chunk.getZ() << 4) + z
|
||||
).getId());
|
||||
if (x == 15 && z == 15) {
|
||||
biomeWrite.set(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// <Chunk Section>
|
||||
palettedChunkSection.writeToNetStream(data);
|
||||
// </Chunk Section>
|
||||
}
|
||||
// <Biomes>
|
||||
data.writeBytes(biomes.toByteArray());
|
||||
// </Biomes>
|
||||
|
||||
netStream.writeVarInt(data.size()); // Size of Data
|
||||
netStream.writeBytes(data.toByteArray()); // Data
|
||||
netStream.writeVarInt(nbtList.size()); // Number of block entities
|
||||
// <NBT>
|
||||
for (CompoundTag compoundTag : nbtList) {
|
||||
netStream.writeNBT(compoundTag);
|
||||
return palettedChunkSection;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeSelf(NetOutputStream netStream) {
|
||||
if (sectionList == null && chunk == null) {
|
||||
log.warn("Empty chunk data!"); //TODO для такого нужна заглушка
|
||||
return;
|
||||
}
|
||||
|
||||
netStream.writeInt(x); // Chunk X
|
||||
netStream.writeInt(z); // Chunk Y
|
||||
netStream.writeBoolean(initChunk); // Init Chunk
|
||||
|
||||
final int maxH = calcAndWriteBitMask(netStream);
|
||||
|
||||
try (ByteArrayOutputNetStream data = new ByteArrayOutputNetStream();
|
||||
ByteArrayOutputNetStream biomes = new ByteArrayOutputNetStream()) {
|
||||
|
||||
AtomicBoolean biomeWrite = new AtomicBoolean(true);
|
||||
|
||||
List<CompoundTag> nbtList = new ArrayList<>();
|
||||
|
||||
for (int h = 0; h < maxH; h++) {
|
||||
final ChunkSection chunkSection = getChunkSection(h);
|
||||
if (chunkSection == null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// <Chunk Section>
|
||||
createPalette(chunkSection, nbtList, biomeWrite, biomes)
|
||||
.writeToNetStream(data);
|
||||
// </Chunk Section>
|
||||
}
|
||||
// <Biomes>
|
||||
data.writeBytes(biomes.toByteArray());
|
||||
// </Biomes>
|
||||
|
||||
netStream.writeVarInt(data.size()); // Size of Data
|
||||
netStream.writeBytes(data.toByteArray()); // Data
|
||||
netStream.writeVarInt(nbtList.size()); // Number of block entities
|
||||
// <NBT>
|
||||
for (CompoundTag compoundTag : nbtList) {
|
||||
netStream.writeNBT(compoundTag);
|
||||
}
|
||||
// </NBT>
|
||||
} catch (IOException e) {
|
||||
log.error("", e);
|
||||
}
|
||||
// </NBT>
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -240,7 +268,7 @@ public class ChunkDataPacket implements SCPacket {
|
||||
this.skyLight.set(bx, by, bz, skyLight);
|
||||
}
|
||||
|
||||
void writeToNetStream(final NetOutputStream netOutputStream) {
|
||||
private int getBitsPerBlock() {
|
||||
int bitsPerBlock = 4;
|
||||
if (palette.size() > 15) {
|
||||
if (palette.size() <= 31)
|
||||
@@ -253,15 +281,12 @@ public class ChunkDataPacket implements SCPacket {
|
||||
bitsPerBlock = 8;
|
||||
}
|
||||
|
||||
// <Palette>
|
||||
netOutputStream.writeUnsignedByte(bitsPerBlock); // Bits Per Block
|
||||
netOutputStream.writeVarInt(palette.size()); // Size of palette
|
||||
palette.forEach(netOutputStream::writeVarInt); // Palette
|
||||
// </Palette>
|
||||
// <Data Array>
|
||||
final int dataLength = (4096/*16*16*16*/ * bitsPerBlock) / 64/*size of long in bits*/;
|
||||
netOutputStream.writeVarInt(dataLength); // Size of Data Array
|
||||
// <Array>
|
||||
return bitsPerBlock;
|
||||
}
|
||||
|
||||
private void writePalette(final int bitsPerBlock,
|
||||
final NetOutputStream netOutputStream) {
|
||||
|
||||
long value = 0;
|
||||
int lastPos = 0;
|
||||
boolean fairy = false;
|
||||
@@ -296,6 +321,21 @@ public class ChunkDataPacket implements SCPacket {
|
||||
}
|
||||
}
|
||||
netOutputStream.writeLong(value);
|
||||
}
|
||||
|
||||
void writeToNetStream(final NetOutputStream netOutputStream) {
|
||||
final int bitsPerBlock = getBitsPerBlock();
|
||||
|
||||
// <Palette>
|
||||
netOutputStream.writeUnsignedByte(bitsPerBlock); // Bits Per Block
|
||||
netOutputStream.writeVarInt(palette.size()); // Size of palette
|
||||
palette.forEach(netOutputStream::writeVarInt); // Palette
|
||||
// </Palette>
|
||||
// <Data Array>
|
||||
final int dataLength = (4096/*16*16*16*/ * bitsPerBlock) / 64/*size of long in bits*/;
|
||||
netOutputStream.writeVarInt(dataLength); // Size of Data Array
|
||||
// <Array>
|
||||
writePalette(bitsPerBlock, netOutputStream);
|
||||
// </Array>
|
||||
// </Data Array>
|
||||
// <Block Light>
|
||||
|
||||
@@ -1,63 +0,0 @@
|
||||
package mc.core.network.proto_1_12_2.serializers;
|
||||
|
||||
import mc.core.world.block.BlockLocation;
|
||||
|
||||
import static com.google.common.math.IntMath.isPowerOfTwo;
|
||||
|
||||
public class BlockLocationSerializer {
|
||||
private static final int[] MULTIPLY_DE_BRUIJN_BIT_POSITION = new int[] {0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8, 31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9};
|
||||
private static final int NUM_X_BITS = 1 + log2(smallestEncompassingPowerOfTwo(30000000));
|
||||
private static final int NUM_Z_BITS = NUM_X_BITS;
|
||||
private static final int NUM_Y_BITS = 64 - NUM_X_BITS - NUM_Z_BITS;
|
||||
private static final int Y_SHIFT = NUM_Z_BITS;
|
||||
private static final int X_SHIFT = Y_SHIFT + NUM_Y_BITS;
|
||||
private static final long X_MASK = (1L << NUM_X_BITS) - 1L;
|
||||
private static final long Y_MASK = (1L << NUM_Y_BITS) - 1L;
|
||||
private static final long Z_MASK = (1L << NUM_Z_BITS) - 1L;
|
||||
|
||||
/*
|
||||
* net.minecraft.util.math.MathHelper#log2(int)
|
||||
*/
|
||||
private static int log2(int value) {
|
||||
return log2DeBruijn(value) - (isPowerOfTwo(value) ? 0 : 1);
|
||||
}
|
||||
|
||||
/*
|
||||
* net.minecraft.util.math.MathHelper#log2DeBruijn(int)
|
||||
*/
|
||||
private static int log2DeBruijn(int value) {
|
||||
value = isPowerOfTwo(value) ? value : smallestEncompassingPowerOfTwo(value);
|
||||
return MULTIPLY_DE_BRUIJN_BIT_POSITION[(int)((long)value * 125613361L >> 27) & 31];
|
||||
}
|
||||
|
||||
/*
|
||||
* net.minecraft.util.math.MathHelper#smallestEncompassingPowerOfTwo(int)
|
||||
*/
|
||||
private static int smallestEncompassingPowerOfTwo(int value) {
|
||||
int i = value - 1;
|
||||
i = i | i >> 1;
|
||||
i = i | i >> 2;
|
||||
i = i | i >> 4;
|
||||
i = i | i >> 8;
|
||||
i = i | i >> 16;
|
||||
return i + 1;
|
||||
}
|
||||
|
||||
public static long toLong(BlockLocation location) {
|
||||
return ((long)location.getX() & X_MASK) << X_SHIFT |
|
||||
((long)location.getY() & Y_MASK) << Y_SHIFT |
|
||||
((long)location.getZ() & Z_MASK);
|
||||
}
|
||||
|
||||
public static BlockLocation fromLong(long value) {
|
||||
BlockLocation location = BlockLocation.ZERO();
|
||||
fromLong(value, location);
|
||||
return location;
|
||||
}
|
||||
|
||||
public static void fromLong(long value, BlockLocation location) {
|
||||
location.setX((int)(value << 64 - X_SHIFT - NUM_X_BITS >> 64 - NUM_X_BITS));
|
||||
location.setY((int)(value << 64 - Y_SHIFT - NUM_Y_BITS >> 64 - NUM_Y_BITS));
|
||||
location.setZ((int)(value << 64 - NUM_Z_BITS >> 64 - NUM_Z_BITS));
|
||||
}
|
||||
}
|
||||
@@ -81,4 +81,9 @@ public class ByteArrayInputNetStream extends NetInputStream_p340 {
|
||||
public void skipBytes(int count) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
bais.close();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,32 +0,0 @@
|
||||
package mc.core.network.proto_1_12_2.serializers;
|
||||
|
||||
import mc.core.world.block.BlockLocation;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
class BlockLocationSerializerTest {
|
||||
private static final ThreadLocalRandom rnd = ThreadLocalRandom.current();
|
||||
private static final int minI = 0, maxI = 10;
|
||||
private int x, y, z;
|
||||
|
||||
@BeforeEach
|
||||
void before() {
|
||||
x = rnd.nextInt(minI, maxI);
|
||||
y = rnd.nextInt(minI, maxI);
|
||||
z = rnd.nextInt(minI, maxI);
|
||||
}
|
||||
|
||||
@Test
|
||||
void serialize() {
|
||||
BlockLocation location = new BlockLocation(x, y, z);
|
||||
final long serializedCoords = BlockLocationSerializer.toLong(location);
|
||||
|
||||
BlockLocation deserLoc = BlockLocationSerializer.fromLong(serializedCoords);
|
||||
|
||||
assertEquals(location, deserLoc);
|
||||
}
|
||||
}
|
||||
@@ -32,6 +32,9 @@ public class PacketDecoder extends ReplayingDecoder<CSPacket> {
|
||||
@Override
|
||||
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
|
||||
State state = ctx.channel().attr(ATTR_STATE).get();
|
||||
/* FIXME: SONAR посчитал нужным здесь задействовать try-with-resources
|
||||
* однако для ByteBuf не существует понятия `close`. Необходимо проанализировать необходимость
|
||||
* использования реализации интерфейса InputStream и если её нет, то убрать. */
|
||||
NetInputStream netStream = new WrapperNetInputStream(in);
|
||||
|
||||
int packetSize = netStream.readVarInt();
|
||||
|
||||
@@ -27,6 +27,9 @@ public class PacketEncoder extends MessageToByteEncoder<SCPacket> {
|
||||
log.debug("Send {}:{}", state, packet);
|
||||
|
||||
try {
|
||||
/* FIXME: SONAR посчитал нужным здесь задействовать try-with-resources
|
||||
* однако для ByteBuf не существует понятия `close`. Необходимо проанализировать необходимость
|
||||
* использования реализации интерфейса OutputStream и если её нет, то убрать. */
|
||||
NetOutputStream netStream = new WrapperNetOutputStream(out);
|
||||
netStream.writeVarInt(id);
|
||||
packet.writeSelf(netStream);
|
||||
|
||||
@@ -32,6 +32,9 @@ public class PacketPostEncoder extends MessageToByteEncoder<ByteBuf> {
|
||||
}
|
||||
|
||||
out.ensureWritable(sizeOfPktSize + pktSize);
|
||||
/* FIXME: SONAR посчитал нужным здесь задействовать try-with-resources
|
||||
* однако для ByteBuf не существует понятия `close`. Необходимо проанализировать необходимость
|
||||
* использования реализации интерфейса OutputStream и если её нет, то убрать. */
|
||||
(new WrapperNetOutputStream(out)).writeVarInt(pktSize);
|
||||
out.writeBytes(msg);
|
||||
}
|
||||
|
||||
@@ -28,7 +28,7 @@ public class PlayerEventListener {
|
||||
|
||||
@Subscriber
|
||||
public void playerChunkLoadHandler(SC_ChunkLoadEvent event) {
|
||||
if (event.getNeedLoadChunks().size() == 0) return;
|
||||
if (event.getNeedLoadChunks().isEmpty()) return;
|
||||
|
||||
final NetChannel channel = event.getPlayer().getChannel();
|
||||
|
||||
@@ -51,7 +51,7 @@ public class PlayerEventListener {
|
||||
|
||||
@Subscriber
|
||||
public void playerChunkUnloadHandler(SC_ChunkUnloadEvent event) {
|
||||
if (event.getNeedUnloadChunks().size() == 0) return;
|
||||
if (event.getNeedUnloadChunks().isEmpty()) return;
|
||||
|
||||
final NetChannel channel = event.getPlayer().getChannel();
|
||||
|
||||
|
||||
@@ -74,7 +74,7 @@ public class PlayHandler extends AbstractStateHandler implements PlayStateHandle
|
||||
return;
|
||||
}
|
||||
|
||||
CS_PlayerMoveEvent event = new CS_PlayerMoveEvent(player, player.getLocation().clone());
|
||||
CS_PlayerMoveEvent event = new CS_PlayerMoveEvent(player, player.getLocation().copy());
|
||||
event.setNewLocation(new EntityLocation(
|
||||
packet.getX(), packet.getY(), packet.getZ(),
|
||||
player.getLocation().getYaw(),
|
||||
|
||||
Reference in New Issue
Block a user