make mixin working on block break
This commit is contained in:
		| @@ -14,4 +14,4 @@ org.gradle.jvmargs=-Xmx1G | |||||||
|  |  | ||||||
| # Dependencies | # Dependencies | ||||||
| 	# currently not on the main fabric site, check on the maven: https://maven.fabricmc.net/net/fabricmc/fabric-api/fabric-api | 	# currently not on the main fabric site, check on the maven: https://maven.fabricmc.net/net/fabricmc/fabric-api/fabric-api | ||||||
| 	fabric_version=0.13.1+build.370-1.16 | 	fabric_version=0.16.2+build.385-1.16.1 | ||||||
|   | |||||||
| @@ -4,7 +4,7 @@ import java.util.Arrays; | |||||||
| import java.util.List; | import java.util.List; | ||||||
| import java.util.Random; | import java.util.Random; | ||||||
|  |  | ||||||
| import de.jottyfan.minecraft.quickiefabric.items.Items; | import de.jottyfan.minecraft.quickiefabric.items.QuickieItems; | ||||||
| import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings; | import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings; | ||||||
| import net.minecraft.block.BlockState; | import net.minecraft.block.BlockState; | ||||||
| import net.minecraft.block.Material; | import net.minecraft.block.Material; | ||||||
| @@ -25,7 +25,7 @@ public class BlockOreNetherSulphor extends OreBlock { | |||||||
|  |  | ||||||
| 	@Override | 	@Override | ||||||
| 	public List<ItemStack> getDroppedStacks(BlockState state, Builder builder) { | 	public List<ItemStack> getDroppedStacks(BlockState state, Builder builder) { | ||||||
| 		return Arrays.asList(new ItemStack[] { new ItemStack(Items.SULPHOR) }); | 		return Arrays.asList(new ItemStack[] { new ItemStack(QuickieItems.SULPHOR) }); | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	@Override | 	@Override | ||||||
|   | |||||||
| @@ -4,7 +4,7 @@ import java.util.Arrays; | |||||||
| import java.util.List; | import java.util.List; | ||||||
| import java.util.Random; | import java.util.Random; | ||||||
|  |  | ||||||
| import de.jottyfan.minecraft.quickiefabric.items.Items; | import de.jottyfan.minecraft.quickiefabric.items.QuickieItems; | ||||||
| import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings; | import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings; | ||||||
| import net.minecraft.block.BlockState; | import net.minecraft.block.BlockState; | ||||||
| import net.minecraft.block.Material; | import net.minecraft.block.Material; | ||||||
| @@ -25,7 +25,7 @@ public class BlockOreSalpeter extends OreBlock { | |||||||
|  |  | ||||||
| 	@Override | 	@Override | ||||||
| 	public List<ItemStack> getDroppedStacks(BlockState state, Builder builder) { | 	public List<ItemStack> getDroppedStacks(BlockState state, Builder builder) { | ||||||
| 		return Arrays.asList(new ItemStack[] { new ItemStack(Items.SALPETER) }); | 		return Arrays.asList(new ItemStack[] { new ItemStack(QuickieItems.SALPETER) }); | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	@Override | 	@Override | ||||||
|   | |||||||
| @@ -4,7 +4,7 @@ import java.util.Arrays; | |||||||
| import java.util.List; | import java.util.List; | ||||||
| import java.util.Random; | import java.util.Random; | ||||||
|  |  | ||||||
| import de.jottyfan.minecraft.quickiefabric.items.Items; | import de.jottyfan.minecraft.quickiefabric.items.QuickieItems; | ||||||
| import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings; | import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings; | ||||||
| import net.minecraft.block.BlockState; | import net.minecraft.block.BlockState; | ||||||
| import net.minecraft.block.Blocks; | import net.minecraft.block.Blocks; | ||||||
| @@ -26,7 +26,7 @@ public class BlockOreSandSalpeter extends OreBlock { | |||||||
|  |  | ||||||
| 	@Override | 	@Override | ||||||
| 	public List<ItemStack> getDroppedStacks(BlockState state, Builder builder) { | 	public List<ItemStack> getDroppedStacks(BlockState state, Builder builder) { | ||||||
| 		return Arrays.asList(new ItemStack[] { new ItemStack(Items.SALPETER), new ItemStack(Blocks.SAND) }); | 		return Arrays.asList(new ItemStack[] { new ItemStack(QuickieItems.SALPETER), new ItemStack(Blocks.SAND) }); | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	@Override | 	@Override | ||||||
|   | |||||||
| @@ -4,7 +4,7 @@ import java.util.Arrays; | |||||||
| import java.util.List; | import java.util.List; | ||||||
| import java.util.Random; | import java.util.Random; | ||||||
|  |  | ||||||
| import de.jottyfan.minecraft.quickiefabric.items.Items; | import de.jottyfan.minecraft.quickiefabric.items.QuickieItems; | ||||||
| import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings; | import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings; | ||||||
| import net.minecraft.block.BlockState; | import net.minecraft.block.BlockState; | ||||||
| import net.minecraft.block.Material; | import net.minecraft.block.Material; | ||||||
| @@ -25,7 +25,7 @@ public class BlockOreSulphor extends OreBlock { | |||||||
|  |  | ||||||
| 	@Override | 	@Override | ||||||
| 	public List<ItemStack> getDroppedStacks(BlockState state, Builder builder) { | 	public List<ItemStack> getDroppedStacks(BlockState state, Builder builder) { | ||||||
| 		return Arrays.asList(new ItemStack[] { new ItemStack(Items.SULPHOR) }); | 		return Arrays.asList(new ItemStack[] { new ItemStack(QuickieItems.SULPHOR) }); | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	@Override | 	@Override | ||||||
|   | |||||||
| @@ -3,7 +3,7 @@ package de.jottyfan.minecraft.quickiefabric.blocks; | |||||||
| import java.util.Arrays; | import java.util.Arrays; | ||||||
| import java.util.List; | import java.util.List; | ||||||
|  |  | ||||||
| import de.jottyfan.minecraft.quickiefabric.items.Items; | import de.jottyfan.minecraft.quickiefabric.items.QuickieItems; | ||||||
| import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings; | import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings; | ||||||
| import net.minecraft.block.BlockState; | import net.minecraft.block.BlockState; | ||||||
| import net.minecraft.block.Blocks; | import net.minecraft.block.Blocks; | ||||||
| @@ -25,6 +25,6 @@ public class BlockSandSalpeter extends GravelBlock { | |||||||
|  |  | ||||||
| 	@Override | 	@Override | ||||||
| 	public List<ItemStack> getDroppedStacks(BlockState state, Builder builder) { | 	public List<ItemStack> getDroppedStacks(BlockState state, Builder builder) { | ||||||
| 			return Arrays.asList(new ItemStack[] { new ItemStack(Items.SALPETER), new ItemStack(Blocks.SAND) }); | 			return Arrays.asList(new ItemStack[] { new ItemStack(QuickieItems.SALPETER), new ItemStack(Blocks.SAND) }); | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
|   | |||||||
| @@ -1,32 +0,0 @@ | |||||||
| package de.jottyfan.minecraft.quickiefabric.event; |  | ||||||
|  |  | ||||||
| import org.spongepowered.asm.mixin.Mixin; |  | ||||||
| import org.spongepowered.asm.mixin.injection.At; |  | ||||||
| import org.spongepowered.asm.mixin.injection.Inject; |  | ||||||
| import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; |  | ||||||
| import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; |  | ||||||
| import org.spongepowered.asm.mixin.injection.callback.LocalCapture; |  | ||||||
|  |  | ||||||
| import net.minecraft.block.Block; |  | ||||||
| import net.minecraft.block.BlockState; |  | ||||||
| import net.minecraft.entity.player.PlayerEntity; |  | ||||||
| import net.minecraft.util.ActionResult; |  | ||||||
| import net.minecraft.util.math.BlockPos; |  | ||||||
| import net.minecraft.world.World; |  | ||||||
|  |  | ||||||
| /** |  | ||||||
|  *  |  | ||||||
|  * @author jotty |  | ||||||
|  * |  | ||||||
|  */ |  | ||||||
| @Mixin(Block.class) |  | ||||||
| public class BlockBreakMixin { |  | ||||||
| //	@Inject(method = "injectBlockBreakCallback", at = @At(value = "INVOKE", target = "Lnet/minecraft/block/Block;onBreak(Lnet/minecraft/world/World;Lnet/minecraft/util/math/BlockPos;Lnet/minecraft/block/BlockState;Lnet/minecraft/entity/player/PlayerEntity;)V"), cancellable = true) |  | ||||||
| 	@Inject(method = "injectBlockBreakCallback", at = @At(value = "INVOKE", target = "Lnet/minecraft/block/Block;onBroken(Lnet/minecraft/world/World;Lnet/minecraft/util/math/BlockPos;Lnet/minecraft/block/BlockState;)V"), cancellable = true) |  | ||||||
| 	private void interactOnBreak(final World world, final BlockPos blockPos, final BlockState blockState, final CallbackInfo info) { |  | ||||||
| 		ActionResult result = BreakBlockCallback.EVENT.invoker().injectBlockBreakCallback(world, blockPos, blockState); |  | ||||||
| 		if (result == ActionResult.FAIL) { |  | ||||||
| 			info.cancel(); |  | ||||||
| 		} |  | ||||||
| 	} |  | ||||||
| } |  | ||||||
| @@ -15,9 +15,9 @@ import net.minecraft.world.World; | |||||||
|  */ |  */ | ||||||
| public interface BreakBlockCallback {	 | public interface BreakBlockCallback {	 | ||||||
| 	Event<BreakBlockCallback> EVENT = EventFactory.createArrayBacked(BreakBlockCallback.class, | 	Event<BreakBlockCallback> EVENT = EventFactory.createArrayBacked(BreakBlockCallback.class, | ||||||
| 		(listeners) -> (world, blockPos, blockState) -> { | 		(listeners) -> (world, blockPos, blockState, playerEntity) -> { | ||||||
| 			for (BreakBlockCallback listener : listeners) { | 			for (BreakBlockCallback listener : listeners) { | ||||||
| 				ActionResult result = listener.injectBlockBreakCallback(world, blockPos, blockState); | 				ActionResult result = listener.injectBlockBreakCallback(world, blockPos, blockState, playerEntity); | ||||||
| 				if (result != ActionResult.PASS) { | 				if (result != ActionResult.PASS) { | ||||||
| 					return result; | 					return result; | ||||||
| 				} | 				} | ||||||
| @@ -25,5 +25,5 @@ public interface BreakBlockCallback { | |||||||
| 			return ActionResult.PASS; | 			return ActionResult.PASS; | ||||||
| 		}); | 		}); | ||||||
| 		 | 		 | ||||||
| 	ActionResult injectBlockBreakCallback(World world, BlockPos blockPos, BlockState blockState); | 	ActionResult injectBlockBreakCallback(World world, BlockPos blockPos, BlockState blockState, PlayerEntity playerEntity); | ||||||
| } | } | ||||||
|   | |||||||
| @@ -7,11 +7,12 @@ import org.apache.logging.log4j.Logger; | |||||||
|  |  | ||||||
| import de.jottyfan.minecraft.quickiefabric.blocks.QuickieBlocks; | import de.jottyfan.minecraft.quickiefabric.blocks.QuickieBlocks; | ||||||
| import de.jottyfan.minecraft.quickiefabric.event.BreakBlockCallback; | import de.jottyfan.minecraft.quickiefabric.event.BreakBlockCallback; | ||||||
| import de.jottyfan.minecraft.quickiefabric.items.Items; | import de.jottyfan.minecraft.quickiefabric.items.QuickieItems; | ||||||
| import de.jottyfan.minecraft.quickiefabric.tools.Tools; | import de.jottyfan.minecraft.quickiefabric.tools.QuickieTools; | ||||||
| import net.fabricmc.fabric.api.client.itemgroup.FabricItemGroupBuilder; | import net.fabricmc.fabric.api.client.itemgroup.FabricItemGroupBuilder; | ||||||
| import net.minecraft.block.Block; | import net.minecraft.block.Block; | ||||||
| import net.minecraft.block.BlockState; | import net.minecraft.block.BlockState; | ||||||
|  | import net.minecraft.block.Blocks; | ||||||
| import net.minecraft.item.BlockItem; | import net.minecraft.item.BlockItem; | ||||||
| import net.minecraft.item.Item; | import net.minecraft.item.Item; | ||||||
| import net.minecraft.item.ItemGroup; | import net.minecraft.item.ItemGroup; | ||||||
| @@ -37,13 +38,13 @@ public class RegistryManager { | |||||||
|  |  | ||||||
| 	private static final String QUICKIEFABRIC = "quickiefabric"; | 	private static final String QUICKIEFABRIC = "quickiefabric"; | ||||||
|  |  | ||||||
| 	public static final ItemGroup QUICKIEFABRIC_GROUP = FabricItemGroupBuilder.create(new Identifier(QUICKIEFABRIC, "all")).icon(() -> new ItemStack(Items.SPEEDPOWDER)).appendItems(stacks -> { | 	public static final ItemGroup QUICKIEFABRIC_GROUP = FabricItemGroupBuilder.create(new Identifier(QUICKIEFABRIC, "all")).icon(() -> new ItemStack(QuickieItems.SPEEDPOWDER)).appendItems(stacks -> { | ||||||
| 		stacks.add(new ItemStack(Items.SALPETER)); | 		stacks.add(new ItemStack(QuickieItems.SALPETER)); | ||||||
| 		stacks.add(new ItemStack(Items.SULPHOR)); | 		stacks.add(new ItemStack(QuickieItems.SULPHOR)); | ||||||
| 		stacks.add(new ItemStack(Items.SPEEDPOWDER)); | 		stacks.add(new ItemStack(QuickieItems.SPEEDPOWDER)); | ||||||
| 		stacks.add(new ItemStack(Items.LEVELUP)); | 		stacks.add(new ItemStack(QuickieItems.LEVELUP)); | ||||||
| 		stacks.add(new ItemStack(Items.PENCIL)); | 		stacks.add(new ItemStack(QuickieItems.PENCIL)); | ||||||
| 		stacks.add(new ItemStack(Tools.SPEEDPOWDERAXE)); | 		stacks.add(new ItemStack(QuickieTools.SPEEDPOWDERAXE)); | ||||||
| 		stacks.add(new ItemStack(QuickieBlocks.ORE_NETHER_SULPHOR)); | 		stacks.add(new ItemStack(QuickieBlocks.ORE_NETHER_SULPHOR)); | ||||||
| 		stacks.add(new ItemStack(QuickieBlocks.ORE_SALPETER)); | 		stacks.add(new ItemStack(QuickieBlocks.ORE_SALPETER)); | ||||||
| 		stacks.add(new ItemStack(QuickieBlocks.ORE_SAND_SALPETER)); | 		stacks.add(new ItemStack(QuickieBlocks.ORE_SAND_SALPETER)); | ||||||
| @@ -71,25 +72,36 @@ public class RegistryManager { | |||||||
|  |  | ||||||
| 	public static final void registerItems() { | 	public static final void registerItems() { | ||||||
| 		LOGGER.debug("registering quickiefabric items"); | 		LOGGER.debug("registering quickiefabric items"); | ||||||
| 		registerItem(Items.SPEEDPOWDER, "speedpowder"); | 		registerItem(QuickieItems.SPEEDPOWDER, "speedpowder"); | ||||||
| 		registerItem(Items.LEVELUP, "levelup"); | 		registerItem(QuickieItems.LEVELUP, "levelup"); | ||||||
| 		registerItem(Items.PENCIL, "pencil"); | 		registerItem(QuickieItems.PENCIL, "pencil"); | ||||||
| 		registerItem(Items.SALPETER, "salpeter"); | 		registerItem(QuickieItems.SALPETER, "salpeter"); | ||||||
| 		registerItem(Items.SULPHOR, "sulphor"); | 		registerItem(QuickieItems.SULPHOR, "sulphor"); | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	public static final void registerTools() { | 	public static final void registerTools() { | ||||||
| 		LOGGER.debug("registering quickiefabric tools"); | 		LOGGER.debug("registering quickiefabric tools"); | ||||||
| 		registerItem(Tools.SPEEDPOWDERAXE, "speedpowderaxe"); | 		registerItem(QuickieTools.SPEEDPOWDERAXE, "speedpowderaxe"); | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	public static final void registerEvents() { | 	public static final void registerEvents() { | ||||||
| 		LOGGER.debug("registering quickiefabric events"); | 		LOGGER.debug("registering quickiefabric events"); | ||||||
| 		BreakBlockCallback.EVENT.register((world, blockPos, blockState) -> { | 		BreakBlockCallback.EVENT.register((world, blockPos, blockState, playerEntity) -> { | ||||||
| 			// TODO: add code to break the corresponding surroundings also if hand hold the right tool | 			// TODO: add code to break the corresponding surroundings also if hand hold the right tool | ||||||
| 			// return ActionResult.PASS; // if the breaking replaces another event, but this does not appear for the speedpowder tools | 			// return ActionResult.PASS; // if the breaking replaces another event, but this does not appear for the speedpowder tools | ||||||
|  |  | ||||||
| 			LOGGER.info("broke block %s at %s", blockState.getBlock().getName(), blockPos.toString()); | 			LOGGER.info("{} broke block {} at {}", playerEntity.getName().asString(), blockState.getBlock().getTranslationKey(), blockPos.toString()); | ||||||
|  | 			BlockState blockStateDown = world.getBlockState(blockPos); | ||||||
|  | 			 | ||||||
|  | 			if (!blockStateDown.isAir()) { // TODO: because we will only break the same blocks, that check becomes obsolete later | ||||||
|  | 				ItemStack itemStack = new ItemStack(blockStateDown.getBlock().asItem()); | ||||||
|  | 				// spawn entity in world | ||||||
|  | 				Block.dropStack(world, blockPos.down(), itemStack); | ||||||
|  | 				world.setBlockState(blockPos.down(), Blocks.AIR.getDefaultState()); | ||||||
|  | 				 | ||||||
|  | 				LOGGER.info("also broke block {} at {}", playerEntity.getName().asString(), blockState.getBlock().getTranslationKey(), blockPos.down().toString()); | ||||||
|  | 			} | ||||||
|  | 			 | ||||||
| 			return ActionResult.SUCCESS; | 			return ActionResult.SUCCESS; | ||||||
| 		}); | 		}); | ||||||
| 	} | 	} | ||||||
|   | |||||||
| @@ -5,7 +5,7 @@ package de.jottyfan.minecraft.quickiefabric.items; | |||||||
|  * @author jotty |  * @author jotty | ||||||
|  * |  * | ||||||
|  */ |  */ | ||||||
| public class Items { | public class QuickieItems { | ||||||
| 	public static final ItemSpeedpowder SPEEDPOWDER = new ItemSpeedpowder(); | 	public static final ItemSpeedpowder SPEEDPOWDER = new ItemSpeedpowder(); | ||||||
| 	public static final ItemLevelup LEVELUP = new ItemLevelup(); | 	public static final ItemLevelup LEVELUP = new ItemLevelup(); | ||||||
| 	public static final ItemPencil PENCIL = new ItemPencil(); | 	public static final ItemPencil PENCIL = new ItemPencil(); | ||||||
| @@ -0,0 +1,31 @@ | |||||||
|  | package de.jottyfan.minecraft.quickiefabric.mixin; | ||||||
|  |  | ||||||
|  | import org.spongepowered.asm.mixin.Mixin; | ||||||
|  | import org.spongepowered.asm.mixin.injection.At; | ||||||
|  | import org.spongepowered.asm.mixin.injection.Inject; | ||||||
|  | import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; | ||||||
|  |  | ||||||
|  | import de.jottyfan.minecraft.quickiefabric.event.BreakBlockCallback; | ||||||
|  | import net.minecraft.block.Block; | ||||||
|  | import net.minecraft.block.BlockState; | ||||||
|  | import net.minecraft.entity.player.PlayerEntity; | ||||||
|  | import net.minecraft.util.ActionResult; | ||||||
|  | import net.minecraft.util.math.BlockPos; | ||||||
|  | import net.minecraft.world.World; | ||||||
|  |  | ||||||
|  | /** | ||||||
|  |  *  | ||||||
|  |  * @author jotty | ||||||
|  |  * | ||||||
|  |  */ | ||||||
|  | @Mixin(Block.class) | ||||||
|  | public class BlockBreakMixin { | ||||||
|  |  | ||||||
|  | 	@Inject(at = @At("HEAD"), method = "onBreak(Lnet/minecraft/world/World;Lnet/minecraft/util/math/BlockPos;Lnet/minecraft/block/BlockState;Lnet/minecraft/entity/player/PlayerEntity;)V") | ||||||
|  | 	private void onBreak(final World world, final BlockPos blockPos, final BlockState blockState, final PlayerEntity playerEntity, final CallbackInfo info) { | ||||||
|  | 		ActionResult result = BreakBlockCallback.EVENT.invoker().injectBlockBreakCallback(world, blockPos, blockState, playerEntity); | ||||||
|  | 		if (result == ActionResult.FAIL) { | ||||||
|  | 			info.cancel(); | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  | } | ||||||
| @@ -5,6 +5,6 @@ package de.jottyfan.minecraft.quickiefabric.tools; | |||||||
|  * @author jotty |  * @author jotty | ||||||
|  * |  * | ||||||
|  */ |  */ | ||||||
| public class Tools { | public class QuickieTools { | ||||||
| 	public static final ToolSpeedpowderAxe SPEEDPOWDERAXE = new ToolSpeedpowderAxe(); | 	public static final ToolSpeedpowderAxe SPEEDPOWDERAXE = new ToolSpeedpowderAxe(); | ||||||
| } | } | ||||||
| @@ -23,7 +23,7 @@ | |||||||
|     ] |     ] | ||||||
|   }, |   }, | ||||||
|   "mixins": [ |   "mixins": [ | ||||||
|     // "quickiefabric.mixins.json" |     "modid.mixins.json" | ||||||
|   ], |   ], | ||||||
|   "depends": { |   "depends": { | ||||||
|     "fabricloader": ">=0.7.4", |     "fabricloader": ">=0.7.4", | ||||||
|   | |||||||
| @@ -1,7 +1,7 @@ | |||||||
| { | { | ||||||
|   "required": true, |   "required": true, | ||||||
|   "minVersion": "0.8", |   "minVersion": "0.8", | ||||||
|   "package": "de.jottyfan.minecraft.quickiefabric.event", |   "package": "de.jottyfan.minecraft.quickiefabric.mixin", | ||||||
|   "compatibilityLevel": "JAVA_8", |   "compatibilityLevel": "JAVA_8", | ||||||
|   "mixins": [ |   "mixins": [ | ||||||
|     "BlockBreakMixin" |     "BlockBreakMixin" | ||||||
		Reference in New Issue
	
	Block a user