在Minecraft 1.12.2的Forge中,你可以使用以下代码来实现该功能:

首先,在你的mod主类中添加以下字段:

public static boolean autoPut = true;
public static BlockPos targetPos = null;

然后,在你的mod主类的init方法中注册一个客户端事件监听器:

MinecraftForge.EVENT_BUS.register(new ClientEventHandler());

接下来,创建一个新的事件监听器类ClientEventHandler,并实现TickEvent事件的监听:

import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.gameevent.TickEvent;

public class ClientEventHandler {
    @SubscribeEvent
    public void onClientTick(TickEvent.ClientTickEvent event) {
        if (event.phase == TickEvent.Phase.END) {
            if (ModClassName.autoPut) {
                if (ModClassName.targetPos == null) {
                    int rotation = MathHelper.floor((double)(player.rotationYaw * 4.0F / 360.0F) + 0.5D) & 3;
                    BlockPos playerPos = player.getPosition();
                    ModClassName.targetPos = new BlockPos(playerPos.getX(), playerPos.getY(), playerPos.getZ());
                    
                    switch (rotation) {
                        case 0: // 0 degrees
                            ModClassName.targetPos = ModClassName.targetPos.east();
                            break;
                        case 1: // 90 degrees
                            ModClassName.targetPos = ModClassName.targetPos.south();
                            break;
                        case 2: // 180 degrees
                            ModClassName.targetPos = ModClassName.targetPos.west();
                            break;
                        case 3: // 270 degrees
                            ModClassName.targetPos = ModClassName.targetPos.north();
                            break;
                    }
                    
                    ModClassName.targetPos = ModClassName.targetPos.up(82);
                }
                
                double distance = player.getDistance(ModClassName.targetPos.getX() + 0.5, ModClassName.targetPos.getY(), ModClassName.targetPos.getZ() + 0.5);
                if (distance > 0.8 && distance < 1.8) {
                    player.connection.sendPacket(new CPacketPlayer.Position(player.posX, player.posY, player.posZ, true));
                    player.setPositionAndUpdate(player.posX, player.posY, player.posZ);
                    player.connection.sendPacket(new CPacketPlayer.Position(player.posX, player.posY - 0.8, player.posZ, false));
                    
                    // Place block at targetPos
                    world.setBlockState(ModClassName.targetPos, Blocks.YOUR_BLOCK.getDefaultState());
                    
                    ModClassName.targetPos = ModClassName.targetPos.down();
                }
            }
        }
    }
}

确保将上面代码中的ModClassName替换为你的mod主类的名称,并将YOUR_BLOCK替换为你要放置的方块。

最后,在你的mod主类中添加以下方法来处理autoPut字段的变化:

public static void setAutoPut(boolean value) {
    autoPut = value;
    if (!value) {
        targetPos = null;
    }
}

现在,你可以在你的mod中使用setAutoPut(true)来启用自动放置功能,使用setAutoPut(false)来停止自动放置功能。这样,在服务器上使用你的mod时也会生效


原文地址: https://www.cveoy.top/t/topic/ivod 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录