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

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

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

package com.alef.simpleclock;

import net.minecraft.block.Block;
import net.minecraft.block.Blocks;
import net.minecraft.client.entity.player.ClientPlayerEntity;
import net.minecraft.world.World;

import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.RegistryEvent;
import net.minecraftforge.event.TickEvent;
import net.minecraftforge.event.entity.EntityJoinWorldEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.InterModComms;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.Mod.EventBusSubscriber;
import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent;
import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent;
import net.minecraftforge.fml.event.lifecycle.InterModEnqueueEvent;
import net.minecraftforge.fml.event.lifecycle.InterModProcessEvent;
import net.minecraftforge.fml.event.server.FMLServerStartingEvent;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.stream.Collectors;

// The value here should match an entry in the META-INF/mods.toml file
@Mod("simpleclock")
public class SimpleClock
{
    // Directly reference a log4j logger.
    private static final Logger LOGGER = LogManager.getLogger();
    public static boolean startTimer;
    public static int tick;

    public SimpleClock() {
        // Register the setup method for modloading
        FMLJavaModLoadingContext.get().getModEventBus().addListener(this::setup);
        // Register the enqueueIMC method for modloading
        FMLJavaModLoadingContext.get().getModEventBus().addListener(this::enqueueIMC);
        // Register the processIMC method for modloading
        FMLJavaModLoadingContext.get().getModEventBus().addListener(this::processIMC);
        // Register the doClientStuff method for modloading
        FMLJavaModLoadingContext.get().getModEventBus().addListener(this::doClientStuff);

        // Register ourselves for server and other game events we are interested in
        MinecraftForge.EVENT_BUS.register(this);
    }

    private void setup(final FMLCommonSetupEvent event)
    {
        // some preinit code
        LOGGER.info("HELLO FROM PREINIT");
        LOGGER.info("DIRT BLOCK >> {}", Blocks.DIRT.getRegistryName());
    }

    private void doClientStuff(final FMLClientSetupEvent event) {
        // do something that can only be done on the client
        LOGGER.info("Got game settings {}", event.getMinecraftSupplier().get().gameSettings);
    }

    private void enqueueIMC(final InterModEnqueueEvent event)
    {
        // some example code to dispatch IMC to another mod
        InterModComms.sendTo("examplemod", "helloworld", () -> { LOGGER.info("Hello world from the MDK"); return "Hello world";});
    }

    private void processIMC(final InterModProcessEvent event)
    {
        // some example code to receive and process InterModComms from other mods
        LOGGER.info("Got IMC {}", event.getIMCStream().
                map(m->m.getMessageSupplier().get()).
                collect(Collectors.toList()));
    }
    // You can use SubscribeEvent and let the Event Bus discover methods to call
    @SubscribeEvent
    public void onServerStarting(FMLServerStartingEvent event) {
        // do something when the server starts
        LOGGER.info("HELLO from server starting");
    }

    // You can use EventBusSubscriber to automatically subscribe events on the contained class (this is subscribing to the MOD
    // Event bus for receiving Registry Events)
    @Mod.EventBusSubscriber(bus=Mod.EventBusSubscriber.Bus.MOD)
    public static class RegistryEvents {
        @SubscribeEvent
        public static void onBlocksRegistry(final RegistryEvent.Register<Block> blockRegistryEvent) {
            // register a new block here
            LOGGER.info("HELLO from Register Block");
        }
    }

    @EventBusSubscriber
    public static class showClock
    {
        @SubscribeEvent
        public static void onJoin(final EntityJoinWorldEvent event)
        {
            if (event.getEntity() != null && event.getEntity() instanceof ClientPlayerEntity)
            {
                LOGGER.info("WELCOME " + event.getEntity().getName() + "!!!");
                event.setCanceled(true);
                if (!SimpleClock.startTimer)
                {
                    SimpleClock.startTimer = true;
                }
            }
        }

        @SubscribeEvent
        public static void timer(final TickEvent.WorldTickEvent event)
        {
            if (SimpleClock.startTimer)
            {
                if (SimpleClock.tick >= 1000)
                {
                    SimpleClock.tick = 0;
                    drawClock(event.world);
                }
                else
                {
                    SimpleClock.tick++;
                }
            }
        }

        private static void drawClock(World world)
        {
            int time = (int) world.getWorldInfo().getDayTime() % 1000;
            LOGGER.info(time);
        }
    }
}

欢迎任何帮助!!!

暂无答案!

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

相关问题