您可以使用以下代码来获取手上武器蓄力完毕所需的游戏刻:\n\njava\nimport net.minecraft.entity.player.EntityPlayer;\nimport net.minecraft.init.Items;\nimport net.minecraft.item.Item;\nimport net.minecraft.item.ItemSword;\nimport net.minecraft.item.ItemStack;\nimport net.minecraftforge.fml.common.Mod;\nimport net.minecraftforge.fml.common.eventhandler.SubscribeEvent;\nimport net.minecraftforge.fml.common.gameevent.TickEvent;\n\n@Mod.EventBusSubscriber\npublic class WeaponChargeTime {\n \n @SubscribeEvent\n public static void onPlayerTick(TickEvent.PlayerTickEvent event) {\n EntityPlayer player = event.player;\n ItemStack heldItem = player.getHeldItemMainhand();\n \n if (heldItem.getItem() instanceof ItemSword) {\n ItemSword sword = (ItemSword) heldItem.getItem();\n int chargeTime = sword.getMaxItemUseDuration(heldItem) - player.getItemInUseCount();\n \n // 将游戏刻转换为秒\n int seconds = chargeTime / 20;\n \n System.out.println("蓄力完毕所需的游戏刻:" + chargeTime);\n System.out.println("蓄力完毕所需的秒数:" + seconds);\n }\n }\n}\n\n\n在这段代码中,我们使用了 Forge 的事件订阅功能,当玩家进行游戏刻更新时,onPlayerTick 方法会被调用。我们获取玩家手中的物品,并检查该物品是否为剑(ItemSword)。然后,我们使用 ItemSwordgetMaxItemUseDuration 方法获取蓄力完毕所需的最大游戏刻数,减去玩家当前使用物品的游戏刻数,从而获得剩余的游戏刻数。最后,我们将游戏刻数转换为秒,并在控制台输出结果。\n\n请注意,这段代码是基于 Minecraft 1.12.2 Forge 的,您需要在您的 mod 中正确注册并使用这个事件处理类。


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

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