Added bentobox addon reload command.

This commit is contained in:
tastybento 2019-05-08 12:18:33 -07:00
parent 9ba62e1711
commit 01022a2500
5 changed files with 219 additions and 4 deletions

View File

@ -1,9 +1,13 @@
package world.bentobox.bentobox.commands;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import world.bentobox.bentobox.api.addons.Addon;
import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.commands.ConfirmableCommand;
import world.bentobox.bentobox.api.localization.TextVariables;
import world.bentobox.bentobox.api.user.User;
/**
@ -24,9 +28,11 @@ public class BentoBoxReloadCommand extends ConfirmableCommand {
@Override
public void setup() {
setPermission("admin.reload");
setParametersHelp("commands.bentobox.reload.parameters");
setDescription("commands.bentobox.reload.description");
}
@Override
public boolean execute(User user, String label, List<String> args) {
if (args.isEmpty()) {
@ -44,14 +50,24 @@ public class BentoBoxReloadCommand extends ConfirmableCommand {
user.sendMessage("commands.bentobox.reload.locales-reloaded");
});
} else if (args.size() == 1) {
if (!getPlugin().getAddonsManager().getAddonByName(args.get(0)).isPresent()) {
user.sendRawMessage("Unknown addon");
Optional<Addon> addon = getPlugin().getAddonsManager().getAddonByName(args.get(0));
if (!addon.isPresent()) {
user.sendMessage("commands.bentobox.reload.unknown-addon");
return false;
}
this.askConfirmation(user, () -> getPlugin().getAddonsManager().getAddonByName(args.get(0)).ifPresent(getPlugin().getAddonsManager()::reloadAddon));
this.askConfirmation(user, () -> {
user.sendMessage("commands.bentobox.reload.addon", TextVariables.DESCRIPTION, args.get(0));
addon.ifPresent(getPlugin().getAddonsManager()::reloadAddon);
});
} else {
showHelp(this, user);
}
return true;
}
@Override
public Optional<List<String>> tabComplete(User user, String alias, List<String> args) {
return Optional.of(getPlugin().getAddonsManager().getAddons().stream().map(a -> a.getDescription().getName()).collect(Collectors.toList()));
}
}

View File

@ -227,6 +227,7 @@ public class AddonsManager {
Path p = addon.getFile().toPath();
disable(addon);
loadAddon(p.toFile());
enableAddon(addon);
}
/**

View File

@ -253,10 +253,13 @@ commands:
about:
description: "display copyright and license info"
reload:
description: "reloads settings, addons (if supported) and locales"
parameters: "[addon]"
description: "reloads all addons, settings and locales or reloads an individual addon"
locales-reloaded: "&2Languages reloaded."
addons-reloaded: "&2Addons reloaded."
settings-reloaded: "&2Settings reloaded."
addon: "&6Reloading [description]"
unknown-addon: "&2Unknown addon!"
version:
plugin-version: "&2BentoBox version: &3[version]"
description: "display BentoBox and addons versions"

View File

@ -26,6 +26,7 @@ import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
@ -44,6 +45,8 @@ public class AddonTest {
static BentoBox plugin;
static JavaPlugin javaPlugin;
private Server server;
@Mock
private AddonsManager am;
@Before
public void setUp() throws Exception {
@ -64,6 +67,10 @@ public class AddonTest {
plugin = mock(BentoBox.class);
Whitebox.setInternalState(BentoBox.class, "instance", plugin);
// Addons manager
when(plugin.getAddonsManager()).thenReturn(am);
// Mock item factory (for itemstacks)
ItemFactory itemFactory = mock(ItemFactory.class);
when(server.getItemFactory()).thenReturn(itemFactory);
@ -150,6 +157,7 @@ public class AddonTest {
TestListener listener = new TestListener();
TestClass test = new TestClass();
test.registerListener(listener);
Mockito.verify(am).registerListener(Mockito.any(), Mockito.eq(listener));
}
@Test

View File

@ -0,0 +1,187 @@
/**
*
*/
package world.bentobox.bentobox.commands;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
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 org.bukkit.Bukkit;
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.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.addons.Addon;
import world.bentobox.bentobox.api.addons.AddonDescription;
import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.managers.AddonsManager;
import world.bentobox.bentobox.managers.CommandsManager;
import world.bentobox.bentobox.managers.LocalesManager;
/**
* @author tastybento
*
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest({Bukkit.class, BentoBox.class, User.class })
public class BentoBoxReloadCommandTest {
@Mock
private BentoBox plugin;
@Mock
private CompositeCommand ac;
@Mock
private User user;
@Mock
private AddonsManager am;
@Mock
private LocalesManager lm;
private BentoBoxReloadCommand reload;
/**
* @throws java.lang.Exception
*/
@Before
public void setUp() throws Exception {
// Set up plugin
Whitebox.setInternalState(BentoBox.class, "instance", plugin);
// Command manager
CommandsManager cm = mock(CommandsManager.class);
when(plugin.getCommandsManager()).thenReturn(cm);
// Parent command has no aliases
when(ac.getSubCommandAliases()).thenReturn(new HashMap<>());
when(ac.getPermissionPrefix()).thenReturn("bentobox.");
// Addons manager
when(plugin.getAddonsManager()).thenReturn(am);
// Addons
Addon addon1 = mock(Addon.class);
AddonDescription desc = new AddonDescription.Builder("main", "BSkyBlock", "1.0.0").build();
when(addon1.getDescription()).thenReturn(desc);
Addon addon2 = mock(Addon.class);
AddonDescription desc2 = new AddonDescription.Builder("main", "AcidIsland", "1.0.0").build();
when(addon2.getDescription()).thenReturn(desc2);
// Linking
Optional<Addon> optionalAddon1 = Optional.of(addon1);
Optional<Addon> optionalAddon2 = Optional.of(addon2);
when(am.getAddonByName(Mockito.eq("bskyblock"))).thenReturn(optionalAddon1);
when(am.getAddonByName(Mockito.eq("acidisland"))).thenReturn(optionalAddon2);
when(am.getAddonByName(Mockito.eq("warps"))).thenReturn(Optional.empty());
when(am.getAddons()).thenReturn(Arrays.asList(addon1, addon2));
// Confirmable command settings
Settings settings = mock(Settings.class);
when(settings.getConfirmationTime()).thenReturn(10);
when(plugin.getSettings()).thenReturn(settings);
// Scheduler
BukkitScheduler sch = mock(BukkitScheduler.class);
PowerMockito.mockStatic(Bukkit.class);
when(Bukkit.getScheduler()).thenReturn(sch);
// Command
reload = new BentoBoxReloadCommand(ac);
}
/**
* @throws java.lang.Exception
*/
@After
public void tearDown() throws Exception {
}
/**
* Test method for {@link world.bentobox.bentobox.commands.BentoBoxReloadCommand#BentoBoxReloadCommand(world.bentobox.bentobox.api.commands.CompositeCommand)}.
*/
@Test
public void testBentoBoxReloadCommand() {
assertNotNull(reload);
}
/**
* Test method for {@link world.bentobox.bentobox.commands.BentoBoxReloadCommand#setup()}.
*/
@Test
public void testSetup() {
assertEquals("bentobox.admin.reload", reload.getPermission());
assertEquals("commands.bentobox.reload.description", reload.getDescription());
assertEquals("commands.bentobox.reload.parameters", reload.getParameters());
}
/**
* Test method for {@link world.bentobox.bentobox.commands.BentoBoxReloadCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
public void testExecuteUserStringListOfStringReloadAll() {
reload.execute(user, "", Collections.emptyList());
Mockito.verify(user).sendMessage("commands.confirmation.confirm",
"[seconds]",
"10");
}
/**
* Test method for {@link world.bentobox.bentobox.commands.BentoBoxReloadCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
public void testExecuteUserStringListOfString() {
reload.execute(user, "", Collections.singletonList("bskyblock"));
Mockito.verify(user).sendMessage("commands.confirmation.confirm",
"[seconds]",
"10");
}
/**
* Test method for {@link world.bentobox.bentobox.commands.BentoBoxReloadCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
public void testExecuteUserStringListOfStringNoAddon() {
reload.execute(user, "", Collections.singletonList("warps"));
Mockito.verify(user).sendMessage("commands.bentobox.reload.unknown-addon");
}
/**
* Test method for {@link world.bentobox.bentobox.commands.BentoBoxReloadCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
public void testExecuteUserStringListOfStringHelp() {
reload.execute(user, "", Arrays.asList("warps", "fhfhfh"));
Mockito.verify(user).sendMessage(
"commands.help.header",
"[label]",
null
);
}
/**
* Test method for {@link world.bentobox.bentobox.commands.BentoBoxReloadCommand#tabComplete(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
public void testTabCompleteUserStringListOfString() {
List<String> tabs = reload.tabComplete(user, "", Collections.emptyList()).get();
assertTrue(tabs.size() == 2);
assertTrue(tabs.contains("BSkyBlock"));
assertTrue(tabs.contains("AcidIsland"));
}
}