private boolean resetServerViewDistance(CommandSender sender, Server server) { if (server == null) { return false; } if (!sender.hasPermission("viewdistance.reset.server") && !sender.hasPermission("viewdistance.reset.server." + server.getName())) { sender.sendMessage("You do not have permission to use this command."); return true; } try { server.setViewDistance(SERVER_DEFAULT_VIEW_DISTANCE); } catch (IllegalArgumentException e) { sender.sendMessage(e.getMessage()); return false; } sender.sendMessage("Server view distance reset. It is now " + server.getViewDistance()); return true; }
@Override public String getName() { return server.getName(); }
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; }
public static Object handle(Object obj) { if (obj instanceof World.Environment) { World.Environment e = (World.Environment) obj; if (e == World.Environment.NETHER) { return "nether"; } else { return "normal"; } } else if (obj instanceof File) { return ((File) obj).toString(); } else if (obj instanceof Player) { Player p = (Player) obj; JSONObject o = new JSONObject(); o.put("name", p.getName()); o.put("op", p.isOp()); o.put("health", p.getHealth()); o.put("ip", p.getAddress().toString()); o.put("itemInHand", p.getItemInHand()); o.put("location", p.getLocation()); o.put("inventory", p.getInventory()); o.put("sneaking", p.isSneaking()); o.put("inVehicle", p.isInsideVehicle()); o.put("sleeping", p.isSleeping()); o.put("world", p.getServer().getWorlds().indexOf(p.getWorld())); return o; } else if (obj instanceof Server) { Server s = (Server) obj; JSONObject o = new JSONObject(); o.put("maxPlayers", s.getMaxPlayers()); o.put("players", Arrays.asList(s.getOnlinePlayers())); o.put("port", s.getPort()); o.put("name", s.getName()); o.put("serverName", s.getServerName()); o.put("version", s.getVersion()); o.put("worlds", s.getWorlds()); return o; } else if (obj instanceof World) { World w = (World) obj; JSONObject o = new JSONObject(); o.put("environment", w.getEnvironment()); o.put("fullTime", w.getFullTime()); o.put("time", w.getTime()); o.put("name", w.getName()); o.put("isThundering", w.isThundering()); o.put("hasStorm", w.hasStorm()); return o; } else if (obj instanceof Plugin) { Plugin p = (Plugin) obj; PluginDescriptionFile d = p.getDescription(); JSONObject o = new JSONObject(); o.put("name", d.getName()); o.put("description", d.getDescription()); o.put("authors", d.getAuthors()); o.put("version", d.getVersion()); o.put("website", d.getWebsite()); o.put("enabled", JSONAPI.instance.getServer().getPluginManager().isPluginEnabled(p)); return o; } else if (obj instanceof ItemStack) { ItemStack i = (ItemStack) obj; JSONObject o = new JSONObject(); o.put("type", i.getTypeId()); o.put("durability", i.getDurability()); o.put("amount", i.getAmount()); return o; } else if (obj instanceof PlayerInventory) { PlayerInventory p = (PlayerInventory) obj; JSONObject o = new JSONObject(); JSONObject armor = new JSONObject(); armor.put("boots", p.getBoots()); armor.put("chestplate", p.getChestplate()); armor.put("helmet", p.getHelmet()); armor.put("leggings", p.getLeggings()); o.put("armor", armor); o.put("hand", p.getItemInHand()); o.put("inventory", Arrays.asList(p.getContents())); return o; } else if (obj instanceof Location) { Location l = (Location) obj; JSONObject o = new JSONObject(); o.put("x", l.getX()); o.put("y", l.getY()); o.put("z", l.getZ()); o.put("pitch", l.getPitch()); o.put("yaw", l.getYaw()); return o; } else if (obj instanceof Plugin[]) { List<Plugin> l = Arrays.asList((Plugin[]) obj); Collections.sort(l, new PluginSorter()); return l; } else if (obj instanceof Object[]) { int l = ((Object[]) obj).length; JSONArray a = new JSONArray(); for (int i = 0; i < l; i++) { a.add(((Object[]) obj)[i]); } return a; } Logger.getLogger("JSONAPI").warning("Uncaugh object! Value:"); Logger.getLogger("JSONAPI").warning(obj.toString()); Logger.getLogger("JSONAPI").warning("Type:"); Logger.getLogger("JSONAPI").warning(obj.getClass().getName()); return new Object(); }
@Override public void queryGameState(Tregmine tregmine) { Server server = tregmine.getServer(); version = server.getName() + " " + server.getVersion(); }