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 BlockBreakEffectsCallback #1023

Open
wants to merge 17 commits into
base: 1.16
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
@@ -0,0 +1,50 @@
/*
* Copyright (c) 2016, 2017, 2018, 2019 FabricMC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package net.fabricmc.fabric.api.event;

import net.minecraft.block.BlockState;
import net.minecraft.entity.Entity;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;

/**
* Callback for the effects displayed when a block is broken (particles and sounds).
*
* <p>This is invoked on both the logical-client and logical-server
*/
public interface BlockBreakEffectsCallback {
Event<BlockBreakEffectsCallback> EVENT = EventFactory.createArrayBacked(BlockBreakEffectsCallback.class, (listeners) -> (world, pos, state, breakingEntity) -> {
for (BlockBreakEffectsCallback event : listeners) {
if (!event.run(world, pos, state, breakingEntity)) {
return false;
}
}

return true;
});

/**
* Determines if a block break effect should be displayed.
*
* @param world the world
* @param pos the position of broken block
* @param state the broken block state
* @param breakingEntity the entity that broke the block
* @return {@code false} to cancel prevents the block break effects from being displayed, {@code true} to pass on to other callbacks
*/
boolean run(World world, BlockPos pos, BlockState state, /* nullable */ Entity breakingEntity);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright (c) 2016, 2017, 2018, 2019 FabricMC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package net.fabricmc.fabric.mixin.event.interaction;

import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Redirect;

import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;

import net.fabricmc.fabric.api.event.BlockBreakEffectsCallback;

@Mixin(Block.class)
public class BlockMixin {
@Redirect(at = @At(value = "INVOKE", target = "Lnet/minecraft/world/World;syncWorldEvent(Lnet/minecraft/entity/player/PlayerEntity;ILnet/minecraft/util/math/BlockPos;I)V"), method = "onBreak")
public void onBreak(World world, PlayerEntity player, int eventId, BlockPos pos, int data, World world2, BlockPos pos2, BlockState state, PlayerEntity player2) {
if (BlockBreakEffectsCallback.EVENT.invoker().run(world, pos, state, player2)) {
world.syncWorldEvent(player, eventId, pos, data);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright (c) 2016, 2017, 2018, 2019 FabricMC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package net.fabricmc.fabric.mixin.event.interaction;

import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Redirect;

import net.minecraft.entity.Entity;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;

import net.fabricmc.fabric.api.event.BlockBreakEffectsCallback;

@Mixin(World.class)
public class WorldMixin {
@Redirect(at = @At(value = "INVOKE", target = "Lnet/minecraft/world/World;syncWorldEvent(ILnet/minecraft/util/math/BlockPos;I)V"), method = "breakBlock")
public void breakBlock(World world, int eventId, BlockPos pos, int data, BlockPos pos2, boolean drop, Entity breakingEntity, int maxUpdateDepth) {
if (BlockBreakEffectsCallback.EVENT.invoker().run(world, pos, world.getBlockState(pos), breakingEntity)) {
world.syncWorldEvent(eventId, pos, data);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
"package": "net.fabricmc.fabric.mixin.event.interaction",
"compatibilityLevel": "JAVA_8",
"mixins": [
"BlockMixin",
"MixinServerPlayerEntity",
"MixinServerPlayerInteractionManager",
"MixinServerPlayNetworkHandler"
"MixinServerPlayNetworkHandler",
"WorldMixin"
],
"client": [
"MixinClientPlayerInteractionManager",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright (c) 2016, 2017, 2018, 2019 FabricMC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package net.fabricmc.fabric.test.event.interaction;

import net.minecraft.block.Blocks;

import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.event.BlockBreakEffectsCallback;

public class BlockBreakEffectsTest implements ModInitializer {
@Override
public void onInitialize() {
BlockBreakEffectsCallback.EVENT.register((world, pos, state, breakingEntity) -> state.getBlock() != Blocks.BEDROCK);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
},
"entrypoints": {
"main": [
"net.fabricmc.fabric.test.event.interaction.BlockBreakEffectsTest",
"net.fabricmc.fabric.test.event.interaction.PlayerBreakBlockTests"
]
}
Expand Down