Chunk regeneration

This commit is contained in:
Jesse Boyd 2016-02-24 04:04:23 +11:00
parent 882394987b
commit 8a2c44759f

View File

@ -1,10 +1,15 @@
package com.plotsquared.sponge.util; package com.plotsquared.sponge.util;
import com.intellectualcrafters.plot.PS;
import com.intellectualcrafters.plot.object.ChunkLoc; import com.intellectualcrafters.plot.object.ChunkLoc;
import com.intellectualcrafters.plot.object.Location; import com.intellectualcrafters.plot.object.Location;
import com.intellectualcrafters.plot.object.Plot; import com.intellectualcrafters.plot.object.Plot;
import com.intellectualcrafters.plot.util.ChunkManager; import com.intellectualcrafters.plot.util.ChunkManager;
import com.intellectualcrafters.plot.util.ReflectionUtils;
import com.intellectualcrafters.plot.util.TaskManager; import com.intellectualcrafters.plot.util.TaskManager;
import net.minecraft.world.ChunkCoordIntPair;
import net.minecraft.world.chunk.IChunkProvider;
import net.minecraft.world.gen.ChunkProviderServer;
import org.spongepowered.api.entity.Entity; import org.spongepowered.api.entity.Entity;
import org.spongepowered.api.entity.living.Living; import org.spongepowered.api.entity.living.Living;
import org.spongepowered.api.entity.living.animal.Animal; import org.spongepowered.api.entity.living.animal.Animal;
@ -22,9 +27,7 @@ public class SpongeChunkManager extends ChunkManager {
public int[] countEntities(final Plot plot) { public int[] countEntities(final Plot plot) {
final Location pos1 = plot.getBottomAbs(); final Location pos1 = plot.getBottomAbs();
final Location pos2 = plot.getTopAbs(); final Location pos2 = plot.getTopAbs();
final World world = SpongeUtil.getWorld(pos1.getWorld());
final String worldname = pos1.getWorld();
final World world = SpongeUtil.getWorld(worldname);
final int bx = pos1.getX(); final int bx = pos1.getX();
final int bz = pos1.getZ(); final int bz = pos1.getZ();
final int tx = pos2.getX(); final int tx = pos2.getX();
@ -69,12 +72,37 @@ public class SpongeChunkManager extends ChunkManager {
@Override @Override
public void regenerateChunk(final String world, final ChunkLoc loc) { public void regenerateChunk(final String world, final ChunkLoc loc) {
final World worldObj = SpongeUtil.getWorld(world); final World spongeWorld = SpongeUtil.getWorld(world);
final Optional<Chunk> chunkOpt = worldObj.getChunk(loc.x << 4, 0, loc.z << 4); final net.minecraft.world.World nmsWorld = (net.minecraft.world.World) spongeWorld;
final Optional<Chunk> chunkOpt = spongeWorld.getChunk(loc.x, 0, loc.z);
if (chunkOpt.isPresent()) { if (chunkOpt.isPresent()) {
Chunk chunk = chunkOpt.get(); try {
// TODO FIXME Chunk spongeChunk = chunkOpt.get();
throw new UnsupportedOperationException("NOT IMPLEMENTED YET"); IChunkProvider provider = nmsWorld.getChunkProvider();
if (!(provider instanceof ChunkProviderServer)) {
PS.debug("Not valid world generator for: " + world);
return;
}
ChunkProviderServer chunkProvider = (ChunkProviderServer) provider;
long pos = loc.x & 4294967295L | (loc.z & 4294967295L) << 32;
net.minecraft.world.chunk.Chunk mcChunk = (net.minecraft.world.chunk.Chunk) spongeChunk;
if (provider.chunkExists(loc.x, loc.z)) {
mcChunk = chunkProvider.loadChunk(loc.x, loc.z);
mcChunk.onChunkUnload();
}
Set<Long> set = (Set<Long>) chunkProvider.getClass().getDeclaredField("droppedChunksSet").get(chunkProvider);
set.remove(pos);
chunkProvider.id2ChunkMap.remove(pos);
mcChunk = provider.provideChunk(loc.x, loc.z);
chunkProvider.id2ChunkMap.add(pos, mcChunk);
chunkProvider.loadedChunks.add(mcChunk);
if (mcChunk != null) {
mcChunk.onChunkLoad();
mcChunk.populateChunk(chunkProvider, chunkProvider, loc.x, loc.z);
}
} catch (Throwable e){
e.printStackTrace();
}
} }
} }