Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add description field to LoS entries #54

Merged
merged 1 commit into from
May 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package com.playmonumenta.libraryofsouls;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import net.kyori.adventure.text.Component;
import org.bukkit.Location;
import org.bukkit.entity.Entity;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -55,4 +57,13 @@ public static Set<String> getSoulLocations() {
SoulsDatabase db = SoulsDatabase.getInstance();
return db.listMobLocations();
}

public static List<Component> getDescription(String soulName) {
SoulsDatabase db = SoulsDatabase.getInstance();
SoulEntry soul = db.getSoul(soulName);
if (soul == null) {
return null;
}
return soul.getDescription();
}
}
61 changes: 53 additions & 8 deletions src/main/java/com/playmonumenta/libraryofsouls/SoulEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,10 @@ public class SoulEntry implements Soul, BestiaryEntryInterface {
private final Set<String> mLocs;
private final List<SoulHistoryEntry> mHistory;
private List<Component> mLore;
private List<Component> mDescription;

/* Create a SoulEntry object with existing history */
public SoulEntry(List<SoulHistoryEntry> history, Set<String> locationNames, List<Component> lore) throws Exception {
public SoulEntry(List<SoulHistoryEntry> history, Set<String> locationNames, List<Component> lore, List<Component> description) throws Exception {
mHistory = history;

if (locationNames == null) {
Expand All @@ -56,11 +57,17 @@ public SoulEntry(List<SoulHistoryEntry> history, Set<String> locationNames, List
}

if (lore == null) {
mLore = new ArrayList<Component>();
mLore = new ArrayList<>();
} else {
mLore = lore;
}

if (description == null) {
mDescription = new ArrayList<>();
} else {
mDescription = description;
}

String refLabel = history.get(0).getLabel();
Component refName = history.get(0).getName();

Expand All @@ -79,6 +86,7 @@ public SoulEntry(Player player, NBTTagCompound nbt) throws Exception {
mHistory = new ArrayList<SoulHistoryEntry>(1);
mHistory.add(newHist);
mLore = new ArrayList<>();
mDescription = new ArrayList<>();
}

/* Update this SoulEntry so new soul is now current; preserve history */
Expand Down Expand Up @@ -219,6 +227,15 @@ public List<Component> getLore() {
return mLore;
}

public void setDescription(List<Component> description, Player player) {
mDescription = description;
SoulsDatabase.getInstance().updateLore(this, player);
}

public List<Component> getDescription() {
return mDescription;
}

/*
* Soul Interface
*--------------------------------------------------------------------------------*/
Expand Down Expand Up @@ -250,8 +267,11 @@ public ItemStack getBestiaryItem(Player player) {

lore.add(Component.text(BestiarySoulInventory.formatWell(getId().getKey()), NamedTextColor.GRAY).decoration(TextDecoration.ITALIC, false));
lore.add(Component.text("Kills: " + BestiaryManager.getKillsForMob(player, this), NamedTextColor.DARK_RED).decoration(TextDecoration.ITALIC, false));
if (!mDescription.isEmpty()) {
lore.addAll(getDescription());
}
if (info.allowsAccessTo(InfoTier.STATS)) {
lore.add(Component.text("Click for mob info", NamedTextColor.LIGHT_PURPLE).decoration(TextDecoration.ITALIC, false));
lore.add(Component.text("Click for more info!", NamedTextColor.LIGHT_PURPLE).decoration(TextDecoration.ITALIC, false));
}

meta.lore(lore);
Expand Down Expand Up @@ -421,6 +441,27 @@ public static SoulEntry fromJson(JsonObject obj, boolean loadHistory) throws Exc
lore.add(Component.text(elem.getAsString()));
}

List<Component> description = new ArrayList<>();
elem = obj.get("description");
if (elem != null && elem.isJsonArray()) {
JsonArray array = elem.getAsJsonArray();
if (array == null) {
throw new Exception("Failed to parse description as JSON array");
}

Iterator<JsonElement> iter = array.iterator();
while (iter.hasNext()) {
JsonElement descriptionElement = iter.next();
if (!descriptionElement.isJsonPrimitive()) {
throw new Exception("description entry for '" + elem + "' is not a string!");
}
Component comp = GSON_SERIALIZER.deserialize(descriptionElement.getAsString());
description.add(comp);
}
} else if (elem != null && elem.isJsonPrimitive()) {
description.add(Component.text(elem.getAsString()));
}

List<SoulHistoryEntry> history = new ArrayList<SoulHistoryEntry>();
elem = obj.get("history");
if (elem != null) {
Expand All @@ -435,7 +476,7 @@ public static SoulEntry fromJson(JsonObject obj, boolean loadHistory) throws Exc
throw new Exception("history entry for '" + elem.toString() + "' is not a string!");
}

history.add(SoulHistoryEntry.fromJson(historyElement.getAsJsonObject(), locs, lore));
history.add(SoulHistoryEntry.fromJson(historyElement.getAsJsonObject(), locs, lore, description));
}
} else {
if (array.size() >= 1) {
Expand All @@ -444,12 +485,12 @@ public static SoulEntry fromJson(JsonObject obj, boolean loadHistory) throws Exc
throw new Exception("history entry for '" + elem.toString() + "' is not a string!");
}

history.add(SoulHistoryEntry.fromJson(historyElement.getAsJsonObject(), locs, lore));
history.add(SoulHistoryEntry.fromJson(historyElement.getAsJsonObject(), locs, lore, description));
}
}
}

return new SoulEntry(history, locs, lore);
return new SoulEntry(history, locs, lore, description);
}

public JsonObject toJson() {
Expand All @@ -462,13 +503,17 @@ public JsonObject toJson() {
obj.add("history", histArray);

JsonArray loreArray = new JsonArray();

for (Component comp : mLore) {
loreArray.add(GSON_SERIALIZER.serialize(comp));
}

obj.add("lore", loreArray);

JsonArray descriptionArray = new JsonArray();
for (Component comp : mDescription) {
descriptionArray.add(GSON_SERIALIZER.serialize(comp));
}
obj.add("description", descriptionArray);

JsonArray locsArray = new JsonArray();
for (String location : mLocs) {
locsArray.add(location);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,19 +81,21 @@ public double height() {
private final Set<String> mLocs;
private final NamespacedKey mId;
private final List<Component> mLore;
private final List<Component> mDescription;
private final @Nullable Double mWidth;
private final @Nullable Double mHeight;
private @Nullable ItemStack mPlaceholder = null;
private @Nullable ItemStack mBoS = null;

/* Create a SoulHistoryEntry object with existing history */
public SoulHistoryEntry(NBTTagCompound nbt, long modifiedOn, String modifiedBy, Set<String> locations, List<Component> lore, @Nullable Double width, @Nullable Double height) throws Exception {
public SoulHistoryEntry(NBTTagCompound nbt, long modifiedOn, String modifiedBy, Set<String> locations, List<Component> lore, List<Component> description, @Nullable Double width, @Nullable Double height) throws Exception {
mNBT = nbt;
mModifiedOn = modifiedOn;
mModifiedBy = modifiedBy;
mLocs = locations;
mId = EntityNBT.fromEntityData(mNBT).getEntityType().getKey();
mLore = lore;
mDescription = description;
mWidth = width;
mHeight = height;

Expand All @@ -116,6 +118,7 @@ public SoulHistoryEntry(Player player, NBTTagCompound nbt) throws Exception {
mLocs = new HashSet<String>();
mId = EntityNBT.fromEntityData(mNBT).getEntityType().getKey();
mLore = new ArrayList<>();
mDescription = new ArrayList<>();
mWidth = hitboxSize.width();
mHeight = hitboxSize.height();

Expand All @@ -133,12 +136,13 @@ public boolean requiresAutoUpdate() {
public SoulHistoryEntry getAutoUpdate(Location loc) throws Exception {
HitboxSize hitboxSize = new HitboxSize(loc, mNBT);
return new SoulHistoryEntry(mNBT,
Instant.now().getEpochSecond(),
"AutoUpdate",
mLocs,
mLore,
hitboxSize.width(),
hitboxSize.height());
Instant.now().getEpochSecond(),
"AutoUpdate",
mLocs,
mLore,
mDescription,
hitboxSize.width(),
hitboxSize.height());
}


Expand Down Expand Up @@ -660,6 +664,13 @@ private void regenerateItems() {
((ListVariable)placeholderWrap.getVariable("Lore")).add("It exists.", null);
}

if (mDescription != null && !mDescription.isEmpty()) {
((ListVariable)placeholderWrap.getVariable("Lore")).add(ChatColor.WHITE + "Description:", null);
((ListVariable)bosWrap.getVariable("Lore")).add(ChatColor.WHITE + "Description:", null);
// Rather than a giant block of text, two words suffice.
((ListVariable)placeholderWrap.getVariable("Lore")).add("It exists.", null);
}

/* If the item has been modified, list when */
if (mModifiedBy != null && !mModifiedBy.isEmpty()) {
/* Relative time on the placeholder item */
Expand Down Expand Up @@ -722,7 +733,7 @@ public JsonObject toJson() {
return obj;
}

public static SoulHistoryEntry fromJson(JsonObject obj, Set<String> locations, List<Component> lore) throws Exception {
public static SoulHistoryEntry fromJson(JsonObject obj, Set<String> locations, List<Component> lore, List<Component> description) throws Exception {
JsonElement elem = obj.get("mojangson");

NBTTagCompound nbt = NBTTagCompound.fromString(elem.getAsString());
Expand All @@ -738,6 +749,6 @@ public static SoulHistoryEntry fromJson(JsonObject obj, Set<String> locations, L
height = obj.get("height").getAsDouble();
}

return new SoulHistoryEntry(nbt, modifiedOn, modifiedBy, locations, lore, width, height);
return new SoulHistoryEntry(nbt, modifiedOn, modifiedBy, locations, lore, description, width, height);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ public void updateLore(SoulEntry soul, Player sender) {
try {
soul.update(sender, soul.getNBT());
} catch (Exception ex) {
sender.sendMessage("Exception when updating lore: " + ex + " for " + soul.getDisplayName());
sender.sendMessage("Exception when updating lore or description: " + ex + " for " + soul.getDisplayName());
}
save();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,66 @@ public static void registerWriteAccessCommands() {
}
soul.setLore(lore, sender);
})))
.withSubcommand(new CommandAPICommand("description")
.withPermission(CommandPermission.fromString("los.bestiary.description"))
.withArguments(new StringArgument("mobLabel").replaceSuggestions(LibraryOfSoulsCommand.LIST_MOBS_FUNCTION))
.withArguments(new TextArgument("description"))
.executesPlayer((sender, args) -> {
String name = (String)args[0];
SoulEntry soul = SoulsDatabase.getInstance().getSoul(name);
if (soul == null) {
throw CommandAPI.failWithString("Mob '" + name + "' not found");
} else {
Component component = Component.text((String)args[1]);
List<Component> compList = new ArrayList<>();
compList.add(component);
soul.setDescription(compList, sender);
}
})
.executesProxy((sender, args) -> {
if (sender.getCallee() instanceof Player player) {
String name = (String)args[0];
SoulEntry soul = SoulsDatabase.getInstance().getSoul(name);
if (soul == null) {
throw CommandAPI.failWithString("Mob '" + name + "' not found");
}
Component component = Component.text((String)args[1]);
List<Component> compList = new ArrayList<>();
compList.add(component);
soul.setDescription(compList, player);
} else {
throw CommandAPI.failWithString("Callee must be instance of Player");
}
}))
.withSubcommand(new CommandAPICommand("description")
.withSubcommand(new CommandAPICommand("clear")
.withPermission(CommandPermission.fromString("los.bestiary.description"))
.withArguments(new StringArgument("mobLabel").replaceSuggestions(LibraryOfSoulsCommand.LIST_MOBS_FUNCTION))
.executesPlayer((sender, args) -> {
String name = (String)args[0];
SoulEntry soul = SoulsDatabase.getInstance().getSoul(name);
if (soul == null) {
throw CommandAPI.failWithString("Mob '" + name + "' not found");
}
soul.setDescription(new ArrayList<>(), sender);
})))
.withSubcommand(new CommandAPICommand("description")
.withSubcommand(new CommandAPICommand("frommainhand")
.withArguments(new StringArgument("mobLabel").replaceSuggestions(LibraryOfSoulsCommand.LIST_MOBS_FUNCTION))
.withPermission(CommandPermission.fromString("los.bestiary.description"))
.executesPlayer((sender, args) -> {
ItemStack item = sender.getInventory().getItemInMainHand();
if (item == null || !item.getItemMeta().hasLore()) {
throw CommandAPI.failWithString("You need a valid item with lore text!");
}
List<Component> lore = item.lore();
String name = (String)args[0];
SoulEntry soul = SoulsDatabase.getInstance().getSoul(name);
if (soul == null) {
throw CommandAPI.failWithString("Mob '" + name + "' not found");
}
soul.setDescription(lore, sender);
})))
.register();
}

Expand Down
Loading