/*
 * Trident - A Multithreaded Server Alternative
 * Copyright 2016 The TridentSDK Team
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.tridentsdk.server.world;

import gnu.trove.map.TLongObjectMap;
import gnu.trove.map.hash.TLongObjectHashMap;

import javax.annotation.concurrent.GuardedBy;
import java.util.Collection;

/**
 * Map of loaded chunks.
 *
 * <p>This class makes concurrency handing easier on the
 * world.</p>
 */
public class ChunkMap {
    /**
     * The lock guarding the chunk map
     */
    private final Object lock = new Object();
    /**
     * The actual map of chunks
     */
    @GuardedBy("lock")
    private final TLongObjectMap<TridentChunk> chunks = new TLongObjectHashMap<TridentChunk>() {
        @Override
        public TridentChunk get(long key) {
            TridentChunk chunk = super.get(key);
            if (chunk != null) {
                return chunk.waitReady();
            }

            return null;
        }
    };
    /**
     * The world holding the chunks in this map
     */
    private final TridentWorld world;

    /**
     * Creates a new ChunkMap for the given world.
     *
     * @param world the world to hold chunks
     */
    public ChunkMap(TridentWorld world) {
        this.world = world;
    }

    /**
     * Obtains the chunk at the given location and
     * determines whether a chunk will be generated if it
     * does not exist yet.
     *
     * @param x the x coordinate
     * @param z the z coordinate
     * @param gen {@code true} to generate if non-existant
     * @return the chunk, or {@code null}
     */
    public TridentChunk get(int x, int z, boolean gen) {
        System.out.printf("x:%s z:%s%n", x, z);
        long key = (long) x << 32 | (long) z;
        synchronized (this.lock) {
            TridentChunk chunk = this.chunks.get(key);
            if (chunk == null && gen) {
                chunk = new TridentChunk(this.world, x, z);
                chunk.generate();
                this.chunks.put(key, chunk);
            }

            return chunk;
        }
    }

    /**
     * All of the loaded chunks.
     *
     * @return the values of the chunk map
     */
    public Collection<TridentChunk> values() {
        synchronized (this.lock) {
            return this.chunks.valueCollection();
        }
    }
}
@@ -2178,74 +2178,58 @@
     
-System.out.printf(%22x:%25s z:%25s%25n%22, x, z);%0A        long key =
+long key = z %3E= 0 ? ((long) x %3C%3C 32) %7C z :
  (long) 
 x %3C%3C
@@ -2228,27 +2228,55 @@
 ng) 
-x
+1
  %3C%3C 3
-2
+1
  %7C 
+((
 (long) 
-z;
+x %3C%3C 32) %7C 0x7FFFFFFF & z);%0A
 %0A   

Fix all chunk problems???