Created
September 11, 2019 09:27
-
-
Save Daomephsta/6b26e437e8abf1a19414a84b5538f414 to your computer and use it in GitHub Desktop.
PlayerBreakBlockEvent
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package io.github.daomephsta.polar.client.mixin; | |
import org.spongepowered.asm.mixin.Final; | |
import org.spongepowered.asm.mixin.Mixin; | |
import org.spongepowered.asm.mixin.Shadow; | |
import org.spongepowered.asm.mixin.injection.At; | |
import org.spongepowered.asm.mixin.injection.Inject; | |
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; | |
import org.spongepowered.asm.mixin.injection.callback.LocalCapture; | |
import io.github.daomephsta.polar.common.callbacks.PlayerBreakBlockCallback; | |
import net.minecraft.block.Block; | |
import net.minecraft.block.BlockState; | |
import net.minecraft.client.MinecraftClient; | |
import net.minecraft.client.network.ClientPlayNetworkHandler; | |
import net.minecraft.client.network.ClientPlayerInteractionManager; | |
import net.minecraft.util.math.BlockPos; | |
import net.minecraft.world.World; | |
@Mixin(ClientPlayerInteractionManager.class) | |
public class MixinClientPlayerInteractionManager | |
{ | |
@Shadow | |
private @Final ClientPlayNetworkHandler networkHandler; | |
@Shadow | |
private @Final MinecraftClient client; | |
@Inject(method = "net/minecraft/client/network/ClientPlayerInteractionManager.breakBlock(Lnet/minecraft/util/math/BlockPos;)Z", | |
at = @At(value = "INVOKE", target = "net/minecraft/block/Block.onBreak(Lnet/minecraft/world/World;Lnet/minecraft/util/math/BlockPos;Lnet/minecraft/block/BlockState;Lnet/minecraft/entity/player/PlayerEntity;)V"), | |
locals = LocalCapture.CAPTURE_FAILHARD, | |
cancellable = true) | |
public void polar_tryBreakBlock(BlockPos pos, CallbackInfoReturnable<Boolean> info, World world, BlockState state, Block block) | |
{ | |
if (!PlayerBreakBlockCallback.EVENT.invoker().tryBreakBlock(world, client.player, pos, state)) | |
info.setReturnValue(false); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package io.github.daomephsta.polar.server.mixin; | |
import org.spongepowered.asm.mixin.Mixin; | |
import org.spongepowered.asm.mixin.Shadow; | |
import org.spongepowered.asm.mixin.injection.At; | |
import org.spongepowered.asm.mixin.injection.Inject; | |
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; | |
import org.spongepowered.asm.mixin.injection.callback.LocalCapture; | |
import io.github.daomephsta.polar.common.callbacks.PlayerBreakBlockCallback; | |
import net.minecraft.block.Block; | |
import net.minecraft.block.BlockState; | |
import net.minecraft.server.network.ServerPlayerEntity; | |
import net.minecraft.server.network.ServerPlayerInteractionManager; | |
import net.minecraft.server.world.ServerWorld; | |
import net.minecraft.util.math.BlockPos; | |
import net.minecraft.world.World; | |
@Mixin(ServerPlayerInteractionManager.class) | |
public class MixinServerPlayerInteractionManager | |
{ | |
@Shadow | |
public ServerWorld world; | |
@Shadow | |
public ServerPlayerEntity player; | |
@Inject(method = "net/minecraft/server/network/ServerPlayerInteractionManager.tryBreakBlock(Lnet/minecraft/util/math/BlockPos;)Z", | |
at = @At(value = "INVOKE", target = "net/minecraft/block/Block.onBreak(Lnet/minecraft/world/World;Lnet/minecraft/util/math/BlockPos;Lnet/minecraft/block/BlockState;Lnet/minecraft/entity/player/PlayerEntity;)V"), | |
locals = LocalCapture.CAPTURE_FAILHARD, | |
cancellable = true) | |
public void polar_tryBreakBlock(BlockPos pos, CallbackInfoReturnable<Boolean> info, World world, BlockState state, Block block) | |
{ | |
if (!PlayerBreakBlockCallback.EVENT.invoker().tryBreakBlock(world, player, pos, state)) | |
info.setReturnValue(false); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package io.github.daomephsta.polar.common.callbacks; | |
import net.fabricmc.fabric.api.event.Event; | |
import net.fabricmc.fabric.api.event.EventFactory; | |
import net.minecraft.block.BlockState; | |
import net.minecraft.entity.player.PlayerEntity; | |
import net.minecraft.util.math.BlockPos; | |
import net.minecraft.world.World; | |
public interface PlayerBreakBlockCallback | |
{ | |
public static final Event<PlayerBreakBlockCallback> EVENT = EventFactory.createArrayBacked(PlayerBreakBlockCallback.class, | |
(callbacks) -> (world, player, pos, state) -> | |
{ | |
for (PlayerBreakBlockCallback callback : callbacks) | |
{ | |
if (!callback.tryBreakBlock(world, player, pos, state)) | |
return false; | |
} | |
return true; | |
}); | |
public boolean tryBreakBlock(World world, PlayerEntity playerEntity, BlockPos pos, BlockState state); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment