PlotSquared/src/main/java/com/plotsquared/sponge/util/SpongeChunkManager.java

187 lines
6.4 KiB
Java
Raw Normal View History

2015-07-30 19:24:01 +02:00
package com.plotsquared.sponge.util;
2015-07-30 16:25:16 +02:00
import java.util.ArrayList;
2015-09-22 15:23:28 +02:00
import java.util.HashSet;
2015-07-30 16:25:16 +02:00
import java.util.List;
2015-09-22 15:23:28 +02:00
import java.util.Set;
2015-07-30 16:25:16 +02:00
import org.spongepowered.api.entity.Entity;
import org.spongepowered.api.entity.living.Living;
import org.spongepowered.api.entity.living.animal.Animal;
import org.spongepowered.api.entity.living.monster.Monster;
2015-07-30 16:25:16 +02:00
import org.spongepowered.api.world.Chunk;
import org.spongepowered.api.world.World;
import org.spongepowered.api.world.storage.ChunkDataStream;
import com.google.common.base.Optional;
import com.google.common.base.Predicate;
2015-07-30 16:25:16 +02:00
import com.intellectualcrafters.plot.object.ChunkLoc;
import com.intellectualcrafters.plot.object.Location;
import com.intellectualcrafters.plot.object.Plot;
import com.intellectualcrafters.plot.object.PlotBlock;
import com.intellectualcrafters.plot.object.PlotId;
import com.intellectualcrafters.plot.util.ChunkManager;
import com.intellectualcrafters.plot.util.SetBlockQueue.ChunkWrapper;
2015-07-30 19:24:01 +02:00
import com.intellectualcrafters.plot.util.TaskManager;
2015-07-30 16:25:16 +02:00
2015-09-13 06:04:31 +02:00
public class SpongeChunkManager extends ChunkManager {
2015-07-30 16:25:16 +02:00
@Override
2015-09-13 06:04:31 +02:00
public void setChunk(final ChunkWrapper loc, final PlotBlock[][] result) {
2015-07-30 16:25:16 +02:00
// TODO Auto-generated method stub
2015-09-13 06:04:31 +02:00
2015-07-30 16:25:16 +02:00
}
2015-09-13 06:04:31 +02:00
2015-07-30 16:25:16 +02:00
@Override
2015-09-13 06:04:31 +02:00
public int[] countEntities(final Plot plot) {
2015-09-22 15:23:28 +02:00
final Location pos1 = plot.getBottomAbs();
final Location pos2 = plot.getTopAbs();
2015-09-13 06:04:31 +02:00
2015-09-11 12:09:22 +02:00
final String worldname = pos1.getWorld();
final World world = SpongeUtil.getWorld(worldname);
final int bx = pos1.getX();
final int bz = pos1.getZ();
final int tx = pos2.getX();
final int tz = pos2.getZ();
final int[] count = new int[5];
2015-09-13 06:04:31 +02:00
world.getEntities(new Predicate<Entity>() {
@Override
2015-09-13 06:04:31 +02:00
public boolean apply(final Entity entity) {
2015-09-11 12:09:22 +02:00
final org.spongepowered.api.world.Location loc = entity.getLocation();
final int x = loc.getBlockX();
2015-09-13 06:04:31 +02:00
if ((x >= bx) && (x <= tx)) {
2015-09-11 12:09:22 +02:00
final int z = loc.getBlockZ();
2015-09-13 06:04:31 +02:00
if ((z >= bz) && (z <= tz)) {
count[0]++;
2015-09-13 06:04:31 +02:00
if (entity instanceof Living) {
count[3]++;
2015-09-13 06:04:31 +02:00
if (entity instanceof Animal) {
count[1]++;
2015-09-13 06:04:31 +02:00
} else if (entity instanceof Monster) {
count[2]++;
}
2015-09-13 06:04:31 +02:00
} else {
count[4]++;
}
}
}
return false;
}
});
2015-09-13 06:04:31 +02:00
return count;
2015-07-30 16:25:16 +02:00
}
2015-09-13 06:04:31 +02:00
2015-07-30 16:25:16 +02:00
@Override
2015-09-13 06:04:31 +02:00
public boolean loadChunk(final String world, final ChunkLoc loc, final boolean force) {
2015-09-11 12:09:22 +02:00
final World worldObj = SpongeUtil.getWorld(world);
2015-07-30 16:25:16 +02:00
return worldObj.loadChunk(loc.x << 4, 0, loc.z << 4, force).isPresent();
}
2015-09-13 06:04:31 +02:00
2015-07-30 16:25:16 +02:00
@Override
2015-09-13 06:04:31 +02:00
public boolean unloadChunk(final String world, final ChunkLoc loc, final boolean save, final boolean safe) {
2015-09-11 12:09:22 +02:00
final World worldObj = SpongeUtil.getWorld(world);
final Optional<Chunk> chunk = worldObj.getChunk(loc.x << 4, 0, loc.z << 4);
2015-09-13 06:04:31 +02:00
if (chunk.isPresent()) {
return worldObj.unloadChunk(chunk.get());
}
2015-07-30 16:25:16 +02:00
return false;
}
2015-09-13 06:04:31 +02:00
2015-07-30 16:25:16 +02:00
@Override
2015-09-22 15:23:28 +02:00
public Set<ChunkLoc> getChunkChunks(final String world) {
final HashSet<ChunkLoc> chunks = new HashSet<ChunkLoc>();
2015-09-11 12:09:22 +02:00
final World worldObj = SpongeUtil.getWorld(world);
final ChunkDataStream storage = worldObj.getWorldStorage().getGeneratedChunks();
2015-09-13 06:04:31 +02:00
while (storage.hasNext()) {
2015-09-11 12:09:22 +02:00
storage.next();
2015-09-13 06:04:31 +02:00
2015-07-30 16:25:16 +02:00
// TODO get chunk from DataContainer
}
return chunks;
}
2015-09-13 06:04:31 +02:00
2015-07-30 16:25:16 +02:00
@Override
2015-09-13 06:04:31 +02:00
public void regenerateChunk(final String world, final ChunkLoc loc) {
2015-09-11 12:09:22 +02:00
final World worldObj = SpongeUtil.getWorld(world);
final Optional<Chunk> chunk = worldObj.getChunk(loc.x << 4, 0, loc.z << 4);
2015-09-13 06:04:31 +02:00
if (chunk.isPresent()) {
2015-07-30 16:25:16 +02:00
// TODO regenerate chunk
}
}
2015-09-13 06:04:31 +02:00
2015-07-30 16:25:16 +02:00
@Override
2015-09-13 06:04:31 +02:00
public void deleteRegionFile(final String world, final ChunkLoc loc) {
2015-07-30 16:25:16 +02:00
// TODO Auto-generated method stub
2015-09-13 06:04:31 +02:00
2015-07-30 16:25:16 +02:00
}
2015-09-13 06:04:31 +02:00
2015-07-30 16:25:16 +02:00
@Override
2015-09-13 06:04:31 +02:00
public void deleteRegionFiles(final String world, final List<ChunkLoc> chunks) {
2015-07-30 16:25:16 +02:00
// TODO Auto-generated method stub
2015-09-13 06:04:31 +02:00
2015-07-30 16:25:16 +02:00
}
2015-09-13 06:04:31 +02:00
@Override
public void deleteRegionFiles(String world, List<ChunkLoc> chunks, Runnable whenDone) {
// TODO Auto-generated method stub
}
2015-09-13 06:04:31 +02:00
2015-07-30 16:25:16 +02:00
@Override
2015-09-13 06:04:31 +02:00
public Plot hasPlot(final String world, final ChunkLoc chunk) {
2015-07-30 16:25:16 +02:00
// TODO Auto-generated method stub
return null;
}
2015-09-13 06:04:31 +02:00
2015-07-30 16:25:16 +02:00
@Override
2015-09-13 06:04:31 +02:00
public boolean copyRegion(final Location pos1, final Location pos2, final Location newPos, final Runnable whenDone) {
2015-07-30 16:25:16 +02:00
// TODO Auto-generated method stub
TaskManager.runTask(whenDone);
return false;
}
2015-09-13 06:04:31 +02:00
2015-07-30 16:25:16 +02:00
@Override
2015-09-13 06:04:31 +02:00
public boolean regenerateRegion(final Location pos1, final Location pos2, final Runnable whenDone) {
2015-07-30 16:25:16 +02:00
// TODO Auto-generated method stub
TaskManager.runTask(whenDone);
return false;
}
2015-09-13 06:04:31 +02:00
2015-07-30 16:25:16 +02:00
@Override
2015-09-13 06:04:31 +02:00
public void swap(final String world, final PlotId id, final PlotId plotid) {
2015-07-30 16:25:16 +02:00
// TODO Auto-generated method stub
2015-09-13 06:04:31 +02:00
2015-07-30 16:25:16 +02:00
}
2015-09-13 06:04:31 +02:00
2015-07-30 16:25:16 +02:00
@Override
2015-09-13 06:04:31 +02:00
public void swap(final String worldname, final Location bot1, final Location top1, final Location bot2, final Location top2) {
2015-07-30 16:25:16 +02:00
// TODO Auto-generated method stub
2015-09-13 06:04:31 +02:00
2015-07-30 16:25:16 +02:00
}
2015-09-13 06:04:31 +02:00
2015-07-30 16:25:16 +02:00
@Override
2015-09-13 06:04:31 +02:00
public void clearAllEntities(final Location pos1, final Location pos2) {
2015-09-11 12:09:22 +02:00
final String worldname = pos1.getWorld();
final World world = SpongeUtil.getWorld(worldname);
final int bx = pos1.getX();
final int bz = pos1.getZ();
final int tx = pos2.getX();
final int tz = pos2.getZ();
2015-09-13 06:04:31 +02:00
world.getEntities(new Predicate<Entity>() {
@Override
2015-09-13 06:04:31 +02:00
public boolean apply(final Entity entity) {
2015-09-11 12:09:22 +02:00
final org.spongepowered.api.world.Location loc = entity.getLocation();
final int x = loc.getBlockX();
2015-09-13 06:04:31 +02:00
if ((x >= bx) && (x <= tx)) {
2015-09-11 12:09:22 +02:00
final int z = loc.getBlockZ();
2015-09-13 06:04:31 +02:00
if ((z >= bz) && (z <= tz)) {
entity.remove();
}
}
return false;
}
});
2015-07-30 16:25:16 +02:00
}
2015-09-13 06:04:31 +02:00
2015-07-30 16:25:16 +02:00
}