Skip to content
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
16 changes: 14 additions & 2 deletions src/main/java/dev/amble/lib/api/ICantBreak.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,28 @@
package dev.amble.lib.api;

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

/**
* An interface which stops a block from being broken
* An interface which stops a block from being broken.
*/
public interface ICantBreak {
/**
* Called when the block was attempted to be broken
* Called when the block was attempted to be broken.
* This exists for backwards compatibility.
*/
default void onTryBreak(World world, BlockPos pos, BlockState state) {
this.onTryBreak(world, pos, state, null);
}

/**
* Called when the block was attempted to be broken but also includes the player who attempts to break it.
*
* @param player The player who attempted to break the block, or null if not available.
*/
default void onTryBreak(World world, BlockPos pos, BlockState state, PlayerEntity player) {
this.onTryBreak(world, pos, state);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.amble.lib.mixin;

import net.minecraft.server.network.ServerPlayerEntity;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
Expand All @@ -19,11 +20,14 @@ public class ServerPlayerInteractionManagerMixin {
@Shadow
protected ServerWorld world;

@Shadow
protected ServerPlayerEntity player;

@Inject(method = "tryBreakBlock", at = @At(value = "HEAD"), cancellable = true)
public void ait$tryBreakBlock(BlockPos pos, CallbackInfoReturnable<Boolean> cir) {
Block block = this.world.getBlockState(pos).getBlock();
if (block instanceof ICantBreak cantBreak) {
cantBreak.onTryBreak(this.world, pos, this.world.getBlockState(pos));
cantBreak.onTryBreak(this.world, pos, this.world.getBlockState(pos), this.player);
cir.setReturnValue(false);
cir.cancel();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,12 @@ public class ClientPlayerInteractionManagerMixin {
World world = this.client.world;
if (world == null)
return;

Block block = world.getBlockState(pos).getBlock();
if (block instanceof ICantBreak cantBreak) {
cantBreak.onTryBreak(world, pos, world.getBlockState(pos));
cantBreak.onTryBreak(world, pos, world.getBlockState(pos), this.client.player);
cir.setReturnValue(false);
cir.cancel();
}
}
}
}
Loading