public void startFinished() { if (bets != null && !bets.isEmpty() && bets.size() > 0) { for (int i = 0; i < bets.size(); i++) { try { Bet b = bets.get(i); if (isTribute(server.getPlayer(b.tribute))) { addScore(b.better, b.wager / 4); addScore(b.tribute, b.wager / 4); } else { subtractScore(b.better, b.wager); } bets.remove(b); } catch (Exception e) { e.printStackTrace(); } } } clearAll(); load(); bets.clear(); state = State.DONE; game = new Finished(this); startTimer(); }
@Test public void playerNoOpSetLvl() { Player player = Mockito.mock(Player.class); cmd.onCommand(player, "setlvl"); Mockito.verify(player) .sendMessage("§6[EC] §eSyntax: /level setLevel <Module> <Value> [Player]"); cmd.onCommand(player, "setlvl", "t"); Mockito.verify(player, Mockito.times(2)) .sendMessage("§6[EC] §eSyntax: /level setLevel <Module> <Value> [Player]"); cmd.onCommand(player, "setlevel", "t", "5"); Mockito.verify(player).sendMessage("§6[EC] §cYou cannot execute that command."); player = Mockito.mock(Player.class); cmd.onCommand(player, "setlevel", "t", "asd"); Mockito.verify(player) .sendMessage("§6[EC] §eSyntax: /level setLevel <Module> <Value> [Player]"); Mockito.verify(player).sendMessage("§6[EC] §e<Value> has to be a natural number"); cmd.onCommand(player, "setlvl", "t", "5", "cryxli"); Mockito.verify(player).sendMessage("§6[EC] §cYou cannot execute that command."); Mockito.when(server.getPlayer(player.getName())).thenReturn(player); cmd.onCommand(player, "setlvl", "t", "5", "cryxli"); Mockito.verify(player, Mockito.times(2)) .sendMessage("§6[EC] §cYou cannot execute that command."); cmd.onCommand(player, "setlvl", "d", "15", "cryxli"); Mockito.verify(player, Mockito.times(3)) .sendMessage("§6[EC] §cYou cannot execute that command."); }
public boolean has(String player, String permission, String world) { Player p = serv.getPlayer(player); if (p != null) return p.hasPermission(permission); // OfflinePlayer op = serv.getOfflinePlayer(player); return false; // TODO: Figure out offline player permission checks with bukkit-perms. }
@Test public void senderSetLvl() { CommandSender sender = Mockito.mock(CommandSender.class); cmd.onCommand(sender, "setlvl"); Mockito.verify(sender) .sendMessage("§6[EC] §eSyntax: /level setLevel <Module> <Value> <Player>"); cmd.onCommand(sender, "setlvl", "t"); Mockito.verify(sender, Mockito.times(2)) .sendMessage("§6[EC] §eSyntax: /level setLevel <Module> <Value> <Player>"); cmd.onCommand(sender, "setlevel", "t", "5"); Mockito.verify(sender, Mockito.times(3)) .sendMessage("§6[EC] §eSyntax: /level setLevel <Module> <Value> <Player>"); cmd.onCommand(sender, "setlvl", "t", "5", "cryxli"); Mockito.verify(sender).sendMessage("§6[EC] §ePlayer cryxli is offline"); sender = Mockito.mock(CommandSender.class); Player player = Mockito.mock(Player.class); Mockito.when(player.getName()).thenReturn("cryxli"); Mockito.when(server.getPlayer(player.getName())).thenReturn(player); cmd.onCommand(sender, "setlvl", "t", "asd", "cryxli"); Mockito.verify(sender) .sendMessage("§6[EC] §eSyntax: /level setLevel <Module> <Value> [Player]"); Mockito.verify(sender).sendMessage("§6[EC] §e<Value> has to be a natural number"); Mockito.verify(sender, Mockito.times(2)).sendMessage(Mockito.anyString()); Mockito.when(server.getPlayer(player.getName())).thenReturn(player); cmd.onCommand(sender, "setlvl", "t", "5", "cryxli"); Mockito.verify(sender).sendMessage("§6[EC] §eTest (T): lv5"); Assert.assertEquals(5, core.getPersistence().getLevel(testModule, player)); cmd.onCommand(sender, "setlvl", "d", "15", "cryxli"); Mockito.verify(sender).sendMessage("§6[EC] §eNo module found"); }
@Override public final boolean onCommand( final CommandSender commandSender, final Command command, final String label, final String[] args) { final String senderName = commandSender.getName(); final Server server = Bukkit.getServer(); final Player sender = server.getPlayer(senderName); if (sender.hasPermission("udsplugin.lockdown")) { if (args.length == 0) { if (!UDSPlugin.getServerInLockdown()) { UDSPlugin.setServerInLockdown(true); UDSPlugin.getPlayers().get(senderName).setLockdownPass(true); server.broadcastMessage(Color.BROADCAST + "Server is now in lockdown."); } else { UDSPlugin.setServerInLockdown(false); server.broadcastMessage(Color.BROADCAST + "Server is no longer in lockdown."); for (Map.Entry<String, UDSPlayer> i : UDSPlugin.getPlayers().entrySet()) { i.getValue().setLockdownPass(false); } } } else if (args.length == 1) { final UDSPlayer serverPlayer = PlayerUtils.matchUDS(args[0]); if (serverPlayer != null) { final String targetName = serverPlayer.getNick(); if (serverPlayer.hasLockdownPass()) { serverPlayer.setLockdownPass(false); sender.sendMessage(Color.MESSAGE + targetName + "'s lockdown pass has been removed."); } else { serverPlayer.setLockdownPass(true); sender.sendMessage( Color.MESSAGE + targetName + " has been issued with a lockdown pass."); } } else { sender.sendMessage(UDSMessage.NO_PLAYER); } } else { sender.sendMessage(UDSMessage.BAD_ARGS); } } else { sender.sendMessage(UDSMessage.NO_PERM); } return true; }
@Test public void playerOpAll() { Player player = Mockito.mock(Player.class); Mockito.when(player.getName()).thenReturn("cryxli"); Mockito.when(player.isOp()).thenReturn(true); core.getPersistence().setExp(testModule, player, 0); cmd.onCommand(player, "all"); Mockito.verify(player).sendMessage("§6[EC] --- ExpCraftPlugin ---"); Mockito.verify(player).sendMessage("§6[EC] §eTest (T): 1"); cmd.onCommand(player, "all", "cryxli"); Mockito.verify(player).sendMessage("§6[EC] §ePlayer cryxli is offline"); Mockito.when(server.getPlayer(player.getName())).thenReturn(player); cmd.onCommand(player, "all", "cryxli"); Mockito.verify(player, Mockito.times(2)).sendMessage("§6[EC] --- ExpCraftPlugin ---"); Mockito.verify(player, Mockito.times(2)).sendMessage("§6[EC] §eTest (T): 1"); }
@Test public void senderAll() { CommandSender sender = Mockito.mock(CommandSender.class); cmd.onCommand(sender, "all"); Mockito.verify(sender).sendMessage("§6[EC] §eSyntax: /level all <Player>"); cmd.onCommand(sender, "all", "cryxli"); Mockito.verify(sender).sendMessage("§6[EC] §ePlayer cryxli is offline"); Player player = Mockito.mock(Player.class); Mockito.when(player.getName()).thenReturn("cryxli"); Mockito.when(server.getPlayer(player.getName())).thenReturn(player); sender = Mockito.mock(CommandSender.class); core.getPersistence().setExp(testModule, player, 0); cmd.onCommand(sender, "all", "cryxli"); Mockito.verify(sender).sendMessage("§6[EC] --- ExpCraftPlugin ---"); Mockito.verify(sender).sendMessage("§6[EC] §eTest (T): 1"); Mockito.verify(sender, Mockito.times(2)).sendMessage(Mockito.anyString()); }
public boolean onCommand(CommandSender sender, Command cmd, String label, String args[]) { boolean isPlayer = sender instanceof Player; if (cmd.getName().equalsIgnoreCase("slenderman")) { if (args.length < 1) { return false; } if (args[0].equalsIgnoreCase("reload")) { config = YamlConfiguration.loadConfiguration(configFile); log.info("Configuration reloaded."); if (isPlayer) sender.sendMessage(ChatColor.GREEN + "Configurations reloaded."); return true; } if (args[0].equalsIgnoreCase("target")) { if (args.length > 1) { player = mc.getPlayer(args[1]); if (!player.isOnline()) { sender.sendMessage("That player is not online."); return true; } if (player.isDead()) { sender.sendMessage("That player is dead."); return true; } if (!config.getStringList("worlds").contains(player.getWorld().getName())) { sender.sendMessage("That player is in a world Slenderman cannot go to."); return true; } slender.target(player); return true; } } if (args[0].equalsIgnoreCase("location")) { sender.sendMessage(slender.slenderman.getLocation().toString()); return true; } } return false; }
@Test public void playerOpGetExp() { Player player = Mockito.mock(Player.class); Mockito.when(player.getName()).thenReturn("cryxli"); Mockito.when(player.isOp()).thenReturn(true); cmd.onCommand(player, "getexp"); Mockito.verify(player).sendMessage("§6[EC] §eSyntax: /level getExp <Module> [Player]"); core.getPersistence().setExp(testModule, player, 0); cmd.onCommand(player, "getexp", "t"); Mockito.verify(player).sendMessage("§6[EC] §eTest (T): lv1 at 0 points"); cmd.onCommand(player, "getexp", "t", "cryxli"); Mockito.verify(player).sendMessage("§6[EC] §ePlayer cryxli is offline"); Mockito.when(server.getPlayer(player.getName())).thenReturn(player); cmd.onCommand(player, "getexp", "t", "cryxli"); Mockito.verify(player, Mockito.times(2)).sendMessage("§6[EC] §eTest (T): lv1 at 0 points"); cmd.onCommand(player, "getexp", "d", "cryxli"); Mockito.verify(player).sendMessage("§6[EC] §eNo module found"); }
@Override public final boolean onCommand( final CommandSender commandSender, final Command command, final String label, final String[] args) { String senderName = commandSender.getName(); Server server = Bukkit.getServer(); Player sender = server.getPlayer(senderName); String commandName = command.getName(); if (commandName.equalsIgnoreCase("udsreload")) { if (sender.hasPermission("udsplugin.udsreload")) { plugin.reloadConfig(); UDSPlugin.getUDSConfig().load(plugin.getConfig()); sender.sendMessage(UDSMessage.MSG_RELOAD); } else { sender.sendMessage(UDSMessage.NO_PERM); } } else { sender.sendMessage(UDSMessage.BAD_COMMAND); } return true; }
@Override public SpoutPlayer getPlayer(String name) { return (SpoutPlayer) server.getPlayer(name); }
public boolean setUp() { try { FileUtils.deleteFolder(invDirectory); FileUtils.deleteFolder(serverDirectory); invDirectory.mkdirs(); Assert.assertTrue(invDirectory.exists()); MockGateway.MOCK_STANDARD_METHODS = false; plugin = PowerMockito.spy(new MultiverseInventories()); core = PowerMockito.spy(new MultiverseCore()); // Let's let all MV files go to bin/test doReturn(invDirectory).when(plugin).getDataFolder(); // Let's let all MV files go to bin/test doReturn(coreDirectory).when(core).getDataFolder(); // Return a fake PDF file. PluginDescriptionFile pdf = PowerMockito.spy( new PluginDescriptionFile( "Multiverse-Inventories", "2.4-test", "com.onarandombox.multiverseinventories.MultiverseInventories")); when(pdf.getAuthors()).thenReturn(new ArrayList<String>()); doReturn(pdf).when(plugin).getDescription(); doReturn(core).when(plugin).getCore(); doReturn(true).when(plugin).isEnabled(); PluginDescriptionFile pdfCore = PowerMockito.spy( new PluginDescriptionFile( "Multiverse-Core", "2.2-Test", "com.onarandombox.MultiverseCore.MultiverseCore")); when(pdfCore.getAuthors()).thenReturn(new ArrayList<String>()); doReturn(pdfCore).when(core).getDescription(); doReturn(true).when(core).isEnabled(); plugin.setServerFolder(serverDirectory); // Add Core to the list of loaded plugins JavaPlugin[] plugins = new JavaPlugin[] {plugin, core}; // Mock the Plugin Manager PluginManager mockPluginManager = PowerMockito.mock(PluginManager.class); when(mockPluginManager.getPlugins()).thenReturn(plugins); when(mockPluginManager.getPlugin("Multiverse-Inventories")).thenReturn(plugin); when(mockPluginManager.getPlugin("Multiverse-Core")).thenReturn(core); when(mockPluginManager.getPermission(anyString())).thenReturn(null); // Make some fake folders to fool the fake MV into thinking these worlds exist File worldNormalFile = new File(plugin.getServerFolder(), "world"); Util.log("Creating world-folder: " + worldNormalFile.getAbsolutePath()); worldNormalFile.mkdirs(); MockWorldFactory.makeNewMockWorld("world", Environment.NORMAL, WorldType.NORMAL); File worldNetherFile = new File(plugin.getServerFolder(), "world_nether"); Util.log("Creating world-folder: " + worldNetherFile.getAbsolutePath()); worldNetherFile.mkdirs(); MockWorldFactory.makeNewMockWorld("world_nether", Environment.NETHER, WorldType.NORMAL); File worldSkylandsFile = new File(plugin.getServerFolder(), "world_the_end"); Util.log("Creating world-folder: " + worldSkylandsFile.getAbsolutePath()); worldSkylandsFile.mkdirs(); MockWorldFactory.makeNewMockWorld("world_the_end", Environment.THE_END, WorldType.NORMAL); File world2File = new File(plugin.getServerFolder(), "world2"); Util.log("Creating world-folder: " + world2File.getAbsolutePath()); world2File.mkdirs(); MockWorldFactory.makeNewMockWorld("world2", Environment.NORMAL, WorldType.NORMAL); // Initialize the Mock server. mockServer = mock(Server.class); when(mockServer.getName()).thenReturn("TestBukkit"); Logger.getLogger("Minecraft").setParent(Util.logger); when(mockServer.getLogger()).thenReturn(Util.logger); when(mockServer.getWorldContainer()).thenReturn(worldsDirectory); when(plugin.getServer()).thenReturn(mockServer); when(core.getServer()).thenReturn(mockServer); when(mockServer.getPluginManager()).thenReturn(mockPluginManager); Answer<Player> playerAnswer = new Answer<Player>() { public Player answer(InvocationOnMock invocation) throws Throwable { String arg; try { arg = (String) invocation.getArguments()[0]; } catch (Exception e) { return null; } Player player = players.get(arg); if (player == null) { player = new MockPlayer(arg, mockServer); players.put(arg, player); } return player; } }; when(mockServer.getPlayer(anyString())).thenAnswer(playerAnswer); when(mockServer.getOfflinePlayer(anyString())).thenAnswer(playerAnswer); when(mockServer.getOfflinePlayers()) .thenAnswer( new Answer<OfflinePlayer[]>() { public OfflinePlayer[] answer(InvocationOnMock invocation) throws Throwable { return players.values().toArray(new Player[players.values().size()]); } }); when(mockServer.getOnlinePlayers()) .thenAnswer( new Answer<Player[]>() { public Player[] answer(InvocationOnMock invocation) throws Throwable { return players.values().toArray(new Player[players.values().size()]); } }); // Give the server some worlds when(mockServer.getWorld(anyString())) .thenAnswer( new Answer<World>() { public World answer(InvocationOnMock invocation) throws Throwable { String arg; try { arg = (String) invocation.getArguments()[0]; } catch (Exception e) { return null; } return MockWorldFactory.getWorld(arg); } }); when(mockServer.getWorld(any(UUID.class))) .thenAnswer( new Answer<World>() { @Override public World answer(InvocationOnMock invocation) throws Throwable { UUID arg; try { arg = (UUID) invocation.getArguments()[0]; } catch (Exception e) { return null; } return MockWorldFactory.getWorld(arg); } }); when(mockServer.getWorlds()) .thenAnswer( new Answer<List<World>>() { public List<World> answer(InvocationOnMock invocation) throws Throwable { return MockWorldFactory.getWorlds(); } }); when(mockServer.createWorld(Matchers.isA(WorldCreator.class))) .thenAnswer( new Answer<World>() { public World answer(InvocationOnMock invocation) throws Throwable { WorldCreator arg; try { arg = (WorldCreator) invocation.getArguments()[0]; } catch (Exception e) { return null; } // Add special case for creating null worlds. // Not sure I like doing it this way, but this is a special case if (arg.name().equalsIgnoreCase("nullworld")) { return MockWorldFactory.makeNewNullMockWorld( arg.name(), arg.environment(), arg.type()); } return MockWorldFactory.makeNewMockWorld( arg.name(), arg.environment(), arg.type()); } }); when(mockServer.unloadWorld(anyString(), anyBoolean())).thenReturn(true); // add mock scheduler BukkitScheduler mockScheduler = mock(BukkitScheduler.class); when(mockScheduler.scheduleSyncDelayedTask(any(Plugin.class), any(Runnable.class), anyLong())) .thenAnswer( new Answer<Integer>() { public Integer answer(InvocationOnMock invocation) throws Throwable { Runnable arg; try { arg = (Runnable) invocation.getArguments()[1]; } catch (Exception e) { return null; } arg.run(); return null; } }); when(mockScheduler.scheduleSyncDelayedTask(any(Plugin.class), any(Runnable.class))) .thenAnswer( new Answer<Integer>() { public Integer answer(InvocationOnMock invocation) throws Throwable { Runnable arg; try { arg = (Runnable) invocation.getArguments()[1]; } catch (Exception e) { return null; } arg.run(); return null; } }); when(mockServer.getScheduler()).thenReturn(mockScheduler); // Set InventoriesListener InventoriesListener il = PowerMockito.spy(new InventoriesListener(plugin)); Field inventoriesListenerField = MultiverseInventories.class.getDeclaredField("inventoriesListener"); inventoriesListenerField.setAccessible(true); inventoriesListenerField.set(plugin, il); // Set Core Field coreField = MultiverseInventories.class.getDeclaredField("core"); coreField.setAccessible(true); coreField.set(plugin, core); // Set server Field serverfield = JavaPlugin.class.getDeclaredField("server"); serverfield.setAccessible(true); serverfield.set(plugin, mockServer); // Set worldManager WorldManager wm = PowerMockito.spy(new WorldManager(core)); Field worldmanagerfield = MultiverseCore.class.getDeclaredField("worldManager"); worldmanagerfield.setAccessible(true); worldmanagerfield.set(core, wm); // Set playerListener MVPlayerListener pl = PowerMockito.spy(new MVPlayerListener(core)); Field playerlistenerfield = MultiverseCore.class.getDeclaredField("playerListener"); playerlistenerfield.setAccessible(true); playerlistenerfield.set(core, pl); // Set entityListener MVEntityListener el = PowerMockito.spy(new MVEntityListener(core)); Field entitylistenerfield = MultiverseCore.class.getDeclaredField("entityListener"); entitylistenerfield.setAccessible(true); entitylistenerfield.set(core, el); // Set weatherListener MVWeatherListener wl = PowerMockito.spy(new MVWeatherListener(core)); Field weatherlistenerfield = MultiverseCore.class.getDeclaredField("weatherListener"); weatherlistenerfield.setAccessible(true); weatherlistenerfield.set(core, wl); // Init our command sender final Logger commandSenderLogger = Logger.getLogger("CommandSender"); commandSenderLogger.setParent(Util.logger); commandSender = mock(CommandSender.class); doAnswer( new Answer<Void>() { public Void answer(InvocationOnMock invocation) throws Throwable { commandSenderLogger.info( ChatColor.stripColor((String) invocation.getArguments()[0])); return null; } }) .when(commandSender) .sendMessage(anyString()); when(commandSender.getServer()).thenReturn(mockServer); when(commandSender.getName()).thenReturn("MockCommandSender"); when(commandSender.isPermissionSet(anyString())).thenReturn(true); when(commandSender.isPermissionSet(Matchers.isA(Permission.class))).thenReturn(true); when(commandSender.hasPermission(anyString())).thenReturn(true); when(commandSender.hasPermission(Matchers.isA(Permission.class))).thenReturn(true); when(commandSender.addAttachment(plugin)).thenReturn(null); when(commandSender.isOp()).thenReturn(true); Bukkit.setServer(mockServer); // Load Multiverse Core core.onLoad(); plugin.onLoad(); // Enable it. core.onEnable(); plugin.onEnable(); return true; } catch (Exception e) { e.printStackTrace(); } return false; }
@Test public void testEnableDebugMode() { // Initialize a fake command Command mockCommand = mock(Command.class); when(mockCommand.getName()).thenReturn("spam"); // Assert debug mode is off Assert.assertEquals(0, (int) myPlugin.config().get(BaseConfig.DEBUG_MODE)); // Send the debug command. String[] cmdArgs = new String[] {"debug", "3"}; myPlugin.onCommand(mockCommandSender, mockCommand, "", cmdArgs); cmdArgs = new String[] {"reload"}; myPlugin.onCommand(mockCommandSender, mockCommand, "", cmdArgs); Assert.assertEquals(3, (int) myPlugin.config().get(BaseConfig.DEBUG_MODE)); Assert.assertEquals( BaseConfig.LOCALE.getDefault().toString(), myPlugin.config().get(BaseConfig.LOCALE).toString()); myPlugin.config().set(BaseConfig.LOCALE, Locale.CANADA); Assert.assertEquals(Locale.CANADA, myPlugin.config().get(BaseConfig.LOCALE)); myPlugin.config().set(Config.MUTE_LENGTH, 3); myPlugin.config().save(); Assert.assertEquals(myPlugin.config().get(Config.MUTE_LENGTH), Integer.valueOf(3)); Player player = mockServer.getPlayer("dumptruckman"); listener.onPlayerCommandPreprocess(new PlayerCommandPreprocessEvent(player, "/g hello")); listener.onPlayerChat(new PlayerChatEvent(player, "hello")); listener.onPlayerChat(new PlayerChatEvent(player, "hello")); listener.onPlayerCommandPreprocess(new PlayerCommandPreprocessEvent(player, "/g hello")); cmdArgs = new String[] {"unmute", "dumptruckman"}; myPlugin.onCommand(mockCommandSender, mockCommand, "", cmdArgs); listener.onPlayerChat(new PlayerChatEvent(player, "hello")); sleep(1002); listener.onPlayerChat(new PlayerChatEvent(player, "hello")); sleep(1002); listener.onPlayerChat(new PlayerChatEvent(player, "hello")); sleep(1002); listener.onPlayerChat(new PlayerChatEvent(player, "hello")); sleep(3000); try { Field field = DefaultSpamHandler.class.getDeclaredField("config"); field.setAccessible(true); field.set(myPlugin.getSpamHandler(), myPlugin.config()); } catch (NoSuchFieldException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } ((DefaultSpamHandler) myPlugin.getSpamHandler()).checkTimes(); listener.onPlayerChat(new PlayerChatEvent(player, "hello")); listener.onPlayerChat(new PlayerChatEvent(player, "hello")); listener.onPlayerChat(new PlayerChatEvent(player, "hello")); listener.onPlayerChat(new PlayerChatEvent(player, "hello")); try { Field field = DefaultSpamHandler.class.getDeclaredField("beenKicked"); field.setAccessible(true); List<OfflinePlayer> kickedPlayers = (List<OfflinePlayer>) field.get(myPlugin.getSpamHandler()); Assert.assertTrue(kickedPlayers.contains(player)); } catch (NoSuchFieldException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } }