2014-04-03 02:08:50 +00:00
|
|
|
/*
|
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-03 02:08:50 +00:00
|
|
|
*
|
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
|
2014-04-03 02:08:50 +00:00
|
|
|
* (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-03 02:08:50 +00:00
|
|
|
*
|
2014-04-04 22:03:18 +00:00
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
2014-04-03 02:08:50 +00:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2014-04-04 22:03:18 +00:00
|
|
|
*/
|
2014-04-03 02:08:50 +00:00
|
|
|
|
|
|
|
package com.sk89q.worldedit.command.tool;
|
|
|
|
|
2018-06-16 05:29:48 +00:00
|
|
|
import com.sk89q.worldedit.EditSession;
|
|
|
|
import com.sk89q.worldedit.LocalConfiguration;
|
|
|
|
import com.sk89q.worldedit.LocalSession;
|
|
|
|
import com.sk89q.worldedit.MaxChangedBlocksException;
|
2014-06-28 02:55:38 +00:00
|
|
|
import com.sk89q.worldedit.entity.Player;
|
|
|
|
import com.sk89q.worldedit.extension.platform.Actor;
|
|
|
|
import com.sk89q.worldedit.extension.platform.Platform;
|
2018-08-13 12:18:12 +00:00
|
|
|
import com.sk89q.worldedit.function.pattern.Pattern;
|
2018-06-16 05:29:48 +00:00
|
|
|
import com.sk89q.worldedit.util.Location;
|
2018-07-30 13:26:06 +00:00
|
|
|
import com.sk89q.worldedit.world.block.BlockStateHolder;
|
|
|
|
import com.sk89q.worldedit.world.block.BlockTypes;
|
2014-04-03 02:08:50 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A tool that can place (or remove) blocks at a distance.
|
|
|
|
*/
|
|
|
|
public class LongRangeBuildTool extends BrushTool implements DoubleActionTraceTool {
|
|
|
|
|
2018-08-13 12:18:12 +00:00
|
|
|
private Pattern primary;
|
|
|
|
private Pattern secondary;
|
2014-04-03 02:08:50 +00:00
|
|
|
|
2018-08-13 12:18:12 +00:00
|
|
|
public LongRangeBuildTool(Pattern secondary, Pattern primary) {
|
2014-04-03 02:08:50 +00:00
|
|
|
super("worldedit.tool.lrbuild");
|
|
|
|
this.primary = primary;
|
|
|
|
this.secondary = secondary;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-06-28 02:55:38 +00:00
|
|
|
public boolean canUse(Actor player) {
|
2014-04-03 02:08:50 +00:00
|
|
|
return player.hasPermission("worldedit.tool.lrbuild");
|
|
|
|
}
|
|
|
|
|
2014-06-28 02:55:38 +00:00
|
|
|
@Override
|
|
|
|
public boolean actSecondary(Platform server, LocalConfiguration config, Player player, LocalSession session) {
|
2018-06-16 05:29:48 +00:00
|
|
|
Location pos = getTargetFace(player);
|
2014-04-03 02:08:50 +00:00
|
|
|
if (pos == null) return false;
|
|
|
|
EditSession eS = session.createEditSession(player);
|
|
|
|
try {
|
2018-08-13 12:18:12 +00:00
|
|
|
BlockStateHolder applied = secondary.apply(pos.toVector());
|
2018-08-16 05:42:11 +00:00
|
|
|
if (applied.getBlockType().getMaterial().isAir()) {
|
2018-06-16 05:29:48 +00:00
|
|
|
eS.setBlock(pos.toVector(), secondary);
|
2014-04-03 02:08:50 +00:00
|
|
|
} else {
|
2018-06-16 05:29:48 +00:00
|
|
|
eS.setBlock(pos.getDirection(), secondary);
|
2014-04-03 02:08:50 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
} catch (MaxChangedBlocksException e) {
|
|
|
|
// one block? eat it
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-06-28 02:55:38 +00:00
|
|
|
public boolean actPrimary(Platform server, LocalConfiguration config, Player player, LocalSession session) {
|
2018-06-16 05:29:48 +00:00
|
|
|
Location pos = getTargetFace(player);
|
2014-04-03 02:08:50 +00:00
|
|
|
if (pos == null) return false;
|
|
|
|
EditSession eS = session.createEditSession(player);
|
|
|
|
try {
|
2018-08-13 12:18:12 +00:00
|
|
|
BlockStateHolder applied = primary.apply(pos.toVector());
|
2018-08-16 05:42:11 +00:00
|
|
|
if (applied.getBlockType().getMaterial().isAir()) {
|
2018-06-16 05:29:48 +00:00
|
|
|
eS.setBlock(pos.toVector(), primary);
|
2014-04-03 02:08:50 +00:00
|
|
|
} else {
|
2018-06-16 05:29:48 +00:00
|
|
|
eS.setBlock(pos.getDirection(), primary);
|
2014-04-03 02:08:50 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
} catch (MaxChangedBlocksException e) {
|
|
|
|
// one block? eat it
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-06-16 05:29:48 +00:00
|
|
|
public Location getTargetFace(Player player) {
|
|
|
|
Location target = player.getBlockTraceFace(getRange(), true);
|
2014-04-03 02:08:50 +00:00
|
|
|
|
|
|
|
if (target == null) {
|
|
|
|
player.printError("No block in sight!");
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return target;
|
|
|
|
}
|
2014-06-28 02:55:38 +00:00
|
|
|
|
2014-04-03 02:08:50 +00:00
|
|
|
}
|