提供: Minecraft Modding Wiki
2020年10月11日 (日) 20:50時点におけるRedice (トーク | 投稿記録)による版 (ページの作成:「{{前提MOD|reqmod="Fabric API 0.3.2 build 230~"}} {{チュートリアル難易度|difficulty=1|clear=none}} LibGuiを利用し、文字を表示させるGUIを作成…」)
(差分) ← 古い版 | 最新版 (差分) | 新しい版 → (差分)
移動先: 案内検索

この記事は"Fabric API 0.3.2 build 230~"を前提MODとしています。

Stone pickaxe.png
中級者向けのチュートリアルです。

LibGuiを利用し、文字を表示させるGUIを作成します。
※サーバーでは動作しませんのでサーバーなどで利用する場合は別の方法を利用してください。

動作確認済みのバージョン:1.16.3

鉱石を生成する

方法

//省略
repositories {
    maven {
    name = "CottonMC"
    url = "https://server.bbkr.space/artifactory/libs-release"
    }
}

dependencies {
    //to change the versions see the gradle.properties file
    minecraft "com.mojang:minecraft:${project.minecraft_version}"
    mappings "net.fabricmc:yarn:${project.yarn_mappings}:v2"
    modImplementation "net.fabricmc:fabric-loader:${project.loader_version}"

    // Fabric API. This is technically optional, but you probably want it anyway.
    modImplementation "net.fabricmc.fabric-api:fabric-api:${project.fabric_version}"

    // PSA: Some older mods, compiled on Loom 0.2.1, might have outdated Maven POMs.
    // You may need to force-disable transitiveness on them.
    
    modImplementation "io.github.cottonmc:LibGui:3.0.0+1.16.3"
}
//省略

「modImplementation "io.github.cottonmc:LibGui:3.0.0+1.16.3"」の「3.0.0+1.16.3」ところは[[LibGuiリリース一覧>https://server.bbkr.space/artifactory/libs-release/io/github/cottonmc/LibGui/]]から利用したいバージョンのディレクトリ名を書き込んでください。
その後、コンソール上で「./gradlew genSources」を実行し、IDEを更新してください。(VisualStudioCodeの場合はShift + Alt + Uで更新できます。)

  • SampleMod.java
package com.example.gui;

import net.fabricmc.api.ModInitializer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemGroup;
import net.minecraft.util.Identifier;
import net.minecraft.util.registry.Registry;

public class SampleMod implements ModInitializer
    {
    /**
     * ModID文字列
     */
    public static final String MOD_ID = "samplemod";
    //アイテム追加
    public static Item SAMPLE_GUI_ITEM = new SampleGuiItem(new Item.Settings().group(ItemGroup.MISC));

    @Override
    public void onInitialize() {
        //アイテム登録
        Registry.register(Registry.ITEM, new Identifier(MOD_ID, "sample_gui_item"), SAMPLE_GUI_ITEM);
    }
}
  • SampleGuiItem.java
package com.example.gui;

import net.minecraft.client.MinecraftClient;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.Hand;
import net.minecraft.util.TypedActionResult;
import net.minecraft.world.World;

public class SampleGuiItem extends Item 
{
    public SampleGuiItem(Settings settings)
    {
        super(settings);
    }
    @Override
    public TypedActionResult<ItemStack> use(World world, PlayerEntity player,Hand hand)//アイテム(SampleGuiItem)を右クリックしたら呼び出されるメソッド
    {
        MinecraftClient.getInstance().openScreen(new SampleGuiItemScreen(new SampleGuiItemGuiDescription()));//GUI呼び出し
        return super.use(world, player, hand);
    }
}
  • SampleGuiItemScreen.java
package com.example.gui;

import io.github.cottonmc.cotton.gui.GuiDescription;
import io.github.cottonmc.cotton.gui.client.CottonClientScreen;

public class SampleGuiItemScreen extends CottonClientScreen
{
    public SampleGuiItemScreen(GuiDescription description) 
    {
        super(description);
    }
}
  • SampleGuiItemGuiDescription.java
package com.example.gui;

import io.github.cottonmc.cotton.gui.client.LightweightGuiDescription;
import io.github.cottonmc.cotton.gui.widget.WGridPanel;
import io.github.cottonmc.cotton.gui.widget.WLabel;

public class SampleGuiItemGuiDescription extends LightweightGuiDescription
{
    public SampleGuiItemGuiDescription() 
    {
        WGridPanel wgp = new WGridPanel();//パネル作成
        setRootPanel(wgp);//メインパネル配置
        wgp.setSize(300, 200);//setSize(X, Y)
        WLabel label = new WLabel("Hello, world!");//WLabel(文字列);
        wgp.add(label, 1, 1);//パネルにラベル配置
        
        wgp.validate(this);
    }
}