Skip to content

Commit

Permalink
✨ 使用mixin进行鼠标按键监听
Browse files Browse the repository at this point in the history
  • Loading branch information
Takaranoao committed Apr 6, 2024
1 parent 18ec4c7 commit 2a7286e
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 4 deletions.
47 changes: 43 additions & 4 deletions common/src/main/java/com/takaranoao/donothitme/DoNotHitMe.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,17 @@
import dev.architectury.registry.client.keymappings.KeyMappingRegistry;
import net.minecraft.client.Minecraft;
import net.minecraft.client.multiplayer.ClientLevel;
import net.minecraft.client.player.LocalPlayer;
import net.minecraft.network.chat.Component;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.Level;
import net.minecraft.world.phys.EntityHitResult;
import net.minecraft.world.phys.HitResult;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class DoNotHitMe {
public static final String MOD_ID = "donothitme";
Expand All @@ -41,12 +44,12 @@ private EventResult onPlayerAttackEntity(Player player, Level level, Entity enti
if (!config.enable) {
return EventResult.pass();
}
if(entity instanceof Player){
if(config.no_pvp){
if (entity instanceof Player) {
if (config.no_pvp) {
return EventResult.interruptFalse();
}
}else if(entity instanceof LivingEntity){
if(config.no_pve){
} else if (entity instanceof LivingEntity) {
if (config.no_pve) {
return EventResult.interruptFalse();
}
}
Expand Down Expand Up @@ -94,4 +97,40 @@ public void handleKeyPress(@NotNull DNHKeyType keyType) {
}
}
}

public boolean handleStartAttack(@NotNull LocalPlayer player, @Nullable HitResult hitResult) {
var config = configManager.getConfig();
if (!config.enable || hitResult == null) {
return true;
}
if (hitResult.getType() == HitResult.Type.BLOCK || hitResult.getType() == HitResult.Type.MISS) {
return true;
}
if (config.keep_main_hand_waving) {
return true;
}
if (hitResult instanceof EntityHitResult) {
var entity = ((EntityHitResult) hitResult).getEntity();
if (entity instanceof Player) {
return !config.no_pvp;
} else if (entity instanceof LivingEntity) {
return !config.no_pve;
}
}
return true;
}


public void handleAfterStartUseItem(@NotNull LocalPlayer player, @Nullable HitResult hitResult) {
var config = configManager.getConfig();
if (!config.enable) {
return;
}
if (player.isHandsBusy()) {
return;
}
if (config.open_off_hand_waving) {
player.swing(InteractionHand.OFF_HAND);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.takaranoao.donothitme.mixin;

import com.takaranoao.donothitme.DoNotHitMe;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.Minecraft;
import net.minecraft.client.player.LocalPlayer;
import net.minecraft.world.phys.HitResult;
import org.jetbrains.annotations.Nullable;
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.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

@Mixin(Minecraft.class)
@Environment(EnvType.CLIENT)
public class MixinMinecraft {
@Shadow
public HitResult hitResult;
@Nullable
@Shadow
public LocalPlayer player;

@Inject(method = "startUseItem", at = @At("RETURN"))
private void startUseItem(CallbackInfo ci) {
var mod = DoNotHitMe.getInstance();
if(mod == null || player == null){
return;
}
mod.handleAfterStartUseItem(player, hitResult);
}
@Inject(method="startAttack", at=@At("HEAD"))
private void startAttack(CallbackInfoReturnable<Boolean> cir) {
var mod = DoNotHitMe.getInstance();
if(mod == null || player == null){
return;
}
if(! mod.handleStartAttack(player, hitResult)){
cir.setReturnValue(false);
}
}
}
1 change: 1 addition & 0 deletions common/src/main/resources/donothitme-common.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"mixins": [
],
"client": [
"MixinMinecraft"
],
"injectors": {
"defaultRequire": 1
Expand Down

0 comments on commit 2a7286e

Please sign in to comment.