Archived
0

refactory Location's

This commit is contained in:
2018-09-08 05:25:24 +03:00
parent 6808ae34f9
commit 09c7626c2d
27 changed files with 269 additions and 319 deletions

View File

@@ -24,10 +24,10 @@ public class CoreEventListener {
log.trace("(GameLoop) playerMoveEventHandler()"); log.trace("(GameLoop) playerMoveEventHandler()");
Chunk chunk; Chunk chunk;
chunk = event.getOldLocation().getChunk(); // Old chunk chunk = event.getPlayer().getWorld().getChunk(event.getOldLocation()); // Old chunk
int ccX = chunk.getX(); int ccX = chunk.getX();
int ccZ = chunk.getZ(); int ccZ = chunk.getZ();
chunk = event.getNewLocation().getChunk(); // Next chunk chunk = event.getPlayer().getWorld().getChunk(event.getNewLocation()); // Next chunk
int ncX = chunk.getX(); int ncX = chunk.getX();
int ncZ = chunk.getZ(); int ncZ = chunk.getZ();
@@ -71,7 +71,11 @@ public class CoreEventListener {
} }
} }
event.getPlayer().getLocation().setXYZ(event.getNewLocation()); event.getPlayer().getLocation().setXYZ(
event.getNewLocation().getX(),
event.getNewLocation().getY(),
event.getNewLocation().getZ()
);
// TODO отсылать клиенту только(!) для корректировки позиции // TODO отсылать клиенту только(!) для корректировки позиции
// SC_PlayerMoveEvent nextEvent = new SC_PlayerMoveEvent(event.getPlayer()); // SC_PlayerMoveEvent nextEvent = new SC_PlayerMoveEvent(event.getPlayer());

View File

@@ -1,21 +1,29 @@
/*
* DmitriyMX <dimon550@gmail.com>
* 2018-08-08
*/
package mc.core; package mc.core;
import lombok.Getter; import lombok.AllArgsConstructor;
import lombok.Setter; import lombok.Data;
import mc.core.world.World; import lombok.NoArgsConstructor;
public class EntityLocation extends Location implements Cloneable { @NoArgsConstructor
@Getter @AllArgsConstructor
@Setter @Data
public class EntityLocation implements Cloneable {
private double x, y, z;
private float yaw, pitch; private float yaw, pitch;
public EntityLocation(double x, double y, double z, float yaw, float pitch, World world) { public static EntityLocation ZERO() {
super(x, y, z, world); return new EntityLocation(0d,0d,0d,0f,0f);
setYawPitch(yaw, pitch); }
public void set(EntityLocation location) {
setXYZ(location.x, location.y, location.z);
setYawPitch(location.yaw, location.pitch);
}
public void setXYZ(double x, double y, double z) {
this.x = x;
this.y = y;
this.z = z;
} }
public void setYawPitch(float yaw, float pitch) { public void setYawPitch(float yaw, float pitch) {
@@ -23,12 +31,25 @@ public class EntityLocation extends Location implements Cloneable {
this.pitch = pitch; this.pitch = pitch;
} }
public void setYawPitch(EntityLocation entityLocation) { public int getBlockX() {
setYawPitch(entityLocation.yaw, entityLocation.pitch); return Double.valueOf(Math.floor(x)).intValue();
}
public int getBlockY() {
return Double.valueOf(Math.floor(y)).intValue();
}
public int getBlockZ() {
return Double.valueOf(Math.floor(z)).intValue();
} }
@Override @Override
public EntityLocation clone() { public EntityLocation clone() {
try {
return (EntityLocation) super.clone(); return (EntityLocation) super.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
return null;
}
} }
} }

View File

@@ -1,91 +0,0 @@
/*
* DmitriyMX <dimon550@gmail.com>
* 2018-08-08
*/
package mc.core;
import lombok.Getter;
import lombok.Setter;
import mc.core.exception.ResourceUnloadedException;
import mc.core.world.World;
import mc.core.world.chunk.Chunk;
import mc.core.world.chunk.ChunkSection;
import java.lang.ref.Reference;
import java.lang.ref.WeakReference;
public class Location implements Cloneable {
@Getter
@Setter
private double x, y, z;
private Reference<World> refWorld;
public Location (double x, double y, double z, World world) {
setXYZ(x, y, z);
setWorld(world);
}
public void setXYZ(double x, double y, double z) {
this.x = x;
this.y = y;
this.z = z;
}
public void setXYZ(Location location) {
setXYZ(location.x, location.y, location.z);
}
public World getWorld() {
if (refWorld == null) {
return null;
} else if (refWorld.get() == null) {
throw new ResourceUnloadedException("You're trying to get unloaded world");
} else {
return refWorld.get();
}
}
public void setWorld (World world) {
this.refWorld = new WeakReference<>(world);
}
public int getBlockX() {
return Double.valueOf(Math.floor(x)).intValue();
}
public int getBlockY() {
return Double.valueOf(Math.floor(y)).intValue();
}
public int getBlockZ() {
return Double.valueOf(Math.floor(z)).intValue();
}
public Chunk getChunk() {
World world = getWorld();
if (world == null) {
return null;
} else {
return world.getChunk(getBlockX() >> 4, getBlockZ() >> 4);
}
}
public ChunkSection getChunkSection() {
Chunk chunk = getChunk();
if (chunk == null) {
return null;
} else {
return chunk.getChunkSection(getBlockY() >> 4);
}
}
@Override
public Location clone() {
try {
return (Location) super.clone();
} catch (CloneNotSupportedException e) { // такое в нашем случае вообще возможно?
e.printStackTrace();
return null;
}
}
}

View File

@@ -12,8 +12,7 @@ import mc.core.player.Player;
import mc.core.player.PlayerManager; import mc.core.player.PlayerManager;
import mc.core.text.Text; import mc.core.text.Text;
import mc.core.text.Title; import mc.core.text.Title;
import mc.core.world.chunk.Chunk; import mc.core.world.World;
import mc.core.world.chunk.ChunkSection;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
@@ -53,7 +52,7 @@ public class FakePlayerManager implements PlayerManager {
private static final NetChannel FAKE_NET_CHANNEL = new FakeNetChannet(); private static final NetChannel FAKE_NET_CHANNEL = new FakeNetChannet();
@Override @Override
public Player createPlayer(String name, EntityLocation defaultLocation) { public Player createPlayer(String name, EntityLocation location, World world) {
return null; return null;
} }

View File

@@ -10,6 +10,7 @@ import mc.core.Config;
import mc.core.EntityLocation; import mc.core.EntityLocation;
import mc.core.network.BroadcastNetChannel; import mc.core.network.BroadcastNetChannel;
import mc.core.network.NetChannel; import mc.core.network.NetChannel;
import mc.core.world.World;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import java.util.*; import java.util.*;
@@ -31,14 +32,13 @@ public class InMemoryPlayerManager implements PlayerManager, Runnable {
} }
@Override @Override
public Player createPlayer(String name, EntityLocation defaultLocation) { public Player createPlayer(String name, EntityLocation location, World world) {
SimplePlayer player = new SimplePlayer(); SimplePlayer player = new SimplePlayer();
player.setId(rand.nextInt(10000)); player.setId(rand.nextInt(10000));
player.setUUID(UUID.nameUUIDFromBytes(name.getBytes())); player.setUUID(UUID.nameUUIDFromBytes(name.getBytes()));
player.setName(name); player.setName(name);
player.getLocation().setXYZ(defaultLocation); player.getLocation().set(location);
player.getLocation().setYawPitch(defaultLocation); player.setWorld(world);
player.getLocation().setWorld(defaultLocation.getWorld());
player.setSettings(new PlayerSettings()); player.setSettings(new PlayerSettings());
synchronized (lock) { synchronized (lock) {

View File

@@ -6,6 +6,7 @@ package mc.core.player;
import mc.core.EntityLocation; import mc.core.EntityLocation;
import mc.core.network.NetChannel; import mc.core.network.NetChannel;
import mc.core.world.World;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
@@ -23,7 +24,8 @@ public interface Player {
void setChannel(NetChannel channel); void setChannel(NetChannel channel);
EntityLocation getLocation(); EntityLocation getLocation();
//TODO надо определиться - нужно ли здесь setLocation() или нет World getWorld();
void setWorld(World world);
boolean isFlying(); boolean isFlying();
void setFlying(boolean value); void setFlying(boolean value);

View File

@@ -6,12 +6,13 @@ package mc.core.player;
import mc.core.EntityLocation; import mc.core.EntityLocation;
import mc.core.network.NetChannel; import mc.core.network.NetChannel;
import mc.core.world.World;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
public interface PlayerManager { public interface PlayerManager {
Player createPlayer(String name, EntityLocation defaultLocation); Player createPlayer(String name, EntityLocation location, World world);
void joinServer(Player player); void joinServer(Player player);
void leftServer(Player player); void leftServer(Player player);
Optional<Player> getPlayer(String name); Optional<Player> getPlayer(String name);

View File

@@ -6,8 +6,12 @@ package mc.core.player;
import lombok.Data; import lombok.Data;
import mc.core.EntityLocation; import mc.core.EntityLocation;
import mc.core.exception.ResourceUnloadedException;
import mc.core.network.NetChannel; import mc.core.network.NetChannel;
import mc.core.world.World;
import java.lang.ref.Reference;
import java.lang.ref.WeakReference;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
@@ -19,21 +23,33 @@ public class SimplePlayer implements Player {
private String name; private String name;
private boolean online = false; private boolean online = false;
private NetChannel channel; private NetChannel channel;
private EntityLocation location = new EntityLocation(0d, 0d, 0d, 0f, 0f, null); private EntityLocation location = EntityLocation.ZERO();
private Reference<World> $refWorld;
private boolean flying = false; private boolean flying = false;
private PlayerSettings settings; private PlayerSettings settings;
private List<Integer> loadedChunks = new ArrayList<>(); private List<Integer> loadedChunks = new ArrayList<>();
public void setLocation(EntityLocation location) {
this.location.setXYZ(location);
this.location.setYawPitch(location);
}
@Override @Override
public UUID getUUID() { public UUID getUUID() {
return uuid; return uuid;
} }
@Override
public World getWorld() {
if ($refWorld == null) {
return null;
} else if ($refWorld.get() == null) {
throw new ResourceUnloadedException("You're trying to get unloaded world");
} else {
return $refWorld.get();
}
}
@Override
public void setWorld(World world) {
this.$refWorld = new WeakReference<>(world);
}
public void setUUID(UUID uuid) { public void setUUID(UUID uuid) {
this.uuid = uuid; this.uuid = uuid;
} }

View File

@@ -5,6 +5,7 @@
package mc.core.world; package mc.core.world;
import mc.core.EntityLocation; import mc.core.EntityLocation;
import mc.core.world.block.BlockLocation;
import mc.core.world.chunk.Chunk; import mc.core.world.chunk.Chunk;
public interface World { public interface World {
@@ -15,4 +16,12 @@ public interface World {
Chunk getChunk(int x, int z); Chunk getChunk(int x, int z);
void setChunk(int x, int z, Chunk chunkSection); void setChunk(int x, int z, Chunk chunkSection);
default Chunk getChunk(BlockLocation location) {
return getChunk(location.getX() >> 4, location.getZ() >> 4);
}
default Chunk getChunk(EntityLocation location) {
return getChunk(location.getBlockX() >> 4, location.getBlockZ() >> 4);
}
} }

View File

@@ -3,7 +3,6 @@ package mc.core.world.block;
import com.flowpowered.nbt.Tag; import com.flowpowered.nbt.Tag;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
import mc.core.Location;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@@ -12,7 +11,7 @@ import java.util.stream.Stream;
public abstract class AbstractBlock implements Block { public abstract class AbstractBlock implements Block {
@Getter @Getter
@Setter @Setter
private Location location; private BlockLocation location;
@Getter @Getter
private int light = 0; private int light = 0;
@Getter @Getter

View File

@@ -1,11 +1,10 @@
package mc.core.world.block; package mc.core.world.block;
import mc.core.Location;
import mc.core.nbt.Taggable; import mc.core.nbt.Taggable;
public interface Block extends Taggable{ public interface Block extends Taggable{
int getLight(); int getLight();
void setLight(int light); void setLight(int light);
BlockType getBlockType(); BlockType getBlockType();
Location getLocation(); BlockLocation getLocation();
} }

View File

@@ -1,19 +1,16 @@
package mc.core.world.block; package mc.core.world.block;
import mc.core.Location;
import mc.core.world.World;
public class BlockFactory { public class BlockFactory {
public Block create(BlockType blockType, int x, int y, int z, World world) { public Block create(BlockType blockType, int x, int y, int z) {
return new EmbeddedBlock(blockType, x, y, z, world); return new EmbeddedBlock(blockType, x, y, z);
} }
/** For first-time generation */ /** For first-time generation */
private class EmbeddedBlock extends AbstractBlock { private class EmbeddedBlock extends AbstractBlock {
EmbeddedBlock(BlockType type, int x, int y, int z, World world) { EmbeddedBlock(BlockType type, int x, int y, int z) {
super(type); super(type);
setLocation(new Location(x,y,z, world)); setLocation(new BlockLocation(x, y, z));
} }
} }
} }

View File

@@ -0,0 +1,26 @@
package mc.core.world.block;
import lombok.*;
@NoArgsConstructor
@AllArgsConstructor
@Data
public class BlockLocation implements Cloneable {
private int x, y, z;
public void setXYZ(int x, int y, int z) {
this.x = x;
this.y = y;
this.z = z;
}
@Override
public BlockLocation clone() {
try {
return (BlockLocation) super.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
return null;
}
}
}

View File

@@ -0,0 +1,40 @@
package mc.core;
import mc.core.world.block.BlockLocation;
import org.junit.Before;
import org.junit.Test;
import java.util.concurrent.ThreadLocalRandom;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
public class TestBlockLocation {
private static final ThreadLocalRandom rnd = ThreadLocalRandom.current();
private static final int minI = 0, maxI = 10;
private int x, y, z;
@Before
public void before() {
x = rnd.nextInt(minI, maxI);
y = rnd.nextInt(minI, maxI);
z = rnd.nextInt(minI, maxI);
}
@Test
public void testEquals() {
BlockLocation loc1 = new BlockLocation(x, y, z);
BlockLocation loc2 = new BlockLocation(x, y, z);
assertEquals(loc1, loc2);
loc2 = new BlockLocation(x+1, y+2, z-3);
assertNotEquals(loc1, loc2);
}
@Test
public void testClone() {
BlockLocation locOrig = new BlockLocation(x, y, z);
BlockLocation locClone = locOrig.clone();
assertEquals(locOrig, locClone);
}
}

View File

@@ -1,35 +1,91 @@
package mc.core; package mc.core;
import mc.core.world.World; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; import java.util.concurrent.ThreadLocalRandom;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame; import static org.junit.Assert.assertNotEquals;
import static org.mockito.Mockito.mock;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SpringConfig.class)
public class TestEntityLocation { public class TestEntityLocation {
@Autowired private static final ThreadLocalRandom rnd = ThreadLocalRandom.current();
@Qualifier("simpleMockWorld") private static final double minD = 0.0d, maxD = 10.0d;
private World mockWorld; private static final float minF = 0.0f, maxF = 359.9f;
private double x, y, z;
private float yaw, pitch;
@Before
public void before() {
x = rnd.nextDouble(minD, maxD);
y = rnd.nextDouble(minD, maxD);
z = rnd.nextDouble(minD, maxD);
yaw = rnd.nextFloat() * (maxF - minF) + minF;
pitch = rnd.nextFloat() * (maxF - minF) + minF;
}
@Test @Test
public void cloneTest() { public void testEquals() {
EntityLocation firstLocation = new EntityLocation(10, 20, 30, 40, 50, mockWorld); EntityLocation loc1 = new EntityLocation(x, y, z, yaw, pitch);
assertSame("Lost world reference before cloning", mockWorld, firstLocation.getWorld()); EntityLocation loc2 = new EntityLocation(x, y, z, yaw, pitch);
EntityLocation locationClone = firstLocation.clone(); assertEquals(loc1, loc2);
assertEquals("X mismatch", firstLocation.getX(), locationClone.getX(), 0); loc2 = new EntityLocation(x+1, y+2, z-3, yaw, pitch);
assertEquals("Y mismatch", firstLocation.getY(), locationClone.getY(), 0); assertNotEquals(loc1, loc2);
assertEquals("Z mismatch", firstLocation.getZ(), locationClone.getZ(), 0);
assertEquals("Pitch mismatch", firstLocation.getPitch(), locationClone.getPitch(), 0); loc2 = new EntityLocation(x, y, z, yaw-1, pitch+2);
assertEquals("Yaw mismatch", firstLocation.getYaw(), locationClone.getYaw(), 0); assertNotEquals(loc1, loc2);
assertSame("World mismatch (accidental clone of the World object?)", firstLocation.getWorld(), locationClone.getWorld()); }
@Test
public void testClone() {
EntityLocation locOrig = new EntityLocation(x, y, z, yaw, pitch);
EntityLocation locClone = locOrig.clone();
assertEquals(locOrig, locClone);
}
@Test
public void testGetBlockXZ() {
EntityLocation location;
location = new EntityLocation(0d, 0, 0d, 0f, 0f);
assertEquals(0, location.getBlockX());
assertEquals(0, location.getBlockZ());
location.setXYZ(0.1d, 0, 0.1d);
assertEquals(0, location.getBlockX());
assertEquals(0, location.getBlockZ());
location.setXYZ(0.5d, 0, 0.5d);
assertEquals(0, location.getBlockX());
assertEquals(0, location.getBlockZ());
location.setXYZ(0.9d, 0, 0.9d);
assertEquals(0, location.getBlockX());
assertEquals(0, location.getBlockZ());
location.setXYZ(1d, 0, 1d);
assertEquals(1, location.getBlockX());
assertEquals(1, location.getBlockZ());
location.setXYZ(-0.1d, 0, -0.1d);
assertEquals(-1, location.getBlockX());
assertEquals(-1, location.getBlockZ());
location.setXYZ(-0.5d, 0, -0.5d);
assertEquals(-1, location.getBlockX());
assertEquals(-1, location.getBlockZ());
location.setXYZ(-0.9d, 0, -0.9d);
assertEquals(-1, location.getBlockX());
assertEquals(-1, location.getBlockZ());
location.setXYZ(-1d, 0, -1d);
assertEquals(-1, location.getBlockX());
assertEquals(-1, location.getBlockZ());
location.setXYZ(-1.1d, 0, -1.1d);
assertEquals(-2, location.getBlockX());
assertEquals(-2, location.getBlockZ());
} }
} }

View File

@@ -1,119 +0,0 @@
package mc.core;
import mc.core.world.World;
import mc.core.world.chunk.Chunk;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.*;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SpringConfig.class)
public class TestLocation {
@Autowired
@Qualifier("chunkedMockWorld")
private World world;
@Test
public void testGetBlockXZ() {
Location location;
location = new Location(0d, 0, 0d, world);
assertEquals(0, location.getBlockX());
assertEquals(0, location.getBlockZ());
location.setXYZ(0.1d, 0, 0.1d);
assertEquals(0, location.getBlockX());
assertEquals(0, location.getBlockZ());
location.setXYZ(0.5d, 0, 0.5d);
assertEquals(0, location.getBlockX());
assertEquals(0, location.getBlockZ());
location.setXYZ(0.9d, 0, 0.9d);
assertEquals(0, location.getBlockX());
assertEquals(0, location.getBlockZ());
location.setXYZ(1d, 0, 1d);
assertEquals(1, location.getBlockX());
assertEquals(1, location.getBlockZ());
location.setXYZ(-0.1d, 0, -0.1d);
assertEquals(-1, location.getBlockX());
assertEquals(-1, location.getBlockZ());
location.setXYZ(-0.5d, 0, -0.5d);
assertEquals(-1, location.getBlockX());
assertEquals(-1, location.getBlockZ());
location.setXYZ(-0.9d, 0, -0.9d);
assertEquals(-1, location.getBlockX());
assertEquals(-1, location.getBlockZ());
location.setXYZ(-1d, 0, -1d);
assertEquals(-1, location.getBlockX());
assertEquals(-1, location.getBlockZ());
location.setXYZ(-1.1d, 0, -1.1d);
assertEquals(-2, location.getBlockX());
assertEquals(-2, location.getBlockZ());
}
@Test
public void testGetChunk() {
Location location;
Chunk chunk;
location = new Location(0d, 0, 0d, world);
chunk = location.getChunk();
assertEquals(0, chunk.getX());
assertEquals(0, chunk.getZ());
location.setXYZ(1d, 0, 1d);
chunk = location.getChunk();
assertEquals(0, chunk.getX());
assertEquals(0, chunk.getZ());
location.setXYZ(15d, 0, 15d);
chunk = location.getChunk();
assertEquals(0, chunk.getX());
assertEquals(0, chunk.getZ());
location.setXYZ(16d, 0, 16d);
chunk = location.getChunk();
assertEquals(1, chunk.getX());
assertEquals(1, chunk.getZ());
location.setXYZ(-0.1d, 0, -0.1d);
chunk = location.getChunk();
assertEquals(-1, chunk.getX());
assertEquals(-1, chunk.getZ());
location.setXYZ(-1d, 0, -1d);
chunk = location.getChunk();
assertEquals(-1, chunk.getX());
assertEquals(-1, chunk.getZ());
location.setXYZ(-15d, 0, -15d);
chunk = location.getChunk();
assertEquals(-1, chunk.getX());
assertEquals(-1, chunk.getZ());
//TODO на практике, таких точных значений не встретиться, но тем не менее данный тест не проходит
//location.setXYZ(-16.0d, 0, -16.0d);
//chunk = location.getChunk();
//assertEquals(-2, chunk.getX());
//assertEquals(-2, chunk.getZ());
location.setXYZ(-16.001d, 0, -16.001d);
chunk = location.getChunk();
assertEquals(-2, chunk.getX());
assertEquals(-2, chunk.getZ());
}
}

View File

@@ -5,6 +5,7 @@
package mc.world.flat; package mc.world.flat;
import lombok.Getter; import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import mc.core.EntityLocation; import mc.core.EntityLocation;
import mc.core.world.World; import mc.core.world.World;
@@ -16,6 +17,7 @@ import mc.core.world.chunk.ChunkSection;
public class FlatWorld implements World { public class FlatWorld implements World {
@Getter @Getter
private final WorldType worldType = WorldType.FLAT; private final WorldType worldType = WorldType.FLAT;
@Setter
private EntityLocation spawn; private EntityLocation spawn;
private ChunkSection chunkSection = new SimpleChunkSection(); private ChunkSection chunkSection = new SimpleChunkSection();
@@ -23,18 +25,12 @@ public class FlatWorld implements World {
public EntityLocation getSpawn() { public EntityLocation getSpawn() {
if (this.spawn == null) { if (this.spawn == null) {
log.warn("Spawn is not defined! Set spawn [0, 6, 0]"); log.warn("Spawn is not defined! Set spawn [0, 6, 0]");
this.spawn = new EntityLocation(0d, 6d, 0d, 0f, 0f, this); this.spawn = new EntityLocation(0d, 6d, 0d, 0f, 0f);
} }
return this.spawn; return this.spawn;
} }
@Override
public void setSpawn(EntityLocation location) {
this.spawn = location;
this.spawn.setWorld(this);
}
@Override @Override
public Chunk getChunk(int x, int z) { public Chunk getChunk(int x, int z) {
Chunk chunk = new SimpleChunk(x, z, this); Chunk chunk = new SimpleChunk(x, z, this);

View File

@@ -9,12 +9,8 @@ import mc.core.world.World;
import mc.core.world.block.Block; import mc.core.world.block.Block;
import mc.core.world.block.BlockFactory; import mc.core.world.block.BlockFactory;
import mc.core.world.block.BlockType; import mc.core.world.block.BlockType;
import mc.core.world.chunk.Chunk;
import mc.core.world.chunk.ChunkSection; import mc.core.world.chunk.ChunkSection;
import java.lang.ref.Reference;
import java.lang.ref.WeakReference;
public class SimpleChunkSection implements ChunkSection { public class SimpleChunkSection implements ChunkSection {
@Override @Override
public int getSkyLight(int x, int y, int z) { public int getSkyLight(int x, int y, int z) {
@@ -63,10 +59,10 @@ public class SimpleChunkSection implements ChunkSection {
public Block getBlock(int x, int y, int z) { public Block getBlock(int x, int y, int z) {
BlockFactory blockFactory = new BlockFactory(); BlockFactory blockFactory = new BlockFactory();
if (y == 0) return blockFactory.create(BlockType.BEDROCK, x, y, z, getWorld()); if (y == 0) return blockFactory.create(BlockType.BEDROCK, x, y, z);
else if (y >= 1 && y <= 2) return blockFactory.create(BlockType.DIRT, x, y, z, getWorld()); else if (y >= 1 && y <= 2) return blockFactory.create(BlockType.DIRT, x, y, z);
else if (y == 3) return blockFactory.create(BlockType.GRASS, x, y, z, getWorld()); else if (y == 3) return blockFactory.create(BlockType.GRASS, x, y, z);
else return blockFactory.create(BlockType.AIR, x, y, z, getWorld()); else return blockFactory.create(BlockType.AIR, x, y, z);
} }
@Override @Override

View File

@@ -45,8 +45,7 @@ public class TeleportManager {
public void apply(int teleportId) { public void apply(int teleportId) {
if (teleportMap.containsKey(teleportId)) { if (teleportMap.containsKey(teleportId)) {
TpData data = teleportMap.remove(teleportId); TpData data = teleportMap.remove(teleportId);
data.player.getLocation().setXYZ(data.newLocation); data.player.getLocation().set(data.newLocation);
data.player.getLocation().setYawPitch(data.newLocation);
} }
} }

View File

@@ -2,7 +2,7 @@ package mc.core.network.proto_1_12_2.packets;
import lombok.Getter; import lombok.Getter;
import lombok.ToString; import lombok.ToString;
import mc.core.Location; import mc.core.world.block.BlockLocation;
import mc.core.network.CSPacket; import mc.core.network.CSPacket;
import mc.core.network.NetInputStream; import mc.core.network.NetInputStream;
import mc.core.network.proto_1_12_2.Direction; import mc.core.network.proto_1_12_2.Direction;
@@ -11,7 +11,7 @@ import mc.core.utils.CompactedCoords;
@Getter @Getter
@ToString @ToString
public class PlayerBlockPlacementPacket implements CSPacket { public class PlayerBlockPlacementPacket implements CSPacket {
private Location location; private BlockLocation location;
private Direction face; private Direction face;
/** true - main hand; false - off hand */ /** true - main hand; false - off hand */
private boolean hand; private boolean hand;
@@ -21,7 +21,7 @@ public class PlayerBlockPlacementPacket implements CSPacket {
public void readSelf(NetInputStream netStream) { public void readSelf(NetInputStream netStream) {
long compactedCoords = netStream.readLong(); long compactedCoords = netStream.readLong();
double[] xyz = CompactedCoords.uncompressXYZ(compactedCoords); double[] xyz = CompactedCoords.uncompressXYZ(compactedCoords);
location = new Location(xyz[0], xyz[1], xyz[2], null); location = new BlockLocation((int)xyz[0], (int)xyz[1], (int)xyz[2]); //FIXME
face = Direction.getById(netStream.readVarInt()); face = Direction.getById(netStream.readVarInt());
hand = (netStream.readVarInt() == 1); hand = (netStream.readVarInt() == 1);
cursorX = netStream.readFloat(); cursorX = netStream.readFloat();

View File

@@ -3,7 +3,7 @@ package mc.core.network.proto_1_12_2.packets;
import lombok.Getter; import lombok.Getter;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.ToString; import lombok.ToString;
import mc.core.Location; import mc.core.world.block.BlockLocation;
import mc.core.network.CSPacket; import mc.core.network.CSPacket;
import mc.core.network.NetInputStream; import mc.core.network.NetInputStream;
import mc.core.network.proto_1_12_2.Direction; import mc.core.network.proto_1_12_2.Direction;
@@ -42,7 +42,7 @@ public class PlayerDiggingPacket implements CSPacket {
} }
private Status status; private Status status;
private Location location; private BlockLocation location;
private Direction face; private Direction face;
@Override @Override
@@ -50,7 +50,7 @@ public class PlayerDiggingPacket implements CSPacket {
status = Status.getById(netStream.readVarInt()); status = Status.getById(netStream.readVarInt());
long compactCoord = netStream.readLong(); long compactCoord = netStream.readLong();
double[] xyz = CompactedCoords.uncompressXYZ(compactCoord); double[] xyz = CompactedCoords.uncompressXYZ(compactCoord);
location = new Location(xyz[0], xyz[1], xyz[2], null); location = new BlockLocation((int)xyz[0], (int)xyz[1], (int)xyz[2]); //FIXME
face = Direction.getById(netStream.readByte()); face = Direction.getById(netStream.readByte());
} }
} }

View File

@@ -48,8 +48,7 @@ public class PlayerPositionAndLookPacket implements SCPacket, CSPacket {
netStream.readDouble(), netStream.readDouble(),
netStream.readDouble(), netStream.readDouble(),
netStream.readFloat(), netStream.readFloat(),
netStream.readFloat(), netStream.readFloat()
null
); );
this.onGround = netStream.readBoolean(); this.onGround = netStream.readBoolean();

View File

@@ -4,7 +4,7 @@
*/ */
package mc.core.network.proto_1_12_2.packets; package mc.core.network.proto_1_12_2.packets;
import mc.core.Location; import mc.core.world.block.BlockLocation;
import mc.core.network.CSPacket; import mc.core.network.CSPacket;
import mc.core.network.NetInputStream; import mc.core.network.NetInputStream;
@@ -12,7 +12,7 @@ public class TabCompletePacket implements CSPacket {
private String text; private String text;
private boolean assumeCommand; private boolean assumeCommand;
private boolean hasPosition; private boolean hasPosition;
private Location location; private BlockLocation location;
@Override @Override
public void readSelf(NetInputStream netStream) { public void readSelf(NetInputStream netStream) {
@@ -27,7 +27,7 @@ public class TabCompletePacket implements CSPacket {
double y = (compactValue >> 26) & 0xFFF; double y = (compactValue >> 26) & 0xFFF;
double z = compactValue << 38 >> 38; // is normal? double z = compactValue << 38 >> 38; // is normal?
this.location = new Location(x, y, z, null); this.location = new BlockLocation((int)x, (int)y, (int)z); //FIXME
} }
} }
} }

View File

@@ -50,10 +50,10 @@ public class TestChunkdataPacket {
BlockFactory blockFactory = new BlockFactory(); BlockFactory blockFactory = new BlockFactory();
if (y == 0) return blockFactory.create(BlockType.BEDROCK, x, y, z, null); if (y == 0) return blockFactory.create(BlockType.BEDROCK, x, y, z);
else if (y >= 1 && y <= 2) return blockFactory.create(BlockType.DIRT, x, y, z, null); else if (y >= 1 && y <= 2) return blockFactory.create(BlockType.DIRT, x, y, z);
else if (y == 3) return blockFactory.create(BlockType.GRASS, x, y, z, null); else if (y == 3) return blockFactory.create(BlockType.GRASS, x, y, z);
else return blockFactory.create(BlockType.AIR, x, y, z, null); else return blockFactory.create(BlockType.AIR, x, y, z);
}); });
world = mock(World.class); world = mock(World.class);

View File

@@ -29,7 +29,7 @@ class PlayerEventListener {
public void playerChunkLoadHandler(SC_ChunkLoadEvent event) { public void playerChunkLoadHandler(SC_ChunkLoadEvent event) {
for(Integer compressXZ : event.getNeedLoadChunks()) { for(Integer compressXZ : event.getNeedLoadChunks()) {
int[] xz = CompactedCoords.uncompressXZ(compressXZ); int[] xz = CompactedCoords.uncompressXZ(compressXZ);
Chunk chunk = event.getPlayer().getLocation().getWorld().getChunk(xz[0], xz[1]); Chunk chunk = event.getPlayer().getWorld().getChunk(xz[0], xz[1]);
ChunkDataPacket packet = new ChunkDataPacket(); ChunkDataPacket packet = new ChunkDataPacket();
packet.setX(xz[0]); packet.setX(xz[0]);

View File

@@ -20,6 +20,7 @@ import mc.core.text.TextColor;
import mc.core.text.TextStyle; import mc.core.text.TextStyle;
import mc.core.utils.CompactedCoords; import mc.core.utils.CompactedCoords;
import mc.core.world.World; import mc.core.world.World;
import mc.core.world.chunk.Chunk;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
@@ -50,7 +51,8 @@ public class LoginHandler extends AbstractStateHandler implements LoginStateHand
Player player = playerManager.getPlayer(packet.getPlayerName()) Player player = playerManager.getPlayer(packet.getPlayerName())
.orElseGet(() -> playerManager.createPlayer( .orElseGet(() -> playerManager.createPlayer(
packet.getPlayerName(), packet.getPlayerName(),
world.getSpawn())); world.getSpawn(),
world));
channel.writeAndFlush(new LoginSuccessPacket( channel.writeAndFlush(new LoginSuccessPacket(
player.getUUID(), player.getUUID(),
@@ -83,9 +85,10 @@ public class LoginHandler extends AbstractStateHandler implements LoginStateHand
// First Chunk // First Chunk
ChunkDataPacket pkt8 = new ChunkDataPacket(); ChunkDataPacket pkt8 = new ChunkDataPacket();
pkt8.setX(player.getLocation().getChunk().getX()); Chunk chunk = player.getWorld().getChunk(player.getLocation());
pkt8.setZ(player.getLocation().getChunk().getZ()); pkt8.setX(chunk.getX());
pkt8.setChunk(player.getLocation().getChunk()); pkt8.setZ(chunk.getZ());
pkt8.setChunk(chunk);
pkt8.setInitChunk(true); pkt8.setInitChunk(true);
channel.writeAndFlush(pkt8); channel.writeAndFlush(pkt8);
player.getLoadedChunks().add(CompactedCoords.compressXZ(0, 0)); player.getLoadedChunks().add(CompactedCoords.compressXZ(0, 0));

View File

@@ -55,8 +55,7 @@ public class PlayHandler extends AbstractStateHandler implements PlayStateHandle
@Handler @Handler
public void onPositionAndLook(Channel channel, PlayerPositionAndLookPacket packet) { public void onPositionAndLook(Channel channel, PlayerPositionAndLookPacket packet) {
Player player = channel.attr(ATTR_PLAYER).get(); Player player = channel.attr(ATTR_PLAYER).get();
player.getLocation().setXYZ(packet.getLocation()); player.getLocation().set(packet.getLocation());
player.getLocation().setYawPitch(packet.getLocation());
} }
@Handler @Handler
@@ -82,8 +81,7 @@ public class PlayHandler extends AbstractStateHandler implements PlayStateHandle
event.setNewLocation(new EntityLocation( event.setNewLocation(new EntityLocation(
packet.getX(), packet.getY(), packet.getZ(), packet.getX(), packet.getY(), packet.getZ(),
player.getLocation().getYaw(), player.getLocation().getYaw(),
player.getLocation().getPitch(), player.getLocation().getPitch()
player.getLocation().getWorld()
)); ));
EventBusGetter.getInstance().post(event); EventBusGetter.getInstance().post(event);
} }