Add new /forge gen command to generate large amounts of chunks.

Usage: /forge gen <position> <count> [dimension] [notifications]
Position is in Block Coords, and can be relative to the player. These will be converted to chunk coords for you.
Count is the number of chunks to load. This is not a radius, actual total number of chunks.
Dimension is optional, you can use this to pregen other worlds.
Notifications is the chunk interval to inform you of the generating progress. This is optional and will be 5% or 100 whichever is higher.

Added new config option to modify vanilla world gen to fix various cascading issues. MC-117810 MC-114332 and more.
This WILL change block placement from vanilla world gen. So this is a opt-in value. Do not report differences in worlds with this flag enabled.
This commit is contained in:
LexManos committed 2017-09-15 19:01:59 -07:00
1 parent 036191cd52
commit 0ad4218bc2
9 files changed
+349 -9

No files matched your search

@@ -0,0 +1,160 @@
/*
* Minecraft Forge
* Copyright (c) 2016.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation version 2.1
* of the License.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
package net.minecraftforge.server.command;
import java.util.ArrayDeque;
import java.util.Queue;
import net.minecraft.command.ICommandSender;
import net.minecraft.server.management.PlayerChunkMapEntry;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.text.TextComponentTranslation;
import net.minecraft.world.WorldServer;
import net.minecraft.world.chunk.Chunk;
import net.minecraftforge.common.DimensionManager;
import net.minecraftforge.common.ForgeChunkManager;
import net.minecraftforge.common.ForgeModContainer;
import net.minecraftforge.common.WorldWorkerManager.IWorker;
public class ChunkGenWorker implements IWorker
{
private final ICommandSender listener;
protected final BlockPos start;
protected final int total;
private final int dim;
private final Queue<BlockPos> queue;
private final int notificationFrequency;
private int lastNotification = 0;
private int genned = 0;
private int oldUnloadDelay = -1;
public ChunkGenWorker(ICommandSender listener, BlockPos start, int total, int dim, int interval)
{
this.listener = listener;
this.start = start;
this.total = total;
this.dim = dim;
this.queue = buildQueue();
this.notificationFrequency = interval != -1 ? interval : Math.max(total / 20, 100); //Every 5% or every 100, whichever is more.
}
protected Queue<BlockPos> buildQueue()
{
Queue<BlockPos> ret = new ArrayDeque<BlockPos>();
ret.add(start);
//This *should* spiral outwards, starting on right side, down, left, up, right, but hey we'll see!
int radius = 1;
while (ret.size() < total)
{
for (int q = -radius + 1; q <= radius && ret.size() < total; q++)
ret.add(start.add(radius, 0, q));
for (int q = radius - 1; q >= -radius && ret.size() < total; q--)
ret.add(start.add(q, 0, radius));
for (int q = radius - 1; q >= -radius && ret.size() < total; q--)
ret.add(start.add(-radius, 0, q));
for (int q = -radius + 1; q <= radius && ret.size() < total; q++)
ret.add(start.add(q, 0, -radius));
radius++;
}
return ret;
}
public TextComponentTranslation getStartMessage()
{
return new TextComponentTranslation("commands.forge.gen.start", total, start.getX(), start.getZ(), dim);
}
@Override
public boolean hasWork()
{
return queue.size() > 0;
}
@Override
public void work()
{
BlockPos next = queue.poll();
if (next != null)
{
WorldServer world = DimensionManager.getWorld(dim);
if (world == null)
{
DimensionManager.initDimension(dim);
world = DimensionManager.getWorld(dim);
if (world == null)
{
listener.sendMessage(new TextComponentTranslation("commands.forge.gen.dim_fail", dim));
queue.clear();
return;
}
}
//While we work we don't want to cause world load spam so pause unloading worlds.
if (oldUnloadDelay == -1)
{
oldUnloadDelay = ForgeModContainer.dimensionUnloadQueueDelay;
ForgeModContainer.dimensionUnloadQueueDelay = Integer.MAX_VALUE;
}
if (++lastNotification >= notificationFrequency)
{
listener.sendMessage(new TextComponentTranslation("commands.forge.gen.progress", total - queue.size(), total));
lastNotification = 0;
}
int x = next.getX();
int z = next.getZ();
Chunk target = world.getChunkFromChunkCoords(x, z);
Chunk[] chunks = { target };
if (!target.isTerrainPopulated())
{
// In order for a chunk to populate, The chunks around its bottom right corner need to be loaded.
// So lets load those chunks, but this needs to be done in a certain order to make this trigger.
// So this does load more chunks then it should, and is a hack, but lets go!.
chunks = new Chunk[] {
target,
world.getChunkFromChunkCoords(x + 1, z),
world.getChunkFromChunkCoords(x + 1, z + 1),
world.getChunkFromChunkCoords(x, z + 1),
};
genned++;
}
for (Chunk chunk : chunks) //Now lets unload them. Note: Saving is done off thread so there may be cache hits, but this should still unload everything.
{
PlayerChunkMapEntry watchers = world.getPlayerChunkMap().getEntry(chunk.x, chunk.z);
if (watchers == null) //If there are no players watching this, this will be null, so we can unload.
world.getChunkProvider().queueUnload(chunk);
}
}
if (queue.size() == 0)
{
listener.sendMessage(new TextComponentTranslation("commands.forge.gen.complete", genned, total, dim));
ForgeModContainer.dimensionUnloadQueueDelay = oldUnloadDelay;
}
}
}
@@ -20,14 +20,21 @@
package net.minecraftforge.server.command;
import java.text.DecimalFormat;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import javax.annotation.Nullable;
import net.minecraft.command.CommandBase;
import net.minecraft.command.CommandException;
import net.minecraft.command.ICommandSender;
import net.minecraft.command.WrongUsageException;
import net.minecraft.server.MinecraftServer;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.text.TextComponentTranslation;
import net.minecraftforge.common.DimensionManager;
import net.minecraftforge.common.WorldWorkerManager;
import net.minecraftforge.server.ForgeTimeTracker;
public class ForgeCommand extends CommandBase {
@@ -64,22 +71,47 @@ public class ForgeCommand extends CommandBase {
}
else if ("tps".equals(args[0]))
{
displayTPS(server, sender,args);
displayTPS(server, sender, args);
}
else if ("tpslog".equals(args[0]))
/*else if ("tpslog".equals(args[0]))
{
doTPSLog(server, sender,args);
doTPSLog(server, sender, args);
}
*/
else if ("track".equals(args[0]))
{
handleTracking(server, sender, args);
}
else if ("gen".equals(args[0]))
{
handleGen(server, sender, args);
}
else
{
throw new WrongUsageException("commands.forge.usage");
}
}
@Override
public List<String> getTabCompletions(MinecraftServer server, ICommandSender sender, String[] args, @Nullable BlockPos targetPos)
{
if (args.length <= 1)
return getListOfStringsMatchingLastWord(args, new String[] {"help", "tps", "track", "gen"});
switch (args[0].toLowerCase(Locale.ENGLISH))
{
case "gen":
if (args.length > 1 && args.length <= 4)
return getTabCompletionCoordinate(args, 1, targetPos);
if (args.length == 5) //Chunk Count? No completion
return Collections.emptyList();
if (args.length == 6) // Dimension, Add support for names? Get list of ids? Meh
return Collections.emptyList();
break;
}
return Collections.emptyList();
}
private void handleTracking(MinecraftServer server, ICommandSender sender, String[] args) throws CommandException
{
if (args.length != 3)
@@ -140,6 +172,23 @@ public class ForgeCommand extends CommandBase {
}
}
private void handleGen(MinecraftServer server, ICommandSender sender, String[] args) throws CommandException
{
// gen x y z chunkCount [dim] [shape?]
if (args.length < 5)
throw new WrongUsageException("commands.forge.gen.usage");
BlockPos blockpos = parseBlockPos(sender, args, 1, false);
int count = parseInt(args[4], 10);
int dim = args.length >= 6 ? parseInt(args[5]) : sender.getEntityWorld().provider.getDimension();
int interval = args.length >= 7 ? parseInt(args[6]) : -1;
BlockPos chunkpos = new BlockPos(blockpos.getX() >> 4, 0, blockpos.getZ() >> 4);
ChunkGenWorker worker = new ChunkGenWorker(sender, chunkpos, count, dim, interval);
sender.sendMessage(worker.getStartMessage());
WorldWorkerManager.addWorker(worker);
}
private static long mean(long[] values)
{
long sum = 0l;