From 4f580c8a37620479a3569966288409d7cacada36 Mon Sep 17 00:00:00 2001 From: ybw0014 Date: Sat, 5 Aug 2023 21:02:22 -0400 Subject: [PATCH] feat(wiki): Allow addons to add wiki pages --- .../slimefun4/api/SlimefunAddon.java | 10 +++ .../slimefun4/api/items/SlimefunItem.java | 19 ++++- .../slimefun4/implementation/Slimefun.java | 7 +- .../guide/SurvivalSlimefunGuide.java | 7 +- .../items/magical/talismans/Talisman.java | 2 +- .../implementation/setup/PostSetup.java | 22 +----- .../slimefun4/utils/WikiUtils.java | 76 +++++++++++++++++++ src/main/resources/languages/en/messages.yml | 1 + .../items/TestSlimefunItem.java | 4 +- 9 files changed, 121 insertions(+), 27 deletions(-) create mode 100644 src/main/java/io/github/thebusybiscuit/slimefun4/utils/WikiUtils.java diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/api/SlimefunAddon.java b/src/main/java/io/github/thebusybiscuit/slimefun4/api/SlimefunAddon.java index 8d8609c8be..503751aef7 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/api/SlimefunAddon.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/api/SlimefunAddon.java @@ -97,4 +97,14 @@ default boolean hasDependency(@Nonnull String dependency) { return description.getDepend().contains(dependency) || description.getSoftDepend().contains(dependency); } + /** + * Thid method returns the wiki URL for all the items this {@link SlimefunAddon}. + * The "%item%" placeholder will be replaced with the SlimefunItem's ID. + * + * @return The wiki URL for this {@link SlimefunAddon}. + */ + default @Nonnull String getWikiURL() { + return ""; + } + } diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/api/items/SlimefunItem.java b/src/main/java/io/github/thebusybiscuit/slimefun4/api/items/SlimefunItem.java index 0d4b1f12fa..32efd33483 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/api/items/SlimefunItem.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/api/items/SlimefunItem.java @@ -874,17 +874,32 @@ public void postRegister() { * * @param page * The associated wiki page + * + * @deprecated Use {@link #addWikiPage(String)} instead. */ + @Deprecated public final void addOfficialWikipage(@Nonnull String page) { + addWikiPage(page); + } + + /** + * This method assign the given wiki page to this Item. + * The page name will replace %item% placeholder in the URL. + * + * @param page + * The associated wiki page name. + */ + public final void addWikiPage(@Nonnull String page) { Validate.notNull(page, "Wiki page cannot be null."); - wikiURL = Optional.of("https://github.com/Slimefun/Slimefun4/wiki/" + page); + Validate.isTrue(getState() != ItemState.UNREGISTERED, "Wiki page can only be added after item has been registered."); + wikiURL = Optional.of(getAddon().getWikiURL().replace("%item%", page)); } /** * This method returns the wiki page that has been assigned to this item. * It will return null, if no wiki page was found. * - * @see SlimefunItem#addOfficialWikipage(String) + * @see SlimefunItem#addWikiPage(String) * * @return This item's wiki page */ diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/Slimefun.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/Slimefun.java index bd4618ac13..fefc37f8cb 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/Slimefun.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/Slimefun.java @@ -380,7 +380,7 @@ private void onPluginStart() { } @Override - public JavaPlugin getJavaPlugin() { + public @Nonnull JavaPlugin getJavaPlugin() { return this; } @@ -389,6 +389,11 @@ public String getBugTrackerURL() { return "https://github.com/Slimefun/Slimefun4/issues"; } + @Override + public @Nonnull String getWikiURL() { + return "https://github.com/Slimefun/Slimefun4/wiki/%item%"; + } + /** * This method gets called when the {@link Plugin} gets disabled. * Most often it is called when the {@link Server} is shutting down or reloading. diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/guide/SurvivalSlimefunGuide.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/guide/SurvivalSlimefunGuide.java index 21cee1d006..46dabee669 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/guide/SurvivalSlimefunGuide.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/guide/SurvivalSlimefunGuide.java @@ -514,7 +514,12 @@ public void displayItem(PlayerProfile profile, SlimefunItem item, boolean addToH Optional wiki = item.getWikipage(); if (wiki.isPresent()) { - menu.addItem(8, new CustomItemStack(Material.KNOWLEDGE_BOOK, ChatColor.WHITE + Slimefun.getLocalization().getMessage(p, "guide.tooltips.wiki"), "", ChatColor.GRAY + "\u21E8 " + ChatColor.GREEN + Slimefun.getLocalization().getMessage(p, "guide.tooltips.open-itemgroup"))); + String message = Slimefun.getLocalization().getMessage(p, "guide.tooltips.wiki-third-party") + .replace("%addon%", item.getAddon().getName()); + if (item.getAddon() instanceof Slimefun) { + message = Slimefun.getLocalization().getMessage(p, "guide.tooltips.wiki"); + } + menu.addItem(8, new CustomItemStack(Material.KNOWLEDGE_BOOK, ChatColor.WHITE + message, "", ChatColor.GRAY + "\u21E8 " + ChatColor.GREEN + Slimefun.getLocalization().getMessage(p, "guide.tooltips.open-itemgroup"))); menu.addMenuClickHandler(8, (pl, slot, itemstack, action) -> { pl.closeInventory(); ChatUtils.sendURL(pl, wiki.get()); diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/magical/talismans/Talisman.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/magical/talismans/Talisman.java index 7ecc6f6fab..fa7e1a9a0b 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/magical/talismans/Talisman.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/magical/talismans/Talisman.java @@ -73,7 +73,6 @@ protected Talisman(ItemGroup itemGroup, SlimefunItemStack item, ItemStack[] reci this.suffix = messageSuffix; this.effects = effects; this.chance = chance; - addOfficialWikipage(WIKI_PAGE); if (!(this instanceof EnderTalisman)) { String name = "&5Ender " + ChatColor.stripColor(getItem().getItemMeta().getDisplayName()); @@ -126,6 +125,7 @@ private SlimefunItemStack getEnderVariant() { @Override public void postRegister() { + addWikiPage(WIKI_PAGE); EnderTalisman talisman = new EnderTalisman(this, getEnderVariant()); talisman.register(getAddon()); } diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/setup/PostSetup.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/setup/PostSetup.java index 13e7e7ef5b..874771b4c1 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/setup/PostSetup.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/setup/PostSetup.java @@ -9,9 +9,7 @@ import java.util.Comparator; import java.util.Iterator; import java.util.List; -import java.util.Map; import java.util.logging.Level; -import java.util.stream.Collectors; import java.util.stream.Stream; import javax.annotation.Nonnull; @@ -21,9 +19,6 @@ import org.bukkit.command.CommandSender; import org.bukkit.inventory.ItemStack; -import com.google.gson.JsonElement; -import com.google.gson.JsonObject; - import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItem; import io.github.thebusybiscuit.slimefun4.implementation.Slimefun; import io.github.thebusybiscuit.slimefun4.implementation.SlimefunItems; @@ -31,7 +26,7 @@ import io.github.thebusybiscuit.slimefun4.implementation.items.multiblocks.MakeshiftSmeltery; import io.github.thebusybiscuit.slimefun4.implementation.items.multiblocks.OreCrusher; import io.github.thebusybiscuit.slimefun4.implementation.items.multiblocks.Smeltery; -import io.github.thebusybiscuit.slimefun4.utils.JsonUtils; +import io.github.thebusybiscuit.slimefun4.utils.WikiUtils; import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.abstractItems.AContainer; import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.abstractItems.MachineRecipe; @@ -43,20 +38,7 @@ private PostSetup() {} public static void setupWiki() { Slimefun.logger().log(Level.INFO, "Loading Wiki pages..."); - try (BufferedReader reader = new BufferedReader(new InputStreamReader(Slimefun.class.getResourceAsStream("/wiki.json"), StandardCharsets.UTF_8))) { - JsonElement element = JsonUtils.parseString(reader.lines().collect(Collectors.joining(""))); - JsonObject json = element.getAsJsonObject(); - - for (Map.Entry entry : json.entrySet()) { - SlimefunItem item = SlimefunItem.getById(entry.getKey()); - - if (item != null) { - item.addOfficialWikipage(entry.getValue().getAsString()); - } - } - } catch (IOException e) { - Slimefun.logger().log(Level.SEVERE, "Failed to load wiki.json file", e); - } + WikiUtils.setupWiki(Slimefun.instance()); } public static void loadItems() { diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/utils/WikiUtils.java b/src/main/java/io/github/thebusybiscuit/slimefun4/utils/WikiUtils.java new file mode 100644 index 0000000000..574ec7d870 --- /dev/null +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/utils/WikiUtils.java @@ -0,0 +1,76 @@ +package io.github.thebusybiscuit.slimefun4.utils; + +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.nio.charset.StandardCharsets; +import java.text.MessageFormat; +import java.util.Map; +import java.util.function.UnaryOperator; +import java.util.logging.Level; +import java.util.stream.Collectors; + +import javax.annotation.Nonnull; + +import org.apache.commons.lang.Validate; +import org.bukkit.plugin.Plugin; + +import io.github.thebusybiscuit.slimefun4.api.SlimefunAddon; +import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItem; + +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; + +/** + * This utility class provides methods to setup the wiki pages for Slimefun addons. + * + * @author ybw0014 + */ +public class WikiUtils { + private WikiUtils() {} + + /** + * This method loads the wiki pages from the wiki.json file in the plugin's resources. + * + * @param plugin + * The plugin to load the wiki pages for. + */ + public static void setupWiki(@Nonnull Plugin plugin) { + setupWiki(plugin, page -> page); + } + + /** + * This method loads the wiki pages from the wiki.json file in the plugin's resources. + * The formatter will make changes to the wiki page name before it is added to the item. + * + * @param plugin + * The plugin to load the wiki pages for. + * @param formatter + * The formatter to apply to the wiki page name. + */ + public static void setupWiki(@Nonnull Plugin plugin, @Nonnull UnaryOperator formatter) { + Validate.notNull(plugin, "The plugin cannot be null"); + Validate.isTrue(plugin instanceof SlimefunAddon, "The plugin must be a SlimefunAddon"); + + try (BufferedReader reader = new BufferedReader(new InputStreamReader(plugin.getClass().getResourceAsStream("/wiki.json"), StandardCharsets.UTF_8))) { + JsonElement element = JsonUtils.parseString(reader.lines().collect(Collectors.joining(""))); + JsonObject json = element.getAsJsonObject(); + + int count = 0; + + for (Map.Entry entry : json.entrySet()) { + SlimefunItem item = SlimefunItem.getById(entry.getKey()); + + if (item != null) { + String page = entry.getValue().getAsString(); + page = formatter.apply(page); + item.addWikiPage(page); + count++; + } + } + + plugin.getLogger().log(Level.INFO, MessageFormat.format("Loaded {0} Wiki pages from {1}", count, plugin.getName())); + } catch (Exception e) { + plugin.getLogger().log(Level.SEVERE, MessageFormat.format("Failed to load wiki.json from {0}", plugin.getName()), e); + } + } +} diff --git a/src/main/resources/languages/en/messages.yml b/src/main/resources/languages/en/messages.yml index c678a28c2e..d831eb6beb 100644 --- a/src/main/resources/languages/en/messages.yml +++ b/src/main/resources/languages/en/messages.yml @@ -61,6 +61,7 @@ guide: open-itemgroup: 'Click to open' versions-notice: 'These are very important when reporting bugs!' wiki: 'View this Item on the official Slimefun Wiki' + wiki-third-party: 'View this Item on the %addon% Wiki (not Slimefun official)' recipes: machine: 'Recipes made in this Machine' diff --git a/src/test/java/io/github/thebusybiscuit/slimefun4/implementation/items/TestSlimefunItem.java b/src/test/java/io/github/thebusybiscuit/slimefun4/implementation/items/TestSlimefunItem.java index 8ccbfbe7d6..7e5d05ffd1 100644 --- a/src/test/java/io/github/thebusybiscuit/slimefun4/implementation/items/TestSlimefunItem.java +++ b/src/test/java/io/github/thebusybiscuit/slimefun4/implementation/items/TestSlimefunItem.java @@ -47,9 +47,9 @@ void testWikiPages() { Assertions.assertFalse(item.getWikipage().isPresent()); // null should not be a valid argument - Assertions.assertThrows(IllegalArgumentException.class, () -> item.addOfficialWikipage(null)); + Assertions.assertThrows(IllegalArgumentException.class, () -> item.addWikiPage(null)); - item.addOfficialWikipage("Test"); + item.addWikiPage("Test"); Optional wiki = item.getWikipage(); Assertions.assertTrue(wiki.isPresent());