Added song item display data

This commit is contained in:
hhhzzzsss 2023-06-16 21:58:46 -05:00
parent 0a67907bea
commit b4858729a9
3 changed files with 47 additions and 1 deletions

View file

@ -11,7 +11,6 @@ import com.mojang.brigadier.suggestion.Suggestions;
import com.mojang.brigadier.suggestion.SuggestionsBuilder;
import net.minecraft.command.CommandSource;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.util.Hand;
import net.minecraft.world.GameMode;
@ -867,6 +866,7 @@ public class CommandProcessor {
}
String name = String.join(" ", Arrays.copyOfRange(split, 1, split.length));
songPlayerNBT.putString(SongItemUtils.DISPLAY_NAME_KEY, name);
SongItemUtils.addSongItemDisplay(stack);
MC.player.setStackInHand(Hand.MAIN_HAND, stack);
MC.interactionManager.clickCreativeStack(MC.player.getStackInHand(Hand.MAIN_HAND), 36 + MC.player.getInventory().selectedSlot);
SongPlayer.addChatMessage("§6Set song's display name to §3" + name);

View file

@ -3,6 +3,13 @@ package com.github.hhhzzzsss.songplayer;
import com.mojang.brigadier.suggestion.Suggestions;
import com.mojang.brigadier.suggestion.SuggestionsBuilder;
import net.minecraft.command.CommandSource;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NbtList;
import net.minecraft.nbt.NbtString;
import net.minecraft.text.LiteralTextContent;
import net.minecraft.text.MutableText;
import net.minecraft.text.Style;
import net.minecraft.text.Text;
import java.io.IOException;
import java.nio.file.Files;
@ -105,4 +112,22 @@ public class Util {
.map(Path::toString),
suggestionsBuilder);
}
public static MutableText getStyledText(String str, Style style) {
MutableText text = MutableText.of(new LiteralTextContent(str));
text.setStyle(style);
return text;
}
public static void setItemName(ItemStack stack, Text text) {
stack.getOrCreateSubNbt(ItemStack.DISPLAY_KEY).putString(ItemStack.NAME_KEY, Text.Serializer.toJson(text));
}
public static void setItemLore(ItemStack stack, Text... loreLines) {
NbtList lore = new NbtList();
for (Text line : loreLines) {
lore.add(NbtString.of(Text.Serializer.toJson(line)));
}
stack.getOrCreateSubNbt(ItemStack.DISPLAY_KEY).put(ItemStack.LORE_KEY, lore);
}
}

View file

@ -1,8 +1,12 @@
package com.github.hhhzzzsss.songplayer.item;
import com.github.hhhzzzsss.songplayer.Util;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.nbt.NbtElement;
import net.minecraft.text.Style;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;
import java.util.Base64;
@ -18,9 +22,26 @@ public class SongItemUtils {
songPlayerNbt.putString(SONG_DATA_KEY, Base64.getEncoder().encodeToString(songData));
songPlayerNbt.putString(FILE_NAME_KEY, filename);
songPlayerNbt.putString(DISPLAY_NAME_KEY, displayName);
addSongItemDisplay(stack);
return stack;
}
public static void addSongItemDisplay(ItemStack stack) {
if (!isSongItem(stack)) return;
NbtCompound songPlayerNbt = getSongItemTag(stack);
String name = songPlayerNbt.getString(DISPLAY_NAME_KEY);
if (name == null || name.length() == 0) name = songPlayerNbt.getString(FILE_NAME_KEY);
if (name == null || name.length() == 0) name = "unnamed";
Text nameText = Util.getStyledText(name, Style.EMPTY.withColor(Formatting.DARK_AQUA).withItalic(false));
Util.setItemName(stack, nameText);
Util.setItemLore(stack,
Util.getStyledText("Song Item", Style.EMPTY.withColor(Formatting.YELLOW).withItalic(false)),
Util.getStyledText("Right click to play", Style.EMPTY.withColor(Formatting.AQUA).withItalic(false)),
Util.getStyledText("Requires SongPlayer 3.0+", Style.EMPTY.withColor(Formatting.GOLD).withItalic(false)),
Util.getStyledText("https://github.com/hhhzzzsss/SongPlayer", Style.EMPTY.withColor(Formatting.GRAY).withItalic(false))
);
}
public static NbtCompound getSongItemTag(ItemStack stack) {
return stack.getSubNbt(SONG_ITEM_KEY);
}