Skip to content

Commit

Permalink
feat(wiki): Allow addons to add wiki pages
Browse files Browse the repository at this point in the history
  • Loading branch information
ybw0014 committed Aug 6, 2023
1 parent 75a781d commit 4f580c8
Show file tree
Hide file tree
Showing 9 changed files with 121 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 "";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ private void onPluginStart() {
}

@Override
public JavaPlugin getJavaPlugin() {
public @Nonnull JavaPlugin getJavaPlugin() {
return this;
}

Expand All @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,12 @@ public void displayItem(PlayerProfile profile, SlimefunItem item, boolean addToH
Optional<String> 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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -126,6 +125,7 @@ private SlimefunItemStack getEnderVariant() {

@Override
public void postRegister() {
addWikiPage(WIKI_PAGE);
EnderTalisman talisman = new EnderTalisman(this, getEnderVariant());
talisman.register(getAddon());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -21,17 +19,14 @@
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;
import io.github.thebusybiscuit.slimefun4.implementation.items.multiblocks.GrindStone;
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;
Expand All @@ -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<String, JsonElement> 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() {
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String> 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<String, JsonElement> 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);
}
}
}
1 change: 1 addition & 0 deletions src/main/resources/languages/en/messages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> wiki = item.getWikipage();
Assertions.assertTrue(wiki.isPresent());
Expand Down

0 comments on commit 4f580c8

Please sign in to comment.