Fixed a random instance crash

This commit is contained in:
Felix Cravic 2020-04-23 13:43:31 +02:00
parent 4570e7a013
commit f4aa328849
3 changed files with 15 additions and 17 deletions

View File

@ -26,7 +26,7 @@ import java.util.UUID;
public class PlayerInit {
private static InstanceContainer instanceContainer;
private static volatile InstanceContainer instanceContainer;
static {
ChunkGeneratorDemo chunkGeneratorDemo = new ChunkGeneratorDemo();
@ -143,6 +143,7 @@ public class PlayerInit {
});
player.setEventCallback(PlayerLoginEvent.class, event -> {
System.out.println("event: " + instanceContainer.hashCode());
event.setSpawningInstance(instanceContainer);
});

View File

@ -32,16 +32,16 @@ public class EntityManager {
// Update entities
switch (updateType) {
case PER_CHUNK:
chunkUpdate(instances, time);
chunkUpdate(time);
break;
case PER_ENTITY_TYPE:
entityTypeUpdate(instances, time);
entityTypeUpdate(time);
break;
case PER_INSTANCE:
instanceUpdate(instances, time);
instanceUpdate(time);
break;
case SINGLE_THREADED:
singleThreaded(instances, time);
singleThreaded(time);
break;
}
@ -52,9 +52,8 @@ public class EntityManager {
*
* @param time
*/
private void chunkUpdate(Set<Instance> instances, long time) {
// TODO optimize for when there are too many entities on one chunk
for (Instance instance : instanceManager.getInstances()) {
private void chunkUpdate(long time) {
for (Instance instance : instances) {
for (Chunk chunk : instance.getChunks()) {
Set<Entity> entities = instance.getChunkEntities(chunk);
@ -75,8 +74,8 @@ public class EntityManager {
*
* @param time
*/
private void entityTypeUpdate(Set<Instance> instances, long time) {
for (Instance instance : instanceManager.getInstances()) {
private void entityTypeUpdate(long time) {
for (Instance instance : instances) {
Set<Player> players = instance.getPlayers();
Set<EntityCreature> creatures = instance.getCreatures();
Set<ObjectEntity> objects = instance.getObjectEntities();
@ -105,10 +104,9 @@ public class EntityManager {
/**
* Each instance get its pool, should suppress most of the problems related to thread-safety
*
* @param instances
* @param time
*/
private void instanceUpdate(Set<Instance> instances, long time) {
private void instanceUpdate(long time) {
for (Instance instance : instances) {
Set<Player> players = instance.getPlayers();
Set<EntityCreature> creatures = instance.getCreatures();
@ -133,10 +131,9 @@ public class EntityManager {
/**
* Single threaded update (like the notchian server)
*
* @param instances
* @param time
*/
private void singleThreaded(Set<Instance> instances, long time) {
private void singleThreaded(long time) {
for (Instance instance : instances) {
Set<Player> players = instance.getPlayers();
Set<EntityCreature> creatures = instance.getCreatures();
@ -164,7 +161,9 @@ public class EntityManager {
playersPool.execute(() -> {
PlayerLoginEvent loginEvent = new PlayerLoginEvent();
playerCache.callEvent(PlayerLoginEvent.class, loginEvent);
Instance spawningInstance = loginEvent.getSpawningInstance() == null ? instanceManager.createInstanceContainer() : loginEvent.getSpawningInstance();
Instance spawningInstance = loginEvent.getSpawningInstance();
if (spawningInstance == null)
throw new NullPointerException("You need to specify a spawning instance in the PlayerLoginEvent");
playerCache.setInstance(spawningInstance);
});

View File

@ -310,7 +310,6 @@ public class Player extends LivingEntity {
int length = visibleChunks.length;
AtomicInteger counter = new AtomicInteger(0);
for (int i = 0; i < length; i++) {
int[] chunkPos = ChunkUtils.getChunkCoord(visibleChunks[i]);
int chunkX = chunkPos[0];
@ -322,7 +321,6 @@ public class Player extends LivingEntity {
}
boolean isLast = counter.get() == length - 1;
if (isLast) {
System.out.println("DEBUG CHUNK: " + chunk);
// This is the last chunk to be loaded , spawn player
super.setInstance(instance);
PlayerSpawnEvent spawnEvent = new PlayerSpawnEvent(instance);