2015-07-30 19:24:01 +02:00
|
|
|
package com.plotsquared.sponge.util;
|
2015-07-30 16:25:16 +02:00
|
|
|
|
2016-02-23 18:04:23 +01:00
|
|
|
import com.intellectualcrafters.plot.PS;
|
2016-02-23 15:55:40 +01:00
|
|
|
import com.intellectualcrafters.plot.object.ChunkLoc;
|
|
|
|
import com.intellectualcrafters.plot.object.Location;
|
|
|
|
import com.intellectualcrafters.plot.object.Plot;
|
|
|
|
import com.intellectualcrafters.plot.util.ChunkManager;
|
|
|
|
import com.intellectualcrafters.plot.util.TaskManager;
|
2016-02-23 18:04:23 +01:00
|
|
|
import net.minecraft.world.chunk.IChunkProvider;
|
|
|
|
import net.minecraft.world.gen.ChunkProviderServer;
|
2015-08-11 20:04:17 +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;
|
|
|
|
|
2016-02-23 15:55:40 +01:00
|
|
|
import java.util.Optional;
|
|
|
|
import java.util.Set;
|
|
|
|
import java.util.function.Predicate;
|
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
|
2016-03-28 23:31:40 +02:00
|
|
|
public int[] countEntities(Plot plot) {
|
|
|
|
Location pos1 = plot.getBottomAbs();
|
|
|
|
Location pos2 = plot.getTopAbs();
|
|
|
|
World world = SpongeUtil.getWorld(pos1.getWorld());
|
|
|
|
int bx = pos1.getX();
|
|
|
|
int bz = pos1.getZ();
|
|
|
|
int tx = pos2.getX();
|
|
|
|
int tz = pos2.getZ();
|
|
|
|
int[] count = new int[6];
|
2016-02-23 15:55:40 +01:00
|
|
|
world.getEntities(entity -> {
|
2016-03-28 23:31:40 +02:00
|
|
|
org.spongepowered.api.world.Location loc = entity.getLocation();
|
|
|
|
int x = loc.getBlockX();
|
2016-02-23 15:55:40 +01:00
|
|
|
if ((x >= bx) && (x <= tx)) {
|
2016-03-28 23:31:40 +02:00
|
|
|
int z = loc.getBlockZ();
|
2016-02-23 15:55:40 +01:00
|
|
|
if ((z >= bz) && (z <= tz)) {
|
|
|
|
count[0]++;
|
|
|
|
if (entity instanceof Living) {
|
|
|
|
count[3]++;
|
|
|
|
if (entity instanceof Animal) {
|
|
|
|
count[1]++;
|
|
|
|
} else if (entity instanceof Monster) {
|
|
|
|
count[2]++;
|
2015-08-11 20:04:17 +02:00
|
|
|
}
|
2016-02-23 15:55:40 +01:00
|
|
|
} else {
|
|
|
|
count[4]++;
|
2015-08-11 20:04:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-02-23 15:55:40 +01:00
|
|
|
return false;
|
2015-08-11 20:04:17 +02:00
|
|
|
});
|
2015-09-13 06:04:31 +02:00
|
|
|
|
2015-08-11 20:04:17 +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
|
2016-03-28 23:31:40 +02:00
|
|
|
public boolean loadChunk(String world, ChunkLoc loc, boolean force) {
|
|
|
|
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
|
2016-03-28 23:31:40 +02:00
|
|
|
public Set<ChunkLoc> getChunkChunks(String world) {
|
2015-10-07 08:33:33 +02:00
|
|
|
// TODO save world;
|
|
|
|
return super.getChunkChunks(world);
|
2015-07-30 16:25:16 +02:00
|
|
|
}
|
2016-02-23 19:12:07 +01:00
|
|
|
|
2015-07-30 16:25:16 +02:00
|
|
|
@Override
|
2016-03-28 23:31:40 +02:00
|
|
|
public void regenerateChunk(String world, ChunkLoc loc) {
|
|
|
|
World spongeWorld = SpongeUtil.getWorld(world);
|
|
|
|
net.minecraft.world.World nmsWorld = (net.minecraft.world.World) spongeWorld;
|
|
|
|
Optional<Chunk> chunkOpt = spongeWorld.getChunk(loc.x, 0, loc.z);
|
2016-02-22 07:20:22 +01:00
|
|
|
if (chunkOpt.isPresent()) {
|
2016-02-23 18:04:23 +01:00
|
|
|
try {
|
|
|
|
Chunk spongeChunk = chunkOpt.get();
|
|
|
|
IChunkProvider provider = nmsWorld.getChunkProvider();
|
|
|
|
if (!(provider instanceof ChunkProviderServer)) {
|
|
|
|
PS.debug("Not valid world generator for: " + world);
|
|
|
|
return;
|
|
|
|
}
|
2016-03-28 23:31:40 +02:00
|
|
|
/* ChunkProviderServer chunkServer = (ChunkProviderServer) provider;
|
2016-02-23 19:12:07 +01:00
|
|
|
IChunkProvider chunkProvider = chunkServer.serverChunkGenerator;
|
|
|
|
|
|
|
|
long pos = ChunkCoordIntPair.chunkXZ2Int(loc.x, loc.z);
|
2016-02-23 18:04:23 +01:00
|
|
|
net.minecraft.world.chunk.Chunk mcChunk = (net.minecraft.world.chunk.Chunk) spongeChunk;
|
2016-02-23 19:12:07 +01:00
|
|
|
if (chunkServer.chunkExists(loc.x, loc.z)) {
|
|
|
|
mcChunk = chunkServer.loadChunk(loc.x, loc.z);
|
2016-02-23 18:04:23 +01:00
|
|
|
mcChunk.onChunkUnload();
|
|
|
|
}
|
2016-02-25 10:13:07 +01:00
|
|
|
Field fieldDroppedChunksSet;
|
|
|
|
try {
|
2016-03-19 19:07:55 +01:00
|
|
|
fieldDroppedChunksSet = chunkServer.getClass().getField("droppedChunksSet");
|
2016-02-25 10:13:07 +01:00
|
|
|
} catch (Throwable t) {
|
2016-03-19 19:07:55 +01:00
|
|
|
fieldDroppedChunksSet = ReflectionUtils.findField(chunkServer.getClass(), Set.class);
|
2016-02-25 10:13:07 +01:00
|
|
|
}
|
|
|
|
Set<Long> set = (Set<Long>) fieldDroppedChunksSet.get(chunkServer);
|
2016-02-23 18:04:23 +01:00
|
|
|
set.remove(pos);
|
2016-03-28 23:31:40 +02:00
|
|
|
ReflectionUtils.findField(chunkServer.getClass(),)
|
2016-02-23 19:12:07 +01:00
|
|
|
chunkServer.id2ChunkMap.remove(pos);
|
|
|
|
mcChunk = chunkProvider.provideChunk(loc.x, loc.z);
|
|
|
|
chunkServer.id2ChunkMap.add(pos, mcChunk);
|
|
|
|
chunkServer.loadedChunks.add(mcChunk);
|
2016-02-23 18:04:23 +01:00
|
|
|
if (mcChunk != null) {
|
|
|
|
mcChunk.onChunkLoad();
|
|
|
|
mcChunk.populateChunk(chunkProvider, chunkProvider, loc.x, loc.z);
|
2016-02-25 10:13:07 +01:00
|
|
|
SetQueue.IMP.queue.sendChunk(world, Arrays.asList(loc));
|
2016-02-23 19:12:07 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
PS.debug("CHUNK IS NULL!?");
|
2016-03-28 23:31:40 +02:00
|
|
|
}*/
|
2016-02-23 18:04:23 +01:00
|
|
|
} catch (Throwable e){
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
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
|
2016-03-28 23:31:40 +02:00
|
|
|
public boolean copyRegion(Location pos1, Location pos2, Location newPos, Runnable whenDone) {
|
2015-10-07 08:33:33 +02:00
|
|
|
// TODO copy a region
|
2015-07-30 16:25:16 +02:00
|
|
|
TaskManager.runTask(whenDone);
|
|
|
|
return false;
|
|
|
|
}
|
2015-09-13 06:04:31 +02:00
|
|
|
|
2015-07-30 16:25:16 +02:00
|
|
|
@Override
|
2016-03-28 23:31:40 +02:00
|
|
|
public void clearAllEntities(Location pos1, Location pos2) {
|
2016-03-29 21:47:59 +02:00
|
|
|
String worldName = pos1.getWorld();
|
|
|
|
World world = SpongeUtil.getWorld(worldName);
|
2016-03-28 23:31:40 +02:00
|
|
|
int bx = pos1.getX();
|
|
|
|
int bz = pos1.getZ();
|
|
|
|
int tx = pos2.getX();
|
|
|
|
int tz = pos2.getZ();
|
2015-09-13 06:04:31 +02:00
|
|
|
world.getEntities(new Predicate<Entity>() {
|
2015-08-11 20:04:17 +02:00
|
|
|
@Override
|
2016-03-28 23:31:40 +02:00
|
|
|
public boolean test(Entity entity) {
|
|
|
|
org.spongepowered.api.world.Location loc = entity.getLocation();
|
|
|
|
int x = loc.getBlockX();
|
2015-09-13 06:04:31 +02:00
|
|
|
if ((x >= bx) && (x <= tx)) {
|
2016-03-28 23:31:40 +02:00
|
|
|
int z = loc.getBlockZ();
|
2015-09-13 06:04:31 +02:00
|
|
|
if ((z >= bz) && (z <= tz)) {
|
2015-08-11 20:04:17 +02:00
|
|
|
entity.remove();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
});
|
2015-07-30 16:25:16 +02:00
|
|
|
}
|
2015-09-13 06:04:31 +02:00
|
|
|
|
2015-10-07 08:33:33 +02:00
|
|
|
@Override
|
|
|
|
public void swap(Location bot1, Location top1, Location bot2, Location top2, Runnable whenDone) {
|
|
|
|
// TODO swap region
|
2016-02-22 07:20:22 +01:00
|
|
|
throw new UnsupportedOperationException("NOT IMPLEMENTED YET");
|
2015-10-07 08:33:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void unloadChunk(String world, ChunkLoc loc, boolean save, boolean safe) {
|
2016-03-28 23:31:40 +02:00
|
|
|
World worldObj = SpongeUtil.getWorld(world);
|
|
|
|
Optional<Chunk> chunk = worldObj.getChunk(loc.x << 4, 0, loc.z << 4);
|
2015-10-07 08:33:33 +02:00
|
|
|
if (chunk.isPresent()) {
|
|
|
|
worldObj.unloadChunk(chunk.get());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-19 20:55:49 +01:00
|
|
|
@Override
|
|
|
|
public boolean regenerateRegion(Location pos1, Location pos2, boolean ignoreAugment, Runnable whenDone) {
|
|
|
|
// TODO Auto-generated method stub
|
|
|
|
throw new UnsupportedOperationException("NOT IMPLEMENTED YET");
|
|
|
|
}
|
|
|
|
|
2016-03-19 19:07:55 +01:00
|
|
|
}
|