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
42 changes: 14 additions & 28 deletions src/main/java/world/bentobox/aoneblock/listeners/CheckPhase.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import org.eclipse.jdt.annotation.Nullable;

import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import net.kyori.adventure.title.Title;

import world.bentobox.aoneblock.AOneBlock;
Expand Down Expand Up @@ -48,20 +47,6 @@

}

/**
* Converts a BentoBox translation key into an Adventure {@link Component}.
* BentoBox's {@code getTranslation()} returns strings with § color codes already
* applied, so {@link LegacyComponentSerializer#legacySection()} is used here.
*
* @param user - the user whose locale is used
* @param key - BentoBox translation key
* @param vars - alternating placeholder key/value pairs
* @return the translated, colored Component
*/
private Component translate(User user, String key, String... vars) {
return LegacyComponentSerializer.legacySection().deserialize(user.getTranslation(key, vars));
}

/**
* Runs end phase commands, sets new phase and runs new phase commands
*
Expand Down Expand Up @@ -100,9 +85,7 @@
// Set the phase name
is.setPhaseName(newPhaseName);
if (user.isPlayer() && user.isOnline() && addon.inWorld(user.getWorld())) {
// Phase names are raw YAML strings — use legacyAmpersand to parse & color codes.
Component titleComponent = LegacyComponentSerializer.legacyAmpersand().deserialize(newPhaseName);
user.getPlayer().showTitle(Title.title(titleComponent, Component.empty()));
user.getPlayer().showTitle(Title.title(Component.text(newPhaseName), Component.empty()));

Check warning on line 88 in src/main/java/world/bentobox/aoneblock/listeners/CheckPhase.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Parameter 1 to this call is marked "@NotNull" but null could be passed.

See more on https://sonarcloud.io/project/issues?id=BentoBoxWorld_AOneBlock&issues=AZ1lp3H60EZQpDQZdCrw&open=AZ1lp3H60EZQpDQZdCrw&pullRequest=485
}
// Run phase start commands
Util.runCommands(user,
Expand Down Expand Up @@ -147,52 +130,55 @@
}

private boolean checkLevelRequirement(Requirement r, User user, Island i, World world) {
// Level checking logic
return addon.getAddonByName("Level").map(l -> {
if (((Level) l).getIslandLevel(world, i.getOwner()) < r.getLevel()) {
user.getPlayer().sendMessage(translate(user, "aoneblock.phase.insufficient-level",
TextVariables.NUMBER, String.valueOf(r.getLevel())));
user.sendMessage("aoneblock.phase.insufficient-level", TextVariables.NUMBER,
String.valueOf(r.getLevel()));
return true;
}
return false;
}).orElse(false);
}

private boolean checkBankRequirement(Requirement r, User user, Island i) {
// Bank checking logic
return addon.getAddonByName("Bank").map(l -> {
if (((Bank) l).getBankManager().getBalance(i).getValue() < r.getBank()) {
user.getPlayer().sendMessage(translate(user, "aoneblock.phase.insufficient-bank-balance",
TextVariables.NUMBER, String.valueOf(r.getBank())));
user.sendMessage("aoneblock.phase.insufficient-bank-balance", TextVariables.NUMBER,
String.valueOf(r.getBank()));
return true;
}
return false;
}).orElse(false);
}

private boolean checkEcoRequirement(Requirement r, User user, World world) {
// Eco checking logic
return addon.getPlugin().getVault().map(vaultHook -> {
if (vaultHook.getBalance(user, world) < r.getEco()) {
user.getPlayer().sendMessage(translate(user, "aoneblock.phase.insufficient-funds",
TextVariables.NUMBER, vaultHook.format(r.getEco())));
user.sendMessage("aoneblock.phase.insufficient-funds", TextVariables.NUMBER,
vaultHook.format(r.getEco()));
return true;
}
return false;
}).orElse(false);
}

private boolean checkPermissionRequirement(Requirement r, User user) {
// Permission checking logic
if (user != null && !user.hasPermission(r.getPermission())) {
user.getPlayer().sendMessage(translate(user, "aoneblock.phase.insufficient-permission",
TextVariables.NAME, r.getPermission()));
user.sendMessage("aoneblock.phase.insufficient-permission", TextVariables.NAME, r.getPermission());
return true;
}
return false;
}

private boolean checkCooldownRequirement(Requirement r, User player, OneBlockIslands is) {
// Cooldown checking logic
long remainingTime = r.getCooldown() - (System.currentTimeMillis() - is.getLastPhaseChangeTime()) / 1000;
if (remainingTime > 0) {
player.getPlayer().sendMessage(translate(player, "aoneblock.phase.cooldown",
TextVariables.NUMBER, String.valueOf(remainingTime)));
player.sendMessage("aoneblock.phase.cooldown", TextVariables.NUMBER, String.valueOf(remainingTime));
return true;
}
return false;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,22 @@
package world.bentobox.aoneblock.listeners;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.mockito.Mockito.atLeast;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.util.List;
import java.util.UUID;

import org.bukkit.entity.Player;
import org.bukkit.entity.Player.Spigot;
import org.eclipse.jdt.annotation.NonNull;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;

import net.md_5.bungee.api.chat.TextComponent;
import world.bentobox.aoneblock.AOneBlock;
import world.bentobox.aoneblock.CommonTestSetup;
import world.bentobox.aoneblock.dataobjects.OneBlockIslands;
Expand All @@ -41,8 +38,6 @@ public class InfoListenerTest extends CommonTestSetup {
private InfoListener il;
@Mock
private @NonNull OneBlockIslands is;
@Mock
private Spigot spigot;

private static final UUID ID = UUID.randomUUID();

Expand Down Expand Up @@ -107,30 +102,20 @@ void testInfoListener() {
* Test method for {@link world.bentobox.aoneblock.listeners.InfoListener#onInfo(world.bentobox.bentobox.api.events.island.IslandInfoEvent)}.
*/
@Test
void testOnInfoOtherAddon() {
IslandInfoEvent e = new IslandInfoEvent(island, ID, false, location, mock(Addon.class));
void testOnInfo() {
IslandInfoEvent e = new IslandInfoEvent(island, ID, false, location, addon);
il.onInfo(e);
checkSpigotMessage("aoneblock.commands.info.count", 0);
verify(lm).get(any(User.class), eq("aoneblock.commands.info.count"));
}

public void checkSpigotMessage(String expectedMessage, int expectedOccurrences) {
// Capture the argument passed to spigot().sendMessage(...) if messages are sent
ArgumentCaptor<TextComponent> captor = ArgumentCaptor.forClass(TextComponent.class);

// Verify that sendMessage() was called at least 0 times (capture any sent messages)
verify(spigot, atLeast(0)).sendMessage(captor.capture());

// Get all captured TextComponents
List<TextComponent> capturedMessages = captor.getAllValues();

// Count the number of occurrences of the expectedMessage in the captured messages
long actualOccurrences = capturedMessages.stream().map(component -> component.toLegacyText()) // Convert each TextComponent to plain text
.filter(messageText -> messageText.contains(expectedMessage)) // Check if the message contains the expected text
.count(); // Count how many times the expected message appears

// Assert that the number of occurrences matches the expectedOccurrences
assertEquals(expectedOccurrences,
actualOccurrences, "Expected message occurrence mismatch: " + expectedMessage);
/**
* Test method for {@link world.bentobox.aoneblock.listeners.InfoListener#onInfo(world.bentobox.bentobox.api.events.island.IslandInfoEvent)}.
*/
@Test
void testOnInfoOtherAddon() {
IslandInfoEvent e = new IslandInfoEvent(island, ID, false, location, mock(Addon.class));
il.onInfo(e);
verify(lm, never()).get(any(User.class), eq("aoneblock.commands.info.count"));
}

}
Loading