0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

MinecraftのPaperPluginでComponentという記法が追加されている件

Posted at

Componentについての備忘録

なんかpaperの新しいバージョンになってから、色々変更が加わって意味がわからなかったので備忘録。

・setDisplayName => displayName
・setTitle => title
・setJoinMessage => joinMessage


みたいなよくわからんアップデートが加わっていた。
どうやら、旧式のStringだとマイクラ環境で表示するには不都合が多いから、新しくComponentで括ってしまおうという魂胆だろうか。

装飾とか

今までは

import org.bukkit.ChatColor;

String text = "あいうえお" + ChatColor.RED;

なんてので完結していた。

しかしComponentが追加されてからは

import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;

Component text = Component.text("あいうえお").color(NamedTextColor.RED);

こっちの方が確実で拡張性のある処理が可能なのだろうが、うーん...イマイチ長くて面倒くさい。

ちなみに、この記事を見た人は気になるだろう。
この形式はどうなるのか。

import net.kyori.adventure.text.Component;
import org.bukkit.ChatColor;

Component text = Component.text(ChatColor.RED + "あいうえお");
redあいうえお

何がしたいのか。
結局、混ぜちゃダメらしい(1敗)。

デコレーション

例えば上のやつを太字にしたい。

import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.format.TextDecoration;

Component text = Component.text("あいうえお").color(NamedTextColor.RED).decorate(TextDecoration.BOLD);

もしくは

import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.format.TextDecoration;

Component text = Component.text("あいうえお").color(NamedTextColor.RED).decoration(TextDecoration.BOLD, true);

何が違うのかって、

decorate => TextDecoration.BOLD
decoration => TextDecoration.BOLD, true

私はてっきり下のやつ要る?と思って暮らしていたのだが、どうやらマイクラ界にはバニラの状態で既にイタリックやボールドがかかっているものがあるらしい(アイテムの説明欄とか)。

そんな「アイテムの説明欄のテキストを弄りたい!でも斜線にはしたくない!」って時にTextDecoration.ITALIC, falseで適応してやればよいということになる。

ちなみに、colorは普通にWhiteなりで上書きすればよいのでBooleanは存在しない。

以上

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?