player在加载自定义mod时变为不可见和不可移动

at0kjp5o  于 2021-07-05  发布在  Java
关注(0)|答案(0)|浏览(234)

我开始编写我自己的minecraft 1.14.4 mod。我想创建一个非常简单的mod,它只会显示一个clok显示游戏时间(我找不到一个类似的更新mod,因为所有其他mod都显示实时时间,而不是游戏时间)。我以forge附带的mod为例,添加了一些自定义代码。
当我加载mod时,玩家就变得看不见,不能移动了。他在那里,我可以环顾四周,挖掘等,但我看不见他,也不能移动。
如果我抑制我的自定义代码,它的工作。。。我觉得很奇怪。
这是我的代码到目前为止(它没有显示任何东西,因为我被困在这个问题)。

  1. package com.alef.simpleclock;
  2. import net.minecraft.block.Block;
  3. import net.minecraft.block.Blocks;
  4. import net.minecraft.client.entity.player.ClientPlayerEntity;
  5. import net.minecraft.world.World;
  6. import net.minecraftforge.common.MinecraftForge;
  7. import net.minecraftforge.event.RegistryEvent;
  8. import net.minecraftforge.event.TickEvent;
  9. import net.minecraftforge.event.entity.EntityJoinWorldEvent;
  10. import net.minecraftforge.eventbus.api.SubscribeEvent;
  11. import net.minecraftforge.fml.InterModComms;
  12. import net.minecraftforge.fml.common.Mod;
  13. import net.minecraftforge.fml.common.Mod.EventBusSubscriber;
  14. import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent;
  15. import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent;
  16. import net.minecraftforge.fml.event.lifecycle.InterModEnqueueEvent;
  17. import net.minecraftforge.fml.event.lifecycle.InterModProcessEvent;
  18. import net.minecraftforge.fml.event.server.FMLServerStartingEvent;
  19. import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
  20. import org.apache.logging.log4j.LogManager;
  21. import org.apache.logging.log4j.Logger;
  22. import java.util.stream.Collectors;
  23. // The value here should match an entry in the META-INF/mods.toml file
  24. @Mod("simpleclock")
  25. public class SimpleClock
  26. {
  27. // Directly reference a log4j logger.
  28. private static final Logger LOGGER = LogManager.getLogger();
  29. public static boolean startTimer;
  30. public static int tick;
  31. public SimpleClock() {
  32. // Register the setup method for modloading
  33. FMLJavaModLoadingContext.get().getModEventBus().addListener(this::setup);
  34. // Register the enqueueIMC method for modloading
  35. FMLJavaModLoadingContext.get().getModEventBus().addListener(this::enqueueIMC);
  36. // Register the processIMC method for modloading
  37. FMLJavaModLoadingContext.get().getModEventBus().addListener(this::processIMC);
  38. // Register the doClientStuff method for modloading
  39. FMLJavaModLoadingContext.get().getModEventBus().addListener(this::doClientStuff);
  40. // Register ourselves for server and other game events we are interested in
  41. MinecraftForge.EVENT_BUS.register(this);
  42. }
  43. private void setup(final FMLCommonSetupEvent event)
  44. {
  45. // some preinit code
  46. LOGGER.info("HELLO FROM PREINIT");
  47. LOGGER.info("DIRT BLOCK >> {}", Blocks.DIRT.getRegistryName());
  48. }
  49. private void doClientStuff(final FMLClientSetupEvent event) {
  50. // do something that can only be done on the client
  51. LOGGER.info("Got game settings {}", event.getMinecraftSupplier().get().gameSettings);
  52. }
  53. private void enqueueIMC(final InterModEnqueueEvent event)
  54. {
  55. // some example code to dispatch IMC to another mod
  56. InterModComms.sendTo("examplemod", "helloworld", () -> { LOGGER.info("Hello world from the MDK"); return "Hello world";});
  57. }
  58. private void processIMC(final InterModProcessEvent event)
  59. {
  60. // some example code to receive and process InterModComms from other mods
  61. LOGGER.info("Got IMC {}", event.getIMCStream().
  62. map(m->m.getMessageSupplier().get()).
  63. collect(Collectors.toList()));
  64. }
  65. // You can use SubscribeEvent and let the Event Bus discover methods to call
  66. @SubscribeEvent
  67. public void onServerStarting(FMLServerStartingEvent event) {
  68. // do something when the server starts
  69. LOGGER.info("HELLO from server starting");
  70. }
  71. // You can use EventBusSubscriber to automatically subscribe events on the contained class (this is subscribing to the MOD
  72. // Event bus for receiving Registry Events)
  73. @Mod.EventBusSubscriber(bus=Mod.EventBusSubscriber.Bus.MOD)
  74. public static class RegistryEvents {
  75. @SubscribeEvent
  76. public static void onBlocksRegistry(final RegistryEvent.Register<Block> blockRegistryEvent) {
  77. // register a new block here
  78. LOGGER.info("HELLO from Register Block");
  79. }
  80. }
  81. @EventBusSubscriber
  82. public static class showClock
  83. {
  84. @SubscribeEvent
  85. public static void onJoin(final EntityJoinWorldEvent event)
  86. {
  87. if (event.getEntity() != null && event.getEntity() instanceof ClientPlayerEntity)
  88. {
  89. LOGGER.info("WELCOME " + event.getEntity().getName() + "!!!");
  90. event.setCanceled(true);
  91. if (!SimpleClock.startTimer)
  92. {
  93. SimpleClock.startTimer = true;
  94. }
  95. }
  96. }
  97. @SubscribeEvent
  98. public static void timer(final TickEvent.WorldTickEvent event)
  99. {
  100. if (SimpleClock.startTimer)
  101. {
  102. if (SimpleClock.tick >= 1000)
  103. {
  104. SimpleClock.tick = 0;
  105. drawClock(event.world);
  106. }
  107. else
  108. {
  109. SimpleClock.tick++;
  110. }
  111. }
  112. }
  113. private static void drawClock(World world)
  114. {
  115. int time = (int) world.getWorldInfo().getDayTime() % 1000;
  116. LOGGER.info(time);
  117. }
  118. }
  119. }

欢迎任何帮助!!!

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题