001/*
002 * Trident - A Multithreaded Server Alternative
003 * Copyright 2014 The TridentSDK Team
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *    http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018package net.tridentsdk.server.world;
019
020import net.tridentsdk.base.Block;
021import net.tridentsdk.base.BoundingBox;
022import net.tridentsdk.entity.Entity;
023import net.tridentsdk.meta.block.Tile;
024import net.tridentsdk.meta.nbt.CompoundTag;
025import net.tridentsdk.world.Chunk;
026import net.tridentsdk.world.ChunkLocation;
027import net.tridentsdk.world.ChunkSnapshot;
028import net.tridentsdk.world.World;
029
030import java.util.ArrayList;
031import java.util.Collection;
032import java.util.Set;
033import java.util.function.Predicate;
034
035public class TridentChunkSnapshot implements ChunkSnapshot {
036    private final TridentWorld world;
037    private final ChunkLocation location;
038    private final CompoundTag tag;
039
040    public TridentChunkSnapshot(TridentWorld world, TridentChunk chunk) {
041        this.world = world;
042        this.location = chunk.location();
043        this.tag = chunk.asNbt();
044    }
045
046    @Override
047    public void apply(Chunk chunk) {
048        ((TridentChunk) chunk).load(tag);
049    }
050
051    @Override
052    public void apply() {
053        ((TridentChunk) world().chunkAt(location(), true)).load(tag);
054    }
055
056    @Override
057    public Set<Entity> entities() {
058        TridentChunk chunk = new TridentChunk((TridentWorld) world(), location);
059        chunk.load(tag);
060        return chunk.entities();
061    }
062
063    @Override
064    public Collection<Tile> tiles() {
065        TridentChunk chunk = new TridentChunk((TridentWorld) world(), location);
066        chunk.load(tag);
067        return chunk.tiles();
068    }
069
070    @Override
071    public void generate() {
072        apply();
073    }
074
075    @Override
076    public boolean load() {
077        return false;
078    }
079
080    @Override
081    public boolean isLoaded() {
082        return false;
083    }
084
085    @Override
086    public ChunkLocation location() {
087        return location;
088    }
089
090    @Override
091    public int x() {
092        return location.x();
093    }
094
095    @Override
096    public int z() {
097        return location.z();
098    }
099
100    @Override
101    public World world() {
102        return world;
103    }
104
105    @Override
106    public Block blockAt(int relX, int y, int relZ) {
107        TridentChunk chunk = new TridentChunk(((TridentWorld) world()), location);
108        chunk.load(tag);
109        return chunk.blockAt(relX, y, relZ);
110    }
111
112    @Override
113    public ChunkSnapshot snapshot() {
114        return this;
115    }
116
117    @Override
118    public void unload() {
119        throw new UnsupportedOperationException("Cannot unload a snapshot");
120    }
121
122    public ArrayList<Entity> getEntities(Entity exclude, BoundingBox boundingBox, Predicate<? super Entity> predicate){
123        return new ArrayList<>();
124    }
125}