3
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Minnecraft Forge Modding チュートリアル (1.19.2版)

Last updated at Posted at 2022-08-25

最近リリースされたMinecraft 1.19.2のForgeですが、日本語記事が少ないのでModの作り方をメモ感覚で書いてみました

Forge Mdkのダウンロード

Forge公式サイトからLatest(最新版)またはRecommended(推奨版)のどちらかを選んでMdkダウンロードをダウンロードして任意の場所に展開します
※Java17を使うのでSDKとランタイムをインストールして置いてください

プロジェクトの読み込み

InteliJ 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)はブロックを置いたり壊したりした時の音が石の音になる

ちなみに...

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

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

結果
2022-08-25_20.40.38.png

建築ブロックのタブにこんなのが登録されているはず
耐久度は設定してないのでTNTみたいにすぐ壊れる

キーバインドの追加

見やすいようにパッケージKeysを作成しておき、KeyBindingsクラスを作る

キーバインドを追加しやすいクラスを作っておきました

Java Keybindingsources.java
//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;
            }
        });
    }
}
Java Keys.java

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の追加

Java ExampleGUI.java

@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の作り方載ってなさすぎやろ...

3
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?