Dynamically grab flags

https://github.com/BentoBoxWorld/BentoBox/issues/1915
This commit is contained in:
tastybento 2022-01-09 19:44:44 -08:00
parent b63df41082
commit 721f0271aa
3 changed files with 397 additions and 53 deletions

View File

@ -35,33 +35,36 @@ import world.bentobox.bentobox.util.Util;
public class AdminSettingsCommand extends CompositeCommand {
private static final String SPAWN_ISLAND = "spawn-island";
private final List<String> PROTECTION_FLAG_NAMES;
private List<String> protectionFlagNames;
private Island island;
private final List<String> SETTING_FLAG_NAMES;
private final List<String> WORLD_SETTING_FLAG_NAMES;
private List<String> settingFlagNames;
private List<String> worldSettingFlagNames;
private @NonNull Optional<Flag> flag = Optional.empty();
private boolean activeState;
private int rank;
private final GameModeAddon gameMode;
public AdminSettingsCommand(CompositeCommand islandCommand) {
super(islandCommand, "settings", "flags", "options");
// make constants
GameModeAddon gameMode = getPlugin().getIWM().getAddon(getWorld()).orElse(null);
PROTECTION_FLAG_NAMES = getPlugin().getFlagsManager().getFlags().stream()
gameMode = getPlugin().getIWM().getAddon(getWorld()).orElse(null);
}
private void makeLists() {
protectionFlagNames = getPlugin().getFlagsManager().getFlags().stream()
.filter(f -> f.getType().equals(Type.PROTECTION))
.filter(f -> f.getGameModes().isEmpty() || gameMode == null || f.getGameModes().contains(gameMode))
.map(Flag::getID)
.collect(Collectors.toList());
SETTING_FLAG_NAMES = getPlugin().getFlagsManager().getFlags().stream()
.toList();
settingFlagNames = getPlugin().getFlagsManager().getFlags().stream()
.filter(f -> f.getType().equals(Type.SETTING))
.filter(f -> f.getGameModes().isEmpty() || gameMode == null || f.getGameModes().contains(gameMode))
.map(Flag::getID)
.collect(Collectors.toList());
WORLD_SETTING_FLAG_NAMES = getPlugin().getFlagsManager().getFlags().stream()
.toList();
worldSettingFlagNames = getPlugin().getFlagsManager().getFlags().stream()
.filter(f -> f.getType().equals(Type.WORLD_SETTING))
.filter(f -> f.getGameModes().isEmpty() || gameMode == null || f.getGameModes().contains(gameMode))
.map(Flag::getID)
.collect(Collectors.toList());
.toList();
}
@Override
@ -112,10 +115,12 @@ public class AdminSettingsCommand extends CompositeCommand {
* @return true if the syntax is correct
*/
private boolean checkSyntax(User user, List<String> args) {
// Update the flag lists
this.makeLists();
if (args.size() == 2) {
// Should be a world setting
// If world settings, then active/disabled, otherwise player flags
if (WORLD_SETTING_FLAG_NAMES.contains(args.get(0).toUpperCase(Locale.ENGLISH))) {
if (worldSettingFlagNames.contains(args.get(0).toUpperCase(Locale.ENGLISH))) {
if (checkActiveDisabled(user, args.get(1))) {
flag = getPlugin().getFlagsManager().getFlag(args.get(0).toUpperCase(Locale.ENGLISH));
return true;
@ -130,8 +135,8 @@ public class AdminSettingsCommand extends CompositeCommand {
return false;
}
if (!SETTING_FLAG_NAMES.contains(args.get(1).toUpperCase(Locale.ENGLISH))
&& !PROTECTION_FLAG_NAMES.contains(args.get(1).toUpperCase(Locale.ENGLISH))) {
if (!settingFlagNames.contains(args.get(1).toUpperCase(Locale.ENGLISH))
&& !protectionFlagNames.contains(args.get(1).toUpperCase(Locale.ENGLISH))) {
user.sendMessage("commands.admin.settings.unknown-flag", TextVariables.NAME, args.get(2));
return false;
}
@ -236,25 +241,28 @@ public class AdminSettingsCommand extends CompositeCommand {
@Override
public Optional<List<String>> tabComplete(User user, String alias, List<String> args) {
// Update with the latest lists
this.makeLists();
String active = ChatColor.stripColor(user.getTranslation("protection.panel.flag-item.setting-active"));
String disabled = ChatColor.stripColor(user.getTranslation("protection.panel.flag-item.setting-disabled"));
List<String> options = new ArrayList<>();
String lastArg = !args.isEmpty() ? args.get(args.size()-1) : "";
System.out.println("Arg size = " + args.size());
if (args.size() == 2) {
// Player names or world settings
options = Util.tabLimit(Util.getOnlinePlayerList(user), lastArg);
options.addAll(WORLD_SETTING_FLAG_NAMES);
options.addAll(worldSettingFlagNames);
if (getIslands().getSpawn(getWorld()).isPresent()) {
options.add(SPAWN_ISLAND);
}
} else if (args.size() == 3) {
// If world settings, then active/disabled, otherwise player flags
if (WORLD_SETTING_FLAG_NAMES.contains(args.get(1).toUpperCase(Locale.ENGLISH))) {
if (worldSettingFlagNames.contains(args.get(1).toUpperCase(Locale.ENGLISH))) {
options = Arrays.asList(active, disabled);
} else {
// Flag IDs
options.addAll(PROTECTION_FLAG_NAMES);
options.addAll(SETTING_FLAG_NAMES);
options.addAll(protectionFlagNames);
options.addAll(settingFlagNames);
}
} else if (args.size() == 4) {
// Get flag in previous argument

View File

@ -78,7 +78,7 @@ public class SettingsTab implements Tab, ClickHandler {
List<Flag> flags = plugin.getFlagsManager().getFlags().stream().filter(f -> f.getType().equals(type))
// We're stripping colors to avoid weird sorting issues
.sorted(Comparator.comparing(flag -> ChatColor.stripColor(user.getTranslation(flag.getNameReference()))))
.collect(Collectors.toList());
.toList();
// Remove any that are not for this game mode
plugin.getIWM().getAddon(world).ifPresent(gm -> flags.removeIf(f -> !f.getGameModes().isEmpty() && !f.getGameModes().contains(gm)));
// Remove any that are the wrong rank or that will be on the top row

View File

@ -0,0 +1,336 @@
package world.bentobox.bentobox.api.commands.admin;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
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.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.World.Environment;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.InventoryType;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemFactory;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.plugin.PluginManager;
import org.bukkit.scheduler.BukkitScheduler;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.stubbing.Answer;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.reflect.Whitebox;
import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.Settings;
import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.flags.Flag.Mode;
import world.bentobox.bentobox.api.localization.TextVariables;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.managers.CommandsManager;
import world.bentobox.bentobox.managers.FlagsManager;
import world.bentobox.bentobox.managers.IslandWorldManager;
import world.bentobox.bentobox.managers.IslandsManager;
import world.bentobox.bentobox.managers.LocalesManager;
import world.bentobox.bentobox.managers.PlayersManager;
import world.bentobox.bentobox.managers.RanksManager;
import world.bentobox.bentobox.util.Util;
/**
* @author tastybento
*
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest({Bukkit.class, BentoBox.class, User.class, Util.class})
public class AdminSettingsCommandTest {
private AdminSettingsCommand asc;
@Mock
private CompositeCommand ac;
@Mock
private User user;
@Mock
private IslandsManager im;
@Mock
private PlayersManager pm;
private UUID notUUID;
@Mock
private Player p;
@Mock
private IslandWorldManager iwm;
@Mock
private Island island;
@Mock
private Location spawnPoint;
@Mock
private World world;
@Mock
private World netherWorld;
@Mock
private World endWorld;
@Mock
private PluginManager pluginManager;
/**
* @throws java.lang.Exception
*/
@Before
public void setUp() throws Exception {
// Set up plugin
BentoBox plugin = mock(BentoBox.class);
Whitebox.setInternalState(BentoBox.class, "instance", plugin);
Util.setPlugin(plugin);
// Command manager
CommandsManager cm = mock(CommandsManager.class);
when(plugin.getCommandsManager()).thenReturn(cm);
// Player
when(user.isOp()).thenReturn(false);
UUID uuid = UUID.randomUUID();
notUUID = UUID.randomUUID();
while(notUUID.equals(uuid)) {
notUUID = UUID.randomUUID();
}
when(user.getUniqueId()).thenReturn(uuid);
when(user.getPlayer()).thenReturn(p);
when(user.getName()).thenReturn("tastybento");
when(user.isPlayer()).thenReturn(true);
User.setPlugin(plugin);
// Parent command has no aliases
when(ac.getSubCommandAliases()).thenReturn(new HashMap<>());
when(ac.getTopLabel()).thenReturn("bskyblock");
when(ac.getWorld()).thenReturn(world);
// World
when(world.getEnvironment()).thenReturn(Environment.NORMAL);
when(netherWorld.getEnvironment()).thenReturn(Environment.NETHER);
when(endWorld.getEnvironment()).thenReturn(Environment.THE_END);
// Server & Scheduler
BukkitScheduler sch = mock(BukkitScheduler.class);
PowerMockito.mockStatic(Bukkit.class);
when(Bukkit.getScheduler()).thenReturn(sch);
// Locales
LocalesManager lm = mock(LocalesManager.class);
when(lm.get(any(), any())).thenReturn("mock translation");
when(plugin.getLocalesManager()).thenReturn(lm);
when(user.getTranslation(anyString())).thenAnswer((Answer<String>) invocation -> invocation.getArgument(0, String.class));
when(user.getTranslation(anyString(), anyString(), anyString())).thenAnswer((Answer<String>) invocation -> invocation.getArgument(0, String.class));
when(user.getTranslation(anyString(), anyString(), anyString(), anyString(), anyString())).thenAnswer((Answer<String>) invocation -> invocation.getArgument(0, String.class));
// IWM
when(plugin.getIWM()).thenReturn(iwm);
// Players manager
when(plugin.getPlayers()).thenReturn(pm);
when(pm.getFlagsDisplayMode(any())).thenReturn(Mode.BASIC);
//Island Manager
when(plugin.getIslands()).thenReturn(im);
// Island - player has island
when(im.getIsland(any(), any(UUID.class))).thenReturn(island);
when(im.hasIsland(any(), any(UUID.class))).thenReturn(true);
// Util
PowerMockito.mockStatic(Util.class);
when(Util.getUUID(anyString())).thenReturn(uuid);
when(Util.tabLimit(any(), any())).thenCallRealMethod();
// Settings
Settings settings = new Settings();
when(plugin.getSettings()).thenReturn(settings);
// Bukkit
PowerMockito.mockStatic(Bukkit.class);
// Mock item factory (for itemstacks)
ItemFactory itemFactory = mock(ItemFactory.class);
ItemMeta bannerMeta = mock(ItemMeta.class);
when(itemFactory.getItemMeta(any())).thenReturn(bannerMeta);
when(Bukkit.getItemFactory()).thenReturn(itemFactory);
Inventory inventory = mock(Inventory.class);
when(Bukkit.createInventory(eq(null), Mockito.anyInt(), any())).thenReturn(inventory);
when(Bukkit.createInventory(eq(null), any(InventoryType.class), any())).thenReturn(inventory);
// Flags manager
when(Bukkit.getPluginManager()).thenReturn(pluginManager);
FlagsManager fm = new FlagsManager(plugin);
when(plugin.getFlagsManager()).thenReturn(fm);
// RnksManager
when(plugin.getRanksManager()).thenReturn(new RanksManager());
asc = new AdminSettingsCommand(ac);
}
/**
* @throws java.lang.Exception
*/
@After
public void tearDown() throws Exception {
User.clearUsers();
Mockito.framework().clearInlineMocks();
}
/**
* Test method for {@link world.bentobox.bentobox.api.commands.admin.AdminSettingsCommand#setup()}.
*/
@Test
public void testSetup() {
assertFalse(asc.isOnlyPlayer());
assertEquals("admin.settings", asc.getPermission());
assertEquals("commands.admin.settings.parameters", asc.getParameters());
assertEquals("commands.admin.settings.description", asc.getDescription());
}
/**
* Test method for {@link world.bentobox.bentobox.api.commands.admin.AdminSettingsCommand#canExecute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
public void testCanExecuteEmpty() {
assertTrue(asc.canExecute(user, "", Collections.emptyList()));
}
/**
* Test method for {@link world.bentobox.bentobox.api.commands.admin.AdminSettingsCommand#canExecute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
public void testCanExecuteOneArgUnknownPlayer() {
when(Util.getUUID(anyString())).thenReturn(null);
assertFalse(asc.canExecute(user, "", Collections.singletonList("tastybento")));
verify(user).sendMessage("general.errors.unknown-player", TextVariables.NAME, "tastybento");
}
/**
* Test method for {@link world.bentobox.bentobox.api.commands.admin.AdminSettingsCommand#canExecute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
public void testCanExecuteOneArgKnownPlayerNoIsland() {
when(im.getIsland(any(), any(UUID.class))).thenReturn(null);
assertFalse(asc.canExecute(user, "", Collections.singletonList("tastybento")));
verify(user).sendMessage("general.errors.player-has-no-island");
}
/**
* Test method for {@link world.bentobox.bentobox.api.commands.admin.AdminSettingsCommand#canExecute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
public void testCanExecuteOneArgKnownPlayerIslandNotOwner() {
when(im.hasIsland(any(), any(UUID.class))).thenReturn(false);
assertFalse(asc.canExecute(user, "", Collections.singletonList("tastybento")));
verify(user).sendMessage("general.errors.player-has-no-island");
}
/**
* Test method for {@link world.bentobox.bentobox.api.commands.admin.AdminSettingsCommand#canExecute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
public void testCanExecuteOneArgKnownPlayer() {
assertTrue(asc.canExecute(user, "", Collections.singletonList("tastybento")));
verify(user, never()).sendMessage(anyString());
}
/**
* Test method for {@link world.bentobox.bentobox.api.commands.admin.AdminSettingsCommand#canExecute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
public void testCanExecuteOneArgSpawnNoSpawn() {
when(Util.getUUID(anyString())).thenReturn(null);
assertFalse(asc.canExecute(user, "", Collections.singletonList("spawn-island")));
verify(user).sendMessage("general.errors.unknown-player", TextVariables.NAME, "spawn-island");
}
/**
* Test method for {@link world.bentobox.bentobox.api.commands.admin.AdminSettingsCommand#canExecute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
public void testCanExecuteOneArgSpawnSpawn() {
when(im.getSpawn(any())).thenReturn(Optional.of(island));
assertTrue(asc.canExecute(user, "", Collections.singletonList("spawn-island")));
verify(user, never()).sendMessage(anyString());
}
/**
* Test method for {@link world.bentobox.bentobox.api.commands.admin.AdminSettingsCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
public void testExecuteUserStringListOfStringNoArgsConsole() {
when(user.isPlayer()).thenReturn(false);
assertFalse(asc.execute(user, "", Collections.emptyList()));
verify(user).sendMessage("general.errors.use-in-game");
}
/**
* Test method for {@link world.bentobox.bentobox.api.commands.admin.AdminSettingsCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
public void testExecuteUserStringListOfStringNoArgs() {
assertTrue(asc.execute(user, "", Collections.emptyList()));
verify(pm).setFlagsDisplayMode(user.getUniqueId(), Mode.EXPERT);
// Open panel
}
/**
* Test method for {@link world.bentobox.bentobox.api.commands.admin.AdminSettingsCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
public void testExecuteUserStringListOfStringArgs() {
assertTrue(asc.execute(user, "", Arrays.asList("blah","blah")));
verify(user).sendMessage("general.success");
}
/**
* Test method for {@link world.bentobox.bentobox.api.commands.admin.AdminSettingsCommand#tabComplete(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
public void testTabCompleteUserStringListOfStringTwoArgs() {
Optional<List<String>> r = asc.tabComplete(user, "", Arrays.asList("b","WORLD_TNT"));
assertFalse(r.isEmpty());
assertEquals("WORLD_TNT_DAMAGE", r.get().get(0));
}
/**
* Test method for {@link world.bentobox.bentobox.api.commands.admin.AdminSettingsCommand#tabComplete(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
public void testTabCompleteUserStringListOfStringThreeArgs() {
Optional<List<String>> r = asc.tabComplete(user, "", Arrays.asList("b","WORLD_TNT", "BEACO"));
assertFalse(r.isEmpty());
assertEquals("BEACON", r.get().get(0));
}
/**
* Test method for {@link world.bentobox.bentobox.api.commands.admin.AdminSettingsCommand#tabComplete(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
public void testTabCompleteUserStringListOfStringFourArgs() {
Optional<List<String>> r = asc.tabComplete(user, "", Arrays.asList("b","b", "PVP_OVERWORLD", "t"));
assertFalse(r.isEmpty());
// TODO - finish this.
}
}