森林蝙蝠
本帖最后由 森林蝙蝠 于 2019-4-3 23:53 编辑

最近forge终于千呼万唤始出来地更新了1.13版本,虽然尚未真正稳定,好歹也是个能拿出来见人的版本了,赶时髦做了这个教程。
注意:forge的代码随时可能变化,这个教程也不会太稳定!

在1.13,gradlew setupDecompWorkspace已经不复存在,不要想着用它去构建forge开发环境了,在idea内直接打开build.gradle即可,类似下图:

(详情见此:http://www.mcbbs.net/thread-827002-1-1.html 第1篇)
注意:选择default wrapper来配置环境即可,现在不能使用gradle 4.9以上的版本去构建,否则就会出错。

JDK8和11均可用来制作forge mod,但是有一点必须提及的是,JDK自带的JS引擎nashorn即将被移除,而coremod系统使用的就是nashorn,颇有一种49年入国军的感觉;
万恶的Scala依赖终于被移除了,以后的普通玩家终于不用享受下载游戏时,被akka actor这个根本用不到的框架卡住的尴尬了。
如我先前的帖子所说,forge 1.13大量使用函数式接口,某海螺和某4z这对夫妇(划掉)可以使用他们心爱的函数式去编程了。



2021.12 数据,可能有更多内容最近forge终于千呼万唤始出来地更新了1.13版本,虽然尚未真正稳定,好歹也是个能拿出来见人的版本了,赶时髦做了这个教程。
注意:forge的代码随时可能变化,这个教程也不会太稳定!


在1.13,gradlew setupDecompWorkspace已经不复存在,不要想着用它去构建forge开发环境了,在idea内直接打开build.gradle即可,类似下图:

(详情见此:http://www.mcbbs.net/thread-827002-1-1.html 第1篇)
注意:选择default wrapper来配置环境即可,现在不能使用gradle 4.9以上的版本去构建,否则就会出错。


JDK8和11均可用来制作forge mod,但是有一点必须提及的是,JDK自带的JS引擎nashorn即将被移除,而coremod系统使用的就是nashorn,颇有一种49年入国军的感觉;
万恶的Scala依赖终于被移除了,以后的普通玩家终于不用享受下载游戏时,被akka actor这个根本用不到的框架卡住的尴尬了。
如我先前的帖子所说,forge 1.13大量使用函数式接口,某海螺和某4z这对夫妇(划掉)可以使用他们心爱的函数式去编程了。






package com.xxxx.yourmod
import net.minecraft.block.Block;
import net.minecraft.init.Blocks;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.RegistryEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.InterModComms;
import net.minecraftforge.fml.common.Mod;
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(MOD_ID)
public class YourMod
{
    // Directly reference a log4j logger.
private static final Logger LOGGER = LogManager.getLogger();
    public static final String MOD_ID=“YourMod”;


    public YourMod() {
  // 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);
  //添加名为preInit的监听器,addListener()方法是也
  FMLJavaModLoadingContext.get().getModEventBus().addListener(this::preInit);
  //添加名为postInit的监听器,addListener()方法是也
  FMLJavaModLoadingContext.get().getModEventBus().addListener(this::postInit); 
  // 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());
    }
   
    public void preInit(FMLCommonSetupEvent evt)
    {
  LOGGER.info("** TRUMP!");
  //事件参数同为FMLCommonSetupEvent,所以该方法与setup()方法等效,特意列出,是为了让读者与1.12的FMLPreInitializationEvent关联起来
    }
   
    public void postInit(InterModProcessEvent evt)
    {
  //FMLPostInitializationEvent不复存在,变成了这个
    }   
    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);
    }
    //IMC,InterModComms,一个向mod发送消息的forge机制,阿仇的永恒发条整合包就是通过TE的IMC支持魔改了TE的合成表
    private void enqueueIMC(final InterModEnqueueEvent event)
    {
  // some example code to dispatch IMC to another mod
InterModComms.sendTo("yourmod", "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");
  }
    }

}









MashKJo
请问Forge模组官方开发教程在哪儿啊,看了一下开发文档只是介绍了一些库。

森林蝙蝠
MashKJo 发表于 2019-2-19 21:16
请问Forge模组官方开发教程在哪儿啊,看了一下开发文档只是介绍了一些库。 ...

一直都不怎么样,所以第三方教程大行其是。

Observateurs
收藏了, 内容挺好的

Yecgaa_
支持支持,希望大佬更完
(函数式最棒了)

啊妖妖舞
收藏一波,然后坐等更新

啧啧啧22
4z大佬的很多东西已经不能用了,Harbinger讲的不是很清,坐等大佬更新

陈宇鹏
eclipse呢=!

sulinly
正在找呢,谢谢了

第一页 上一页 下一页 最后一页