Plex-FAWE/worldedit-core/src/main/java/com/sk89q/worldedit/command/GenerationCommands.java

469 lines
22 KiB
Java
Raw Normal View History

/*
2014-04-04 22:03:18 +00:00
* WorldEdit, a Minecraft world manipulation toolkit
* Copyright (C) sk89q <http://www.sk89q.com>
* Copyright (C) WorldEdit team and contributors
*
2014-04-04 22:03:18 +00:00
* This program 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, either version 3 of the License, or
* (at your option) any later version.
*
2014-04-04 22:03:18 +00:00
* This program 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.
*
2014-04-04 22:03:18 +00:00
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
2014-04-04 22:03:18 +00:00
*/
package com.sk89q.worldedit.command;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.sk89q.worldedit.command.util.Logging.LogMode.ALL;
import static com.sk89q.worldedit.command.util.Logging.LogMode.PLACEMENT;
import static com.sk89q.worldedit.command.util.Logging.LogMode.POSITION;
import com.boydti.fawe.Fawe;
import com.boydti.fawe.config.BBC;
import com.boydti.fawe.object.FawePlayer;
import com.boydti.fawe.util.MainUtil;
import com.boydti.fawe.util.MathMan;
import com.boydti.fawe.util.TextureUtil;
import com.boydti.fawe.util.image.ImageUtil;
2018-12-23 16:19:33 +00:00
import com.sk89q.worldedit.EditSession;
import com.sk89q.worldedit.LocalSession;
import com.sk89q.worldedit.WorldEdit;
import com.sk89q.worldedit.WorldEditException;
import com.sk89q.worldedit.command.util.CommandPermissions;
import com.sk89q.worldedit.command.util.CommandPermissionsConditionGenerator;
import com.sk89q.worldedit.command.util.Logging;
import com.sk89q.worldedit.entity.Player;
import com.sk89q.worldedit.function.generator.CavesGen;
import com.sk89q.worldedit.function.mask.Mask;
import com.sk89q.worldedit.function.operation.Operations;
import com.sk89q.worldedit.function.pattern.Pattern;
import com.sk89q.worldedit.function.visitor.RegionVisitor;
import com.sk89q.worldedit.internal.annotation.Range;
import com.sk89q.worldedit.internal.annotation.Selection;
import com.sk89q.worldedit.internal.expression.ExpressionException;
2019-01-09 07:13:44 +00:00
import com.sk89q.worldedit.math.BlockVector2;
2018-12-23 16:19:33 +00:00
import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldedit.math.Vector3;
import com.sk89q.worldedit.regions.CuboidRegion;
import com.sk89q.worldedit.regions.Region;
import com.sk89q.worldedit.util.TreeGenerator.TreeType;
2019-02-16 07:27:00 +00:00
import com.sk89q.worldedit.world.biome.BiomeType;
2019-04-03 13:25:16 +00:00
import com.sk89q.worldedit.world.block.BlockType;
import java.awt.RenderingHints;
2019-04-03 13:25:16 +00:00
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import java.util.List;
import org.enginehub.piston.annotation.Command;
import org.enginehub.piston.annotation.CommandContainer;
import org.enginehub.piston.annotation.param.Arg;
import org.enginehub.piston.annotation.param.Switch;
import org.enginehub.piston.inject.InjectedValueAccess;
2019-04-03 13:25:16 +00:00
/**
* Commands for the generation of shapes and other objects.
*/
@CommandContainer(superTypes = CommandPermissionsConditionGenerator.Registration.class)
2019-07-22 06:57:12 +00:00
//@Command(aliases = {}, desc = "Create structures and features: [More Info](https://goo.gl/KuLFRW)")
public class GenerationCommands extends MethodCommands {
private final WorldEdit worldEdit;
/**
* Create a new instance.
*
* @param worldEdit reference to WorldEdit
*/
public GenerationCommands(WorldEdit worldEdit) {
checkNotNull(worldEdit);
this.worldEdit = worldEdit;
}
@Command(
name = "/caves",
desc = "Generates a cave network"
)
@CommandPermissions("worldedit.generation.caves")
@Logging(PLACEMENT)
public void caves(FawePlayer fp, LocalSession session, EditSession editSession, @Selection Region region,
@Arg(desc = "TODO", def = "8") int size,
@Arg(desc = "TODO", def = "40") int frequency,
@Arg(desc = "TODO", def = "7") int rarity,
@Arg(desc = "TODO", def = "8") int minY,
@Arg(desc = "TODO", def = "127") int maxY,
@Arg(desc = "TODO", def = "1") int systemFrequency,
@Arg(desc = "TODO", def = "25") int individualRarity,
@Arg(desc = "TODO", def = "0") int pocketChance,
@Arg(desc = "TODO", def = "0") int pocketMin,
@Arg(desc = "TODO", def = "3") int pocketMax, InjectedValueAccess context) throws WorldEditException {
2018-09-07 15:09:31 +00:00
fp.checkConfirmationRegion(() -> {
CavesGen gen = new CavesGen(size, frequency, rarity, minY, maxY, systemFrequency, individualRarity, pocketChance, pocketMin, pocketMax);
editSession.generate(region, gen);
BBC.VISITOR_BLOCK.send(fp, editSession.getBlockChangeCount());
2018-09-07 23:10:36 +00:00
}, getArguments(context), region, context);
}
@Command(
name = "/ores",
desc = "Generates ores"
)
@CommandPermissions("worldedit.generation.ore")
@Logging(PLACEMENT)
public void ores(FawePlayer player, LocalSession session, EditSession editSession, @Selection Region region, Mask mask, InjectedValueAccess context) throws WorldEditException {
2018-09-07 15:09:31 +00:00
player.checkConfirmationRegion(() -> {
editSession.addOres(region, mask);
BBC.VISITOR_BLOCK.send(player, editSession.getBlockChangeCount());
2018-09-07 23:10:36 +00:00
}, getArguments(context), region, context);
}
@Command(
name = "/img",
desc = "Generate an image"
)
@CommandPermissions("worldedit.generation.image")
@Logging(PLACEMENT)
2019-07-17 06:11:55 +00:00
public void image(Player player, LocalSession session, EditSession editSession, String arg, @Arg(name = "randomize", desc = "boolean", def = "true") boolean randomize,
@Arg(desc = "TODO", def = "100") int threshold, @Arg(name = "dimensions", desc = "BlockVector2", def = "") BlockVector2 dimensions) throws WorldEditException, IOException {
TextureUtil tu = Fawe.get().getCachedTextureUtil(randomize, 0, threshold);
URL url = new URL(arg);
if (!url.getHost().equalsIgnoreCase("i.imgur.com") && !url.getHost().equalsIgnoreCase("empcraft.com")) {
throw new IOException("Only i.imgur.com or empcraft.com/ui links are allowed!");
}
BufferedImage image = MainUtil.readImage(url);
if (dimensions != null) {
image = ImageUtil.getScaledInstance(image, dimensions.getBlockX(), dimensions.getBlockZ(), RenderingHints.VALUE_INTERPOLATION_BILINEAR, false);
}
2019-03-31 16:09:20 +00:00
BlockVector3 pos1 = player.getLocation().toBlockPoint();
2019-01-09 07:13:44 +00:00
BlockVector3 pos2 = pos1.add(image.getWidth() - 1, 0, image.getHeight() - 1);
CuboidRegion region = new CuboidRegion(pos1, pos2);
int[] count = new int[1];
final BufferedImage finalImage = image;
RegionVisitor visitor = new RegionVisitor(region, pos -> {
try {
int x = pos.getBlockX() - pos1.getBlockX();
int z = pos.getBlockZ() - pos1.getBlockZ();
int color = finalImage.getRGB(x, z);
BlockType block = tu.getNearestBlock(color);
count[0]++;
if (block != null) return editSession.setBlock(pos, block.getDefaultState());
return false;
} catch (Throwable e) {
e.printStackTrace();
}
return false;
2019-07-22 06:57:12 +00:00
});
Operations.completeBlindly(visitor);
BBC.VISITOR_BLOCK.send(player, editSession.getBlockChangeCount());
}
@Command(
name = "/ore",
desc = "Generates ores"
)
@CommandPermissions("worldedit.generation.ore")
@Logging(PLACEMENT)
public void ore(FawePlayer player, LocalSession session, EditSession editSession, @Selection Region region, Mask mask, Pattern material, @Range(min = 0) int size, int freq, @Range(min = 0, max = 100) int rarity, @Range(min = 0, max = 255) int minY, @Range(min = 0, max = 255) int maxY, InjectedValueAccess context) throws WorldEditException {
2018-09-07 15:09:31 +00:00
player.checkConfirmationRegion(() -> {
editSession.addOre(region, mask, material, size, freq, rarity, minY, maxY);
BBC.VISITOR_BLOCK.send(player, editSession.getBlockChangeCount());
2018-09-07 23:10:36 +00:00
}, getArguments(context), region, context);
}
@Command(
name = "/hcyl",
desc = "Generates a hollow cylinder."
)
@CommandPermissions("worldedit.generation.cylinder")
@Logging(PLACEMENT)
public void hcyl(FawePlayer fp, Player player, LocalSession session, EditSession editSession,
@Arg(desc = "The pattern of blocks to generate")
Pattern pattern,
BlockVector2 radius,
@Arg(desc = "The height of the cylinder", def = "1")
int height,
@Range(min = 1) @Arg(name = "thickness", desc = "double", def = "1") double thickness, InjectedValueAccess context) throws WorldEditException {
double max = MathMan.max(radius.getBlockX(), radius.getBlockZ());
worldEdit.checkMaxRadius(max);
2019-01-09 07:13:44 +00:00
BlockVector3 pos = session.getPlacementPosition(player);
2018-09-07 15:09:31 +00:00
fp.checkConfirmationRadius(() -> {
int affected = editSession.makeHollowCylinder(pos, pattern, radius.getX(), radius.getZ(), Math.min(256, height), thickness - 1);
BBC.VISITOR_BLOCK.send(fp, affected);
}, getArguments(context), (int) max, context);
}
@Command(
name = "/cyl",
desc = "Generates a cylinder."
)
@CommandPermissions("worldedit.generation.cylinder")
@Logging(PLACEMENT)
public void cyl(FawePlayer fp, Player player, LocalSession session, EditSession editSession,
@Arg(desc = "The pattern of blocks to generate")
Pattern pattern,
BlockVector2 radius,
@Arg(desc = "The height of the cylinder", def = "1")
int height,
@Switch(name = 'h', desc = "Make a hollow cylinder")
boolean hollow, InjectedValueAccess context) throws WorldEditException {
double max = Math.max(radius.getBlockX(), radius.getBlockZ());
worldEdit.checkMaxRadius(max);
2019-01-09 07:13:44 +00:00
BlockVector3 pos = session.getPlacementPosition(player);
2018-09-07 15:09:31 +00:00
fp.checkConfirmationRadius(() -> {
int affected = editSession.makeCylinder(pos, pattern, radius.getX(), radius.getZ(), Math.min(256, height), !hollow);
BBC.VISITOR_BLOCK.send(fp, affected);
}, getArguments(context), (int) max, context);
}
@Command(
name = "/hsphere",
desc = "Generates a hollow sphere."
)
@CommandPermissions("worldedit.generation.sphere")
@Logging(PLACEMENT)
public void hsphere(FawePlayer fp, Player player, LocalSession session, EditSession editSession,
@Arg(desc = "The pattern of blocks to generate")
Pattern pattern,
2019-07-17 10:50:54 +00:00
@Arg(desc = "The radii of the sphere. Order is N/S, U/D, E/W") BlockVector3 radii,
@Switch(name = 'r', desc = "Raise the bottom of the sphere to the placement position")
boolean raised,
InjectedValueAccess context) throws WorldEditException {
sphere(fp, player, session, editSession, pattern, radii, raised, true, context);
}
@Command(
name = "/sphere",
desc = "Generates a filled sphere."
)
@CommandPermissions("worldedit.generation.sphere")
@Logging(PLACEMENT)
2019-07-17 10:50:54 +00:00
public void sphere(FawePlayer fp, Player player, LocalSession session, EditSession editSession,
@Arg(desc = "The pattern of blocks to generate")
Pattern pattern,
@Arg(desc = "The radii of the sphere. Order is N/S, U/D, E/W")
BlockVector3 radii,
@Switch(name = 'r', desc = "Raise the bottom of the sphere to the placement position")
boolean raised,
@Switch(name = 'h', desc = "Make a hollow sphere")
boolean hollow, InjectedValueAccess context) throws WorldEditException {
2019-07-22 06:57:12 +00:00
double max = MathMan.max(radii.getBlockX(), radii.getBlockY(), radii.getBlockZ());
worldEdit.checkMaxRadius(max);
2019-01-09 07:13:44 +00:00
BlockVector3 pos = session.getPlacementPosition(player);
2019-07-17 10:50:54 +00:00
BlockVector3 finalPos = raised ? pos.add(0, radii.getY(), 0) : pos;
2018-09-07 15:09:31 +00:00
fp.checkConfirmationRadius(() -> {
2019-07-17 10:50:54 +00:00
int affected = editSession.makeSphere(finalPos, pattern, radii.getX(), radii.getY(), radii.getZ(), !hollow);
2018-09-07 15:09:31 +00:00
player.findFreePosition();
BBC.VISITOR_BLOCK.send(fp, affected);
2018-09-07 23:10:36 +00:00
}, getArguments(context), (int) max, context);
}
@Command(
name = "forestgen",
desc = "Generate a forest"
)
@CommandPermissions("worldedit.generation.forest")
@Logging(POSITION)
public int forestGen(Player player, LocalSession session, EditSession editSession,
@Arg(desc = "The size of the forest, in blocks", def = "10")
int size,
@Arg(desc = "The type of forest", def = "tree")
TreeType type,
@Range(min = 0, max = 100) @Arg(desc = "The density of the forest, between 0 and 100", def = "5")
double density) throws WorldEditException {
density = density / 100;
2018-06-19 07:03:09 +00:00
int affected = editSession.makeForest(session.getPlacementPosition(player), size, density, type);
player.print(affected + " trees created.");
return affected;
}
@Command(
name = "pumpkins",
desc = "Generate pumpkin patches"
)
@CommandPermissions("worldedit.generation.pumpkins")
@Logging(POSITION)
public int pumpkins(Player player, LocalSession session, EditSession editSession,
@Arg(desc = "The size of the patch", def = "10")
int size,
@Arg(desc = "//TODO", def = "10")
int apothem,
@Range(min = 0, max = 100) @Arg(desc = "//TODO ", def = "0.02")
double density) throws WorldEditException {
int affected = editSession.makePumpkinPatches(session.getPlacementPosition(player), apothem, density);
BBC.COMMAND_PUMPKIN.send(player, affected);
return affected;
}
@Command(
name = "/hpyramid",
desc = "Generate a hollow pyramid"
)
@CommandPermissions("worldedit.generation.pyramid")
@Logging(PLACEMENT)
public void hollowPyramid(FawePlayer fp, Player player, LocalSession session, EditSession editSession,
@Arg(desc = "The pattern of blocks to set")
Pattern pattern,
@Arg(desc = "The size of the pyramid")
int size, InjectedValueAccess context) throws WorldEditException {
pyramid(fp, player, session, editSession, pattern, size, true, context);
}
@Command(
name = "/pyramid",
desc = "Generate a filled pyramid"
)
@CommandPermissions("worldedit.generation.pyramid")
@Logging(PLACEMENT)
public void pyramid(FawePlayer fp, Player player, LocalSession session, EditSession editSession,
@Arg(desc = "The pattern of blocks to set")
Pattern pattern,
@Arg(desc = "The size of the pyramid")
int size,
@Switch(name = 'h', desc = "Make a hollow pyramid")
boolean hollow,
InjectedValueAccess context) throws WorldEditException {
2018-12-23 16:19:33 +00:00
BlockVector3 pos = session.getPlacementPosition(player);
worldEdit.checkMaxRadius(size);
2018-09-07 15:09:31 +00:00
fp.checkConfirmationRadius(() -> {
int affected = editSession.makePyramid(pos, pattern, size, !hollow);
player.findFreePosition();
BBC.VISITOR_BLOCK.send(fp, affected);
2018-09-07 23:10:36 +00:00
}, getArguments(context), size, context);
}
@Command(
name = "/generate",
aliases = { "/gen", "/g" },
desc = "Generates a shape according to a formula.",
descFooter = "See also https://tinyurl.com/weexpr."
)
@CommandPermissions("worldedit.generation.shape")
@Logging(ALL)
2019-07-17 10:50:54 +00:00
public void generate(FawePlayer fp, Player player, LocalSession session, EditSession editSession,
@Selection Region region,
@Arg(desc = "The pattern of blocks to set")
Pattern pattern,
@Arg(desc = "Expression to test block placement locations and set block type", variable = true)
List<String> expression,
@Switch(name = 'h', desc = "Generate a hollow shape")
boolean hollow,
@Switch(name = 'r', desc = "Use the game's coordinate origin")
boolean useRawCoords,
@Switch(name = 'o', desc = "Use the placement's coordinate origin")
boolean offset,
@Switch(name = 'c', desc = "Use the selection's center as origin")
2019-07-22 06:57:12 +00:00
boolean offsetCenter,
InjectedValueAccess context) throws WorldEditException {
2018-12-23 16:19:33 +00:00
final Vector3 zero;
Vector3 unit;
if (useRawCoords) {
2018-12-23 16:19:33 +00:00
zero = Vector3.ZERO;
unit = Vector3.ONE;
} else if (offset) {
2018-12-23 16:19:33 +00:00
zero = session.getPlacementPosition(player).toVector3();
unit = Vector3.ONE;
} else if (offsetCenter) {
2018-12-23 16:19:33 +00:00
final Vector3 min = region.getMinimumPoint().toVector3();
final Vector3 max = region.getMaximumPoint().toVector3();
zero = max.add(min).multiply(0.5);
2018-12-23 16:19:33 +00:00
unit = Vector3.ONE;
} else {
2018-12-23 16:19:33 +00:00
final Vector3 min = region.getMinimumPoint().toVector3();
final Vector3 max = region.getMaximumPoint().toVector3();
zero = max.add(min).multiply(0.5);
unit = max.subtract(zero);
2018-12-23 16:19:33 +00:00
if (unit.getX() == 0) unit = unit.withX(1.0);
if (unit.getY() == 0) unit = unit.withY(1.0);
if (unit.getZ() == 0) unit = unit.withZ(1.0);
}
2019-01-09 07:13:44 +00:00
final Vector3 unit1 = unit;
fp.checkConfirmationRegion(() -> {
2018-09-07 15:09:31 +00:00
try {
2019-07-17 10:50:54 +00:00
int affected = editSession.makeShape(region, zero, unit1, pattern, String.join(" ", expression), hollow, session.getTimeout());
2018-09-07 15:09:31 +00:00
player.findFreePosition();
BBC.VISITOR_BLOCK.send(fp, affected);
} catch (ExpressionException e) {
player.printError(e.getMessage());
2018-09-07 15:09:31 +00:00
}
2018-09-07 23:10:36 +00:00
}, getArguments(context), region, context);
}
@Command(
name = "/generatebiome",
aliases = { "/genbiome", "/gb" },
desc = "Sets biome according to a formula.",
2019-07-17 10:50:54 +00:00
descFooter = "Formula must return positive numbers (true) if the point is inside the shape\n" +
"Sets the biome of blocks in that shape.\n"
+"See also https://tinyurl.com/weexpr."
)
@CommandPermissions("worldedit.generation.shape.biome")
@Logging(ALL)
2019-07-22 06:57:12 +00:00
public void generateBiome(FawePlayer fp, LocalSession session, EditSession editSession,
@Selection Region region,
@Arg(desc = "The biome type to set")
BiomeType target,
@Arg(desc = "Expression to test block placement locations and set biome type", variable = true)
List<String> expression,
@Switch(name = 'h', desc = "Generate a hollow shape")
boolean hollow,
@Switch(name = 'r', desc = "Use the game's coordinate origin")
boolean useRawCoords,
@Switch(name = 'o', desc = "Use the placement's coordinate origin")
boolean offset,
@Switch(name = 'c', desc = "Use the selection's center as origin")
2019-07-22 06:57:12 +00:00
boolean offsetCenter,
InjectedValueAccess context) throws WorldEditException {
2018-12-23 16:19:33 +00:00
final Vector3 zero;
Vector3 unit;
if (useRawCoords) {
2018-12-23 16:19:33 +00:00
zero = Vector3.ZERO;
unit = Vector3.ONE;
} else if (offset) {
zero = session.getPlacementPosition(fp.toWorldEditPlayer()).toVector3();
2018-12-23 16:19:33 +00:00
unit = Vector3.ONE;
} else if (offsetCenter) {
2018-12-23 16:19:33 +00:00
final Vector3 min = region.getMinimumPoint().toVector3();
final Vector3 max = region.getMaximumPoint().toVector3();
zero = max.add(min).multiply(0.5);
2018-12-23 16:19:33 +00:00
unit = Vector3.ONE;
} else {
2018-12-23 16:19:33 +00:00
final Vector3 min = region.getMinimumPoint().toVector3();
final Vector3 max = region.getMaximumPoint().toVector3();
zero = max.add(min).multiply(0.5);
unit = max.subtract(zero);
2018-12-23 16:19:33 +00:00
if (unit.getX() == 0) unit = unit.withX(1.0);
if (unit.getY() == 0) unit = unit.withY(1.0);
if (unit.getZ() == 0) unit = unit.withZ(1.0);
}
2019-01-09 07:13:44 +00:00
final Vector3 unit1 = unit;
2018-09-07 15:09:31 +00:00
fp.checkConfirmationRegion(() -> {
try {
final int affected = editSession.makeBiomeShape(region, zero, unit1, target, String.join(" ", expression), hollow, session.getTimeout());
2018-09-07 15:09:31 +00:00
BBC.VISITOR_FLAT.send(fp, affected);
} catch (ExpressionException e) {
2019-07-22 06:57:12 +00:00
fp.printError(e.getMessage());
2018-09-07 15:09:31 +00:00
}
2018-09-07 23:10:36 +00:00
}, getArguments(context), region, context);
}
}