下面列出了怎么用net.minecraft.world.gen.ChunkProviderServer的API类实例代码及写法,或者点击链接到github查看源代码。
private void injectChunkIntoWorld(int x, int z, Chunk chunk) {
ChunkProviderServer provider = (ChunkProviderServer) this.getWorldObj().getChunkProvider();
chunk.dirty = true;
// claimedChunks[x - getgetOwnedChunks().getMinX()][z - getgetOwnedChunks().getMinZ()] = chunk;
loadedChunksMap.put(ChunkPos.asLong(x, z), chunk);
provider.loadedChunks.put(ChunkPos.asLong(x, z), chunk);
chunk.onLoad();
PlayerChunkMap map = ((WorldServer) this.getWorldObj()).getPlayerChunkMap();
PlayerChunkMapEntry entry = new PlayerChunkMapEntry(map, x, z);
long i = PlayerChunkMap.getIndex(x, z);
// TODO: In future we need to do better to account for concurrency.
map.entryMap.put(i, entry);
map.entries.add(entry);
entry.sentToPlayers = true;
// TODO: In future we need to do better to account for concurrency.
entry.players = Collections.unmodifiableList(shipHolder.getWatchingPlayers());
}
private static void saveChunkData(ChunkProviderServer chunkProviderServer, Chunk chunkIn)
{
try
{
chunkIn.setLastSaveTime(chunkIn.getWorld().getTotalWorldTime());
chunkProviderServer.chunkLoader.saveChunk(chunkIn.getWorld(), chunkIn);
}
catch (IOException ioexception)
{
//LOGGER.error((String)"Couldn\'t save chunk", (Throwable)ioexception);
}
catch (MinecraftException minecraftexception)
{
//LOGGER.error((String)"Couldn\'t save chunk; already in use by another instance of Minecraft?", (Throwable)minecraftexception);
}
try
{
chunkProviderServer.chunkLoader.saveExtraChunkData(chunkIn.getWorld(), chunkIn);
}
catch (Exception exception)
{
//LOGGER.error((String)"Couldn\'t save entities", (Throwable)exception);
}
}
private static void logLoadOnRequest(ChunkProviderServer provider, int x, int z)
{
long currentTick = MinecraftServer.getServer().getTickCounter();
long lastAccessed = provider.lastAccessed(x, z);
long diff = currentTick - lastAccessed;
logInfo(" Last accessed: {0, number} Current Tick: {1, number} [{2, number}]", lastAccessed, currentTick, diff);
logInfo(" Finding Spawn Point: {0}", provider.worldObj.findingSpawnPoint);
logInfo(" Load chunk on request: {0}", provider.loadChunkOnProvideRequest);
logInfo(" Calling Forge Tick: {0}", MinecraftServer.callingForgeTick);
logInfo(" Load chunk on forge tick: {0}", MinecraftServer.cauldronConfig.loadChunkOnForgeTick.getValue());
long providerTickDiff = currentTick - provider.initialTick;
if (providerTickDiff <= 100)
{
logInfo(" Current Tick - Initial Tick: {0, number}", providerTickDiff);
}
}
@Override
public Coord findNearestStructure(VanillaStructure type, Coord pos) {
ChunkProviderServer chunkProvider = ((WorldServer)world).getChunkProvider();
String structureName = VanillaStructure.getName(type);
BlockPos structurebp = null;
try{
structurebp = chunkProvider.getNearestStructurePos(world, structureName, pos.getBlockPos(), false);
} catch(NullPointerException e){
// happens for some reason if structure type is disabled in Chunk Generator Settings
}
if(structurebp == null) return null;
return new Coord(structurebp);
}
private ModDimensionDef makeModDimDef() {
return getModContainers().stream()
.flatMap(modContainer -> modContainer.getDimensionList().stream())
.filter(modDimensionDef -> modDimensionDef.getChunkProviderName()
.equals(((ChunkProviderServer) this.getBaseMetaTileEntity().getWorld().getChunkProvider()).currentChunkProvider.getClass().getName()))
.findFirst().orElse(null);
}
public void processChunkGC() {
chunkGCTickCount++;
if (chunkLoadCount >= server.chunkGCLoadThresh && server.chunkGCLoadThresh > 0) {
chunkLoadCount = 0;
} else if (chunkGCTickCount >= server.chunkGCPeriod && server.chunkGCPeriod > 0) {
chunkGCTickCount = 0;
} else {
return;
}
ChunkProviderServer cps = world.getChunkProvider();
for (net.minecraft.world.chunk.Chunk chunk : cps.id2ChunkMap.values()) {
// If in use, skip it
if (isChunkInUse(chunk.x, chunk.z)) {
continue;
}
// Already unloading?
if (cps.droppedChunksSet.contains(ChunkPos.asLong(chunk.x, chunk.z))) {
continue;
}
// Add unload request
cps.queueUnload(chunk);
}
}
public QueuedChunk(int x, int z, AnvilChunkLoader loader, World world, ChunkProviderServer provider) {
this.x = x;
this.z = z;
this.loader = loader;
this.world = world;
this.provider = provider;
}
private boolean canParticlePassThrough(World world, BlockPos pos) {
ChunkProviderServer serverProvider = (ChunkProviderServer) world.getChunkProvider();
// If the chunk isn't loaded, then no problem ignore it.
long chunkKey = ChunkPos.asLong(pos.getX() >> 4, pos.getZ() >> 4);
if (!serverProvider.loadedChunks.containsKey(chunkKey)) {
this.isParticleDead = true;
return true;
}
IBlockState collisionState = serverProvider.loadedChunks.get(chunkKey)
.getBlockState(pos);
// Otherwise we have to bypass the world blockstate get because sponge has some protection on it.
// System.out.println("oof");
return collisionState.getBlock().isPassable(world, pos);
}
public void unloadShipChunksFromWorld() {
ChunkProviderServer provider = (ChunkProviderServer) world().getChunkProvider();
for (int x = getOwnedChunks().minX(); x <= getOwnedChunks().maxX(); x++) {
for (int z = getOwnedChunks().minZ(); z <= getOwnedChunks().maxZ(); z++) {
provider.queueUnload(claimedChunkCache.getChunkAt(x, z));
}
}
}
public void injectChunkIntoWorld(Chunk chunk, int x, int z, boolean putInId2ChunkMap) {
VSChunkClaim chunkClaim = parent.getOwnedChunks();
chunk.generateSkylightMap();
chunk.checkLight();
// Make sure this chunk knows we own it.
((IPhysicsChunk) chunk).setParentPhysicsObject(Optional.of(this.parent));
ChunkProviderServer provider = (ChunkProviderServer) world.getChunkProvider();
chunk.dirty = true;
claimedChunks[x - chunkClaim.minX()][z - chunkClaim.minZ()] = chunk;
if (putInId2ChunkMap) {
provider.loadedChunks.put(ChunkPos.asLong(x, z), chunk);
}
chunk.onLoad();
// We need to set these otherwise certain events like Sponge's PhaseTracker will refuse to work properly with ships!
chunk.setTerrainPopulated(true);
chunk.setLightPopulated(true);
// Inject the entry into the player chunk map.
// Sanity check first
if (!((WorldServer) world).isCallingFromMinecraftThread()) {
throw new IllegalThreadStateException("We cannot call this crap from another thread!");
}
PlayerChunkMap map = ((WorldServer) world).getPlayerChunkMap();
PlayerChunkMapEntry entry = map.getOrCreateEntry(x, z);
entry.sentToPlayers = true;
entry.players = parent.getWatchingPlayers();
}
private static boolean unloadChunk(net.minecraft.world.chunk.Chunk chunk) {
net.minecraft.world.World mcWorld = chunk.getWorld();
ChunkProviderServer chunkProviderServer = (ChunkProviderServer) chunk.getWorld().getChunkProvider();
boolean saveChunk = false;
if (chunk.needsSaving(true)) {
saveChunk = true;
}
for (ClassInheritanceMultiMap<Entity> classinheritancemultimap : chunk.getEntityLists())
{
chunk.getWorld().unloadEntities(classinheritancemultimap);
}
if (saveChunk) {
saveChunkData(chunkProviderServer, chunk);
}
chunkProviderServer.id2ChunkMap.remove(ChunkPos.asLong(chunk.x, chunk.z));
((ChunkBridge) chunk).bridge$setScheduledForUnload(-1);
org.spongepowered.api.world.Chunk spongeChunk = (org.spongepowered.api.world.Chunk) chunk;
for (Direction direction : CARDINAL_SET) {
Vector3i neighborPosition = spongeChunk.getPosition().add(direction.asBlockOffset());
ChunkProviderBridge spongeChunkProvider = (ChunkProviderBridge) mcWorld.getChunkProvider();
net.minecraft.world.chunk.Chunk neighbor = spongeChunkProvider.bridge$getLoadedChunkWithoutMarkingActive(neighborPosition.getX(),
neighborPosition.getZ());
if (neighbor != null) {
int neighborIndex = directionToIndex(direction);
int oppositeNeighborIndex = directionToIndex(direction.getOpposite());
((ChunkBridge) spongeChunk).bridge$setNeighborChunk(neighborIndex, null);
((ChunkBridge) neighbor).bridge$setNeighborChunk(oppositeNeighborIndex, null);
}
}
return true;
}
public static boolean regenerateChunk(Chunk chunk) {
boolean unload = unloadChunk(chunk);
ChunkProviderServer chunkProviderServer = (ChunkProviderServer) chunk.getWorld().getChunkProvider();
Chunk newChunk = chunkProviderServer.chunkGenerator.generateChunk(chunk.x, chunk.z);
PlayerChunkMapEntry playerChunk = ((WorldServer) chunk.getWorld()).getPlayerChunkMap().getEntry(chunk.x, chunk.z);
if (playerChunk != null) {
playerChunk.chunk = newChunk;
}
chunkLoadPostProcess(newChunk);
refreshChunk(newChunk);
return newChunk != null;
}
public static void logChunkLoad(ChunkProviderServer provider, String msg, int x, int z, boolean logLoadOnRequest)
{
if (MinecraftServer.cauldronConfig.chunkLoadLogging.getValue())
{
logInfo("{0} Chunk At [{1}] ({2}, {3})", msg, provider.worldObj.provider.dimensionId, x, z);
if (logLoadOnRequest)
{
logLoadOnRequest(provider, x, z);
}
logStack();
}
}
public static void logChunkUnload(ChunkProviderServer provider, int x, int z, String msg)
{
if (MinecraftServer.cauldronConfig.chunkUnloadLogging.getValue())
{
logInfo("{0} [{1}] ({2}, {3})", msg, provider.worldObj.provider.dimensionId, x, z);
long currentTick = MinecraftServer.getServer().getTickCounter();
long lastAccessed = provider.lastAccessed(x, z);
long diff = currentTick - lastAccessed;
logInfo(" Last accessed: {0, number} Current Tick: {1, number} [{2, number}]", lastAccessed, currentTick, diff);
}
}
@Override
public Chunk loadChunk(World world, int x, int z, boolean generate) {
ChunkProviderServer provider = (ChunkProviderServer) world.getChunkProvider();
if (generate) {
return provider.provideChunk(x, z);
} else {
return provider.loadChunk(x, z);
}
}
@Override
public Chunk loadChunk(World world, int x, int z, boolean generate) {
ChunkProviderServer provider = (ChunkProviderServer) world.getChunkProvider();
if (generate) {
return provider.provideChunk(x, z);
} else {
return provider.loadChunk(x, z);
}
}
@Override
public Chunk loadChunk(World world, int x, int z, boolean generate) {
ChunkProviderServer provider = (ChunkProviderServer) world.getChunkProvider();
if (generate) {
return provider.originalLoadChunk(x, z);
} else {
return provider.loadChunk(x, z);
}
}
@Override
public ExtendedBlockStorage[] getCachedSections(World world, int cx, int cz) {
Chunk chunk = ((ChunkProviderServer)world.getChunkProvider()).id2ChunkMap.getValueByKey(ChunkCoordIntPair.chunkXZ2Int(cx, cz));
if (chunk != null) {
return chunk.getBlockStorageArray();
}
return null;
}
@Override
public boolean regenerateChunk(World world, int x, int z, BaseBiome biome, Long seed) {
IChunkProvider provider = world.getChunkProvider();
if (!(provider instanceof ChunkProviderServer)) {
return false;
}
ChunkProviderServer chunkServer = (ChunkProviderServer) provider;
IChunkProvider chunkProvider = chunkServer.serverChunkGenerator;
long pos = ChunkCoordIntPair.chunkXZ2Int(x, z);
Chunk mcChunk;
if (chunkServer.chunkExists(x, z)) {
mcChunk = chunkServer.loadChunk(x, z);
mcChunk.onChunkUnload();
}
try {
Field droppedChunksSetField = chunkServer.getClass().getDeclaredField("field_73248_b");
droppedChunksSetField.setAccessible(true);
Set droppedChunksSet = (Set) droppedChunksSetField.get(chunkServer);
droppedChunksSet.remove(pos);
} catch (Throwable e) {
MainUtil.handleError(e);
}
chunkServer.id2ChunkMap.remove(pos);
mcChunk = chunkProvider.provideChunk(x, z);
chunkServer.id2ChunkMap.add(pos, mcChunk);
chunkServer.loadedChunks.add(mcChunk);
if (mcChunk != null) {
mcChunk.onChunkLoad();
mcChunk.populateChunk(chunkProvider, chunkProvider, x, z);
}
return true;
}
@Override
public Chunk loadChunk(World world, int x, int z, boolean generate) {
ChunkProviderServer provider = (ChunkProviderServer) world.getChunkProvider();
if (generate) {
return provider.provideChunk(x, z);
} else {
return provider.loadChunk(x, z);
}
}
@Override
public Chunk loadChunk(World world, int x, int z, boolean generate) {
ChunkProviderServer provider = (ChunkProviderServer) world.getChunkProvider();
if (generate) {
return provider.provideChunk(x, z);
} else {
return provider.loadChunk(x, z);
}
}
@Override
public Chunk loadChunk(World world, int x, int z, boolean generate) {
ChunkProviderServer provider = (ChunkProviderServer) world.getChunkProvider();
if (generate) {
return provider.originalLoadChunk(x, z);
} else {
return provider.loadChunk(x, z);
}
}
@Override
public ExtendedBlockStorage[] getCachedSections(World world, int cx, int cz) {
Chunk chunk = (Chunk) ((ChunkProviderServer)world.getChunkProvider()).loadedChunkHashMap.getValueByKey(ChunkCoordIntPair.chunkXZ2Int(cx, cz));
if (chunk != null) {
return chunk.getBlockStorageArray();
}
return null;
}
@Override
public Chunk loadChunk(World world, int x, int z, boolean generate) {
ChunkProviderServer provider = (ChunkProviderServer) world.getChunkProvider();
if (generate) {
return provider.provideChunk(x, z);
} else {
return provider.loadChunk(x, z);
}
}
@Override
public Chunk loadChunk(World world, int x, int z, boolean generate) {
ChunkProviderServer provider = (ChunkProviderServer) world.getChunkProvider();
if (generate) {
return provider.provideChunk(x, z);
} else {
return provider.loadChunk(x, z);
}
}
private void runRetrogen(WorldServer world, ChunkPos chunkCoords, String retroClass)
{
long worldSeed = world.getSeed();
Random fmlRandom = new Random(worldSeed);
long xSeed = fmlRandom.nextLong() >> 2 + 1L;
long zSeed = fmlRandom.nextLong() >> 2 + 1L;
long chunkSeed = (xSeed * chunkCoords.chunkXPos + zSeed * chunkCoords.chunkZPos) ^ worldSeed;
fmlRandom.setSeed(chunkSeed);
ChunkProviderServer providerServer = world.getChunkProvider();
IChunkGenerator generator = ObfuscationReflectionHelper.getPrivateValue(ChunkProviderServer.class, providerServer, "field_186029_c", "chunkGenerator");
delegates.get(retroClass).delegate.generate(fmlRandom, chunkCoords.chunkXPos, chunkCoords.chunkZPos, world, generator, providerServer);
FMLLog.fine("Retrogenerated chunk for %s", retroClass);
completeRetrogen(chunkCoords, world, retroClass);
}
private void visitStructures(WorldServer world, IStructureVisitor visitor) {
ChunkProviderServer provider = world.getChunkProvider();
IChunkGenerator inner = provider.chunkGenerator;
if (inner != null) {
for (IStructureGenProvider<?> p : providers)
tryVisit(visitor, inner, p);
}
}
public static Chunk syncChunkLoad(World world, AnvilChunkLoader loader, ChunkProviderServer provider, int x, int z) {
return instance.getSkipQueue(new QueuedChunk(x, z, loader, world, provider));
}
public static void queueChunkLoad(World world, AnvilChunkLoader loader, ChunkProviderServer provider, int x, int z, Runnable runnable) {
instance.add(new QueuedChunk(x, z, loader, world, provider), runnable);
}
public static boolean createFillerChunk(GPPlayerData playerData, WorldServer world, int chunkX, int chunkZ) {
ChunkProviderServer chunkProviderServer = world.getChunkProvider();
Chunk chunk = ((ChunkProviderBridge) chunkProviderServer).bridge$getLoadedChunkWithoutMarkingActive(chunkX, chunkZ);
if (chunk == null) {
return false;
}
unloadChunk(chunk);
org.spongepowered.api.world.Chunk fillerChunk = (org.spongepowered.api.world.Chunk) chunkProviderServer.chunkGenerator.generateChunk(chunk.x, chunk.z);
int maxBuildHeight = fillerChunk.getWorld().getDimension().getBuildHeight() - 1;
BlockSnapshot[][][] snapshots = new BlockSnapshot[18][maxBuildHeight][18];
BlockSnapshot startBlock = fillerChunk.createSnapshot(0, 0, 0);
Location<World> startLocation =
new Location<World>(fillerChunk.getWorld(), startBlock.getPosition().getX() - 1, 0, startBlock.getPosition().getZ() - 1);
for (int x = 0; x < snapshots.length; x++) {
for (int z = 0; z < snapshots[0][0].length; z++) {
for (int y = 0; y < snapshots[0].length; y++) {
snapshots[x][y][z] = fillerChunk.getWorld()
.createSnapshot(startLocation.getBlockX() + x, startLocation.getBlockY() + y, startLocation.getBlockZ() + z);
}
}
}
fillerChunk = null;
playerData.fillerBlocks = snapshots;
if (chunk != null) {
world.getChunkProvider().id2ChunkMap.put(ChunkPos.asLong(chunk.x, chunk.z), chunk);
org.spongepowered.api.world.Chunk spongeChunk = (org.spongepowered.api.world.Chunk) chunk;
for (Direction direction : CARDINAL_SET) {
Vector3i neighborPosition = spongeChunk.getPosition().add(direction.asBlockOffset());
ChunkProviderBridge spongeChunkProvider = (ChunkProviderBridge) world.getChunkProvider();
net.minecraft.world.chunk.Chunk neighbor = spongeChunkProvider.bridge$getLoadedChunkWithoutMarkingActive(neighborPosition.getX(),
neighborPosition.getZ());
if (neighbor != null) {
int neighborIndex = directionToIndex(direction);
int oppositeNeighborIndex = directionToIndex(direction.getOpposite());
((ChunkBridge) spongeChunk).bridge$setNeighborChunk(neighborIndex, neighbor);
((ChunkBridge) neighbor).bridge$setNeighborChunk(oppositeNeighborIndex, (net.minecraft.world.chunk.Chunk)(Object) chunk);
}
}
}
return true;
}