diff options
author | Keuin <[email protected]> | 2020-12-24 23:11:36 +0800 |
---|---|---|
committer | Keuin <[email protected]> | 2020-12-24 23:11:36 +0800 |
commit | 9163a994959797df64d6cbb77a8ede60150f4e58 (patch) | |
tree | 895b10f58f5f68d1a17c1ad7ca24f31260551d97 | |
parent | 5b76ebe34e5237f48a12baa70b33105c02fc9d5f (diff) |
1.1.0-alpha: now handles block break events
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | gradle.properties | 2 | ||||
-rw-r--r-- | src/main/java/com/keuin/blame/Blame.java | 2 | ||||
-rw-r--r-- | src/main/java/com/keuin/blame/EventHandler.java | 33 | ||||
-rw-r--r-- | src/main/java/com/keuin/blame/adapter/handler/PlaceBlockHandler.java | 22 | ||||
-rw-r--r-- | src/main/java/com/keuin/blame/mixin/BlockPlaceMixin.java | 30 | ||||
-rw-r--r-- | src/main/java/com/keuin/blame/mixin/ExampleMixin.java | 15 | ||||
-rw-r--r-- | src/main/resources/blame.mixins.json | 2 |
8 files changed, 82 insertions, 25 deletions
@@ -31,4 +31,5 @@ bin/ # fabric run/ +run.1152 logs/
\ No newline at end of file diff --git a/gradle.properties b/gradle.properties index da819eb..2b79e6f 100644 --- a/gradle.properties +++ b/gradle.properties @@ -6,7 +6,7 @@ minecraft_version=1.16.4 yarn_mappings=1.16.4+build.6 loader_version=0.10.6+build.214 # Mod Properties -mod_version=1.0.0 +mod_version=1.1.0-alpha maven_group=com.keuin.blame archives_base_name=blame-fabric # Dependencies diff --git a/src/main/java/com/keuin/blame/Blame.java b/src/main/java/com/keuin/blame/Blame.java index 6ecfa97..095d634 100644 --- a/src/main/java/com/keuin/blame/Blame.java +++ b/src/main/java/com/keuin/blame/Blame.java @@ -2,6 +2,7 @@ package com.keuin.blame; import com.google.gson.Gson; import com.keuin.blame.adapter.*; +import com.keuin.blame.adapter.handler.PlaceBlockHandler; import com.keuin.blame.command.BlameBlockCommand; import com.keuin.blame.config.BlameConfig; import com.keuin.blame.lookup.LookupManager; @@ -73,6 +74,7 @@ public class Blame implements ModInitializer { // hook game events AttackEntityCallback.EVENT.register(new AttackEntityAdapter(EventHandler.INSTANCE)); + PlaceBlockHandler.EVENT.register(EventHandler.INSTANCE); PlayerBlockBreakEvents.AFTER.register(new BreakBlockAdapter(EventHandler.INSTANCE)); UseBlockCallback.EVENT.register(new UseBlockAdapter(EventHandler.INSTANCE)); UseEntityCallback.EVENT.register(new UseEntityAdapter(EventHandler.INSTANCE)); diff --git a/src/main/java/com/keuin/blame/EventHandler.java b/src/main/java/com/keuin/blame/EventHandler.java index f2e4f09..87cf2d5 100644 --- a/src/main/java/com/keuin/blame/EventHandler.java +++ b/src/main/java/com/keuin/blame/EventHandler.java @@ -5,7 +5,6 @@ import com.keuin.blame.data.LogEntryFactory; import com.keuin.blame.data.entry.LogEntry; import com.keuin.blame.data.enums.ActionType; import com.keuin.blame.util.MinecraftUtil; -import com.keuin.blame.util.PrintUtil; import net.minecraft.block.BlockState; import net.minecraft.block.entity.BlockEntity; import net.minecraft.entity.Entity; @@ -18,7 +17,7 @@ import net.minecraft.util.registry.Registry; import net.minecraft.world.World; import org.jetbrains.annotations.Nullable; -public class EventHandler implements AttackEntityHandler, BreakBlockHandler, UseBlockHandler, UseEntityHandler, UseItemHandler { +public class EventHandler implements AttackEntityHandler, PlaceBlockHandler, BreakBlockHandler, UseBlockHandler, UseEntityHandler, UseItemHandler { public static final EventHandler INSTANCE = new EventHandler(); @@ -38,12 +37,29 @@ public class EventHandler implements AttackEntityHandler, BreakBlockHandler, Use ActionType.BLOCK_USE ); SubmitWorker.INSTANCE.submit(entry); - PrintUtil.broadcast("use_block; block_id=" + blockId + "; world=" + worldString); +// PrintUtil.broadcast("use_block; block_id=" + blockId + "; world=" + worldString); // TODO: 增加判断,事件触发的时候用户不一定真正使用了方块(也可能是无效的动作)。放置方块的时候也会触发这个事件 // PrintUtil.broadcast(String.format("player %s use block %s", playerEntity.getName().getString(), world.getBlockState(blockHitResult.getBlockPos()))); } @Override + public void onPlayerPlaceBlock(World world, PlayerEntity playerEntity, BlockPos blockPos, BlockState blockState, BlockEntity blockEntity) { + String blockId = Registry.BLOCK.getId(blockState.getBlock()).toString(); + String worldString = MinecraftUtil.worldToString(world); + LogEntry entry = LogEntryFactory.playerWithBlock( + playerEntity, + worldString, + blockId, + blockPos, + worldString, + ActionType.BLOCK_PLACE + ); + SubmitWorker.INSTANCE.submit(entry); +// PrintUtil.broadcast(String.format("place_block; world=%s, player_entity=%s, block_pos=%s, block_state=%s, block_entity=%s", +// world, playerEntity, blockPos, blockState, blockEntity)); + } + + @Override public void onPlayerBreakBlock(World world, PlayerEntity playerEntity, BlockPos blockPos, BlockState blockState, BlockEntity blockEntity) { String worldString = MinecraftUtil.worldToString(world); String blockId = Registry.BLOCK.getId(blockState.getBlock()).toString(); @@ -56,7 +72,7 @@ public class EventHandler implements AttackEntityHandler, BreakBlockHandler, Use ActionType.BLOCK_BREAK ); SubmitWorker.INSTANCE.submit(entry); - PrintUtil.broadcast("break_block; block_id=" + blockId + "; world=" + worldString); +// PrintUtil.broadcast("break_block; block_id=" + blockId + "; world=" + worldString); // PrintUtil.broadcast(String.format("player %s break block %s", playerEntity.getName().getString(), blockState)); } @@ -73,7 +89,7 @@ public class EventHandler implements AttackEntityHandler, BreakBlockHandler, Use ActionType.ENTITY_ATTACK ); SubmitWorker.INSTANCE.submit(entry); - PrintUtil.broadcast("attack_entity; entity_id=" + entityId); +// PrintUtil.broadcast("attack_entity; entity_id=" + entityId); // PrintUtil.broadcast(String.format("player %s attack entity %s", playerEntity.getName().getString(), entity)); } @@ -90,10 +106,10 @@ public class EventHandler implements AttackEntityHandler, BreakBlockHandler, Use ActionType.ENTITY_USE ); SubmitWorker.INSTANCE.submit(entry); - PrintUtil.broadcast("use_entity; entity_id=" + entityId); +// PrintUtil.broadcast("use_entity; entity_id=" + entityId); // TODO: 增加判断,无效的时候也会触发这个事件 // TODO: 增加cooldown,过滤掉两个相邻重复事件(时间间隔大概为20ms+) - PrintUtil.broadcast(String.format("player %s use entity %s", playerEntity.getName().getString(), entity)); +// PrintUtil.broadcast(String.format("player %s use entity %s", playerEntity.getName().getString(), entity)); } @Override @@ -106,8 +122,9 @@ public class EventHandler implements AttackEntityHandler, BreakBlockHandler, Use ActionType.ITEM_USE ); SubmitWorker.INSTANCE.submit(entry); - PrintUtil.broadcast("use_item; item_id=" + itemId); +// PrintUtil.broadcast("use_item; item_id=" + itemId); // TODO: 增加cooldown,过滤掉两个相邻重复事件(时间间隔大概为20ms+) // PrintUtil.broadcast(String.format("player %s use item %s", playerEntity.getName().getString(), playerEntity.getStackInHand(hand))); } + } diff --git a/src/main/java/com/keuin/blame/adapter/handler/PlaceBlockHandler.java b/src/main/java/com/keuin/blame/adapter/handler/PlaceBlockHandler.java new file mode 100644 index 0000000..fa5bab5 --- /dev/null +++ b/src/main/java/com/keuin/blame/adapter/handler/PlaceBlockHandler.java @@ -0,0 +1,22 @@ +package com.keuin.blame.adapter.handler; + +import net.fabricmc.fabric.api.event.Event; +import net.fabricmc.fabric.api.event.EventFactory; +import net.minecraft.block.BlockState; +import net.minecraft.block.entity.BlockEntity; +import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.util.math.BlockPos; +import net.minecraft.world.World; +import org.jetbrains.annotations.Nullable; + +public interface PlaceBlockHandler { + + Event<PlaceBlockHandler> EVENT = EventFactory.createArrayBacked(PlaceBlockHandler.class, + (listeners) -> (world, playerEntity, blockPos, blockState, blockEntity) -> { + for (PlaceBlockHandler listener : listeners) { + listener.onPlayerPlaceBlock(world, playerEntity, blockPos, blockState, blockEntity); + } + }); + + void onPlayerPlaceBlock(World world, @Nullable PlayerEntity playerEntity, BlockPos blockPos, BlockState blockState, BlockEntity blockEntity); +} diff --git a/src/main/java/com/keuin/blame/mixin/BlockPlaceMixin.java b/src/main/java/com/keuin/blame/mixin/BlockPlaceMixin.java new file mode 100644 index 0000000..adee24c --- /dev/null +++ b/src/main/java/com/keuin/blame/mixin/BlockPlaceMixin.java @@ -0,0 +1,30 @@ +package com.keuin.blame.mixin; + +import com.keuin.blame.adapter.handler.PlaceBlockHandler; +import net.minecraft.advancement.criterion.AbstractCriterion; +import net.minecraft.advancement.criterion.PlacedBlockCriterion; +import net.minecraft.item.ItemStack; +import net.minecraft.server.network.ServerPlayerEntity; +import net.minecraft.util.math.BlockPos; +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; + +/** + * Custom event driver for player placing blocks + */ +@Mixin(PlacedBlockCriterion.class) +public abstract class BlockPlaceMixin extends AbstractCriterion<PlacedBlockCriterion.Conditions> { + + @Inject(at = @At(value = "HEAD"), method = "trigger") + public void trigger(ServerPlayerEntity player, BlockPos blockPos, ItemStack stack, CallbackInfo ci) { + PlaceBlockHandler.EVENT.invoker().onPlayerPlaceBlock( + player.world, + player, + blockPos, + player.world.getBlockState(blockPos), + player.world.getBlockEntity(blockPos) + ); + } +}
\ No newline at end of file diff --git a/src/main/java/com/keuin/blame/mixin/ExampleMixin.java b/src/main/java/com/keuin/blame/mixin/ExampleMixin.java deleted file mode 100644 index ecd713b..0000000 --- a/src/main/java/com/keuin/blame/mixin/ExampleMixin.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.keuin.blame.mixin; - -import net.minecraft.client.gui.screen.TitleScreen; -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; - -@Mixin(TitleScreen.class) -public class ExampleMixin { - @Inject(at = @At("HEAD"), method = "init()V") - private void init(CallbackInfo info) { - System.out.println("This line is printed by an example mod mixin!"); - } -} diff --git a/src/main/resources/blame.mixins.json b/src/main/resources/blame.mixins.json index ffac63d..9b70e23 100644 --- a/src/main/resources/blame.mixins.json +++ b/src/main/resources/blame.mixins.json @@ -7,6 +7,6 @@ "defaultRequire": 1 }, "mixins": [ - "ExampleMixin" + "BlockPlaceMixin" ] } |