Nomove

模组

· 作者 PlantsVSZombies1

防止玩家在任何物品栏中移动物品。

0 本站下载 0 关注 更新于 3 周前

最新 1.2.0 · 26.3 · Fabric

一键开服(送7天)

NoMove 是一款适用于 Minecraft 1.21.1/26.3 的轻量级单人游戏模组,基于 NeoForge/Fabric 模组加载器开发。该模组的核心功能是全面禁止玩家以任何方式移动物品,包括在物品栏中移动、交换、以及转移等行为,从根源上限制物品的流转操作。

模组通过 Mixin 技术注入底层 Slot 类实现功能,无需复杂配置,安装即生效。无论是玩家背包、箱子、熔炉还是其他容器界面,所有物品拖动、以及点击移动的操作都会被拦截,有效防止物品误操作,保持物品栏布局固定不变。

适用于需要严格管控物品流转的单人游戏场景,如挑战玩法、特殊规则生存、解谜地图等。

NoMoveMod.java

package com.nomove;

import com.mojang.brigadier.arguments.StringArgumentType;
import net.minecraft.commands.Commands;
import net.minecraft.core.registries.Registries;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.damagesource.DamageSource;
import net.minecraft.world.damagesource.DamageType;
import net.minecraft.world.entity.player.Player;
import net.neoforged.fml.ModContainer;
import net.neoforged.fml.common.Mod;
import net.neoforged.fml.config.ModConfig;
import net.neoforged.neoforge.common.NeoForge;
import net.neoforged.neoforge.event.RegisterCommandsEvent;

@Mod(NoMoveMod.MODID)
public class NoMoveMod {
    public static final String MODID = "nomove";

    public static final ResourceKey<DamageType> EXECUTION_DAMAGE = ResourceKey.create(
        Registries.DAMAGE_TYPE,
        ResourceLocation.fromNamespaceAndPath(MODID, "execution")
    );

    public NoMoveMod(ModContainer container) {

        container.registerConfig(ModConfig.Type.CLIENT, NoMoveConfig.SPEC, "nomove-client.toml");
        
        NeoForge.EVENT_BUS.addListener(this::registerCommands);
    }

    public static void executeKill(Player player) {
        DamageSource source = player.damageSources().source(EXECUTION_DAMAGE);
        player.hurt(source, Float.MAX_VALUE);
    }

    private void registerCommands(RegisterCommandsEvent event) {
        event.getDispatcher().register(
            Commands.literal("nomove")
                .requires(source -> source.hasPermission(2))
                
                .then(Commands.literal("config_client")
                    .then(Commands.literal("moving_items")
                        .then(Commands.argument("value", StringArgumentType.word())
                            .suggests((ctx, builder) -> {
                                builder.suggest("true");
                                builder.suggest("false");
                                return builder.buildFuture();
                            })
                            .executes(context -> {
                                ServerPlayer player = context.getSource().getPlayer();
                                boolean isChinese = player != null && player.clientInformation().language().startsWith("zh");

                                String val = StringArgumentType.getString(context, "value").toLowerCase();
                                boolean boolVal;
                                
                                if (val.equals("true")) {
                                    boolVal = true;
                                } else if (val.equals("false")) {
                                    boolVal = false;
                                } else {
                                    if (isChinese) {
                                        context.getSource().sendFailure(Component.literal("§c参数错误!只能是 true 或 false"));
                                    } else {
                                        context.getSource().sendFailure(Component.literal("§cInvalid argument! Must be true or false"));
                                    }
                                    return 0;
                                }

                                NoMoveConfig.MOVING_ITEMS.set(boolVal);

                                String statusColor = boolVal ? "§a" : "§c";
                                String statusText = String.valueOf(boolVal);
                                
                                if (isChinese) {
                                    context.getSource().sendSuccess(() -> 
                                        Component.literal("§eNoMove: 移动物品拦截已设置为 " + statusColor + statusText), true);
                                } else {
                                    context.getSource().sendSuccess(() -> 
                                        Component.literal("§eNoMove: Moving items prevention set to " + statusColor + statusText), true);
                                }
                                
                                return 1;
                            })
                        )
                    )
                )
        );
    }
}

NoMoveConfig.java

package com.nomove;

import net.neoforged.neoforge.common.ModConfigSpec;

public class NoMoveConfig {
    private static final ModConfigSpec.Builder BUILDER = new ModConfigSpec.Builder();

    public static final ModConfigSpec.BooleanValue MOVING_ITEMS = BUILDER
            .comment("Set to true to prevent moving items, false to allow it.")
            .define("moving_items", true);

```java
public static final ModConfigSpec SPEC = BUILDER.build();
}

PlayerMixin.java

package com.nomove.mixin;

import com.nomove.NoMoveMod;
import net.minecraft.world.damagesource.DamageSource;
import net.minecraft.world.entity.player.Player;
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.CallbackInfoReturnable;

@Mixin(Player.class)
public abstract class PlayerMixin {

    @Inject(method = "hurt", at = @At("HEAD"), cancellable = true)
    private void nomove$forceKillCreative(DamageSource source, float amount, CallbackInfoReturnable<Boolean> cir) {
        if (source.is(NoMoveMod.EXECUTION_DAMAGE)) {
            Player player = (Player) (Object) this;
            
            if (player.getAbilities().instabuild) {
                player.getAbilities().instabuild = false;
                boolean result = player.hurt(source, amount);
                if (player.isAlive()) {
                    player.getAbilities().instabuild = true;
                }
                cir.setReturnValue(result);
            }
        }
    }
}

SlotMixin.java

package com.nomove.mixin;

import com.nomove.NoMoveConfig;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.Slot;
import net.minecraft.world.item.ItemStack;
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.CallbackInfoReturnable;

@Mixin(Slot.class)
public abstract class SlotMixin {

    @Inject(method = "mayPickup", at = @At("HEAD"), cancellable = true)
    private void nomove$preventPickup(Player player, CallbackInfoReturnable<Boolean> cir) {
        if (NoMoveConfig.MOVING_ITEMS.get()) {
            cir.setReturnValue(false);
        }
    }

    @Inject(method = "mayPlace", at = @At("HEAD"), cancellable = true)
    private void nomove$preventPlace(ItemStack stack, CallbackInfoReturnable<Boolean> cir) {
        if (NoMoveConfig.MOVING_ITEMS.get()) {
            cir.setReturnValue(false);
        }
    }
}

你是这个资源的作者?登录后可认领此资源。

评论 0

0.0
暂无评分
5 星
0
4 星
0
3 星
0
2 星
0
1 星
0
我的评分

登录后参与评分

还没有评论,来抢沙发吧

评论配图

相似资源