在Minecraft 1122的Forgemcp版本为stable-39里实现以下功能:如果autoPut=true就转向离目前水平方向最近的整坐标整坐标:0度90度180度270度然后转向垂直方向82度然后向后移动08米放置方块再向后移动1米放置方块在向后移动1米如此往复知道autoPut=false停止要求该代码在服务器也能使用
在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 著作权归作者所有。请勿转载和采集!