Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
<!-- Non-minecraft related dependencies -->
<junit.version>5.10.2</junit.version>
<mockito.version>5.11.0</mockito.version>
<mock-bukkit.version>v1.21-SNAPSHOT</mock-bukkit.version>
<mock-bukkit.version>4.110.0</mock-bukkit.version>
<!-- More visible way how to change dependency versions -->
<paper.version>1.21.11-R0.1-SNAPSHOT</paper.version>
<bentobox.version>3.14.0</bentobox.version>
Expand All @@ -53,7 +53,7 @@
<!-- Revision variable removes warning about dynamic version -->
<revision>${build.version}-SNAPSHOT</revision>
<!-- This allows to change between versions and snapshots. -->
<build.version>1.6.1</build.version>
<build.version>1.6.2</build.version>
<build.number>-LOCAL</build.number>
<!-- Sonar Cloud -->
<sonar.projectKey>BentoBoxWorld_Challenges</sonar.projectKey>
Expand Down Expand Up @@ -205,8 +205,8 @@
</dependency>
<!-- MockBukkit -->
<dependency>
<groupId>com.github.MockBukkit</groupId>
<artifactId>MockBukkit</artifactId>
<groupId>org.mockbukkit.mockbukkit</groupId>
<artifactId>mockbukkit-v1.21</artifactId>
<version>${mock-bukkit.version}</version>
<scope>test</scope>
</dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -736,7 +736,7 @@ public void importDatabaseFile(User user, World world, String fileName)
}
catch (Exception e)
{
this.addon.getPlugin().logStacktrace(e);
reportImportFailure(user, world, e);
return;
}

Expand All @@ -745,6 +745,21 @@ public void importDatabaseFile(User user, World world, String fileName)
}


/**
* Logs the stacktrace and, if a player triggered the import, surfaces a chat error
* so they know to look at the server console.
*/
private void reportImportFailure(User user, World world, Exception e)
{
this.addon.getPlugin().logStacktrace(e);
if (user != null && user.isPlayer())
{
Utils.sendMessage(user, world, Constants.ERRORS + "import-failed",
"[message]", e.getMessage() == null ? e.getClass().getSimpleName() : e.getMessage());
}
}


/**
* This method loads downloaded challenges into memory.
* @param user User who calls downloaded challenge loading
Expand Down Expand Up @@ -813,7 +828,7 @@ public void loadDownloadedChallenges(User user, World world, String downloadStri
}
catch (Exception e)
{
this.addon.getPlugin().logStacktrace(e);
reportImportFailure(user, world, e);
return;
}

Expand Down
1 change: 1 addition & 0 deletions src/main/resources/locales/en-US.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1232,6 +1232,7 @@ challenges:
island-level: "<red>Your island must be at least level [number] to complete this challenge!"
no-load: "<red>Error: Could not load [file]. Error: [message]."
load-error: "<red>Error: Cannot load [value]."
import-failed: "<red>Could not import challenges: [message]. Check the server console for details."
no-rank: "<red>You do not have a high enough rank to do that."
cannot-remove-items: "<red>Some items cannot be removed from your inventory!"
exist-challenges-or-levels: "<red>Challenges already exist in your world. Cannot proceed!"
Expand Down
12 changes: 12 additions & 0 deletions src/test/java/world/bentobox/challenges/ChallengesAddonTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,15 @@
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockbukkit.mockbukkit.MockBukkit;
import org.mockito.Mock;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.mockito.stubbing.Answer;

import world.bentobox.challenges.panel.PanelTestHelper;

import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.Settings;
import world.bentobox.bentobox.api.addons.Addon;
Expand Down Expand Up @@ -101,6 +104,15 @@ public class ChallengesAddonTest {
@BeforeEach
public void setUp() throws Exception {
closeable = MockitoAnnotations.openMocks(this);
// Force Bukkit's Tag.<clinit> to run against a real MockBukkit ServerMock before
// we install the Mockito static mock below. Without this, Tag.<clinit> can later
// fire while Bukkit is statically mocked, permanently null-ing every Tag constant
// for the JVM and corrupting any subsequent test that creates an ItemStack
// (e.g. CommonPagedPanelTest -> ItemType.<clinit> -> MaterialTags.<clinit> ->
// Objects.requireNonNull(Tag.ALL_SIGNS) -> NPE).
MockBukkit.mock();
PanelTestHelper.primeBukkitRegistry();
MockBukkit.unmock();
// Set up plugin
WhiteBox.setInternalState(BentoBox.class, "instance", plugin);
when(plugin.getLogger()).thenReturn(Logger.getAnonymousLogger());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ public int getPageIndex() throws Exception {
void setUp() {
closeable = MockitoAnnotations.openMocks(this);
ServerMock mbServer = MockBukkit.mock();
PanelTestHelper.primeBukkitRegistry();

when(addon.getChallengesManager()).thenReturn(manager);
PanelTestHelper.setupUserTranslations(user);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ public PanelItem getReturnButton() {
void setUp() {
closeable = MockitoAnnotations.openMocks(this);
ServerMock mbServer = MockBukkit.mock();
PanelTestHelper.primeBukkitRegistry();

when(addon.getChallengesManager()).thenReturn(manager);
PanelTestHelper.setupUserTranslations(user);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class ConversationUtilsTest {
void setUp() {
closeable = MockitoAnnotations.openMocks(this);
ServerMock mbServer = MockBukkit.mock();
PanelTestHelper.primeBukkitRegistry();

when(user.getTranslation(anyString())).thenAnswer(
(Answer<String>) inv -> inv.getArgument(0, String.class));
Expand Down
17 changes: 17 additions & 0 deletions src/test/java/world/bentobox/challenges/panel/PanelTestHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,23 @@
*/
public class PanelTestHelper {

/**
* Force Bukkit's material registry to be populated while MockBukkit's mock
* server is active. Call this immediately after `MockBukkit.mock()`.
*
* <p>Why: `org.bukkit.inventory.ItemType.&lt;clinit&gt;` iterates over Material and
* looks each entry up in the registry. If it runs before MockBukkit has populated
* the registry (e.g., when a panel test that doesn't extend AbstractChallengesTest
* runs first on CI's filesystem ordering), it throws NoSuchElementException, and
* ItemStackMock stays in an errored state for the rest of the JVM — poisoning every
* subsequent test. Touching `Tag.LEAVES` forces the registry to populate before
* ItemType loads.
*/
public static void primeBukkitRegistry() {
@SuppressWarnings("unused")
var unused = org.bukkit.Tag.LEAVES;
}

/**
* Set up user translation mocks to return the key for any invocation.
* Uses a lenient default answer that returns the first String argument
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class AdminPanelTest {
void setUp() throws Exception {
closeable = MockitoAnnotations.openMocks(this);
ServerMock mbServer = MockBukkit.mock();
PanelTestHelper.primeBukkitRegistry();

when(addon.getChallengesManager()).thenReturn(manager);
PanelTestHelper.setupUserTranslations(user);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class ListChallengesPanelTest {
void setUp() {
closeable = MockitoAnnotations.openMocks(this);
ServerMock mbServer = MockBukkit.mock();
PanelTestHelper.primeBukkitRegistry();

when(addon.getChallengesManager()).thenReturn(manager);
PanelTestHelper.setupUserTranslations(user);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ class GameModePanelTest {
void setUp() {
closeable = MockitoAnnotations.openMocks(this);
ServerMock mbServer = MockBukkit.mock();
PanelTestHelper.primeBukkitRegistry();

when(addon.getChallengesManager()).thenReturn(manager);
PanelTestHelper.setupUserTranslations(user);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class MultiplePanelTest {
void setUp() {
closeable = MockitoAnnotations.openMocks(this);
ServerMock mbServer = MockBukkit.mock();
PanelTestHelper.primeBukkitRegistry();

PanelTestHelper.setupUserTranslations(user);
when(user.getWorld()).thenReturn(world);
Expand Down
Loading