Merge branch 'proto_1.12.2' into world
This commit is contained in:
@@ -10,6 +10,7 @@ import lombok.extern.slf4j.Slf4j;
|
||||
import mc.core.events.CS_PlayerMoveEvent;
|
||||
import mc.core.events.EventBusGetter;
|
||||
import mc.core.events.SC_ChunkLoadEvent;
|
||||
import mc.core.events.SC_ChunkUnloadEvent;
|
||||
import mc.core.player.PlayerManager;
|
||||
import mc.core.time.TimeProcessor;
|
||||
import mc.core.utils.CompactedCoords;
|
||||
@@ -17,6 +18,8 @@ import mc.core.world.World;
|
||||
import mc.core.world.chunk.Chunk;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
import java.util.Iterator;
|
||||
|
||||
@Slf4j
|
||||
public class GameLoop extends Thread {
|
||||
private final TpsWatcher TPS_WATCHER = TpsWatcher.getInstance();
|
||||
@@ -48,22 +51,37 @@ public class GameLoop extends Thread {
|
||||
@Subscribe
|
||||
public void playerMoveEventHandler(CS_PlayerMoveEvent event) {
|
||||
log.trace("(GameLoop) playerMoveEventHandler()");
|
||||
event.getPlayer().getLocation().setXYZ(event.getNewLocation());
|
||||
|
||||
Chunk chunk = event.getNewLocation().getChunk(); // Next chunk
|
||||
int ncX = chunk.getX();
|
||||
int ncZ = chunk.getZ();
|
||||
chunk = event.getPlayer().getLocation().getChunk(); // Current chunk
|
||||
Chunk chunk;
|
||||
chunk = event.getOldLocation().getChunk(); // Old chunk
|
||||
int ccX = chunk.getX();
|
||||
int ccZ = chunk.getZ();
|
||||
chunk = event.getNewLocation().getChunk(); // Next chunk
|
||||
int ncX = chunk.getX();
|
||||
int ncZ = chunk.getZ();
|
||||
|
||||
if (event.isRecalcChunk() || (ncX != ccX && ncZ != ccZ)) {
|
||||
final int viewDistance = event.getPlayer().getSettings().getViewDistance();
|
||||
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;
|
||||
|
||||
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()) {
|
||||
EventBusGetter.INSTANCE.post(eventChunkUnload);
|
||||
}
|
||||
|
||||
SC_ChunkLoadEvent eventChunkLoad = new SC_ChunkLoadEvent(event.getPlayer());
|
||||
for (int cZ = cMinZ; cZ <= cMaxZ; cZ++) {
|
||||
for (int cX = cMinX; cX <= cMaxX; cX++) {
|
||||
@@ -71,6 +89,7 @@ public class GameLoop extends Thread {
|
||||
if (!event.getPlayer().getLoadedChunks().contains(compressXZ)) {
|
||||
if (!event.getPlayer().getLoadedChunks().contains(compressXZ)) {
|
||||
eventChunkLoad.getNeedLoadChunks().add(compressXZ);
|
||||
event.getPlayer().getLoadedChunks().add(compressXZ);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -81,6 +100,8 @@ public class GameLoop extends Thread {
|
||||
}
|
||||
}
|
||||
|
||||
event.getPlayer().getLocation().setXYZ(event.getNewLocation());
|
||||
|
||||
// TODO отсылать клиенту только(!) для корректировки позиции
|
||||
// SC_PlayerMoveEvent nextEvent = new SC_PlayerMoveEvent(event.getPlayer());
|
||||
// nextEvent.setNewLocation(event.getNewLocation());
|
||||
|
||||
@@ -50,15 +50,15 @@ public class Location implements Cloneable {
|
||||
}
|
||||
|
||||
public int getBlockX() {
|
||||
return (int) x;
|
||||
return Double.valueOf(Math.floor(x)).intValue();
|
||||
}
|
||||
|
||||
public int getBlockY() {
|
||||
return (int) y;
|
||||
return Double.valueOf(Math.floor(y)).intValue();
|
||||
}
|
||||
|
||||
public int getBlockZ() {
|
||||
return (int) z;
|
||||
return Double.valueOf(Math.floor(z)).intValue();
|
||||
}
|
||||
|
||||
public Chunk getChunk() {
|
||||
@@ -66,7 +66,7 @@ public class Location implements Cloneable {
|
||||
if (world == null) {
|
||||
return null;
|
||||
} else {
|
||||
return world.getChunk((int)(x / 16), (int)(z / 16));
|
||||
return world.getChunk(getBlockX() >> 4, getBlockZ() >> 4);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -75,7 +75,7 @@ public class Location implements Cloneable {
|
||||
if (chunk == null) {
|
||||
return null;
|
||||
} else {
|
||||
return chunk.getChunkSection((int)(y / 16));
|
||||
return chunk.getChunkSection(getBlockY() >> 4);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
16
core/src/main/java/mc/core/events/SC_ChunkUnloadEvent.java
Normal file
16
core/src/main/java/mc/core/events/SC_ChunkUnloadEvent.java
Normal file
@@ -0,0 +1,16 @@
|
||||
package mc.core.events;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import mc.core.player.Player;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@RequiredArgsConstructor
|
||||
public class SC_ChunkUnloadEvent extends EventBase {
|
||||
@Getter
|
||||
private final Player player;
|
||||
@Getter
|
||||
private List<Integer> needUnloadChunks = new ArrayList<>();
|
||||
}
|
||||
@@ -20,4 +20,23 @@ public class CompactedCoords {
|
||||
(int)(short) (compactValue | 0xFFFF0000)
|
||||
};
|
||||
}
|
||||
|
||||
private static int floor_double(double value) {
|
||||
int i = (int)value;
|
||||
return value < (double)i ? i - 1 : i;
|
||||
}
|
||||
|
||||
public static long compressXYZ(double x, double y, double z) {
|
||||
return ((floor_double(x) & 0x3FFFFFF) << 38)
|
||||
| ((floor_double(y) & 0xFFF) << 26)
|
||||
| (floor_double(z) & 0x3FFFFFF);
|
||||
}
|
||||
|
||||
public static double[] uncompressXYZ(long compactValue) {
|
||||
return new double[]{
|
||||
compactValue >> 38,
|
||||
(compactValue >> 26) & 0x0FFF,
|
||||
compactValue << 38 >> 38 // is normal?
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user