Skip to content

Commit

Permalink
Refactor WikiLookup error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinthegreat1 committed May 4, 2024
1 parent 40d395f commit f7b1389
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ protected HandledScreenMixin(Text title) {
public void skyblocker$keyPressed(int keyCode, int scanCode, int modifiers, CallbackInfoReturnable<Boolean> cir) {
if (this.client != null && this.focusedSlot != null && keyCode != 256) {
//wiki lookup
if (!this.client.options.inventoryKey.matchesKey(keyCode, scanCode) && WikiLookup.wikiLookup.matchesKey(keyCode, scanCode)) {
if (!this.client.options.inventoryKey.matchesKey(keyCode, scanCode) && WikiLookup.wikiLookup.matchesKey(keyCode, scanCode) && client.player != null) {
WikiLookup.openWiki(this.focusedSlot, client.player);
}
//item protection
Expand Down
20 changes: 9 additions & 11 deletions src/main/java/de/hysky/skyblocker/skyblock/item/WikiLookup.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
import net.minecraft.client.util.InputUtil;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.screen.slot.Slot;
import net.minecraft.text.Text;
import net.minecraft.util.Util;
import org.jetbrains.annotations.NotNull;
import org.lwjgl.glfw.GLFW;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -30,19 +32,15 @@ public static void init() {
));
}

public static void openWiki(Slot slot, PlayerEntity player) {
public static void openWiki(@NotNull Slot slot, @NotNull PlayerEntity player) {
if (!Utils.isOnSkyblock() || !SkyblockerConfigManager.get().general.wikiLookup.enableWikiLookup) return;

ItemUtils.getItemIdOptional(slot.getStack()).map(id -> ItemRepository.getWikiLink(id, player)).ifPresent(wikiLink -> {
try {
CompletableFuture.runAsync(() -> Util.getOperatingSystem().open(wikiLink));
} catch (IndexOutOfBoundsException | IllegalStateException e) {
LOGGER.error("[Skyblocker] Error while retrieving wiki article...", e);
if (player != null) {
ItemUtils.getItemIdOptional(slot.getStack())
.map(ItemRepository::getWikiLink)
.ifPresentOrElse(wikiLink -> CompletableFuture.runAsync(() -> Util.getOperatingSystem().open(wikiLink)).exceptionally(e -> {
LOGGER.error("[Skyblocker] Error while retrieving wiki article...", e);
player.sendMessage(Constants.PREFIX.get().append("Error while retrieving wiki article, see logs..."), false);
}
}
});
return null;
}), () -> player.sendMessage(Constants.PREFIX.get().append(Text.translatable("skyblocker.wikiLookup.noArticleFound")), false));
}

}
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
package de.hysky.skyblocker.skyblock.itemlist;

import de.hysky.skyblocker.config.SkyblockerConfigManager;
import de.hysky.skyblocker.utils.Constants;
import de.hysky.skyblocker.utils.ItemUtils;
import de.hysky.skyblocker.utils.NEURepoManager;
import io.github.moulberry.repo.data.NEUCraftingRecipe;
import io.github.moulberry.repo.data.NEUItem;
import io.github.moulberry.repo.data.NEURecipe;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.text.Text;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -67,10 +64,9 @@ private static void loadRecipes(NEUItem item) {
}
}

public static String getWikiLink(String internalName, PlayerEntity player) {
public static String getWikiLink(String internalName) {
NEUItem item = NEURepoManager.NEU_REPO.getItems().getItemBySkyblockId(internalName);
if (item == null || item.getInfo() == null || item.getInfo().isEmpty()) {
warnNoWikiLink(player);
return null;
}

Expand All @@ -83,16 +79,9 @@ public static String getWikiLink(String internalName, PlayerEntity player) {
} else if (wikiLink1.startsWith(wikiDomain)) {
return wikiLink1;
}
warnNoWikiLink(player);
return null;
}

private static void warnNoWikiLink(PlayerEntity player) {
if (player != null) {
player.sendMessage(Constants.PREFIX.get().append(Text.translatable("skyblocker.wikiLookup.noArticleFound")), false);
}
}

public static List<SkyblockCraftingRecipe> getRecipes(String internalName) {
List<SkyblockCraftingRecipe> result = new ArrayList<>();
for (SkyblockCraftingRecipe recipe : recipes) {
Expand Down

0 comments on commit f7b1389

Please sign in to comment.