Skip to content

Commit

Permalink
chore: move pattern to util
Browse files Browse the repository at this point in the history
  • Loading branch information
ybw0014 committed Oct 16, 2024
1 parent 92645fc commit d034570
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import java.util.Locale;
import java.util.Optional;
import java.util.logging.Level;
import java.util.regex.Pattern;

import javax.annotation.Nonnull;
import javax.annotation.ParametersAreNonnullByDefault;
Expand Down Expand Up @@ -49,6 +48,7 @@
import io.github.thebusybiscuit.slimefun4.implementation.tasks.AsyncRecipeChoiceTask;
import io.github.thebusybiscuit.slimefun4.utils.ChatUtils;
import io.github.thebusybiscuit.slimefun4.utils.ChestMenuUtils;
import io.github.thebusybiscuit.slimefun4.utils.WikiUtils;
import io.github.thebusybiscuit.slimefun4.utils.compatibility.VersionedItemFlag;
import io.github.thebusybiscuit.slimefun4.utils.itemstack.SlimefunGuideItem;

Expand All @@ -69,7 +69,6 @@
public class SurvivalSlimefunGuide implements SlimefunGuideImplementation {

private static final int MAX_ITEM_GROUPS = 36;
private static final Pattern SLIMEFUN_WIKI_PATTERN = Pattern.compile("^" + Pattern.quote(Slimefun.instance().getWikiUrlTemplate()).replace("%item%", "(.+)") + "$");

private final int[] recipeSlots = { 3, 4, 5, 12, 13, 14, 21, 22, 23 };
private final ItemStack item;
Expand Down Expand Up @@ -518,7 +517,7 @@ public void displayItem(PlayerProfile profile, SlimefunItem item, boolean addToH

if (wiki.isPresent()) {
String message = Slimefun.getLocalization().getMessage(p, "guide.tooltips.wiki.third-party");
if (SLIMEFUN_WIKI_PATTERN.matcher(wiki.get()).matches()) {
if (WikiUtils.isSlimefunOfficialWiki(wiki.get())) {
message = Slimefun.getLocalization().getMessage(p, "guide.tooltips.wiki.slimefun");
}

Expand All @@ -532,7 +531,7 @@ public void displayItem(PlayerProfile profile, SlimefunItem item, boolean addToH
));
menu.addMenuClickHandler(8, (pl, slot, itemstack, action) -> {
pl.closeInventory();
if (!(item.getAddon() instanceof Slimefun)) {
if (!WikiUtils.isSlimefunOfficialWiki(wiki.get())) {
Slimefun.getLocalization().sendMessage(pl, "messages.wiki-third-party",
msg -> msg.replace("%addon%", item.getAddon().getName()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.util.Map;
import java.util.function.UnaryOperator;
import java.util.logging.Level;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import javax.annotation.Nonnull;
Expand All @@ -15,6 +16,7 @@

import io.github.thebusybiscuit.slimefun4.api.SlimefunAddon;
import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItem;
import io.github.thebusybiscuit.slimefun4.implementation.Slimefun;

import com.google.common.base.Preconditions;
import com.google.gson.JsonElement;
Expand All @@ -26,6 +28,9 @@
* @author ybw0014
*/
public class WikiUtils {

private static final Pattern SLIMEFUN_WIKI_PATTERN = buildPattern(Slimefun.instance().getWikiUrlTemplate());

private WikiUtils() {}

/**
Expand Down Expand Up @@ -74,4 +79,32 @@ public static void setupWiki(@Nonnull Plugin plugin, @Nonnull UnaryOperator<Stri
plugin.getLogger().log(Level.SEVERE, MessageFormat.format("Failed to load wiki.json from {0}", plugin.getName()), e);
}
}

/**
* Checks if the given URL is a Slimefun official wiki URL.
*
* @param url
* The URL to check
*
* @return
* Whether the URL is a Slimefun official wiki URL
*/
public static boolean isSlimefunOfficialWiki(@Nonnull String url) {
Preconditions.checkArgument(url != null, "The URL cannot be null");

return SLIMEFUN_WIKI_PATTERN.matcher(url).matches();
}

@Nonnull
private static Pattern buildPattern(@Nonnull String template) {
Preconditions.checkArgument(template != null, "The template cannot be null");

String regexTemplate = template.replace(".", "\\.")
.replace("/", "\\/")
.replace("%item%", ".+");

String regex = "^" + regexTemplate + "$";

return Pattern.compile(regex);
}
}

0 comments on commit d034570

Please sign in to comment.