Example #1
0
  protected void checkChunks() {
    if (this.chunk == null
        || (this.chunk.getX() != ((int) this.x >> 4))
        || this.chunk.getZ() != ((int) this.z >> 4)) {
      if (this.chunk != null) {
        this.chunk.removeEntity(this);
      }
      this.chunk = this.level.getChunk((int) this.x >> 4, (int) this.z >> 4, true);

      if (!this.justCreated) {
        Map<Integer, Player> newChunk =
            this.level.getChunkPlayers((int) this.x >> 4, (int) this.z >> 4);
        for (Player player : new ArrayList<>(this.hasSpawned.values())) {
          if (!newChunk.containsKey(player.getLoaderId())) {
            this.despawnFrom(player);
          } else {
            newChunk.remove(player.getLoaderId());
          }
        }

        for (Player player : newChunk.values()) {
          this.spawnTo(player);
        }
      }

      if (this.chunk == null) {
        return;
      }

      this.chunk.addEntity(this);
    }
  }