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

Add a new blockBreakEffect method and deprecate World#playEffect #2110

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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 @@ -370,6 +370,14 @@ public boolean playEffect(Vector3 position, int type, int data) {
return true;
}

@Override
public boolean playBreakBlockEffect(Vector3 position, com.sk89q.worldedit.world.block.BlockState block) {
World world = getWorld();

world.playEffect(BukkitAdapter.adapt(world, position), Effect.STEP_SOUND, BukkitAdapter.adapt(block.getBlockType())); // Bukkit doesn't use a blockstate, instead uses material
return true;
}

@Override
public WeatherType getWeather() {
if (getWorld().isThundering()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import com.sk89q.worldedit.util.Location;
import com.sk89q.worldedit.util.formatting.text.TranslatableComponent;
import com.sk89q.worldedit.world.World;
import com.sk89q.worldedit.world.block.BlockState;
import com.sk89q.worldedit.world.block.BlockType;
import com.sk89q.worldedit.world.block.BlockTypes;

Expand All @@ -57,7 +58,8 @@ public boolean actPrimary(Platform server, LocalConfiguration config, Player pla
int ox = clicked.getBlockX();
int oy = clicked.getBlockY();
int oz = clicked.getBlockZ();
BlockType initialType = clicked.getExtent().getBlock(clicked.toVector().toBlockPoint()).getBlockType();
BlockState initialState = clicked.getExtent().getBlock(clicked.toVector().toBlockPoint());
BlockType initialType = initialState.getBlockType();

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

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

((World) clicked.getExtent()).queueBlockBreakEffect(server, pos, initialType,
((World) clicked.getExtent()).queueBlockBreakEffect(server, pos, initialState,
clicked.toVector().toBlockPoint().distanceSq(pos));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import com.sk89q.worldedit.util.Location;
import com.sk89q.worldedit.util.formatting.text.TranslatableComponent;
import com.sk89q.worldedit.world.World;
import com.sk89q.worldedit.world.block.BlockState;
import com.sk89q.worldedit.world.block.BlockType;
import com.sk89q.worldedit.world.block.BlockTypes;

Expand Down Expand Up @@ -60,7 +61,8 @@ public boolean actPrimary(Platform server, LocalConfiguration config, Player pla
World world = (World) clicked.getExtent();

BlockVector3 origin = clicked.toVector().toBlockPoint();
BlockType initialType = world.getBlock(origin).getBlockType();
BlockState initialState = world.getBlock(origin);
BlockType initialType = initialState.getBlockType();

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

try {
recurse(server, editSession, world, clicked.toVector().toBlockPoint(),
clicked.toVector().toBlockPoint(), range, initialType, new HashSet<>());
clicked.toVector().toBlockPoint(), range, initialState, new HashSet<>());
} catch (MaxChangedBlocksException e) {
player.printError(TranslatableComponent.of("worldedit.tool.max-block-changes"));
} finally {
Expand All @@ -87,7 +89,8 @@ public boolean actPrimary(Platform server, LocalConfiguration config, Player pla
}

private static void recurse(Platform server, EditSession editSession, World world, BlockVector3 pos,
BlockVector3 origin, double size, BlockType initialType, Set<BlockVector3> visited) throws MaxChangedBlocksException {
BlockVector3 origin, double size, BlockState initialState, Set<BlockVector3> visited) throws MaxChangedBlocksException {
BlockType initialType = initialState.getBlockType();

final double distanceSq = origin.distanceSq(pos);
if (distanceSq > size * size || visited.contains(pos)) {
Expand All @@ -102,20 +105,20 @@ private static void recurse(Platform server, EditSession editSession, World worl

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

world.queueBlockBreakEffect(server, pos, initialType, distanceSq);
world.queueBlockBreakEffect(server, pos, initialState, distanceSq);

recurse(server, editSession, world, pos.add(1, 0, 0),
origin, size, initialType, visited);
origin, size, initialState, visited);
recurse(server, editSession, world, pos.add(-1, 0, 0),
origin, size, initialType, visited);
origin, size, initialState, visited);
recurse(server, editSession, world, pos.add(0, 0, 1),
origin, size, initialType, visited);
origin, size, initialState, visited);
recurse(server, editSession, world, pos.add(0, 0, -1),
origin, size, initialType, visited);
origin, size, initialState, visited);
recurse(server, editSession, world, pos.add(0, 1, 0),
origin, size, initialType, visited);
origin, size, initialState, visited);
recurse(server, editSession, world, pos.add(0, -1, 0),
origin, size, initialType, visited);
origin, size, initialState, visited);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
import com.sk89q.worldedit.math.Vector3;
import com.sk89q.worldedit.util.Direction;
import com.sk89q.worldedit.util.SideEffectSet;
import com.sk89q.worldedit.world.block.BlockState;
import com.sk89q.worldedit.world.block.BlockStateHolder;
import com.sk89q.worldedit.world.block.BlockType;
import com.sk89q.worldedit.world.block.BlockTypes;
import com.sk89q.worldedit.world.weather.WeatherType;
import com.sk89q.worldedit.world.weather.WeatherTypes;
Expand Down Expand Up @@ -104,7 +104,12 @@ public boolean playEffect(Vector3 position, int type, int data) {
}

@Override
public boolean queueBlockBreakEffect(Platform server, BlockVector3 position, BlockType blockType, double priority) {
public boolean playBreakBlockEffect(Vector3 position, BlockState block) {
return false;
}

@Override
public boolean queueBlockBreakEffect(Platform server, BlockVector3 position, BlockState blockState, double priority) {
if (taskId == -1) {
taskId = server.schedule(0, 1, () -> {
int max = Math.max(1, Math.min(30, effectQueue.size() / 3));
Expand All @@ -122,7 +127,7 @@ public boolean queueBlockBreakEffect(Platform server, BlockVector3 position, Blo
return false;
}

effectQueue.offer(new QueuedEffect(position.toVector3(), blockType, priority));
effectQueue.offer(new QueuedEffect(position.toVector3(), blockState, priority));

return true;
}
Expand Down Expand Up @@ -162,18 +167,17 @@ public void setWeather(WeatherType weatherType, long duration) {

private class QueuedEffect implements Comparable<QueuedEffect> {
private final Vector3 position;
private final BlockType blockType;
private final BlockState blockState;
private final double priority;

private QueuedEffect(Vector3 position, BlockType blockType, double priority) {
private QueuedEffect(Vector3 position, BlockState blockState, double priority) {
this.position = position;
this.blockType = blockType;
this.blockState = blockState;
this.priority = priority;
}

@SuppressWarnings("deprecation")
public void play() {
playEffect(position, 2001, blockType.getLegacyId());
playBreakBlockEffect(position, blockState);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -307,9 +307,22 @@ default boolean regenerate(Region region, Extent extent, RegenOptions options) {
* @param type the effect type
* @param data the effect data
* @return true if the effect was played
* @deprecated magic numbers
*/
@Deprecated
boolean playEffect(Vector3 position, int type, int data);


/**
* Plays a block break effect and sound at
* the given position.
*
* @param position position
* @param block block
* @return true if the effect was played
*/
boolean playBreakBlockEffect(Vector3 position, BlockState block);

/**
* Queue a block break effect.
*
Expand All @@ -318,8 +331,23 @@ default boolean regenerate(Region region, Extent extent, RegenOptions options) {
* @param blockType the block type
* @param priority the priority
* @return true if the effect was played
* @deprecated use a blockstate instead of block type
*/
@Deprecated
default boolean queueBlockBreakEffect(Platform server, BlockVector3 position, BlockType blockType, double priority) {
return queueBlockBreakEffect(server, position, blockType.getDefaultState(), priority);
}

/**
* Queue a block break effect.
*
* @param server the server
* @param position the position
* @param blockState the block state
* @param priority the priority
* @return true if the effect was played
*/
boolean queueBlockBreakEffect(Platform server, BlockVector3 position, BlockType blockType, double priority);
boolean queueBlockBreakEffect(Platform server, BlockVector3 position, BlockState blockState, double priority);

/**
* Gets the weather type of the world.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@
import net.minecraft.world.level.ChunkPos;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.biome.Biome;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.LevelEvent;
import net.minecraft.world.level.block.LiquidBlock;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.chunk.ChunkAccess;
Expand Down Expand Up @@ -494,6 +496,12 @@ public boolean playEffect(Vector3 position, int type, int data) {
return true;
}

@Override
public boolean playBreakBlockEffect(Vector3 position, BlockState block) {
getWorld().globalLevelEvent(LevelEvent.PARTICLES_DESTROY_BLOCK, FabricAdapter.toBlockPos(position.toBlockPoint()), Block.getId(FabricAdapter.adapt(block)));
return true;
}

@Override
public WeatherType getWeather() {
LevelData info = getWorld().getLevelData();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@
import net.minecraft.world.level.ChunkPos;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.biome.Biome;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.LevelEvent;
import net.minecraft.world.level.block.LiquidBlock;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.chunk.ChunkAccess;
Expand Down Expand Up @@ -477,6 +479,12 @@ public boolean playEffect(Vector3 position, int type, int data) {
return true;
}

@Override
public boolean playBreakBlockEffect(Vector3 position, BlockState block) {
getWorld().globalLevelEvent(LevelEvent.PARTICLES_DESTROY_BLOCK, ForgeAdapter.toBlockPos(position.toBlockPoint()), Block.getId(ForgeAdapter.adapt(block)));
return true;
}

@Override
public WeatherType getWeather() {
LevelData info = getWorld().getLevelData();
Expand Down