-
Notifications
You must be signed in to change notification settings - Fork 588
feat: allow addons to add wiki pages to items #3937
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
Open
ybw0014
wants to merge
20
commits into
Slimefun:master
Choose a base branch
from
ybw0014:feat/wiki
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 12 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
4f580c8
feat(wiki): Allow addons to add wiki pages
ybw0014 f86d54f
chore: Fix type
ybw0014 881ea3f
feat(api): add custom wiki page
ybw0014 622b3e1
chore: use Preconditions
ybw0014 ede8d29
fix: check argument
ybw0014 5ad0a36
feat(wiki): add warning message for non-Slimefun items
ybw0014 20d95d4
fix(wiki): replace the placeholder in message
ybw0014 03065e8
feat(wiki): limit to github only
ybw0014 68a86e8
docs(wiki): add javadocs
ybw0014 c71c50a
revert: 68a86e83a4abdb0698d1a1b5b2c7880ea6011c1e.
ybw0014 06dd2f2
revert: 03065e8f4a639d26db2bd7ff99211332d7e43b11.
ybw0014 18b73b0
Merge remote-tracking branch 'upstream/master' into feat/wiki
ybw0014 057877e
chore: add Nonnull annotation
ybw0014 38967df
refactor: update the button, make the name shorter
ybw0014 7aac23a
fix(wiki): fix addon name replacement
ybw0014 313811c
Merge branch 'master' into feat/wiki
ybw0014 357ea18
Merge branch 'master' into feat/wiki
ybw0014 e11f430
feat: disallow changing wiki URL after it has been set
ybw0014 92645fc
feat: match the url pattern to detect official Slimefun wiki
ybw0014 d034570
chore: move pattern to util
ybw0014 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
src/main/java/io/github/thebusybiscuit/slimefun4/utils/WikiUtils.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
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.bukkit.plugin.Plugin; | ||
|
||
import io.github.thebusybiscuit.slimefun4.api.SlimefunAddon; | ||
import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItem; | ||
|
||
import com.google.common.base.Preconditions; | ||
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) { | ||
Preconditions.checkArgument(plugin != null, "The plugin cannot be null"); | ||
Preconditions.checkArgument(formatter != null, "The formatter cannot be null"); | ||
Preconditions.checkArgument(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); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.