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-08-12 14:03:07 +00:00
|
|
|
import com.boydti.fawe.config.BBC;
|
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-12 14:03:07 +00:00
|
|
|
import com.sk89q.worldedit.function.pattern.Pattern;
|
2018-12-23 16:19:33 +00:00
|
|
|
import com.sk89q.worldedit.math.BlockVector3;
|
2018-06-16 05:29:48 +00:00
|
|
|
import com.sk89q.worldedit.util.Location;
|
2018-12-27 00:39:10 +00:00
|
|
|
import com.sk89q.worldedit.world.block.BaseBlock;
|
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-12 14:03:07 +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;
|
2018-10-31 01:06:00 +00:00
|
|
|
try (EditSession eS = session.createEditSession(player)) {
|
2018-10-14 10:40:53 +00:00
|
|
|
BlockVector3 blockPoint = pos.toVector().toBlockPoint();
|
2018-12-27 00:39:10 +00:00
|
|
|
BaseBlock applied = secondary.apply(blockPoint);
|
2018-12-23 16:19:33 +00:00
|
|
|
if (applied.getBlockType().getMaterial().isAir()) {
|
|
|
|
eS.setBlock(blockPoint, secondary);
|
|
|
|
} else {
|
2019-06-25 17:07:47 +00:00
|
|
|
eS.setBlock(pos.toVector().subtract(pos.getDirection()).toBlockPoint(), secondary);
|
2018-12-23 16:19:33 +00:00
|
|
|
}
|
|
|
|
return true;
|
2019-07-06 00:46:48 +00:00
|
|
|
} catch (MaxChangedBlocksException ignored) {
|
2018-12-23 16:19:33 +00:00
|
|
|
// one block? eat it
|
|
|
|
}
|
|
|
|
return false;
|
2019-06-25 17:07:47 +00:00
|
|
|
|
2014-04-03 02:08:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-06-28 02:55:38 +00:00
|
|
|
public boolean actPrimary(Platform server, LocalConfiguration config, Player player, LocalSession session) {
|
2019-04-04 10:28:41 +00:00
|
|
|
Location pos = getTargetFace(player);
|
2019-06-25 17:07:47 +00:00
|
|
|
if (pos == null) return false;
|
2018-10-31 01:06:00 +00:00
|
|
|
try (EditSession eS = session.createEditSession(player)) {
|
2019-06-25 17:07:47 +00:00
|
|
|
BlockVector3 blockPoint = pos.toVector().toBlockPoint();
|
2018-12-27 00:39:10 +00:00
|
|
|
BaseBlock applied = primary.apply(blockPoint);
|
2018-12-23 16:19:33 +00:00
|
|
|
if (applied.getBlockType().getMaterial().isAir()) {
|
|
|
|
eS.setBlock(blockPoint, primary);
|
|
|
|
} else {
|
2019-06-25 17:07:47 +00:00
|
|
|
eS.setBlock(pos.toVector().subtract(pos.getDirection()).toBlockPoint(), primary);
|
2018-12-23 16:19:33 +00:00
|
|
|
}
|
|
|
|
return true;
|
2019-07-06 00:46:48 +00:00
|
|
|
} catch (MaxChangedBlocksException ignored) {
|
2018-12-23 16:19:33 +00:00
|
|
|
// one block? eat it
|
2014-04-03 02:08:50 +00:00
|
|
|
}
|
2019-07-06 00:46:48 +00:00
|
|
|
return false;
|
2014-04-03 02:08:50 +00:00
|
|
|
}
|
|
|
|
|
2019-07-06 00:46:48 +00:00
|
|
|
private Location getTargetFace(Player player) {
|
2018-06-16 05:29:48 +00:00
|
|
|
Location target = player.getBlockTraceFace(getRange(), true);
|
2019-07-06 00:46:48 +00:00
|
|
|
if (this.range > -1) {
|
|
|
|
target = player.getBlockTrace(getRange(), true);
|
|
|
|
} else {
|
|
|
|
target = player.getBlockTrace(MAX_RANGE, false);
|
|
|
|
}
|
2014-04-03 02:08:50 +00:00
|
|
|
|
|
|
|
if (target == null) {
|
2018-08-12 14:03:07 +00:00
|
|
|
BBC.NO_BLOCK.send(player);
|
2014-04-03 02:08:50 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return target;
|
|
|
|
}
|
2014-06-28 02:55:38 +00:00
|
|
|
|
2019-06-25 17:07:47 +00:00
|
|
|
}
|