Skip to content

Commit

Permalink
Added bestiary lore from mainhand (#51)
Browse files Browse the repository at this point in the history
run /bestiary lore frommainhand <mobname> to copy the lore from your mainhand onto the mob's lore.
  • Loading branch information
Wembler23 authored Mar 8, 2024
1 parent ae1b24a commit e5008b0
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ public void onEnable() {

if (!Config.isReadOnly()) {
LibraryOfSoulsCommand.registerWriteAccessCommands();
BestiaryCommand.registerWriteAccessCommands();
}

new SoulsDatabase(this, !Config.isReadOnly());
Expand Down
47 changes: 35 additions & 12 deletions src/main/java/com/playmonumenta/libraryofsouls/SoulEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.goncalomb.bukkit.nbteditor.nbt.EntityNBT;
import com.goncalomb.bukkit.nbteditor.nbt.variables.BooleanVariable;
import com.goncalomb.bukkit.nbteditor.nbt.variables.EffectsVariable;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
Expand All @@ -21,6 +22,7 @@
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.format.TextDecoration;
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
Expand All @@ -37,12 +39,14 @@
import org.jetbrains.annotations.Nullable;

public class SoulEntry implements Soul, BestiaryEntryInterface {
public static final Gson GSON = new Gson();
public static final GsonComponentSerializer GSON_SERIALIZER = GsonComponentSerializer.gson();
private final Set<String> mLocs;
private final List<SoulHistoryEntry> mHistory;
private String mLore;
private List<Component> mLore;

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

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

if (lore == null) {
mLore = "";
mLore = new ArrayList<Component>();
} else {
mLore = lore;
}
Expand All @@ -74,7 +78,7 @@ public SoulEntry(Player player, NBTTagCompound nbt) throws Exception {
mLocs = new HashSet<String>();
mHistory = new ArrayList<SoulHistoryEntry>(1);
mHistory.add(newHist);
mLore = "";
mLore = new ArrayList<>();
}

/* Update this SoulEntry so new soul is now current; preserve history */
Expand Down Expand Up @@ -206,12 +210,12 @@ public Entity summon(Location loc) {
return mHistory.get(0).summon(loc);
}

public void setLore(String lore, Player player) {
public void setLore(List<Component> lore, Player player) {
mLore = lore;
SoulsDatabase.getInstance().updateLore(this, player);
}

public String getLore() {
public List<Component> getLore() {
return mLore;
}

Expand Down Expand Up @@ -396,12 +400,25 @@ public static SoulEntry fromJson(JsonObject obj, boolean loadHistory) throws Exc
}
}

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

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

List<SoulHistoryEntry> history = new ArrayList<SoulHistoryEntry>();
Expand Down Expand Up @@ -444,7 +461,13 @@ public JsonObject toJson() {
}
obj.add("history", histArray);

obj.addProperty("lore", mLore);
JsonArray loreArray = new JsonArray();

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

obj.add("lore", loreArray);

JsonArray locsArray = new JsonArray();
for (String location : mLocs) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,14 @@ public double height() {
private final String mLabel;
private final Set<String> mLocs;
private final NamespacedKey mId;
private final String mLore;
private final List<Component> mLore;
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, String lore, @Nullable Double width, @Nullable Double height) throws Exception {
public SoulHistoryEntry(NBTTagCompound nbt, long modifiedOn, String modifiedBy, Set<String> locations, List<Component> lore, @Nullable Double width, @Nullable Double height) throws Exception {
mNBT = nbt;
mModifiedOn = modifiedOn;
mModifiedBy = modifiedBy;
Expand Down Expand Up @@ -115,7 +115,7 @@ public SoulHistoryEntry(Player player, NBTTagCompound nbt) throws Exception {
mModifiedBy = player.getName();
mLocs = new HashSet<String>();
mId = EntityNBT.fromEntityData(mNBT).getEntityType().getKey();
mLore = "";
mLore = new ArrayList<>();
mWidth = hitboxSize.width();
mHeight = hitboxSize.height();

Expand Down Expand Up @@ -653,11 +653,17 @@ private void regenerateItems() {
}
}

if (mLore != null && !mLore.equals("")) {
if (mLore != null && !mLore.isEmpty()) {
((ListVariable)placeholderWrap.getVariable("Lore")).add(ChatColor.WHITE + "Lore:", null);
((ListVariable)bosWrap.getVariable("Lore")).add(ChatColor.WHITE + "Lore:", null);
((ListVariable)placeholderWrap.getVariable("Lore")).add(mLore, null);
((ListVariable)bosWrap.getVariable("Lore")).add(mLore, null);
//This is going to be terrible. Maybe I should just combine them?
for (Component comp : mLore) {
((ListVariable)placeholderWrap.getVariable("Lore")).add(comp.toString(), null);
}
for (Component comp : mLore) {
((ListVariable)bosWrap.getVariable("Lore")).add(comp.toString(), null);
}

}

/* If the item has been modified, list when */
Expand Down Expand Up @@ -722,7 +728,7 @@ public JsonObject toJson() {
return obj;
}

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

NBTTagCompound nbt = NBTTagCompound.fromString(elem.getAsString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,20 @@ public static void register() {
inv.openInventory(sender);
}
}))
.withSubcommand(new CommandAPICommand("deleteall")
.withPermission(CommandPermission.fromString("los.bestiary.deleteall"))
.withArguments(new EntitySelectorArgument.OnePlayer("player"))
.executes((sender, args) -> {
Player player = (Player)args[0];
BestiaryManager.deleteAll(player);
}))
.register();
}

public static void registerWriteAccessCommands() {
final String command = "bestiary";

new CommandAPICommand(command)
.withSubcommand(new CommandAPICommand("lore")
.withPermission(CommandPermission.fromString("los.bestiary.lore"))
.withArguments(new StringArgument("mobLabel").replaceSuggestions(LibraryOfSoulsCommand.LIST_MOBS_FUNCTION))
Expand All @@ -122,7 +136,10 @@ public static void register() {
if (soul == null) {
throw CommandAPI.failWithString("Mob '" + name + "' not found");
} else {
soul.setLore((String)args[1], sender);
Component component = Component.text((String)args[1]);
List<Component> compList = new ArrayList<Component>();
compList.add(component);
soul.setLore(compList, sender);
}
})
.executesProxy((sender, args) -> {
Expand All @@ -132,30 +149,43 @@ public static void register() {
if (soul == null) {
throw CommandAPI.failWithString("Mob '" + name + "' not found");
}
soul.setLore((String)args[1], player);
Component component = Component.text((String)args[1]);
List<Component> compList = new ArrayList<Component>();
compList.add(component);
soul.setLore(compList, player);
} else {
throw CommandAPI.failWithString("Callee must be instance of Player");
}
}))
.withSubcommand(new CommandAPICommand("lore")
.withSubcommand(new CommandAPICommand("clear")
.withPermission(CommandPermission.fromString("los.bestiary.lore"))
.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.setLore("", sender);
})))
.withSubcommand(new CommandAPICommand("deleteall")
.withPermission(CommandPermission.fromString("los.bestiary.deleteall"))
.withArguments(new EntitySelectorArgument.OnePlayer("player"))
.executes((sender, args) -> {
Player player = (Player)args[0];
BestiaryManager.deleteAll(player);
}))
.withSubcommand(new CommandAPICommand("lore")
.withSubcommand(new CommandAPICommand("clear")
.withPermission(CommandPermission.fromString("los.bestiary.lore"))
.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.setLore(new ArrayList<>(), sender);
})))
.withSubcommand(new CommandAPICommand("lore")
.withSubcommand(new CommandAPICommand("frommainhand")
.withArguments(new StringArgument("mobLabel").replaceSuggestions(LibraryOfSoulsCommand.LIST_MOBS_FUNCTION))
.withPermission(CommandPermission.fromString("los.bestiary.lore"))
.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.setLore(lore, sender);
})))
.register();
}

Expand All @@ -177,13 +207,13 @@ private void openInventory(Player player) {
}

private ItemStack getLoreItem(SoulEntry soul) {
String lore = soul.getLore();
List<Component> lore = soul.getLore();

ItemStack loreItem = new ItemStack(Material.BOOK);
ItemMeta meta = loreItem.getItemMeta();
meta.displayName(Component.text("Lore", NamedTextColor.WHITE).decoration(TextDecoration.ITALIC, false).decoration(TextDecoration.BOLD, true));

if (lore == null || lore.equals("")) {
if (lore == null || lore.isEmpty()) {
List<Component> itemLore = new ArrayList<>();
itemLore.add(Component.text("This is a bug. Or at the very least, should be.", NamedTextColor.WHITE).decoration(TextDecoration.ITALIC, true));

Expand All @@ -192,15 +222,7 @@ private ItemStack getLoreItem(SoulEntry soul) {
return loreItem;
}

List<Component> itemLore = new ArrayList<>();

String[] loreArray = lore.split("~~~");

for (String a : loreArray) {
itemLore.add(Component.text(a, NamedTextColor.WHITE).decoration(TextDecoration.ITALIC, true));
}

meta.lore(itemLore);
meta.lore(lore);
loreItem.setItemMeta(meta);

return loreItem;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ public BestiarySoulInventory(Player player, SoulEntry soul, BestiaryArea parent,
_inventory.setItem(13, armorItem);
_inventory.setItem(15, damageItem);
_inventory.setItem(22, equipmentPageItem);
if (!soul.getLore().equals("")) {
if (!soul.getLore().isEmpty()) {
_inventory.setItem(29, speedItem);
_inventory.setItem(31, effectItem);
_inventory.setItem(33, loreItem);
Expand Down Expand Up @@ -724,13 +724,13 @@ private static ItemStack getSpeedItem(EntityNBT entityNBT, double speed, double
}

public ItemStack getLoreItem(SoulEntry soul) {
String lore = soul.getLore();
List<Component> lore = soul.getLore();

ItemStack loreItem = new ItemStack(Material.BOOK);
ItemMeta meta = loreItem.getItemMeta();
meta.displayName(Component.text("Lore", NamedTextColor.WHITE).decoration(TextDecoration.ITALIC, false).decoration(TextDecoration.BOLD, true));

if (lore == null || lore.equals("")) {
if (lore == null || lore.isEmpty()) {
List<Component> itemLore = new ArrayList<>();
itemLore.add(Component.text("This is a bug. Or at the very least, should be.", NamedTextColor.WHITE).decoration(TextDecoration.ITALIC, true));

Expand All @@ -741,10 +741,8 @@ public ItemStack getLoreItem(SoulEntry soul) {

List<Component> itemLore = new ArrayList<>();

String[] loreArray = lore.split("~~~");

for (String a : loreArray) {
itemLore.add(Component.text(a, NamedTextColor.WHITE).decoration(TextDecoration.ITALIC, true));
for (Component comp : lore) {
itemLore.add(comp);
}

meta.lore(itemLore);
Expand Down

0 comments on commit e5008b0

Please sign in to comment.