Minecraft Forge Modding 教程(1.19.2版)

由于最近发布的Minecraft 1.19.2中的Forge缺乏日语文章,所以我以备忘的方式写下了制作Mod的方法。

下载Forge Mdk

请从Forge官方网站上选择Latest(最新版)或Recommended(推荐版)的Mdk下载,并将其解压到任意位置。
请注意,由于将使用Java17,因此请预先安装SDK和运行时。

读取项目

在IntelliJ IDEA等编辑器中加载并完成加载后,打开位于Resources文件夹中的META-INF文件夹中的mods.toml文件。

中间应该是这样的感觉


# This is an example mods.toml file. It contains the data relating to the loading mods.
# There are several mandatory fields (#mandatory), and many more that are optional (#optional).
# The overall format is standard TOML format, v0.5.0.
# Note that there are a couple of TOML lists in this file.
# Find more information on toml format here:  https://github.com/toml-lang/toml
# The name of the mod loader type to load - for regular FML @Mod mods it should be javafml
modLoader="javafml" #mandatory
# A version range to match for said mod loader - for regular FML @Mod it will be the forge version
loaderVersion="[43,)" #mandatory This is typically bumped every Minecraft version by Forge. See our download page for lists of versions.
# The license for you mod. This is mandatory metadata and allows for easier comprehension of your redistributive properties.
# Review your options at https://choosealicense.com/. All rights reserved is the default copyright stance, and is thus the default here.
license="All rights reserved"
# A URL to refer people to when problems occur with this mod
#issueTrackerURL="https://change.me.to.your.issue.tracker.example.invalid/" #optional
# A list of mods - how many allowed here is determined by the individual mod loader
[[mods]] #mandatory
# The modid of the mod
modId="modid" #mandatory
# The version number of the mod - there's a few well known ${} variables useable here or just hardcode it
# ${file.jarVersion} will substitute the value of the Implementation-Version as read from the mod's JAR file metadata
# see the associated build.gradle script for how to populate this completely automatically during a build
version="${file.jarVersion}" #mandatory
 # A display name for the mod
displayName="Example MOD" #mandatory
# A URL to query for updates for this mod. See the JSON update specification https://mcforge.readthedocs.io/en/latest/gettingstarted/autoupdate/
#updateJSONURL="https://change.me.example.invalid/updates.json" #optional
# A URL for the "homepage" for this mod, displayed in the mod UI
#displayURL="https://change.me.to.your.mods.homepage.example.invalid/" #optional
# A file name (in the root of the mod JAR) containing a logo for display
logoFile="icon.png" #optional
# A text field displayed in the mod UI
credits="" #optional
# A text field displayed in the mod UI
authors="" #optional
# Display Test controls the display for your mod in the server connection screen
# MATCH_VERSION means that your mod will cause a red X if the versions on client and server differ. This is the default behaviour and should be what you choose if you have server and client elements to your mod.
# IGNORE_SERVER_VERSION means that your mod will not cause a red X if it's present on the server but not on the client. This is what you should use if you're a server only mod.
# IGNORE_ALL_VERSION means that your mod will not cause a red X if it's present on the client or the server. This is a special case and should only be used if your mod has no server component.
# NONE means that no display test is set on your mod. You need to do this yourself, see IExtensionPoint.DisplayTest for more information. You can define any scheme you wish with this value.
# IMPORTANT NOTE: this is NOT an instruction as to which environments (CLIENT or DEDICATED SERVER) your mod loads on. Your mod should load (and maybe do nothing!) whereever it finds itself.
#displayTest="MATCH_VERSION" # MATCH_VERSION is the default if nothing is specified (#optional)

# The description text for the mod (multi line!) (#mandatory)
description='''
'''
# A dependency - use the . to indicate dependency for a specific modid. Dependencies are optional.
[[dependencies.modid]] #optional
    # the modid of the dependency
    modId="forge" #mandatory
    # Does this dependency have to exist - if not, ordering below must be specified
    mandatory=true #mandatory
    # The version range of the dependency
    versionRange="[43,)" #mandatory
    # An ordering relationship for the dependency - BEFORE or AFTER required if the relationship is not mandatory
    ordering="NONE"
    # Side this dependency is applied on - BOTH, CLIENT or SERVER
    side="BOTH"
# Here's another dependency
[[dependencies.modid]]
    modId="minecraft"
    mandatory=true
# This version range declares a minimum of the current minecraft version up to but not including the next major version
    versionRange="[1.19.2,1.20)"
    ordering="NONE"
    side="BOTH"

请将modId替换为您喜欢的id。

    • modId:ModのID

 

    • credits:Modのクレジット

 

    • displayName:Mods画面に表示されるModの名前

 

    • logoFile:Modのアイコン resourcesがルートフォルダ

 

    • ※”/”は使えない(クラッシュする)

credits:Modのクレジット
authors:作者名
description:Modの説明

修改主类

打开带有 @Mod(Examplemod.MODID) 注解的类,并将 MODID 设置为在 mods.toml 文件中配置的 ModId。
* 如果 ModId 不一致,可能会导致崩溃。

添加方块

public static final DeferredRegister<Block> BLOCKS = DeferredRegister.create(ForgeRegistries.BLOCKS, MODID);

这样就可以创建一个可以注册块的变量。

public static final RegistryObject<Block> EXAMPLE_BLOCK = BLOCKS.register("example_block", () -> new Block(BlockBehaviour.Properties.of(Material.STONE)));

通过BLOCK.register(…)可以注册方块(应该)
BlockBehaviour.Properties.of(Material.STONE)会使得放置或破坏方块时的声音变成石头的声音。

顺便提一下… tí …)

BLOCKS.register(modEventBus);
ITEMS.register(modEventBus);

コンストラクタ内でこれをしないと登録されないので注意

2022-08-25_20.40.38.png

建筑方块的标签中应该注册了这样的内容,耐久度未设置,所以会像TNT一样很快损毁。

增加按键绑定

为了方便查看,预先创建一个名为”Keys”的包,并创建一个名为KeyBindings的类。

我为您创建了一个易于添加键绑定的类。

//package名は自動で設定される

import net.minecraft.client.KeyMapping;
import net.minecraft.client.Minecraft;
import net.minecraftforge.client.event.RegisterKeyMappingsEvent;
import net.minecraftforge.client.settings.IKeyConflictContext;

public class KeyBindingTasks {
    public void Register(KeyMapping keyMapping, Runnable runnable){
        new RegisterKeyMappingsEvent(Minecraft.getInstance().options).register(keyMapping);
        keyMapping.setKeyConflictContext(new IKeyConflictContext() {
            @Override
            public boolean isActive() {
                runnable.run();
                return false;
            }

            @Override
            public boolean conflicts(IKeyConflictContext other) {
                return false;
            }
        });
    }
}

public class Keys {
    public static String Category = "key.categories." + MODID;
    public static String TestBindingDisplayName = "key." + MODID + ".testkeybind";
    public static KeyMapping TestKeyBind = new KeyMapping(TestBindingDisplayName, KeyConflictContext.IN_GAME, InputConstants.Type.KEYSYM, GLFW.GLFW_KEY_N, Category);

    public static void Register(){
        KeyBindingTasks KeyBindingTask = new KeyBindingTasks();

        KeyBindingTask.Register(TestKeyBind, () -> {
            ExampleGUI examplegui = new ExampleGUI();
            Minecraft.getInstance().setScreen(examplegui);
        });
    }
}

讲解

    • Category:キーバインドのカテゴリ

 

    • TestBindingDisplayName:キーバインドの表示名

 

    • KeyConflictContext.IN_GAME:ゲーム内でのみ有効

 

    • InputConstants.Type.KEYSYM:よくわからん()

 

    • GLFW.GLFW_KEY_N:Nキーを押すと発動する

 

    • KeyBindingTasks:さっきのクラスをコンストラクタ

 

    • KeyBindingTask.Register(): 第1引数はTestKeyBind,第2引数はRunnable

 

    ()->{} ←これ
ExampleGUI examplegui = new ExampleGUI();
Minecraft.getInstance().setScreen(examplegui);

這會稍後處理

添加自定义GUI


@OnlyIn(Dist.CLIENT)
public class ExampleGUI extends Screen {
    public ExampleGUI() {
        this(false);
    }

    public ExampleGUI(boolean p_96733_) {
        super(Component.translatable("narrator.screen.examplegui"));
    }
    
    protected void init() {
        super.init();

    }
    public void render(@NotNull PoseStack p_96562_, int p_96563_, int p_96564_, float p_96565_) {
        this.renderDirtBackground(p_96563_);
        drawCenteredString(p_96562_, this.font, this.title, this.width / 2, 5, 16777215);
        super.render(p_96562_, p_96563_, p_96564_, p_96565_);
    }

    public boolean isPauseScreen() {
        return false;
    }

    public boolean shouldCloseOnEsc() {
        return true;
    }
}

基本上就是這樣

按钮或文本框的布局

在super.init();之后

Component CLOSE_BUTTON = Component.translatable(MODID + ".buttons.close");
Component EDITBOX = Component.translatable(MODID + ".editbox.edit");

EditBox editBox = new EditBox(this.font, this.width / 2 - 100, 30, 200, 20, EDITBOX);
this.addRenderableWidget(editBox);

this.addRenderableWidget(new Button(this.width / 2 - 100, (this.height / 9) * 8, 200, 20, CLOSE_BUTTON, (button) -> {
    this.minecraft.setScreen(null);
}));

解説

this.minecraft.setScreen(null);はゲーム画面に戻れる関数

public boolean isPauseScreen() {
    return false;
}

これは、ポーズ画面であるかどうかを示す関数
trueにするとこのGUIを開いた時にセーブされる

public boolean shouldCloseOnEsc() {
    return true;
}

これは、エスケープキーで閉じれるかどうかを示す関数
trueにするとエスケープキーで閉じれなくなる

public void render(@NotNull PoseStack p_96562_, int p_96563_, int p_96564_, float p_96565_) {
}

可以绘制执行了 this.addRenderableWidget() 的部件并更改背景的函数。
可以将背景渲染为土块的函数 this.renderDirtBackground(p_96563_) (经常在设置界面等看到的背景)。

在render函数内部

drawCenteredString(p_96562_, this.font, this.title, this.width / 2, 5, 16777215);

をするとタイトルを表示できます

2022-08-25_21.14.03.png

langファイルは適当でヨシ!!

ちなみに…
editBox.getValue()でEditboxのテキストを取得できるようです

最后

这个自定义 GUI 的制作方法真的太少了吧…

广告
将在 10 秒后关闭
bannerAds