Add lore manipulation helpers

This commit is contained in:
Tara 2023-01-21 18:20:28 -07:00
parent 78b5a5f790
commit de66f0b7ca
7 changed files with 287 additions and 2 deletions

View file

@ -5,4 +5,4 @@ org.gradle.daemon=false
mc_version=1.19.2 mc_version=1.19.2
forge_version=43.2.3 forge_version=43.2.3
myversion=1.0.2.4 myversion=1.0.3.0

View file

@ -0,0 +1,25 @@
package dev.zontreck.libzontreck.bossbars;
import net.minecraft.client.Minecraft;
import net.minecraft.server.bossevents.CustomBossEvents;
import net.minecraft.server.commands.BossBarCommands;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.entity.player.Player;
public class BossBarUtils {
public class BossBarStructure
{
public String Header;
public int Percent;
}
public static void sendBossBarToPlayer(Player p, BossBarStructure struc)
{
if(Minecraft.getInstance().level.isClientSide())return;
ServerPlayer sp = (ServerPlayer)p;
if(sp==null)return;
//sp.server.getCustomBossEvents().create(null, null)
}
}

View file

@ -0,0 +1,69 @@
package dev.zontreck.libzontreck.items.lore;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.json.Json;
import javax.json.JsonArrayBuilder;
import javax.json.JsonObjectBuilder;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
import net.minecraft.nbt.Tag;
// Extra container
public class ExtraLore {
public List<LoreEntry> LoreData = new ArrayList<>();
public void save(CompoundTag tag)
{
ListTag lores = new ListTag();
for (LoreEntry loreEntry : LoreData) {
loreEntry.save(lores);
}
// Extra entry in display:Lore list
tag.put("extra", lores);
}
public ListTag save(){
ListTag lores = new ListTag();
for (LoreEntry tag : LoreData) {
tag.save(lores);
}
return lores;
}
// This json object is what goes inside the actual item lore. This is not the entry used to save the state
public String saveJson(){
String ret = "";
JsonObjectBuilder loreEntry = Json.createObjectBuilder();
JsonArrayBuilder jab = Json.createArrayBuilder();
for (LoreEntry loreEntryx : LoreData) {
jab.add(loreEntryx.saveJson());
}
loreEntry.add("extra", jab);
loreEntry.add("text", "");
ret=loreEntry.build().toString();
return ret;
}
public ExtraLore(CompoundTag tags)
{
ListTag tag = tags.getList("extra", CompoundTag.TAG_COMPOUND);
Iterator<Tag> tagsx = tag.iterator();
while(tagsx.hasNext())
{
Tag t = tagsx.next();
CompoundTag ct = (CompoundTag)t;
LoreData.add(new LoreEntry(ct));
}
}
public ExtraLore()
{
}
}

View file

@ -0,0 +1,103 @@
package dev.zontreck.libzontreck.items.lore;
import dev.zontreck.libzontreck.chat.ChatColor;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
import net.minecraft.nbt.StringTag;
import net.minecraft.nbt.Tag;
import net.minecraft.world.item.ItemStack;
public class LoreContainer {
public int loreEntryNumber;
public ExtraLore miscData;
private ItemStack associatedItem;
public LoreContainer(CompoundTag container, ItemStack associated)
{
loreEntryNumber = container.getInt("pos");
miscData = new ExtraLore(container.getCompound("state"));
this.associatedItem = associated;
}
public LoreContainer(ItemStack stack)
{
this.associatedItem=stack;
// Set the loreentrynumber appropriately, and insert a blank entry to hold it's position
CompoundTag display = stack.getOrCreateTag().getCompound(ItemStack.TAG_DISPLAY);
ListTag loreEntries = null;
if(display!= null)
{
loreEntries = display.getList(ItemStack.TAG_LORE, Tag.TAG_COMPOUND);
if(loreEntries==null)
{
loreEntryNumber=0;
}else {
loreEntryNumber = loreEntries.size(); // This will be the next position
}
}else {
loreEntryNumber=0;
}
miscData = new ExtraLore();
LoreEntry blank = new LoreEntry();
blank.text = ChatColor.WHITE + "Nothing to see here";
miscData.LoreData.add(blank);
commitLore();
}
public void commitLore()
{
AssertLoreExists();
// Set the Lore
ListTag lst = associatedItem.getTag().getCompound(ItemStack.TAG_DISPLAY).getList(ItemStack.TAG_LORE, Tag.TAG_STRING);
// Set the lore entry
lst.set(loreEntryNumber, StringTag.valueOf(miscData.saveJson()));
}
private void AssertLoreExists()
{
AssertTag();
AssertDisplay();
AssertLore();
}
private void AssertTag()
{
if(!associatedItem.hasTag()){
associatedItem.setTag(new CompoundTag());
}
}
private void AssertDisplay()
{
CompoundTag tag = associatedItem.getTag();
CompoundTag display = tag.getCompound(ItemStack.TAG_DISPLAY);
if(display==null)
{
tag.put(ItemStack.TAG_DISPLAY, new CompoundTag());
}
}
private void AssertLore()
{
CompoundTag tag = associatedItem.getTag();
CompoundTag display = tag.getCompound(ItemStack.TAG_DISPLAY);
ListTag lore = display.getList(ItemStack.TAG_LORE, Tag.TAG_STRING);
if(lore == null)
{
lore = new ListTag();
display.put(ItemStack.TAG_LORE, lore);
//tag.put(ItemStack.TAG_DISPLAY, display);
//associatedItem.setTag(tag);
}
}
}

View file

@ -0,0 +1,63 @@
package dev.zontreck.libzontreck.items.lore;
import javax.json.Json;
import javax.json.JsonObjectBuilder;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
public class LoreEntry
{
public boolean bold;
public boolean italic;
public boolean underlined;
public boolean strikethrough;
public boolean obfuscated;
public String color="";
public String text="";
public LoreEntry (CompoundTag tag)
{
bold = tag.getBoolean("bold");
italic = tag.getBoolean("italic");
underlined = tag.getBoolean("underlined");
strikethrough = tag.getBoolean("strikethrough");
obfuscated = tag.getBoolean("obfuscated");
color = tag.getString("color");
text = tag.getString("text");
}
public void save(ListTag parentTag){
CompoundTag tag = new CompoundTag();
tag.putBoolean("bold", bold);
tag.putBoolean("italic", italic);
tag.putBoolean("underlined", underlined);
tag.putBoolean("strikethrough", strikethrough);
tag.putBoolean("obfuscated", obfuscated);
tag.putString("color", color);
tag.putString("text", text);
parentTag.add(tag);
}
// Only json saving is available.
// The NBT Variant should be saved to the mod's custom tag container due to the way lore must be formatted
public JsonObjectBuilder saveJson()
{
JsonObjectBuilder obj = Json.createObjectBuilder();
obj.add("bold", bold);
obj.add("italic", italic);
obj.add("underlined", underlined);
obj.add("strikethrough", strikethrough);
obj.add("obfuscated", obfuscated);
obj.add("color", color);
obj.add("text", text);
return obj;
}
public LoreEntry(){}
}

View file

@ -0,0 +1,25 @@
package dev.zontreck.libzontreck.items.lore;
public enum LoreType {
UNKNOWN((byte) 0x00),
STATS((byte) 0x01),
ORIGINAL_CRAFTER((byte)0x02);
private final byte type;
private LoreType(byte Option) {
type = Option;
}
public static LoreType valueOf(byte b) {
LoreType _T = LoreType.UNKNOWN;
_T = values()[b];
return _T;
}
public byte get()
{
return type;
}
}

View file

@ -19,7 +19,7 @@ modId="libzontreck" #mandatory
# The version number of the mod - there's a few well known ${} variables useable here or just hardcode it # 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 # ${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 # see the associated build.gradle script for how to populate this completely automatically during a build
version="1.0.2.4" #mandatory version="1.0.3.0" #mandatory
# A display name for the mod # A display name for the mod
displayName="LibZontreck" #mandatory displayName="LibZontreck" #mandatory
# A URL to query for updates for this mod. See the JSON update specification https://mcforge.readthedocs.io/en/latest/gettingstarted/autoupdate/ # A URL to query for updates for this mod. See the JSON update specification https://mcforge.readthedocs.io/en/latest/gettingstarted/autoupdate/