Search in sources :

Example 61 with CommandSender

use of org.bukkit.command.CommandSender in project LandLord by SpatiumPrinceps.

the class Update method onResetLands.

/**
 * Resets all lands to the default flag state
 */
public void onResetLands(CommandSender sender) {
    sender.sendMessage("Starting to reset lands...");
    for (World w : Bukkit.getWorlds()) {
        for (ProtectedRegion pr : plugin.getWgHandler().getWG().getRegionManager(w).getRegions().values()) {
            if (pr.getId().split("_").length == 3 && Bukkit.getWorld(pr.getId().split("_")[0]) == w) {
                List<String> flaggy = Landlord.getInstance().getConfig().getStringList("Flags");
                Set<String> flags = new HashSet<>();
                flaggy.forEach(s -> flags.add(s.split(" ")[0]));
                // Iterate over all existing flags
                for (Flag<?> flag : DefaultFlag.getFlags()) {
                    if (flag instanceof StateFlag) {
                        boolean failed = false;
                        if (flags.contains(flag.getName())) {
                            // Filters the config list for the right line and split that line in the mid at :
                            String[] rules = flaggy.stream().filter(s -> s.startsWith(flag.getName())).findFirst().get().split(":");
                            if (rules.length == 2) {
                                String[] defSplit = rules[0].split(" ");
                                if (defSplit.length == 3) {
                                    StateFlag.State state = StateFlag.State.valueOf(defSplit[1].toUpperCase());
                                    if (defSplit[2].equals("nonmembers"))
                                        pr.setFlag(flag.getRegionGroupFlag(), RegionGroup.NON_MEMBERS);
                                    pr.setFlag((StateFlag) flag, state);
                                } else {
                                    failed = true;
                                }
                            } else {
                                failed = true;
                            }
                        } else {
                            pr.getFlags().remove(flag);
                        }
                        if (failed) {
                            Bukkit.getLogger().warning("ERROR: Your flag definition is invalid!");
                            break;
                        }
                    }
                }
                String name = Bukkit.getOfflinePlayer(pr.getOwners().getUniqueIds().iterator().next()).getName();
                String greeting = lm.getRawString("Alerts.defaultGreeting").replace("%owner%", name);
                String farewell = lm.getRawString("Alerts.defaultFarewell").replace("%owner%", name);
                String actualGreeting = pr.getFlag(DefaultFlag.GREET_MESSAGE);
                String actualFarewell = pr.getFlag(DefaultFlag.FAREWELL_MESSAGE);
                if (!greeting.equals(actualGreeting)) {
                    pr.setFlag(DefaultFlag.GREET_MESSAGE, greeting);
                }
                if (!farewell.equals(actualFarewell)) {
                    pr.setFlag(DefaultFlag.FAREWELL_MESSAGE, farewell);
                }
            }
        }
    }
    sender.sendMessage("Finished resetting lands!");
}
Also used : LandlordCommand(biz.princeps.landlord.commands.LandlordCommand) RegionGroup(com.sk89q.worldguard.protection.flags.RegionGroup) CommandSender(org.bukkit.command.CommandSender) ProtectedRegion(com.sk89q.worldguard.protection.regions.ProtectedRegion) Landlord(biz.princeps.landlord.Landlord) Set(java.util.Set) Flag(com.sk89q.worldguard.protection.flags.Flag) HashSet(java.util.HashSet) DefaultFlag(com.sk89q.worldguard.protection.flags.DefaultFlag) List(java.util.List) World(org.bukkit.World) StateFlag(com.sk89q.worldguard.protection.flags.StateFlag) Bukkit(org.bukkit.Bukkit) ProtectedRegion(com.sk89q.worldguard.protection.regions.ProtectedRegion) StateFlag(com.sk89q.worldguard.protection.flags.StateFlag) World(org.bukkit.World) HashSet(java.util.HashSet)

Example 62 with CommandSender

use of org.bukkit.command.CommandSender in project LandLord by SpatiumPrinceps.

the class Update method onUpdateLands.

/**
 * Supposed to add missing flags to existing lands, remove non existing flags
 */
public void onUpdateLands(CommandSender issuer) {
    issuer.sendMessage("Starting to update lands...");
    for (World w : Bukkit.getWorlds()) {
        for (ProtectedRegion pr : plugin.getWgHandler().getWG().getRegionManager(w).getRegions().values()) {
            if (pr.getId().split("_").length == 3 && w.getName().equals(pr.getId().split("_")[0])) {
                List<String> flaggy = Landlord.getInstance().getConfig().getStringList("Flags");
                Set<String> flags = new HashSet<>();
                flaggy.forEach(s -> flags.add(s.split(" ")[0]));
                // Iterate over all existing flags
                for (Flag<?> flag : DefaultFlag.getFlags()) {
                    if (flag instanceof StateFlag) {
                        boolean failed = false;
                        if (flags.contains(flag.getName())) {
                            // Filters the config list for the right line and split that line in the mid at :
                            String[] rules = flaggy.stream().filter(s -> s.startsWith(flag.getName())).findFirst().get().split(":");
                            if (rules.length == 2) {
                                if (!pr.getFlags().containsKey(flag)) {
                                    String[] defSplit = rules[0].split(" ");
                                    if (defSplit.length == 3) {
                                        StateFlag.State state = StateFlag.State.valueOf(defSplit[1].toUpperCase());
                                        if (defSplit[2].equals("nonmembers"))
                                            pr.setFlag(flag.getRegionGroupFlag(), RegionGroup.NON_MEMBERS);
                                        pr.setFlag((StateFlag) flag, state);
                                    } else {
                                        failed = true;
                                    }
                                }
                            } else {
                                failed = true;
                            }
                        } else {
                            pr.getFlags().remove(flag);
                        }
                        if (failed) {
                            Bukkit.getLogger().warning("ERROR: Your flag definition is invalid!");
                            break;
                        }
                    }
                }
                String name = Bukkit.getOfflinePlayer(pr.getOwners().getUniqueIds().iterator().next()).getName();
                String greeting = lm.getRawString("Alerts.defaultGreeting").replace("%owner%", name);
                String farewell = lm.getRawString("Alerts.defaultFarewell").replace("%owner%", name);
                if (!pr.getFlags().containsKey(DefaultFlag.GREET_MESSAGE)) {
                    pr.setFlag(DefaultFlag.GREET_MESSAGE, greeting);
                }
                if (!pr.getFlags().containsKey(DefaultFlag.FAREWELL_MESSAGE)) {
                    pr.setFlag(DefaultFlag.FAREWELL_MESSAGE, farewell);
                }
            }
        }
    }
    issuer.sendMessage("Finished updating lands!");
}
Also used : LandlordCommand(biz.princeps.landlord.commands.LandlordCommand) RegionGroup(com.sk89q.worldguard.protection.flags.RegionGroup) CommandSender(org.bukkit.command.CommandSender) ProtectedRegion(com.sk89q.worldguard.protection.regions.ProtectedRegion) Landlord(biz.princeps.landlord.Landlord) Set(java.util.Set) Flag(com.sk89q.worldguard.protection.flags.Flag) HashSet(java.util.HashSet) DefaultFlag(com.sk89q.worldguard.protection.flags.DefaultFlag) List(java.util.List) World(org.bukkit.World) StateFlag(com.sk89q.worldguard.protection.flags.StateFlag) Bukkit(org.bukkit.Bukkit) ProtectedRegion(com.sk89q.worldguard.protection.regions.ProtectedRegion) StateFlag(com.sk89q.worldguard.protection.flags.StateFlag) World(org.bukkit.World) HashSet(java.util.HashSet)

Example 63 with CommandSender

use of org.bukkit.command.CommandSender in project AuthMeReloaded by AuthMe.

the class UnregisterAdminCommandTest method shouldHandleUnknownPlayer.

@Test
public void shouldHandleUnknownPlayer() {
    // given
    String user = "bobby";
    given(dataSource.isAuthAvailable(user)).willReturn(false);
    CommandSender sender = mock(CommandSender.class);
    // when
    command.executeCommand(sender, Collections.singletonList(user));
    // then
    verify(dataSource, only()).isAuthAvailable(user);
    verify(commandService).send(sender, MessageKey.UNKNOWN_USER);
}
Also used : CommandSender(org.bukkit.command.CommandSender) Test(org.junit.Test)

Example 64 with CommandSender

use of org.bukkit.command.CommandSender in project AuthMeReloaded by AuthMe.

the class UnregisterAdminCommandTest method shouldInvokeUnregisterProcess.

@Test
public void shouldInvokeUnregisterProcess() {
    // given
    String user = "personaNonGrata";
    given(dataSource.isAuthAvailable(user)).willReturn(true);
    Player player = mock(Player.class);
    given(bukkitService.getPlayerExact(user)).willReturn(player);
    CommandSender sender = mock(CommandSender.class);
    // when
    command.executeCommand(sender, Collections.singletonList(user));
    // then
    verify(dataSource, only()).isAuthAvailable(user);
    verify(bukkitService).getPlayerExact(user);
    verify(management).performUnregisterByAdmin(sender, user, player);
}
Also used : Player(org.bukkit.entity.Player) CommandSender(org.bukkit.command.CommandSender) Test(org.junit.Test)

Example 65 with CommandSender

use of org.bukkit.command.CommandSender in project AuthMeReloaded by AuthMe.

the class DataStatisticsTest method shouldOutputStatistics.

@Test
public void shouldOutputStatistics() {
    // given
    CommandSender sender = mock(CommandSender.class);
    given(singletonStore.retrieveAllOfType()).willReturn(mockListOfSize(Object.class, 7));
    given(singletonStore.retrieveAllOfType(Reloadable.class)).willReturn(mockListOfSize(Reloadable.class, 4));
    given(singletonStore.retrieveAllOfType(SettingsDependent.class)).willReturn(mockListOfSize(SettingsDependent.class, 3));
    given(singletonStore.retrieveAllOfType(HasCleanup.class)).willReturn(mockListOfSize(HasCleanup.class, 2));
    given(dataSource.getAccountsRegistered()).willReturn(219);
    given(playerCache.getLogged()).willReturn(12);
    // Clear any loggers that might exist and trigger the generation of two loggers
    Map loggers = ReflectionTestUtils.getFieldValue(ConsoleLoggerFactory.class, null, "consoleLoggers");
    loggers.clear();
    ConsoleLoggerFactory.get(String.class);
    ConsoleLoggerFactory.get(Integer.class);
    // when
    dataStatistics.execute(sender, Collections.emptyList());
    // then
    ArgumentCaptor<String> stringCaptor = ArgumentCaptor.forClass(String.class);
    verify(sender, atLeastOnce()).sendMessage(stringCaptor.capture());
    assertThat(stringCaptor.getAllValues(), containsInAnyOrder(ChatColor.BLUE + "AuthMe statistics", "Singleton Java classes: 7", "(Reloadable: 4 / SettingsDependent: 3 / HasCleanup: 2)", "LimboPlayers in memory: 1", "Total players in DB: 219", "PlayerCache size: 12 (= logged in players)", "Total logger instances: 2"));
}
Also used : SettingsDependent(fr.xephi.authme.initialization.SettingsDependent) CommandSender(org.bukkit.command.CommandSender) HasCleanup(fr.xephi.authme.initialization.HasCleanup) HashMap(java.util.HashMap) Map(java.util.Map) Reloadable(fr.xephi.authme.initialization.Reloadable) Test(org.junit.Test)

Aggregations

CommandSender (org.bukkit.command.CommandSender)295 Test (org.junit.Test)171 Player (org.bukkit.entity.Player)94 Matchers.containsString (org.hamcrest.Matchers.containsString)68 ConsoleCommandSender (org.bukkit.command.ConsoleCommandSender)50 CoreStateInitException (com.solinia.solinia.Exceptions.CoreStateInitException)43 PlayerAuth (fr.xephi.authme.data.auth.PlayerAuth)30 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)29 PermissionNode (fr.xephi.authme.permission.PermissionNode)17 BlockCommandSender (org.bukkit.command.BlockCommandSender)16 ValidationResult (fr.xephi.authme.service.ValidationService.ValidationResult)12 Location (org.bukkit.Location)10 HashedPassword (fr.xephi.authme.security.crypts.HashedPassword)9 ArrayList (java.util.ArrayList)9 List (java.util.List)9 FoundCommandResult (fr.xephi.authme.command.FoundCommandResult)8 IOException (java.io.IOException)7 Entity (org.bukkit.entity.Entity)7 IEssentialsCommand (com.earth2me.essentials.commands.IEssentialsCommand)6 NoChargeException (com.earth2me.essentials.commands.NoChargeException)6