Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correct all BlockTools to use the Location's World #2650

Merged
merged 1 commit into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,11 @@ public boolean canUse(Actor player) {

@Override
public boolean actPrimary(Platform server, LocalConfiguration config, Player player, LocalSession session, Location clicked, @Nullable Direction face) {
World world = BlockTool.requireWorld(clicked);
int ox = clicked.getBlockX();
int oy = clicked.getBlockY();
int oz = clicked.getBlockZ();
BlockType initialType = clicked.getExtent().getBlock(clicked.toVector().toBlockPoint()).getBlockType();
BlockType initialType = world.getBlock(clicked.toVector().toBlockPoint()).getBlockType();

if (initialType.getMaterial().isAir()) {
return false;
Expand All @@ -67,7 +68,7 @@ public boolean actPrimary(Platform server, LocalConfiguration config, Player pla
return false;
}

try (EditSession editSession = session.createEditSession(player)) {
try (EditSession editSession = BlockTool.createEditSession(player, session, clicked)) {
editSession.getSurvivalExtent().setToolUse(config.superPickaxeManyDrop);

try {
Expand All @@ -81,8 +82,9 @@ public boolean actPrimary(Platform server, LocalConfiguration config, Player pla

editSession.setBlock(pos, BlockTypes.AIR.getDefaultState());

((World) clicked.getExtent()).queueBlockBreakEffect(server, pos, initialType,
clicked.toVector().toBlockPoint().distanceSq(pos));
world.queueBlockBreakEffect(
server, pos, initialType, clicked.toVector().toBlockPoint().distanceSq(pos)
);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public boolean canUse(Actor player) {
private boolean handleCycle(LocalConfiguration config, Player player, LocalSession session,
Location clicked, boolean forward) {

World world = (World) clicked.getExtent();
World world = BlockTool.requireWorld(clicked);

BlockVector3 blockPoint = clicked.toVector().toBlockPoint();
BaseBlock block = world.getFullBlock(blockPoint);
Expand Down Expand Up @@ -87,7 +87,7 @@ private boolean handleCycle(LocalConfiguration config, Player player, LocalSessi
Property<Object> objProp = (Property<Object>) currentProperty;
BaseBlock newBlock = block.with(objProp, currentProperty.getValues().get(index));

try (EditSession editSession = session.createEditSession(player)) {
try (EditSession editSession = BlockTool.createEditSession(player, session, clicked)) {
editSession.disableBuffering();

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public boolean canUse(Actor player) {
public boolean actPrimary(Platform server, LocalConfiguration config, Player player, LocalSession session, Location clicked, @Nullable Direction face) {
BlockBag bag = session.getBlockBag(player);

try (EditSession editSession = session.createEditSession(player)) {
try (EditSession editSession = BlockTool.createEditSession(player, session, clicked)) {
try {
editSession.disableBuffering();
BlockVector3 position = clicked.toVector().toBlockPoint();
Expand All @@ -77,7 +77,7 @@ public boolean actPrimary(Platform server, LocalConfiguration config, Player pla

@Override
public boolean actSecondary(Platform server, LocalConfiguration config, Player player, LocalSession session, Location clicked, @Nullable Direction face) {
BaseBlock targetBlock = player.getWorld().getFullBlock(clicked.toVector().toBlockPoint());
BaseBlock targetBlock = clicked.getExtent().getFullBlock(clicked.toVector().toBlockPoint());

if (targetBlock != null) {
pattern = targetBlock;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

package com.sk89q.worldedit.command.tool;

import com.sk89q.worldedit.EditSession;
import com.sk89q.worldedit.LocalConfiguration;
import com.sk89q.worldedit.LocalSession;
import com.sk89q.worldedit.entity.Player;
Expand All @@ -27,11 +28,41 @@
import com.sk89q.worldedit.internal.util.NonAbstractForCompatibility;
import com.sk89q.worldedit.util.Direction;
import com.sk89q.worldedit.util.Location;
import com.sk89q.worldedit.world.World;

import javax.annotation.Nullable;

public interface BlockTool extends Tool {

/**
* Helper method to require the world that should be used for a tool action.
*
* @param clicked the location that was clicked
* @return the world to use
*/
static World requireWorld(Location clicked) {
if (!(clicked.getExtent() instanceof World world)) {
throw new IllegalArgumentException("Location is not in a world: " + clicked);
}
return world;
}

/**
* Helper method to create an {@link EditSession} for a tool action.
*
* @param player the player
* @param clicked the location that was clicked
*/
static EditSession createEditSession(Player player, LocalSession session, Location clicked) {
World overrideToRestore = session.getWorldOverride();
session.setWorldOverride(BlockTool.requireWorld(clicked));
try {
return session.createEditSession(player);
} finally {
session.setWorldOverride(overrideToRestore);
}
}

/**
* Perform the primary action of this tool.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,15 @@ public boolean actPrimary(Platform server, LocalConfiguration config,
Player player, LocalSession session, Location clicked,
@Nullable Direction face) {

final World world = (World) clicked.getExtent();
final World world = BlockTool.requireWorld(clicked);
final BlockState state = world.getBlock(clicked.toVector().toBlockPoint());

if (!isTreeBlock(state.getBlockType())) {
player.printError(TranslatableComponent.of("worldedit.tool.deltree.not-tree"));
return true;
}

try (EditSession editSession = session.createEditSession(player)) {
try (EditSession editSession = BlockTool.createEditSession(player, session, clicked)) {
try {
final Set<BlockVector3> blockSet = bfs(world, clicked.toVector().toBlockPoint());
if (blockSet == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public boolean canUse(Actor player) {

@Override
public boolean actPrimary(Platform server, LocalConfiguration config, Player player, LocalSession session, Location clicked, @Nullable Direction face) {
World world = (World) clicked.getExtent();
World world = BlockTool.requireWorld(clicked);

BlockVector3 origin = clicked.toVector().toBlockPoint();
BlockType initialType = world.getBlock(origin).getBlockType();
Expand All @@ -72,7 +72,7 @@ public boolean actPrimary(Platform server, LocalConfiguration config, Player pla
return true;
}

try (EditSession editSession = session.createEditSession(player)) {
try (EditSession editSession = BlockTool.createEditSession(player, session, clicked)) {
try {
recurse(editSession, origin, origin, range, initialType, new HashSet<>());
} catch (MaxChangedBlocksException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public boolean canUse(Actor player) {
@Override
public boolean actPrimary(Platform server, LocalConfiguration config, Player player, LocalSession session, Location clicked, @Nullable Direction face) {

World world = (World) clicked.getExtent();
World world = BlockTool.requireWorld(clicked);
BlockVector3 blockPoint = clicked.toVector().toBlockPoint();
BaseBlock block = world.getFullBlock(blockPoint);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public boolean canUse(Actor player) {

@Override
public boolean actPrimary(Platform server, LocalConfiguration config, Player player, LocalSession session, Location clicked, @Nullable Direction face) {
World world = (World) clicked.getExtent();
World world = BlockTool.requireWorld(clicked);

BlockVector3 origin = clicked.toVector().toBlockPoint();
BlockType initialType = world.getBlock(origin).getBlockType();
Expand All @@ -70,7 +70,7 @@ public boolean actPrimary(Platform server, LocalConfiguration config, Player pla
return false;
}

try (EditSession editSession = session.createEditSession(player)) {
try (EditSession editSession = BlockTool.createEditSession(player, session, clicked)) {
editSession.getSurvivalExtent().setToolUse(config.superPickaxeManyDrop);

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class SelectionWand implements DoubleActionBlockTool {

@Override
public boolean actSecondary(Platform server, LocalConfiguration config, Player player, LocalSession session, Location clicked, @Nullable Direction face) {
RegionSelector selector = session.getRegionSelector(player.getWorld());
RegionSelector selector = session.getRegionSelector(BlockTool.requireWorld(clicked));
BlockVector3 blockPoint = clicked.toVector().toBlockPoint();

if (selector.selectPrimary(blockPoint, ActorSelectorLimits.forActor(player))) {
Expand All @@ -47,7 +47,7 @@ public boolean actSecondary(Platform server, LocalConfiguration config, Player p

@Override
public boolean actPrimary(Platform server, LocalConfiguration config, Player player, LocalSession session, Location clicked, @Nullable Direction face) {
RegionSelector selector = session.getRegionSelector(player.getWorld());
RegionSelector selector = session.getRegionSelector(BlockTool.requireWorld(clicked));
BlockVector3 blockPoint = clicked.toVector().toBlockPoint();

if (selector.selectSecondary(blockPoint, ActorSelectorLimits.forActor(player))) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,14 @@ public boolean canUse(Actor player) {

@Override
public boolean actPrimary(Platform server, LocalConfiguration config, Player player, LocalSession session, Location clicked, @Nullable Direction face) {
World world = (World) clicked.getExtent();
World world = BlockTool.requireWorld(clicked);
BlockVector3 blockPoint = clicked.toVector().toBlockPoint();
final BlockType blockType = world.getBlock(blockPoint).getBlockType();
if (blockType == BlockTypes.BEDROCK && !player.canDestroyBedrock()) {
return false;
}

try (EditSession editSession = session.createEditSession(player)) {
try (EditSession editSession = BlockTool.createEditSession(player, session, clicked)) {
editSession.getSurvivalExtent().setToolUse(config.superPickaxeDrop);
editSession.setBlock(blockPoint, BlockTypes.AIR.getDefaultState());
} catch (MaxChangedBlocksException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public boolean actPrimary(Platform server, LocalConfiguration config, Player pla
}
BlockBag bag = session.getBlockBag(player);

try (EditSession editSession = session.createEditSession(player)) {
try (EditSession editSession = BlockTool.createEditSession(player, session, clicked)) {
BlockStateHolder<?> block = editSession.getFullBlock(clicked.toVector().toBlockPoint());

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public boolean canUse(Actor player) {
@Override
public boolean actPrimary(Platform server, LocalConfiguration config, Player player, LocalSession session, Location clicked, @Nullable Direction face) {

try (EditSession editSession = session.createEditSession(player)) {
try (EditSession editSession = BlockTool.createEditSession(player, session, clicked)) {
try {
boolean successful = false;

Expand Down